Skip to content

Commit

Permalink
Revert "Improve Sync To User performance (batch 2) (#1897)"
Browse files Browse the repository at this point in the history
This reverts commit d78d0ca.
  • Loading branch information
kitallis committed Dec 24, 2020
1 parent 7b61d43 commit cace599
Show file tree
Hide file tree
Showing 19 changed files with 201 additions and 79 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/v3/blood_sugars_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def sync_to_user

private

def model_sync_scope
def region_records
super.for_v3
end

Expand Down
15 changes: 9 additions & 6 deletions app/controllers/api/v3/patients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@ def request_metadata
{request_user_id: current_user.id, request_facility_id: current_facility.id}
end

def region_records
super
.includes(:address, :phone_numbers, :business_identifiers)
end

def current_facility_records
model_sync_scope
.where(id: current_facility.prioritized_patients.pluck(:id))
region_records
.where(registration_facility: current_facility)
.updated_on_server_since(current_facility_processed_since, limit)
end

def other_facility_records
other_facilities_limit = limit - current_facility_records.count
other_patient_records =
current_sync_region.syncable_patients.pluck(:id) - current_facility.prioritized_patients.pluck(:id)

model_sync_scope
.where(id: other_patient_records)
region_records
.where.not(registration_facility: current_facility)
.updated_on_server_since(other_facilities_processed_since, other_facilities_limit)
end

Expand Down
27 changes: 15 additions & 12 deletions app/controllers/concerns/api/v3/sync_to_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,37 @@ 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
model_sync_scope
.where(patient: current_facility.prioritized_patients.pluck(:id))
region_records
.where(patient: prioritized_patients)
.updated_on_server_since(current_facility_processed_since, limit)
end

def other_facility_records
other_facilities_limit = limit - current_facility_records.size
other_patient_records =
current_sync_region.syncable_patients.pluck(:id) - current_facility.prioritized_patients.pluck(:id)
other_facilities_limit = limit - current_facility_records.count

model_sync_scope
.where(patient: other_patient_records)
region_records
.where.not(patient: prioritized_patients)
.updated_on_server_since(other_facilities_processed_since, other_facilities_limit)
end

private

def model_sync_scope
controller_name.classify.constantize.for_sync
end

def records_to_sync
current_facility_records + other_facility_records
end

def prioritized_patients
current_facility.registered_patients.with_discarded
end

def processed_until(records)
records.last.updated_at.strftime(APIController::TIME_WITHOUT_TIMEZONE_FORMAT) if records.any?
records.last.updated_at.strftime(APIController::TIME_WITHOUT_TIMEZONE_FORMAT) if records.present?
end

def response_process_token
Expand Down
4 changes: 3 additions & 1 deletion app/models/appointment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ class Appointment < ApplicationRecord
validates :device_created_at, presence: true
validates :device_updated_at, presence: true

scope :for_sync, -> { with_discarded }
scope :syncable_to_region, ->(region) {
with_discarded.where(patient: Patient.syncable_to_region(region))
}

def self.all_overdue
where(status: "scheduled")
Expand Down
4 changes: 3 additions & 1 deletion app/models/blood_pressure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ class BloodPressure < ApplicationRecord
.where(arel_table[:diastolic].lt(THRESHOLDS[:hypertensive][:diastolic]))
}

scope :for_sync, -> { with_discarded }
scope :syncable_to_region, ->(region) {
with_discarded.where(patient: Patient.syncable_to_region(region))
}

def critical?
systolic >= THRESHOLDS[:critical][:systolic] || diastolic >= THRESHOLDS[:critical][:diastolic]
Expand Down
5 changes: 4 additions & 1 deletion app/models/blood_sugar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ class BloodSugar < ApplicationRecord
V3_TYPES = %i[random post_prandial fasting].freeze

scope :for_v3, -> { where(blood_sugar_type: V3_TYPES) }
scope :for_sync, -> { with_discarded }

scope :syncable_to_region, ->(region) {
with_discarded.where(patient: Patient.syncable_to_region(region))
}

