-
Notifications
You must be signed in to change notification settings - Fork 1
[Don't merge] Mbsw add caching #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
frbl
wants to merge
47
commits into
roqua:master
Choose a base branch
from
compsy:mbsw-add-caching
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 30 commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
3976b58
Merge pull request #2 from roqua/master
frbl a29a6aa
Basic Cassandra framework
Babbie 2789501
Add cassandra username / password to physiqual engine
frbl 8bdb368
Updated dummy initializer to have cassandra config
frbl eb19fb3
Merge pull request #3 from compsy/fb-add-cassandra-config
Babbie 22421ee
Commit for IDE transfer
Babbie a87d466
Commit for Frank check
Babbie a6b82e9
CassandraConnection creation and exporter prep work
Babbie b88334c
Add comments explaining the properties
frbl 1e12dac
typo
frbl 64f5488
Merge pull request #5 from compsy/fb-add-comments
Babbie ace8180
remove trailing whitespacing
frbl e566fd9
Caching first implementation
Babbie 3465384
Merge remote-tracking branch 'origin/mbsw-add-caching' into mbsw-add-…
Babbie c8dd572
Some refactoring
Babbie 50f0de4
Some refactoring
Babbie e7e9c82
Some refactoring
Babbie 4e2d81a
Merge remote-tracking branch 'origin/mbsw-add-caching' into mbsw-add-…
Babbie 18f4f49
Merge remote-tracking branch 'origin/mbsw-add-caching' into mbsw-add-…
Babbie 61f7447
Merge remote-tracking branch 'origin/mbsw-add-caching' into mbsw-add-…
Babbie 848ffeb
Fix bugs, pull candidate
Babbie e99e3a4
Rubocop
Babbie e913523
Entirety of caching moved to Sidekiq.
Babbie 1249f5b
Rubocop
Babbie 899b204
Rubocop
Babbie 47e7db2
Merge remote-tracking branch 'origin/mbsw-add-caching' into mbsw-add-…
Babbie e526a64
Merge remote-tracking branch 'origin/mbsw-add-caching' into mbsw-add-…
Babbie dfabe4b
Merge remote-tracking branch 'origin/mbsw-add-caching' into mbsw-add-…
Babbie fdd981c
Rubocop
Babbie c826f51
Merge remote-tracking branch 'origin/mbsw-add-caching' into mbsw-add-…
Babbie de0f43a
Cleaned up the code a bit
frbl 4dba7d7
Added some specs
frbl 3b4ac05
Removed the time splitting construction, refactored the code to use e…
frbl 20a5165
WIP
frbl 7c21ba3
More refactoring, added some specs
frbl cfb95f0
Updated specs to expect a user instead of just a token
frbl e6f9b5c
remove failing specs and rubocop'
frbl 6da8226
Added some more specs
frbl 2febdcd
Remove focus
frbl 3aa34d1
Reduced complexity of some methods
frbl dee8a79
Fixed some specs
frbl b131062
Hoped to fix the codeclimate issues
frbl 8c54436
Moved function in class
frbl 7686025
Include information required since caching update
Babbie 2fb84e8
Add redis url config
Babbie 056541f
Add redis url config
Babbie 5019e3a
Add redis url config
Babbie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| require 'sidekiq' | ||
|
|
||
| module Physiqual | ||
| class CacheWorker | ||
| include Sidekiq::Worker | ||
| def perform(table, user_id, from, to) | ||
| token = User.find_by_user_id(user_id).physiqual_token | ||
| return [] unless token.complete? | ||
| session = Sessions::TokenAuthorizedSession.new(token) | ||
| @data_service = DataServices::DataServiceFactory.fabricate!(token.class.csrf_token, session) | ||
| @user_id = user_id | ||
| connection = DataServices::CassandraConnection.instance | ||
| from = Time.zone.parse(from) | ||
| to = Time.zone.parse(to) | ||
| store_data(connection, table, from, to) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def store_data(connection, table, from, to) | ||
| entries = DataServices::CassandraDataService.get_data(connection, @user_id, table, from, to) | ||
| data_service_function = get_data_function(table) | ||
| new_entries = [] | ||
| if entries.blank? | ||
| new_entries = data_service_function.call(from, to) | ||
| else | ||
| if entries.first.start_date > from | ||
| new_entries += data_service_function.call(from, entries.first.start_date) | ||
| end | ||
| find_gaps(entries) do |from_gap, to_gap| | ||
| new_entries += data_service_function.call(from_gap, to_gap) | ||
| end | ||
| if entries.last.end_date < to | ||
| new_entries += data_service_function.call(entries.last.end_date, to) | ||
| end | ||
| end | ||
| cache(connection, table, @user_id, new_entries) if new_entries.present? | ||
| rescue Errors::NotSupportedError => e | ||
| Rails.logger.warn e.message | ||
| end | ||
|
|
||
| def get_data_function(table) | ||
| case table | ||
| when 'heart_rate' | ||
| @data_service.method(:heart_rate) | ||
| when 'sleep' | ||
| @data_service.method(:sleep) | ||
| when 'calories' | ||
| @data_service.method(:calories) | ||
| when 'distance' | ||
| @data_service.method(:distance) | ||
| when 'steps' | ||
| @data_service.method(:steps) | ||
| when 'activities' | ||
| @data_service.method(:activities) | ||
| end | ||
| end | ||
|
|
||
| def find_gaps(entries) | ||
| entries.each_with_index do |entry, i| | ||
| break if i == entries.length - 1 | ||
| if entry.end_date != entries[i + 1].start_date | ||
| yield(entry.end_date, entries[i + 1].start_date) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def cache(connection, table, user_id, new_entries) | ||
| year = new_entries.first.measurement_moment.strftime('%Y').to_i | ||
| times = [] | ||
| start_dates = [] | ||
| end_dates = [] | ||
| values = [] | ||
| new_entries.each do |entry| | ||
| if year != entry.measurement_moment.strftime('%Y').to_i | ||
| connection.insert(table, user_id, year, times, start_dates, end_dates, values) | ||
| year = entry.measurement_moment.strftime('%Y').to_i | ||
| times = [] | ||
| start_dates = [] | ||
| end_dates = [] | ||
| values = [] | ||
| end | ||
| times << entry.measurement_moment | ||
| start_dates << entry.start_date | ||
| end_dates << entry.end_date | ||
| values << entry.values.first | ||
| if entry == new_entries.last | ||
| connection.insert(table, user_id, year, times, start_dates, end_dates, values) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| require 'singleton' | ||
| require 'cassandra' | ||
|
|
||
| module Physiqual | ||
| module DataServices | ||
| class CassandraConnection | ||
| include Singleton | ||
| SLICE_SIZE = 100 | ||
|
|
||
| def initialize | ||
| @cluster = nil | ||
| if !ENV['CASSANDRA_USERNAME'] || !ENV['CASSANDRA_PASSWORD'] || ENV['CASSANDRA_USERNAME'] == '' | ||
| @cluster = Cassandra.cluster( | ||
| hosts: (ENV['CASSANDRA_HOST_URLS'] || 'physiqual.dev').split(' ') | ||
| ) | ||
| else | ||
| @cluster = Cassandra.cluster( | ||
| username: ENV['CASSANDRA_USERNAME'], | ||
| password: ENV['CASSANDRA_PASSWORD'], | ||
| hosts: (ENV['CASSANDRA_HOST_URLS'] || 'physiqual.dev').split(' ') | ||
| ) | ||
| end | ||
| @session = @cluster.connect('physiqual') | ||
|
|
||
| init_db | ||
| init_insert | ||
| init_query | ||
| end | ||
|
|
||
| def insert(table, user_id, year, times, start_dates, end_dates, values) | ||
| times_slices, start_dates_slices, end_dates_slices, values_slices = | ||
| slice(times, start_dates, end_dates, values) | ||
| times_slices.each_with_index do |times_slice, i| | ||
| start_dates_slice = start_dates_slices[i] | ||
| end_dates_slice = end_dates_slices[i] | ||
| values_slice = values_slices[i] | ||
| batch = @session.batch do |b| | ||
| times_slice.each_with_index do |time, j| | ||
| value = BigDecimal(values_slice[j], Float::DIG + 1) | ||
| case table | ||
| when 'heart_rate' | ||
| insert_type = @insert_heart_rate | ||
| when 'sleep' | ||
| insert_type = @insert_sleep | ||
| when 'calories' | ||
| insert_type = @insert_calories | ||
| when 'distance' | ||
| insert_type = @insert_distance | ||
| when 'steps' | ||
| insert_type = @insert_steps | ||
| when 'activities' | ||
| insert_type = @insert_activities | ||
| value = values_slice[j] | ||
| end | ||
| b.add(insert_type, arguments: [user_id, year, time.to_time, start_dates_slice[j].to_time, | ||
| end_dates_slice[j].to_time, value]) | ||
| end | ||
| end | ||
| @session.execute(batch) | ||
| end | ||
| end | ||
|
|
||
| def slice(times, start_dates, end_dates, values) | ||
| return times.each_slice(SLICE_SIZE).to_a, | ||
| start_dates.each_slice(SLICE_SIZE).to_a, | ||
| end_dates.each_slice(SLICE_SIZE).to_a, | ||
| values.each_slice(SLICE_SIZE).to_a | ||
| end | ||
|
|
||
| def query_heart_rate(user_id, year, from, to) | ||
| @session.execute(@query_heart_rate, arguments: [user_id, year, from, to]) | ||
| end | ||
|
|
||
| def query_sleep(user_id, year, from, to) | ||
| @session.execute(@query_sleep, arguments: [user_id, year, from, to]) | ||
| end | ||
|
|
||
| def query_calories(user_id, year, from, to) | ||
| @session.execute(@query_calories, arguments: [user_id, year, from, to]) | ||
| end | ||
|
|
||
| def query_distance(user_id, year, from, to) | ||
| @session.execute(@query_distance, arguments: [user_id, year, from, to]) | ||
| end | ||
|
|
||
| def query_steps(user_id, year, from, to) | ||
| @session.execute(@query_steps, arguments: [user_id, year, from, to]) | ||
| end | ||
|
|
||
| def query_activities(user_id, year, from, to) | ||
| @session.execute(@query_activities, arguments: [user_id, year, from, to]) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def init_db | ||
| create_table('heart_rate', 'decimal') | ||
| create_table('sleep', 'decimal') | ||
| create_table('calories', 'decimal') | ||
| create_table('distance', 'decimal') | ||
| create_table('steps', 'decimal') | ||
| create_table('activities', 'varchar') | ||
| end | ||
|
|
||
| def create_table(name, value_type) | ||
| @session.execute(" | ||
| CREATE TABLE IF NOT EXISTS #{name} ( | ||
| user_id text, year int, time timestamp, start_date timestamp, end_date timestamp, value #{value_type}, | ||
| PRIMARY KEY ((user_id, year), time) | ||
| ) | ||
| ") | ||
| end | ||
|
|
||
| def init_insert | ||
| @insert_heart_rate = prepare_insert('heart_rate') | ||
| @insert_sleep = prepare_insert('sleep') | ||
| @insert_calories = prepare_insert('calories') | ||
| @insert_distance = prepare_insert('distance') | ||
| @insert_steps = prepare_insert('steps') | ||
| @insert_activities = prepare_insert('activities') | ||
| end | ||
|
|
||
| def prepare_insert(table_name) | ||
| @session.prepare(" | ||
| INSERT INTO #{table_name} ( | ||
| user_id, year, time, start_date, end_date, value | ||
| ) VALUES ( | ||
| ?, ?, ?, ?, ?, ? | ||
| ) | ||
| ") | ||
| end | ||
|
|
||
| def init_query | ||
| @query_heart_rate = prepare_query('heart_rate') | ||
| @query_sleep = prepare_query('sleep') | ||
| @query_calories = prepare_query('calories') | ||
| @query_distance = prepare_query('distance') | ||
| @query_steps = prepare_query('steps') | ||
| @query_activities = prepare_query('activities') | ||
| end | ||
|
|
||
| def prepare_query(table_name) | ||
| @session.prepare(" | ||
| SELECT time, start_date, end_date, value | ||
| FROM #{table_name} | ||
| WHERE user_id = ? AND year = ? AND time >= ? AND time <= ? | ||
| ORDER BY time ASC | ||
| ") | ||
| end | ||
| end | ||
| end | ||
| end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Waarom is die conversie naar bigdecimal nodig?