Skip to content

Commit 78d80c1

Browse files
aagrawalrtslAyushi Agrawal
andauthored
adding partitioned table for reporting_facility_monthly_follow_ups_a… (#5746)
…nd_registrations **Story card:** [sc-17573](https://app.shortcut.com/simpledotorg/story/17573/move-reporting-facility-monthly-follow-ups-and-registrations-to-a-partitioned-table) ## Because We need to move a our mat views to partitioned table ## This addresses Creating a partitioned table reporting_facility_monthly_follow_ups_and_registrations under simple_reporting schema Both mat view and partitioned table version will exist for a small time for monitoring in prod ## Test instructions Suite test --------- Co-authored-by: Ayushi Agrawal <ayushiagrawal@RTSL-P172G93770.local>
1 parent fdeafc3 commit 78d80c1

9 files changed

+520
-31
lines changed

app/models/reports/facility_monthly_follow_up_and_registration.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ def self.materialized?
88
true
99
end
1010

11+
def self.partitioned?
12+
true
13+
end
14+
1115
NON_COUNT_FIELDS = %i[
1216
block_region_id
1317
district_region_id

app/models/reports/patient_state.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ def self.partitioned?
1717
true
1818
end
1919

20-
def self.partitioned_refresh(refresh_month)
21-
ActiveRecord::Base.connection.exec_query(
22-
"CALL simple_reporting.add_shard_to_table('#{refresh_month}', 'reporting_patient_states')"
23-
)
24-
end
25-
2620
def self.by_assigned_region(region_or_source)
2721
region = region_or_source.region
2822
where("assigned_#{region.region_type}_region_id" => region.id)

app/models/reports/view.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,11 @@ def self.fetch_refresh_month_for_date(date_of_refresh)
5353
month_offset = (day_of_month / 2) + 1
5454
day_of_month.odd? ? month_of_refresh.prev_month : (month_of_refresh - month_offset.month)
5555
end
56+
57+
def self.partitioned_refresh(refresh_month)
58+
ActiveRecord::Base.connection.exec_query(
59+
"CALL simple_reporting.add_shard_to_table('#{refresh_month}', '#{table_name}')"
60+
)
61+
end
5662
end
5763
end

db/migrate/20251219061210_create_partitioned_table_reporting_facility_monthly_follow_ups_and_registrations.rb

Lines changed: 236 additions & 0 deletions
Large diffs are not rendered by default.

db/structure.sql

Lines changed: 245 additions & 1 deletion
Large diffs are not rendered by default.

lib/tasks/reporting.rake

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@
22

33
namespace :reporting do
44
desc "Do a full refresh of the partitioned reporting table"
5-
task full_partitioned_refresh: :environment do
6-
reporting_tables = %w[
7-
Reports::PatientState
8-
].freeze
5+
task :full_partitioned_refresh, [:table_name] => :environment do |t, args|
6+
unless args[:table_name].present?
7+
puts "ERROR: table_name is required."
8+
puts "Usage: rake reporting:full_partitioned_refresh[reporting_patient_states]"
9+
exit 1
10+
end
911

10-
reporting_tables.each do |reporting_table|
11-
klass = reporting_table.constantize
12-
ActiveRecord::Base.connection.exec_query(
13-
"TRUNCATE TABLE simple_reporting.#{klass.table_name}"
14-
)
15-
Reports::Month.order(:month_date).select(:month_date).each_with_index do |reporting_month, index|
16-
RefreshReportingPartitionedTableJob.set(wait_until: Time.now + (index * 45).minutes).perform_async(reporting_month.month_date.to_s, klass.table_name)
17-
end
12+
reporting_table = args[:table_name]
13+
Reports::Month.order(:month_date).select(:month_date).each_with_index do |reporting_month, index|
14+
RefreshReportingPartitionedTableJob.set(wait_until: Time.now + (index * 45).minutes).perform_async(reporting_month.month_date.to_s, reporting_table)
1815
end
1916
end
2017

spec/models/reports/facility_monthly_follow_up_and_registration_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,10 @@
225225
expect(total.monthly_follow_ups_htn_only + total.monthly_follow_ups_htn_and_dm).to eq(dashboard_htn_follow_ups)
226226
expect(total.monthly_follow_ups_dm_only + total.monthly_follow_ups_htn_and_dm).to eq(dashboard_dm_follow_ups)
227227
end
228+
229+
describe "#partitioned?" do
230+
it "returns true" do
231+
expect(described_class.partitioned?).to be(true)
232+
end
233+
end
228234
end

spec/models/reports/patient_state_spec.rb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -863,18 +863,6 @@ def patient_states(patient, from: nil, to: nil)
863863
end
864864
end
865865

866-
describe "#partitioned_refresh" do
867-
let(:refresh_month) { Date.new(2024, 6, 1) }
868-
869-
it "executes the correct SQL to refresh the partition" do
870-
expect(ActiveRecord::Base.connection).to receive(:exec_query).with(
871-
"CALL simple_reporting.add_shard_to_table('#{refresh_month}', 'reporting_patient_states')"
872-
)
873-
874-
described_class.partitioned_refresh(refresh_month)
875-
end
876-
end
877-
878866
context "screening" do
879867
it "doesn't consider the patient if it's only screened" do
880868
diagnosed_patient = create(:patient, recorded_at: Date.new(2024, 5, 1), diagnosed_confirmed_at: Date.new(2024, 6, 1))

spec/models/reports/view_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,18 @@ def table_descriptions_sql(table_name)
8080
end
8181
end
8282
end
83+
84+
describe "#partitioned_refresh" do
85+
let(:refresh_month) { Date.new(2024, 6, 1) }
86+
87+
it "executes the correct SQL to refresh the partition" do
88+
[Reports::PatientState, Reports::FacilityMonthlyFollowUpAndRegistration].each do |klass|
89+
expect(ActiveRecord::Base.connection).to receive(:exec_query).with(
90+
"CALL simple_reporting.add_shard_to_table('#{refresh_month}', '#{klass.table_name}')"
91+
)
92+
93+
klass.partitioned_refresh(refresh_month)
94+
end
95+
end
96+
end
8397
end

0 commit comments

Comments
 (0)