about summary refs log tree commit diff
path: root/app/lib/admin/metrics/dimension
diff options
context:
space:
mode:
Diffstat (limited to 'app/lib/admin/metrics/dimension')
-rw-r--r--app/lib/admin/metrics/dimension/base_dimension.rb32
-rw-r--r--app/lib/admin/metrics/dimension/languages_dimension.rb4
-rw-r--r--app/lib/admin/metrics/dimension/servers_dimension.rb4
-rw-r--r--app/lib/admin/metrics/dimension/software_versions_dimension.rb6
-rw-r--r--app/lib/admin/metrics/dimension/sources_dimension.rb4
-rw-r--r--app/lib/admin/metrics/dimension/space_usage_dimension.rb6
-rw-r--r--app/lib/admin/metrics/dimension/tag_languages_dimension.rb6
-rw-r--r--app/lib/admin/metrics/dimension/tag_servers_dimension.rb6
8 files changed, 51 insertions, 17 deletions
diff --git a/app/lib/admin/metrics/dimension/base_dimension.rb b/app/lib/admin/metrics/dimension/base_dimension.rb
index 5872c22cb..bd2e4ecec 100644
--- a/app/lib/admin/metrics/dimension/base_dimension.rb
+++ b/app/lib/admin/metrics/dimension/base_dimension.rb
@@ -1,23 +1,34 @@
 # frozen_string_literal: true
 
 class Admin::Metrics::Dimension::BaseDimension
+  CACHE_TTL = 5.minutes.freeze
+
   def self.with_params?
     false
   end
 
+  attr_reader :loaded
+
+  alias loaded? loaded
+
   def initialize(start_at, end_at, limit, params)
     @start_at = start_at&.to_datetime
     @end_at   = end_at&.to_datetime
     @limit    = limit&.to_i
     @params   = params
+    @loaded   = false
   end
 
   def key
     raise NotImplementedError
   end
 
+  def cache_key
+    ["metrics/dimension/#{key}", @start_at, @end_at, @limit, canonicalized_params].join(';')
+  end
+
   def data
-    raise NotImplementedError
+    load
   end
 
   def self.model_name
@@ -30,11 +41,28 @@ class Admin::Metrics::Dimension::BaseDimension
 
   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
+    raise NotImplementedError
+  end
+
   def time_period
     (@start_at..@end_at)
   end
 
   def params
-    raise NotImplementedError
+    {}
+  end
+
+  def canonicalized_params
+    params.to_h.to_a.sort_by { |k, _v| k.to_s }.map { |k, v| "#{k}=#{v}" }.join(';')
   end
 end
diff --git a/app/lib/admin/metrics/dimension/languages_dimension.rb b/app/lib/admin/metrics/dimension/languages_dimension.rb
index 1cc5f4120..f1cf82cf2 100644
--- a/app/lib/admin/metrics/dimension/languages_dimension.rb
+++ b/app/lib/admin/metrics/dimension/languages_dimension.rb
@@ -7,7 +7,9 @@ class Admin::Metrics::Dimension::LanguagesDimension < Admin::Metrics::Dimension:
     'languages'
   end
 
-  def data
+  protected
+
+  def perform_query
     sql = <<-SQL.squish
       SELECT locale, count(*) AS value
       FROM users
diff --git a/app/lib/admin/metrics/dimension/servers_dimension.rb b/app/lib/admin/metrics/dimension/servers_dimension.rb
index 3e80b6625..91bcce655 100644
--- a/app/lib/admin/metrics/dimension/servers_dimension.rb
+++ b/app/lib/admin/metrics/dimension/servers_dimension.rb
@@ -5,7 +5,9 @@ class Admin::Metrics::Dimension::ServersDimension < Admin::Metrics::Dimension::B
     'servers'
   end
 
-  def data
+  protected
+
+  def perform_query
     sql = <<-SQL.squish
       SELECT accounts.domain, count(*) AS value
       FROM statuses
