-
Notifications
You must be signed in to change notification settings - Fork 37
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
Improve Sync To User performance (batch 2) #1897
Changes from 5 commits
1ef492c
18247a5
e6c99e6
25b5a20
5f0a890
2a40c22
5129124
a25afe4
a789fd6
b856e3f
d8a8311
bd8306c
a664679
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ def sync_to_user | |
|
||
private | ||
|
||
def region_records | ||
def model_sync_scope | ||
super.for_v3 | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,33 +2,30 @@ module Api::V3::SyncToUser | |
extend ActiveSupport::Concern | ||
|
||
included do | ||
def region_records | ||
model = controller_name.classify.constantize | ||
model.syncable_to_region(current_sync_region) | ||
end | ||
|
||
def current_facility_records | ||
region_records | ||
.where(patient: prioritized_patients) | ||
model_sync_scope | ||
.where(patient: current_facility.syncable_patients.pluck(:id)) | ||
.updated_on_server_since(current_facility_processed_since, limit) | ||
end | ||
|
||
def other_facility_records | ||
other_facilities_limit = limit - current_facility_records.count | ||
other_facilities_limit = limit - current_facility_records.size | ||
other_patient_records = | ||
current_sync_region.syncable_patients.pluck(:id) - current_facility.syncable_patients.pluck(:id) | ||
|
||
region_records | ||
.where.not(patient: prioritized_patients) | ||
model_sync_scope | ||
.where(patient: other_patient_records) | ||
.updated_on_server_since(other_facilities_processed_since, other_facilities_limit) | ||
end | ||
|
||
private | ||
|
||
def records_to_sync | ||
current_facility_records + other_facility_records | ||
def model_sync_scope | ||
controller_name.classify.constantize.for_sync | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I understand why you are returning a AR scope here instead of the actual model, but its a confusing mismatch with the name and conventions around something named There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I wasn't happy with continuing to calling it |
||
end | ||
|
||
def prioritized_patients | ||
current_facility.registered_patients.with_discarded | ||
def records_to_sync | ||
current_facility_records + other_facility_records | ||
end | ||
|
||
def processed_until(records) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -209,4 +209,8 @@ def valid_block | |
errors.add(:zone, "not present in the facility group") | ||
end | ||
end | ||
|
||
def syncable_patients | ||
registered_patients.with_discarded | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a bit dissonant? "Syncable" in general means you are registered in, assigned to, or plan to visit. We're using a simplified definition here because it doesn't really matter when used for facility prioritization. However, does this set us up to do things like facility-level sync incorrectly? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To address the dissonance, I suspect we'd either:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,9 +21,7 @@ class PrescriptionDrug < ApplicationRecord | |
validates :is_protocol_drug, inclusion: {in: [true, false]} | ||
validates :is_deleted, inclusion: {in: [true, false]} | ||
|
||
scope :syncable_to_region, ->(region) { | ||
with_discarded.where(patient: Patient.syncable_to_region(region)) | ||
} | ||
scope :for_sync, -> { with_discarded } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a couple tiny tests for this scope across these models? |
||
|
||
def self.prescribed_as_of(date) | ||
where("device_created_at <= ?", date.end_of_day) | ||
|
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.
Would a couple semantic methods help with readability?
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.
They might, I can play around with this. But our general approach was to reduce the number of methods in this mixin. Every method that we add, is an opportunity for someone to override it in some random place in some controller that inherits from it. The thinner this mixin is in terms of pure method volume, the cleaner the internal API is.
I'll take a shot at this and see if it helps anyway. Will respond back here.