Skip to content

Commit 5c9a372

Browse files
authored
feat: Dr. Rai Statins Indicator (#5644)
**Story card:** [sc-16076](https://app.shortcut.com/simpledotorg/story/16076/dr-rai-statins-indicator) ## Because Statins Indicator is the final indicator of the set we choose to launch with. This indicator has SQL as its datasource — as opposed to the data in the Dashboard. ## This addresses - Leveraging the mechanism to use SQL queries as a datasource (from #5643); upgrading assumptions where necessary - Creating the Statins Indicator (alongside its query) - Adding the Statins Indicator to the Dr. Rai component - Smoothening out component decisions ## Test instructions suite tests
1 parent a55e05c commit 5c9a372

21 files changed

+476
-395
lines changed

.github/CODEOWNERS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
/config/data/medications.yml @harimohanraj89
88

9+
# Igbanam's
10+
*dr_rai* @igbanam
11+
912
# See docs and examples for this file at
1013

1114
# https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/about-code-owners

app/assets/stylesheets/dr_rai.scss

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,15 +314,27 @@ $font-condensed: "Roboto Condensed", sans-serif;
314314
.link-button {
315315
background-color: transparent;
316316
border: none;
317-
color: #007eff;
318-
cursor: pointer;
319317
font-size: 16px;
320318
padding: 5px 0px;
321319
text-align: left;
322320

323-
&:hover {
324-
text-decoration: underline;
325-
text-underline-offset: 3px;
321+
&-active {
322+
cursor: pointer;
323+
color: #007eff;
324+
325+
&:hover {
326+
text-decoration: underline;
327+
text-underline-offset: 3px;
328+
}
329+
}
330+
331+
&-inactive {
332+
color: #8cc5ff;
333+
cursor: not-allowed;
334+
335+
&:hover {
336+
text-decoration: none;
337+
}
326338
}
327339
}
328340

app/components/dashboard/dr_rai_report.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
<%# <button class="link-button edit">Edit</button> %>
7575
<ul>
7676
<% indicators.each do |indicator| %>
77-
<li><button class="link-button advancer" data-target-type="<%= indicator.target_type %>" data-target-ui="<%= indicator.target_type_frontend %>" data-indicator-id=<%= indicator.id %> data-indicator-denominator="<%= indicator_denominator(indicator) %>" data-indicator-previous-numerator="<%= indicator_previous_numerator(indicator) %>" data-indicator-action="<%= indicator.action %>" data-indicator-action-active="<%= indicator.action_active %>" data-indicator-action-passive="<%= indicator.action_passive %>" data-indicator-unit="<%= indicator.unit %>"><%= indicator.display_name %></button></li>
77+
<li><button role="button" class="link-button advancer <%= indicator.has_action_plans? ? "link-button-inactive" : "link-button-active" %>" <%= "disabled" if indicator.has_action_plans? %> data-target-type="<%= indicator.target_type %>" data-target-ui="<%= indicator.target_type_frontend %>" data-indicator-id=<%= indicator.id %> data-indicator-denominator="<%= indicator_denominator(indicator) %>" data-indicator-previous-numerator="<%= indicator_previous_numerator(indicator) %>" data-indicator-action="<%= indicator.action %>" data-indicator-action-active="<%= indicator.action_active %>" data-indicator-action-passive="<%= indicator.action_passive %>" data-indicator-unit="<%= indicator.unit %>"><%= indicator.display_name %></button></li>
7878
<% end %>
7979
</ul>
8080
</div>

app/components/dashboard/dr_rai_report.rb

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Root component for Dr. Rai Reports
22
class Dashboard::DrRaiReport < ApplicationComponent
3-
attr_reader :quarterlies, :indicators, :region, :action_plans
3+
attr_reader :quarterlies, :region
44
attr_accessor :selected_period
55

66
def initialize(quarterlies, region_slug, selected_quarter = nil, lite = false)
@@ -12,21 +12,31 @@ def initialize(quarterlies, region_slug, selected_quarter = nil, lite = false)
1212
else
1313
Period.new(type: :quarter, value: selected_quarter)
1414
end
15-
@action_plans = DrRai::ActionPlan
15+
end
16+
17+
def indicators
18+
@indicators ||= custom_indicators
19+
end
20+
21+
def action_plans
22+
@action_plans ||= DrRai::ActionPlan
1623
.includes(:dr_rai_target)
1724
.where(
1825
region: @region,
1926
dr_rai_target: {period: @selected_period.value.to_s}
2027
)
21-
@indicators = custom_indicators
28+
end
29+
30+
def existing_indicators
31+
action_plans.map { |ap| ap.indicator.type }
2232
end
2333

2434
def custom_indicators
2535
return nil if @lite
2636
return [] unless region.source_type == "Facility"
27-
DrRai::Indicator.all.filter do |indicator|
28-
indicator.is_supported?(region)
29-
end
37+
DrRai::Indicator
38+
.all
39+
.filter { |indicator| indicator.is_supported?(region) }
3040
end
3141

3242
def indicator_previous_numerator(indicator)

app/models/dr_rai/data/statin.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class DrRai::Data::Statin < ApplicationRecord
2+
include DrRai::Chartable
3+
4+
default_scope { where(month_date: 1.year.ago..Date.today) }
5+
6+
chartable_internal_keys :eligible_patients, :patients_prescribed_statins
7+
chartable_period_key :month_date
8+
chartable_outer_grouping :aggregate_root
9+
end

app/models/dr_rai/indicator.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,36 @@ def period
2929
Period.new(type: :quarter, value: target.period)
3030
end
3131

32+
def has_action_plans?
33+
@region_exists ||= DrRai::ActionPlan
34+
.joins(:dr_rai_indicator)
35+
.where(dr_rai_indicator: {type: type})
36+
.exists?
37+
end
38+
3239
def target_type
3340
DrRai::Target::TYPES[target_type_frontend]
3441
end
3542

3643
def numerator(region, the_period = period)
44+
0 unless is_supported?(region)
3745
numerators(region)[the_period]
3846
end
3947

4048
def denominator(region, the_period = period)
49+
0 unless is_supported?(region)
4150
denominators(region)[the_period]
4251
end
4352

4453
def numerators(region)
54+
[] unless is_supported?(region)
4555
datasource(region).map do |t, data|
4656
[t, data[numerator_key]]
4757
end.to_h
4858
end
4959

5060
def denominators(region)
61+
[] unless is_supported?(region)
5162
datasource(region).map do |t, data|
5263
[t, data[denominator_key]]
5364
end.to_h
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module DrRai
2+
class StatinsIndicator < Indicator
3+
attr_reader :region
4+
5+
def datasource(region)
6+
@region = region
7+
@query ||= DrRai::Data::Statin.chartable
8+
if @query.key? region.name
9+
@query[region.name]
10+
else
11+
@query[region.slug]
12+
end
13+
end
14+
15+
def display_name
16+
"Statins"
17+
end
18+
19+
def target_type_frontend
20+
"percent"
21+
end
22+
23+
def numerator_key
24+
:patients_prescribed_statins
25+
end
26+
27+
def denominator_key
28+
:eligible_patients
29+
end
30+
31+
def unit
32+
"patients"
33+
end
34+
35+
def action_passive
36+
"prescribed statins"
37+
end
38+
39+
def action_active
40+
"Prescribe statins for"
41+
end
42+
43+
def percentage
44+
raise "Unimplemented"
45+
end
46+
47+
def is_supported?(region)
48+
@is_supported ||= (datasource(region).present? && true)
49+
end
50+
end
51+
end

app/models/dr_rai/titration_indicator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def unit
3737
end
3838

3939
def is_supported?(region)
40-
case CountryConfig.current[:name]
40+
@is_supported ||= case CountryConfig.current[:name]
4141
when "Bangladesh"
4242
region.path.split(".").include?("nhf")
4343
when "Ethiopia"

app/queries/dr_rai/indicator_function.rb

Lines changed: 0 additions & 28 deletions
This file was deleted.

app/queries/dr_rai/query_factory.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ def for klazz, from: nil, to: nil
1717

1818
if klazz <= Data::Titration
1919
instance = DrRai::TitrationQueryFactory.new(from, to)
20+
elsif klazz <= Data::Statin
21+
instance = DrRai::StatinsQueryFactory.new(from, to)
2022
else
2123
raise "Unsupported"
2224
end
@@ -51,6 +53,10 @@ def updater
5153
raise "Unimplemented"
5254
end
5355

56+
def months_between
57+
(@to_date.year * 12 + @to_date.month) - (@from_date.year * 12 + @from_date.month)
58+
end
59+
5460
private
5561

5662
def set_date_boundaries!

0 commit comments

Comments
 (0)