discourse/app/models/concerns/reports/trust_level_growth.rb
Martin Brennan 15838aa756
DEV: Convert AdminReport component to gjs (#31011)
This commit converts the `AdminReport` component, which is quite
high complexity, to gjs. After this initial round, ideally this
component would be broken up into smaller components because it is
getting quite big now.

Also in this commit:

* Add an option to display the report description in a tooltip, which
was
   the main way the description was shown until recently. We want to use
   this on the dashboard view mostly.
* Move admin report "mode" definitions to the server-side Report model,
inside a `Report::MODES` constant, collecting the modes defined in
various
   places in the UI into one place
* Refactor report code to refer to mode definitions
* Add a `REPORT_MODES` constant in JS via javascript.rake and refactor
  JS to refer to the modes
* Delete old admin report components that are no longer used
  (trust-level-counts, counts, per-day-counts) which were replaced
  by admin-report-counters a while ago
* Add a new `registerReportModeComponent` plugin API, some plugins
   introduce their own modes (like AI's `emotion`) and components and
   we need a way to render them
2025-01-29 10:33:43 +10:00

71 lines
2.0 KiB
Ruby

# frozen_string_literal: true
module Reports::TrustLevelGrowth
extend ActiveSupport::Concern
class_methods do
def report_trust_level_growth(report)
report.modes = [Report::MODES[:stacked_chart]]
filters = %w[tl1_reached tl2_reached tl3_reached tl4_reached]
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)
ORDER BY date(created_at)
SQL
data = Hash[filters.collect { |x| [x, []] }]
builder = DB.build(sql)
builder.query.each do |row|
filters.each do |filter|
data[filter] << {
x: row.date.strftime("%Y-%m-%d"),
y: row.instance_variable_get("@#{filter}"),
}
end
end
requests =
filters.map do |filter|
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"
{
req: filter,
label: I18n.t("reports.trust_level_growth.xaxis.#{filter}"),
color: color,
data: data[filter],
}
end
report.data = requests
end
end
end