# 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
def cache_key
["metrics/measure/#{key}", @start_at, @end_at, canonicalized_params].join(';')
def key
raise NotImplementedError
def unit
nil
def total_in_time_range?
true
def total
load[:total]
def previous_total
load[:previous_total]
def data
load[:data]
def self.model_name
self.class.name
def read_attribute_for_serialization(key)
send(key) if respond_to?(key)
protected
def load
unless loaded?
@values = Rails.cache.fetch(cache_key, expires_in: CACHE_TTL) { perform_queries }.with_indifferent_access
@loaded = true
@values
def perform_queries
{
total: perform_total_query,
previous_total: perform_previous_total_query,
data: perform_data_query,
}
def perform_total_query
def perform_previous_total_query
def perform_data_query
def time_period
(@start_at..@end_at)
def previous_time_period
((@start_at - length_of_period)..(@end_at - length_of_period))
def length_of_period
@length_of_period ||= @end_at - @start_at
def params
{}
def canonicalized_params
params.to_h.to_a.sort_by { |k, _v| k.to_s }.map { |k, v| "#{k}=#{v}" }.join(';')