Skip to content
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 1) #1896

Merged
merged 4 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions app/controllers/api/v3/facilities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,29 @@ def force_resync?
end

def records_to_sync
Facility
.updated_on_server_since(other_facilities_processed_since, limit)
.includes(:facility_group)
.where.not(facility_group: nil)
if Flipper.enabled?(:regions_prep)
other_facility_records
.with_block_region_id
.includes(:facility_group)
.where.not(facility_group: nil)
else
other_facility_records
.includes(:facility_group)
.where.not(facility_group: nil)
end
end

private

# Memoize this call so that we don't end up making thousands of calls to check user for each facility
def block_level_sync?
return @block_level_sync_enabled if defined? @block_level_sync_enabled
@block_level_sync_enabled = current_user&.block_level_sync?
end

def sync_region_id(facility)
if current_user&.block_level_sync?
facility.region.block_region.id
if block_level_sync?
facility.block_region_id
else
facility.facility_group_id
end
Expand Down
5 changes: 5 additions & 0 deletions app/models/facility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class Facility < ApplicationRecord
foreign_key: "assigned_facility_id"

pg_search_scope :search_by_name, against: {name: "A", slug: "B"}, using: {tsearch: {prefix: true, any_word: true}}
scope :with_block_region_id, -> {
joins("INNER JOIN regions facility_regions ON facility_regions.source_id = facilities.id")
.joins("INNER JOIN regions block_region ON block_region.path @> facility_regions.path AND block_region.region_type = 'block'")
.select("block_region.id AS block_region_id, facilities.*")
}

enum facility_size: {
community: "community",
Expand Down