0
0
Fork 0

Add caching layer to metrics (#17617)

This commit is contained in:
Eugen Rochko 2022-02-22 15:27:08 +01:00 committed by GitHub
parent 8338826963
commit b377022cf9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 165 additions and 55 deletions

View file

@ -1,6 +1,8 @@
# frozen_string_literal: true
class Admin::Metrics::Retention
CACHE_TTL = 5.minutes.freeze
class Cohort < ActiveModelSerializers::Model
attributes :period, :frequency, :data
end
@ -9,13 +11,37 @@ class Admin::Metrics::Retention
attributes :date, :rate, :value
end
attr_reader :loaded
alias loaded? loaded
def initialize(start_at, end_at, frequency)
@start_at = start_at&.to_date
@end_at = end_at&.to_date
@frequency = %w(day month).include?(frequency) ? frequency : 'day'
@loaded = false
end
def cache_key
['metrics/retention', @start_at, @end_at, @frequency].join(';')
end
def cohorts
load
end
protected
def load
unless loaded?
@values = Rails.cache.fetch(cache_key, expires_in: CACHE_TTL) { perform_query }
@loaded = true
end
@values
end
def perform_query
sql = <<-SQL.squish
SELECT axis.*, (
WITH new_users AS (