diff --git a/app/lib/admin/metrics/dimension/software_versions_dimension.rb b/app/lib/admin/metrics/dimension/software_versions_dimension.rb
index 34917404d..816615f99 100644
--- a/app/lib/admin/metrics/dimension/software_versions_dimension.rb
+++ b/app/lib/admin/metrics/dimension/software_versions_dimension.rb
@@ -7,12 +7,12 @@ class Admin::Metrics::Dimension::SoftwareVersionsDimension < Admin::Metrics::Dim
     'software_versions'
   end
 
-  def data
+  protected
+
+  def perform_query
     [mastodon_version, ruby_version, postgresql_version, redis_version]
   end
 
-  private
-
   def mastodon_version
     value = Mastodon::Version.to_s
 
diff --git a/app/lib/admin/metrics/dimension/sources_dimension.rb b/app/lib/admin/metrics/dimension/sources_dimension.rb
index a9f061809..122807cdc 100644
--- a/app/lib/admin/metrics/dimension/sources_dimension.rb
+++ b/app/lib/admin/metrics/dimension/sources_dimension.rb
@@ -5,7 +5,9 @@ class Admin::Metrics::Dimension::SourcesDimension < Admin::Metrics::Dimension::B
     'sources'
   end
 
-  def data
+  protected
+
+  def perform_query
     sql = <<-SQL.squish
       SELECT oauth_applications.name, count(*) AS value
       FROM users
diff --git a/app/lib/admin/metrics/dimension/space_usage_dimension.rb b/app/lib/admin/metrics/dimension/space_usage_dimension.rb
index aa00a2e18..5867c5bab 100644
--- a/app/lib/admin/metrics/dimension/space_usage_dimension.rb
+++ b/app/lib/admin/metrics/dimension/space_usage_dimension.rb
@@ -8,12 +8,12 @@ class Admin::Metrics::Dimension::SpaceUsageDimension < Admin::Metrics::Dimension
     'space_usage'
   end
 
-  def data
+  protected
+
+  def perform_query
     [postgresql_size, redis_size, media_size]
   end
 
-  private
-
   def postgresql_size
     value = ActiveRecord::Base.connection.execute('SELECT pg_database_size(current_database())').first['pg_database_size']
 
diff --git a/app/lib/admin/metrics/dimension/tag_languages_dimension.rb b/app/lib/admin/metrics/dimension/tag_languages_dimension.rb
index afbc8cde8..e1349c229 100644
--- a/app/lib/admin/metrics/dimension/tag_languages_dimension.rb
+++ b/app/lib/admin/metrics/dimension/tag_languages_dimension.rb
@@ -11,7 +11,9 @@ class Admin::Metrics::Dimension::TagLanguagesDimension < Admin::Metrics::Dimensi
     'tag_languages'
   end
 
-  def data
+  protected
+
+  def perform_query
     sql = <<-SQL.squish
       SELECT COALESCE(statuses.language, 'und') AS language, count(*) AS value
       FROM statuses
@@ -28,8 +30,6 @@ class Admin::Metrics::Dimension::TagLanguagesDimension < Admin::Metrics::Dimensi
     rows.map { |row| { key: row['language'], human_key: standard_locale_name(row['language']), value: row['value'].to_s } }
   end
 
-  private
-
   def params
     @params.permit(:id)
   end
diff --git a/app/lib/admin/metrics/dimension/tag_servers_dimension.rb b/app/lib/admin/metrics/dimension/tag_servers_dimension.rb
index 12c5980d7..7ddf3378c 100644
--- a/app/lib/admin/metrics/dimension/tag_servers_dimension.rb
+++ b/app/lib/admin/metrics/dimension/tag_servers_dimension.rb
@@ -9,7 +9,9 @@ class Admin::Metrics::Dimension::TagServersDimension < Admin::Metrics::Dimension
     'tag_servers'
   end
 
-  def data
+  protected
+
+  def perform_query
     sql = <<-SQL.squish
       SELECT accounts.domain, count(*) AS value
       FROM statuses
@@ -27,8 +29,6 @@ class Admin::Metrics::Dimension::TagServersDimension < Admin::Metrics::Dimension
     rows.map { |row| { key: row['domain'] || Rails.configuration.x.local_domain, human_key: row['domain'] || Rails.configuration.x.local_domain, value: row['value'].to_s } }
   end
 
-  private
-
   def params
     @params.permit(:id)
   end