/
registrations_query.rb
51 lines (43 loc) · 1.44 KB
/
registrations_query.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class MyFacilities::RegistrationsQuery
# Wrap query method calls with the appropriate timezone in which the reports will be consumed
# This is probably the Rails.application.config.country[:time_zone]
# Example: `Time.use_zone('timezone string') { bp_control_query_object.cohort_registrations }`
include QuarterHelper
include MonthHelper
include DayHelper
attr_reader :periods
def initialize(facilities: Facility.all, period: :quarter, last_n: 3)
# period can be :quarter, :month, :day.
# last_n is the number of quarters/months/days for which data is to be returned
@facilities = facilities
@period = period
@periods = period_list(period, last_n)
end
def registrations
@registrations ||=
PatientRegistrationsPerDayPerFacility
.where(facility: @facilities)
.where("(year, #{@period}) IN (#{periods_as_sql_list})")
end
def total_registrations
@total_registrations ||=
Patient
.with_hypertension
.where(registration_facility: @facilities)
end
private
def period_list(period, last_n)
case period
when :quarter
last_n_quarters(n: last_n, inclusive: true)
when :month
last_n_months(n: last_n, inclusive: true)
.map { |month| [month.year, month.month] }
when :day
last_n_days(n: last_n)
end
end
def periods_as_sql_list
@periods.map { |year, period| "('#{year}', '#{period}')" }.join(",")
end
end