2021-10-14 14:44:59 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Admin::Metrics::Dimension::BaseDimension
|
2022-02-22 09:27:08 -05:00
|
|
|
CACHE_TTL = 5.minutes.freeze
|
|
|
|
|
2021-11-25 07:07:38 -05:00
|
|
|
def self.with_params?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2022-02-22 09:27:08 -05:00
|
|
|
attr_reader :loaded
|
|
|
|
|
|
|
|
alias loaded? loaded
|
|
|
|
|
2021-11-25 07:07:38 -05:00
|
|
|
def initialize(start_at, end_at, limit, params)
|
2021-10-14 14:44:59 -04:00
|
|
|
@start_at = start_at&.to_datetime
|
|
|
|
@end_at = end_at&.to_datetime
|
|
|
|
@limit = limit&.to_i
|
2021-11-25 07:07:38 -05:00
|
|
|
@params = params
|
2022-02-22 09:27:08 -05:00
|
|
|
@loaded = false
|
2021-10-14 14:44:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def key
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2022-02-22 09:27:08 -05:00
|
|
|
def cache_key
|
|
|
|
["metrics/dimension/#{key}", @start_at, @end_at, @limit, canonicalized_params].join(';')
|
|
|
|
end
|
|
|
|
|
2021-10-14 14:44:59 -04:00
|
|
|
def data
|
2022-02-22 09:27:08 -05:00
|
|
|
load
|
2021-10-14 14:44:59 -04: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-22 09:27:08 -05:00
|
|
|
def load
|
|
|
|
unless loaded?
|
|
|
|
@values = Rails.cache.fetch(cache_key, expires_in: CACHE_TTL) { perform_query }
|
|
|
|
@loaded = true
|
|
|
|
end
|
|
|
|
|
|
|
|
@values
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform_query
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2021-10-14 14:44:59 -04:00
|
|
|
def time_period
|
2021-11-25 07:07:38 -05:00
|
|
|
(@start_at..@end_at)
|
|
|
|
end
|
|
|
|
|
|
|
|
def params
|
2022-02-22 09:27:08 -05:00
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
def canonicalized_params
|
|
|
|
params.to_h.to_a.sort_by { |k, _v| k.to_s }.map { |k, v| "#{k}=#{v}" }.join(';')
|
2021-10-14 14:44:59 -04:00
|
|
|
end
|
|
|
|
end
|