THRESHOLDS = {
high: {random: 300,
Expand Down
4 changes: 3 additions & 1 deletion app/models/encounter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class Encounter < ApplicationRecord
has_many :blood_pressures, through: :observations, source: :observable, source_type: "BloodPressure"
has_many :blood_sugars, through: :observations, source: :observable, source_type: "BloodSugar"

scope :for_sync, -> { with_discarded }
scope :syncable_to_region, ->(region) {
with_discarded.where(patient: Patient.syncable_to_region(region))
}

def self.generate_id(facility_id, patient_id, encountered_on)
UUIDTools::UUID
Expand Down
4 changes: 0 additions & 4 deletions app/models/facility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,6 @@ def valid_block
end
end

def prioritized_patients
registered_patients.with_discarded
end

def self.localized_facility_size(facility_size)
return unless facility_size
I18n.t("activerecord.facility.facility_size.#{facility_size}", default: facility_size.capitalize)
Expand Down
4 changes: 3 additions & 1 deletion app/models/medical_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class MedicalHistory < ApplicationRecord
enum hypertension: MEDICAL_HISTORY_ANSWERS, _prefix: true
enum diagnosed_with_hypertension: MEDICAL_HISTORY_ANSWERS, _prefix: true

scope :for_sync, -> { with_discarded }
scope :syncable_to_region, ->(region) {
with_discarded.where(patient: Patient.syncable_to_region(region))
}

def indicates_hypertension_risk?
prior_heart_attack_boolean || prior_stroke_boolean
Expand Down
3 changes: 1 addition & 2 deletions app/models/patient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ class Patient < ApplicationRecord

attribute :call_result, :string

scope :with_nested_sync_resources, -> { includes(:address, :phone_numbers, :business_identifiers) }
scope :for_sync, -> { with_discarded.with_nested_sync_resources }
scope :syncable_to_region, ->(region) { region.syncable_patients }
scope :search_by_address, ->(term) { joins(:address).merge(Address.search_by_street_or_village(term)) }
scope :with_diabetes, -> { joins(:medical_history).merge(MedicalHistory.diabetes_yes) }
scope :with_hypertension, -> { joins(:medical_history).merge(MedicalHistory.hypertension_yes) }
Expand Down
4 changes: 3 additions & 1 deletion app/models/prescription_drug.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class PrescriptionDrug < ApplicationRecord
validates :is_protocol_drug, inclusion: {in: [true, false]}
validates :is_deleted, inclusion: {in: [true, false]}

scope :for_sync, -> { with_discarded }
scope :syncable_to_region, ->(region) {
with_discarded.where(patient: Patient.syncable_to_region(region))
}

def self.prescribed_as_of(date)
where("device_created_at <= ?", date.end_of_day)
Expand Down
4 changes: 2 additions & 2 deletions app/models/region.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ def syncable_patients
case region_type
when "block"
registered_patients.with_discarded
.or(assigned_patients.with_discarded)
.union(assigned_patients.with_discarded)
.union(appointed_patients.with_discarded)
else
registered_patients.with_discarded
registered_patients
end
end

Expand Down
27 changes: 22 additions & 5 deletions spec/models/appointment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,28 @@
end
end

describe ".for_sync" do
it "includes discarded appointments" do
discarded_appointment = create(:appointment, deleted_at: Time.now)

expect(described_class.for_sync).to include(discarded_appointment)
describe ".syncable_to_region" do
it "returns all patients registered in the region" do
facility_group = create(:facility_group)
facility = create(:facility, facility_group: facility_group)
patient = create(:patient)
other_patient = create(:patient)

allow(Patient).to receive(:syncable_to_region).with(facility_group).and_return([patient])

appointments = [
create(:appointment, patient: patient, facility: facility),
create(:appointment, patient: patient, facility: facility).tap(&:discard),
create(:appointment, patient: patient)
]

_other_appointments = [
create(:appointment, patient: other_patient, facility: facility),
create(:appointment, patient: other_patient, facility: facility).tap(&:discard),
create(:appointment, patient: other_patient)
]

expect(Appointment.syncable_to_region(facility_group)).to contain_exactly(*appointments)
end
end
end
Expand Down
27 changes: 22 additions & 5 deletions spec/models/blood_pressure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,28 @@
end
end

describe ".for_sync" do
it "includes discarded blood pressures" do
discarded_bp = create(:blood_pressure, deleted_at: Time.now)

expect(described_class.for_sync).to include(discarded_bp)
describe ".syncable_to_region" do
it "returns all patients registered in the region" do
facility_group = create(:facility_group)
facility = create(:facility, facility_group: facility_group)
patient = create(:patient)
other_patient = create(:patient)

allow(Patient).to receive(:syncable_to_region).with(facility_group).and_return([patient])

blood_pressures = [
create(:blood_pressure, patient: patient, facility: facility),
create(:blood_pressure, patient: patient, facility: facility).tap(&:discard),
create(:blood_pressure, patient: patient)
]

_other_blood_pressures = [
create(:blood_pressure, patient: other_patient, facility: facility),
create(:blood_pressure, patient: other_patient, facility: facility).tap(&:discard),
create(:blood_pressure, patient: other_patient)
]

expect(BloodPressure.syncable_to_region(facility_group)).to contain_exactly(*blood_pressures)
end
end
end
Expand Down
27 changes: 22 additions & 5 deletions spec/models/blood_sugar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
end
end

describe "Scopes" do
describe "scopes" do
let!(:fasting) { create(:blood_sugar, blood_sugar_type: :fasting) }
let!(:random) { create(:blood_sugar, blood_sugar_type: :random) }
let!(:post_prandial) { create(:blood_sugar, blood_sugar_type: :post_prandial) }
Expand All @@ -50,11 +50,28 @@
end
end

describe ".for_sync" do
it "includes discarded blood sugars" do
discarded_blood_sugar = create(:blood_sugar, deleted_at: Time.now)
describe ".syncable_to_region" do
it "returns all patients registered in the region" do
facility_group = create(:facility_group)
facility = create(:facility, facility_group: facility_group)
patient = create(:patient)
other_patient = create(:patient)

expect(described_class.for_sync).to include(discarded_blood_sugar)
allow(Patient).to receive(:syncable_to_region).with(facility_group).and_return([patient])

blood_sugars = [
create(:blood_sugar, patient: patient, facility: facility),
create(:blood_sugar, patient: patient, facility: facility).tap(&:discard),
create(:blood_sugar, patient: patient)
]

_other_blood_sugars = [
create(:blood_sugar, patient: other_patient, facility: facility),
create(:blood_sugar, patient: other_patient, facility: facility).tap(&:discard),
create(:blood_sugar, patient: other_patient)
]

expect(BloodSugar.syncable_to_region(facility_group)).to contain_exactly(*blood_sugars)
end
end
end
Expand Down
27 changes: 22 additions & 5 deletions spec/models/encounter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,28 @@
end

describe "Scopes" do
describe ".for_sync" do
it "includes discarded encounters" do
discarded_encounter = create(:encounter, deleted_at: Time.now)

expect(described_class.for_sync).to include(discarded_encounter)
describe ".syncable_to_region" do
it "returns all patients registered in the region" do
facility_group = create(:facility_group)
facility = create(:facility, facility_group: facility_group)
patient = create(:patient)
other_patient = create(:patient)

allow(Patient).to receive(:syncable_to_region).with(facility_group).and_return([patient])

encounters = [
create(:encounter, patient: patient, facility: facility),
create(:encounter, patient: patient, facility: facility).tap(&:discard),
create(:encounter, patient: patient)
]

_other_encounters = [
create(:encounter, patient: other_patient, facility: facility),
create(:encounter, patient: other_patient, facility: facility).tap(&:discard),
create(:encounter, patient: other_patient)
]

expect(Encounter.syncable_to_region(facility_group)).to contain_exactly(*encounters)
end
end
end
Expand Down
23 changes: 19 additions & 4 deletions spec/models/medical_history_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,26 @@
end

describe "Scopes" do
describe ".for_sync" do
it "includes discarded medical histories" do
discarded_medical_history = create(:medical_history, deleted_at: Time.now)
describe ".syncable_to_region" do
it "returns all patients registered in the region" do
facility_group = create(:facility_group)
patient = create(:patient)
other_patient = create(:patient)

expect(described_class.for_sync).to include(discarded_medical_history)
allow(Patient).to receive(:syncable_to_region).with(facility_group).and_return([patient])

MedicalHistory.destroy_all
medical_histories = [
create(:medical_history, patient: patient),
create(:medical_history, patient: patient).tap(&:discard)
]

_other_medical_histories = [
create(:medical_history, patient: other_patient),
create(:medical_history, patient: other_patient).tap(&:discard)
]

expect(MedicalHistory.syncable_to_region(facility_group)).to contain_exactly(*medical_histories)
end
end
end
Expand Down
Loading

0 comments on commit cace599

Please sign in to comment.