-
Notifications
You must be signed in to change notification settings - Fork 34
/
missed_visits_query.rb
95 lines (84 loc) · 3.88 KB
/
missed_visits_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
class MyFacilities::MissedVisitsQuery
# 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
attr_reader :periods
def initialize(facilities: Facility.all, period: :quarter, last_n: 3)
# period can be :quarter, :month.
# last_n is the number of quarters/months for which data is to be returned
@facilities = facilities
@period = period
@periods = period_list(period, last_n)
@latest_period = @periods.first
@last_n = last_n
end
def calls_made
period_start = (@period == :quarter ? local_quarter_start(*@latest_period) : local_month_start(*@latest_period))
period_end = @period == :quarter ? period_start.end_of_quarter : period_start.end_of_month
@calls_made ||=
CallLog
.result_completed
.joins('INNER JOIN phone_number_authentications
ON phone_number_authentications.phone_number = call_logs.caller_phone_number')
.joins("INNER JOIN facilities ON facilities.id = phone_number_authentications.registration_facility_id")
.where(phone_number_authentications: {registration_facility_id: @facilities})
.where("call_logs.created_at >= ? AND call_logs.created_at <= ?", period_start, period_end)
.group("facilities.id::uuid")
end
def bp_query_by_cohort
@bp_query_by_cohort ||=
@periods.map { |year, period|
bp_query = if @period == :month
MyFacilities::BloodPressureControlQuery.new(facilities: @facilities,
cohort_period: {cohort_period: :month,
registration_year: year,
registration_month: period})
else
MyFacilities::BloodPressureControlQuery.new(facilities: @facilities,
cohort_period: {cohort_period: :quarter,
registration_year: year,
registration_quarter: period})
end
[[year, period], bp_query]
}.to_h
end
def missed_visits_by_facility
@missed_visits_by_facility ||=
bp_query_by_cohort.map { |(year, period), bp_query|
bp_query.cohort_registrations.group(:registration_facility_id).count.map { |facility_id, patient_count|
[[facility_id, year, period],
{patients: patient_count.to_i,
missed: bp_query.cohort_missed_visits_count_by_facility[facility_id].to_i}]
}.to_h
}.reduce(:merge)
end
def missed_visit_totals
@missed_visit_totals ||=
bp_query_by_cohort.map { |(year, period), bp_query|
cohort_registrations = bp_query.cohort_registrations.count
cohort_missed_visits_count = bp_query.cohort_missed_visits_count
[[year, period],
{patients: cohort_registrations.to_i,
missed: cohort_missed_visits_count.to_i}]
}.to_h
end
def total_registrations
registrations_query = MyFacilities::RegistrationsQuery.new(facilities: @facilities,
period: @selected_period,
last_n: @last_n)
@total_registrations ||=
registrations_query.total_registrations.group(:registration_facility_id).count
end
private
def period_list(period, last_n)
case period
when :quarter
last_n_quarters(n: last_n, inclusive: false)
when :month
last_n_months(n: last_n, inclusive: false)
.map { |month| [month.year, month.month] }
end
end
end