2020-10-29 11:49:09 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Reports::TrustLevelGrowth
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
def report_trust_level_growth(report)
|
|
|
|
report.modes = [:stacked_chart]
|
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
filters = %w[tl1_reached tl2_reached tl3_reached tl4_reached]
|
2020-10-29 11:49:09 -05:00
|
|
|
|
|
|
|
sql = <<~SQL
|
|
|
|
SELECT
|
|
|
|
date(created_at),
|
|
|
|
(
|
|
|
|
count(*) filter (WHERE previous_value::integer < 1 AND new_value = '1')
|
|
|
|
) as tl1_reached,
|
|
|
|
(
|
|
|
|
count(*) filter (WHERE previous_value::integer < 2 AND new_value = '2')
|
|
|
|
) as tl2_reached,
|
|
|
|
(
|
|
|
|
count(*) filter (WHERE previous_value::integer < 3 AND new_value = '3')
|
|
|
|
) as tl3_reached,
|
|
|
|
(
|
|
|
|
count(*) filter (WHERE previous_value::integer < 4 AND new_value = '4')
|
|
|
|
) as tl4_reached
|
|
|
|
FROM user_histories
|
|
|
|
WHERE (
|
|
|
|
created_at >= '#{report.start_date}'
|
|
|
|
AND created_at <= '#{report.end_date}'
|
|
|
|
)
|
|
|
|
AND (
|
|
|
|
action = #{UserHistory.actions[:change_trust_level]}
|
|
|
|
OR action = #{UserHistory.actions[:auto_trust_level_change]}
|
|
|
|
)
|
|
|
|
GROUP BY date(created_at)
|
2022-11-16 21:11:09 -06:00
|
|
|
ORDER BY date(created_at)
|
2020-10-29 11:49:09 -05:00
|
|
|
SQL
|
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
data = Hash[filters.collect { |x| [x, []] }]
|
2020-10-29 11:49:09 -05:00
|
|
|
|
|
|
|
builder = DB.build(sql)
|
|
|
|
builder.query.each do |row|
|
|
|
|
filters.each do |filter|
|
|
|
|
data[filter] << {
|
|
|
|
x: row.date.strftime("%Y-%m-%d"),
|
2023-01-09 06:20:10 -06:00
|
|
|
y: row.instance_variable_get("@#{filter}"),
|
2020-10-29 11:49:09 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
requests =
|
|
|
|
filters.map do |filter|
|
2023-05-04 01:35:19 -05:00
|
|
|
color = report.colors[0]
|
|
|
|
color = report.colors[1] if filter == "tl1_reached"
|
|
|
|
color = report.colors[2] if filter == "tl2_reached"
|
|
|
|
color = report.colors[3] if filter == "tl3_reached"
|
2020-10-29 11:49:09 -05:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
{
|
|
|
|
req: filter,
|
|
|
|
label: I18n.t("reports.trust_level_growth.xaxis.#{filter}"),
|
|
|
|
color: color,
|
|
|
|
data: data[filter],
|
|
|
|
}
|
|
|
|
end
|
2020-10-29 11:49:09 -05:00
|
|
|
|
|
|
|
report.data = requests
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|