Files
jcoffey-dev 319ed76dbd
Crowdin / Upload translations / upload-translations (push) Skipped
Historical data migration test / test (14-alpine) (push) Canceled after 0s
Historical data migration test / test (15-alpine) (push) Canceled after 0s
Historical data migration test / test (16-alpine) (push) Canceled after 0s
Historical data migration test / test (17-alpine) (push) Canceled after 0s
Bundler Audit / security (push) Canceled after 0s
Haml Linting / lint (push) Canceled after 0s
JavaScript Linting / lint (push) Canceled after 0s
Ruby Linting / lint (push) Canceled after 0s
JavaScript Testing / test (push) Canceled after 0s
Ruby Testing / build (production) (push) Canceled after 0s
Ruby Testing / test (.ruby-version) (push) Canceled after 0s
Ruby Testing / test (3.3) (push) Canceled after 0s
Ruby Testing / test (3.4) (push) Canceled after 0s
Ruby Testing / End to End testing (.ruby-version) (push) Canceled after 0s
Ruby Testing / End to End testing (3.3) (push) Canceled after 0s
Ruby Testing / End to End testing (3.4) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Canceled after 0s
Check i18n / check-i18n (push) Canceled after 0s
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (javascript) (push) Canceled after 0s
CodeQL / Analyze (ruby) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (.ruby-version, opensearchproject/opensearch:2) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (3.3, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (3.4, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Canceled after 0s
Check formatting / lint (push) Canceled after 0s
CSS Linting / lint (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:8.19.2) (push) Canceled after 0s
Chromatic / Check for relevant changes (push) Canceled after 0s
Chromatic / Run Chromatic (push) Canceled after 0s
Import Mastodon v4.7.3
Snapshot of upstream tag v4.7.3 (commit 8ff11c2bb7b9f0714e947b10fa3629c6542c7121) from
https://github.com/mastodon/mastodon, unmodified.
2026-10-05 22:03:13 -07:00

127 lines
2.2 KiB
Ruby

# frozen_string_literal: true
class Admin::Metrics::Measure::BaseMeasure
CACHE_TTL = 5.minutes.freeze
def self.with_params?
false
end
attr_reader :loaded
alias loaded? loaded
def initialize(start_at, end_at, params)
@start_at = start_at&.to_datetime
@end_at = end_at&.to_datetime
@params = params
@loaded = false
if @start_at.present? && @end_at.present?
@start_at = [@start_at, @end_at - 2.years].max
else
@start_at = nil
@end_at = nil
end
end
def cache_key
["metrics/measure/#{key}", @start_at, @end_at, canonicalized_params].join(';')
end
def key
raise NotImplementedError
end
def unit
nil
end
def total_in_time_range?
true
end
def total
load[:total]
end
def previous_total
load[:previous_total]
end
def data
load[:data]
end
def self.model_name
self.class.name
end
def read_attribute_for_serialization(key)
send(key) if respond_to?(key)
end
protected
def load
unless loaded?
@values = Rails.cache.fetch(cache_key, expires_in: CACHE_TTL) { perform_queries }.with_indifferent_access
@loaded = true
end
@values
end
def perform_queries
{
total: perform_total_query,
previous_total: perform_previous_total_query,
data: perform_data_query,
}
end
def perform_total_query
raise NotImplementedError
end
def perform_previous_total_query
raise NotImplementedError
end
def perform_data_query
raise NotImplementedError
end
def time_period
(@start_at.to_date..@end_at.to_date)
end
def previous_time_period
((@start_at.to_date - (length_of_period + 1))..(@end_at.to_date - (length_of_period + 1)))
end
def length_of_period
@length_of_period ||= @end_at.to_date - @start_at.to_date
end
def params
{}
end
def canonicalized_params
params.to_h.to_a.sort_by { |k, _v| k.to_s }.map { |k, v| "#{k}=#{v}" }.join(';')
end
def earliest_status_id
snowflake_id(@start_at.beginning_of_day)
end
def latest_status_id
snowflake_id(@end_at.end_of_day)
end
def snowflake_id(datetime)
Mastodon::Snowflake.id_at(datetime, with_random: false)
end
end