Skip to content

Commit

Permalink
Improve Sync To User performance (batch 1) (#1896)
Browse files Browse the repository at this point in the history
Optimize facilities controller

Co-authored-by: Prabhanshu Gupta <[email protected]>
  • Loading branch information
kitallis and prabhanshuguptagit authored Dec 22, 2020
1 parent f680f3c commit 518c477
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
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

0 comments on commit 518c477

Please sign in to comment.