2024-01-24 18:38:10 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class AnnualReport::Percentiles < AnnualReport::Source
|
2024-11-04 19:11:06 +09:00
|
|
|
def self.prepare(year)
|
|
|
|
AnnualReport::StatusesPerAccountCount.connection.exec_query(<<~SQL.squish, nil, [year, Mastodon::Snowflake.id_at(DateTime.new(year).beginning_of_year), Mastodon::Snowflake.id_at(DateTime.new(year).end_of_year)])
|
|
|
|
INSERT INTO annual_report_statuses_per_account_counts (year, account_id, statuses_count)
|
|
|
|
SELECT $1, account_id, count(*)
|
|
|
|
FROM statuses
|
|
|
|
WHERE id BETWEEN $2 AND $3
|
|
|
|
AND (local OR uri IS NULL)
|
|
|
|
GROUP BY account_id
|
|
|
|
ON CONFLICT (year, account_id) DO NOTHING
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2024-01-24 18:38:10 +09:00
|
|
|
def generate
|
|
|
|
{
|
|
|
|
percentiles: {
|
2024-11-04 19:11:06 +09:00
|
|
|
statuses: 100.0 - ((total_with_fewer_statuses / (total_with_any_statuses + 1.0)) * 100),
|
2024-01-24 18:38:10 +09:00
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def statuses_created
|
2024-09-05 04:50:33 +09:00
|
|
|
@statuses_created ||= report_statuses.count
|
2024-01-24 18:38:10 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def total_with_fewer_statuses
|
2024-11-04 19:11:06 +09:00
|
|
|
@total_with_fewer_statuses ||= AnnualReport::StatusesPerAccountCount.where(year: year).where(statuses_count: ...statuses_created).count
|
2024-01-24 18:38:10 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def total_with_any_statuses
|
2024-11-04 19:11:06 +09:00
|
|
|
@total_with_any_statuses ||= AnnualReport::StatusesPerAccountCount.where(year: year).count
|
2024-01-24 18:38:10 +09:00
|
|
|
end
|
|
|
|
end
|