discourse/app/models/concerns/reports/dau_by_mau.rb
Natalie Tay 628520e541
PERF: Compute dashboard KPIs in a single pass (#40705)
Each KPI requested its report with a `prev_period` facet, which runs a
second scan over the previous period just to produce the comparison
value.

This PR now requests the report once over the combined [prev_start, end]
window and split the daily series by date: current is [start, end],
previous is the equal-length window before it, so we make one scan
instead of two.
2026-06-10 21:09:27 +08:00

56 lines
1.7 KiB
Ruby
Vendored

# frozen_string_literal: true
module Reports::DauByMau
extend ActiveSupport::Concern
class_methods do
def report_dau_by_mau(report)
report.labels = [
{ type: :date, property: :x, title: I18n.t("reports.default.labels.day") },
{ type: :percent, property: :y, title: I18n.t("reports.default.labels.percent") },
]
report.average = true
report.percent = true
data_start = report.facets.include?(:prev_period) ? report.prev_start_date : report.start_date
data_points = UserVisit.count_by_active_users(data_start, report.end_date)
report.data = []
compute_dau_by_mau =
Proc.new do |data_point|
if data_point["mau"] == 0
0
else
((data_point["dau"].to_f / data_point["mau"].to_f) * 100).ceil(2)
end
end
dau_avg =
Proc.new do |points|
if !points.empty?
sum = points.sum { |data_point| compute_dau_by_mau.call(data_point) }
(sum.to_f / points.count.to_f).ceil(2)
end
end
if report.facets.include?(:prev_period)
current_start = report.start_date.to_date
data_points, prev_data_points =
data_points.partition { |data_point| data_point["date"].to_date >= current_start }
report.prev_period = dau_avg.call(prev_data_points)
end
data_points.each do |data_point|
report.data << { x: data_point["date"], y: compute_dau_by_mau.call(data_point) }
end
if report.facets.include?(:prev30Days)
prev30_days_data =
UserVisit.count_by_active_users(report.start_date - 30.days, report.start_date)
report.prev30Days = dau_avg.call(prev30_days_data)
end
end
end
end