2021-10-15 05:44:59 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Admin::Metrics::Measure::BaseMeasure
|
2022-02-23 01:27:08 +11:00
|
|
|
CACHE_TTL = 5.minutes.freeze
|
|
|
|
|
2021-11-25 23:07:38 +11:00
|
|
|
def self.with_params?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2022-02-23 01:27:08 +11:00
|
|
|
attr_reader :loaded
|
|
|
|
|
|
|
|
alias loaded? loaded
|
|
|
|
|
2021-11-25 23:07:38 +11:00
|
|
|
def initialize(start_at, end_at, params)
|
2021-10-15 05:44:59 +11:00
|
|
|
@start_at = start_at&.to_datetime
|
|
|
|
@end_at = end_at&.to_datetime
|
2021-11-25 23:07:38 +11:00
|
|
|
@params = params
|
2022-02-23 01:27:08 +11:00
|
|
|
@loaded = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_key
|
|
|
|
["metrics/measure/#{key}", @start_at, @end_at, canonicalized_params].join(';')
|
2021-10-15 05:44:59 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
def key
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2022-03-09 18:52:32 +11:00
|
|
|
def unit
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def total_in_time_range?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2021-10-15 05:44:59 +11:00
|
|
|
def total
|
2022-02-23 01:27:08 +11:00
|
|
|
load[:total]
|
2021-10-15 05:44:59 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
def previous_total
|
2022-02-23 01:27:08 +11:00
|
|
|
load[:previous_total]
|
2021-10-15 05:44:59 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
2022-02-23 01:27:08 +11:00
|
|
|
load[:data]
|
2021-10-15 05:44:59 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.model_name
|
|
|
|
self.class.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_attribute_for_serialization(key)
|
|
|
|
send(key) if respond_to?(key)
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2022-02-23 01:27:08 +11:00
|
|
|
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
|
|
|
|
|
2021-10-15 05:44:59 +11:00
|
|
|
def time_period
|
2021-11-25 23:07:38 +11:00
|
|
|
(@start_at..@end_at)
|
2021-10-15 05:44:59 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
def previous_time_period
|
2021-11-25 23:07:38 +11:00
|
|
|
((@start_at - length_of_period)..(@end_at - length_of_period))
|
2021-10-15 05:44:59 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
def length_of_period
|
|
|
|
@length_of_period ||= @end_at - @start_at
|
|
|
|
end
|
2021-11-25 23:07:38 +11:00
|
|
|
|
|
|
|
def params
|
2022-02-23 01:27:08 +11:00
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
def canonicalized_params
|
|
|
|
params.to_h.to_a.sort_by { |k, _v| k.to_s }.map { |k, v| "#{k}=#{v}" }.join(';')
|
2021-11-25 23:07:38 +11:00
|
|
|
end
|
2021-10-15 05:44:59 +11:00
|
|
|
end
|