From f1e35a60e6eaba20afb1c4f789d153fc03dcc0d3 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 11 Jun 2024 10:20:38 -0400 Subject: [PATCH] Method for date trunc compare --- app/lib/admin/metrics/measure/instance_accounts_measure.rb | 7 ++----- .../admin/metrics/measure/instance_followers_measure.rb | 7 ++----- app/lib/admin/metrics/measure/instance_follows_measure.rb | 7 ++----- .../metrics/measure/instance_media_attachments_measure.rb | 7 ++----- app/lib/admin/metrics/measure/instance_reports_measure.rb | 7 ++----- app/lib/admin/metrics/measure/instance_statuses_measure.rb | 7 ++----- app/lib/admin/metrics/measure/new_users_measure.rb | 7 ++----- app/lib/admin/metrics/measure/query_helper.rb | 6 ++++++ app/lib/admin/metrics/measure/resolved_reports_measure.rb | 7 ++----- app/lib/admin/metrics/measure/tag_servers_measure.rb | 7 ++----- 10 files changed, 24 insertions(+), 45 deletions(-) diff --git a/app/lib/admin/metrics/measure/instance_accounts_measure.rb b/app/lib/admin/metrics/measure/instance_accounts_measure.rb index d669f5a7ee8..9c110ea598e 100644 --- a/app/lib/admin/metrics/measure/instance_accounts_measure.rb +++ b/app/lib/admin/metrics/measure/instance_accounts_measure.rb @@ -35,11 +35,8 @@ class Admin::Metrics::Measure::InstanceAccountsMeasure < Admin::Metrics::Measure Account .select(:id) .where(account_domain_sql(params[:include_subdomains])) - .where( - <<~SQL.squish - DATE_TRUNC('day', accounts.created_at)::date = axis.period - SQL - ).to_sql + .where(daily_period(:accounts)) + .to_sql end def params diff --git a/app/lib/admin/metrics/measure/instance_followers_measure.rb b/app/lib/admin/metrics/measure/instance_followers_measure.rb index 9cbd8a2392b..0681f3d2cf7 100644 --- a/app/lib/admin/metrics/measure/instance_followers_measure.rb +++ b/app/lib/admin/metrics/measure/instance_followers_measure.rb @@ -36,11 +36,8 @@ class Admin::Metrics::Measure::InstanceFollowersMeasure < Admin::Metrics::Measur .select(:id) .joins(:account) .where(account_domain_sql(params[:include_subdomains])) - .where( - <<~SQL.squish - DATE_TRUNC('day', follows.created_at)::date = axis.period - SQL - ).to_sql + .where(daily_period(:follows)) + .to_sql end def params diff --git a/app/lib/admin/metrics/measure/instance_follows_measure.rb b/app/lib/admin/metrics/measure/instance_follows_measure.rb index c219b8354fe..e2fbb200f97 100644 --- a/app/lib/admin/metrics/measure/instance_follows_measure.rb +++ b/app/lib/admin/metrics/measure/instance_follows_measure.rb @@ -36,11 +36,8 @@ class Admin::Metrics::Measure::InstanceFollowsMeasure < Admin::Metrics::Measure: .select(:id) .joins(:target_account) .where(account_domain_sql(params[:include_subdomains])) - .where( - <<~SQL.squish - DATE_TRUNC('day', follows.created_at)::date = axis.period - SQL - ).to_sql + .where(daily_period(:follows)) + .to_sql end def params diff --git a/app/lib/admin/metrics/measure/instance_media_attachments_measure.rb b/app/lib/admin/metrics/measure/instance_media_attachments_measure.rb index c6abb9a0006..b0aa81f52e3 100644 --- a/app/lib/admin/metrics/measure/instance_media_attachments_measure.rb +++ b/app/lib/admin/metrics/measure/instance_media_attachments_measure.rb @@ -45,11 +45,8 @@ class Admin::Metrics::Measure::InstanceMediaAttachmentsMeasure < Admin::Metrics: .select('COALESCE(media_attachments.file_file_size, 0) + COALESCE(media_attachments.thumbnail_file_size, 0) AS size') .joins(:account) .where(account_domain_sql(params[:include_subdomains])) - .where( - <<~SQL.squish - DATE_TRUNC('day', media_attachments.created_at)::date = axis.period - SQL - ).to_sql + .where(daily_period(:media_attachments)) + .to_sql end def select_target diff --git a/app/lib/admin/metrics/measure/instance_reports_measure.rb b/app/lib/admin/metrics/measure/instance_reports_measure.rb index 519a50b6f77..2be5a6e7842 100644 --- a/app/lib/admin/metrics/measure/instance_reports_measure.rb +++ b/app/lib/admin/metrics/measure/instance_reports_measure.rb @@ -36,11 +36,8 @@ class Admin::Metrics::Measure::InstanceReportsMeasure < Admin::Metrics::Measure: .select(:id) .joins(:target_account) .where(account_domain_sql(params[:include_subdomains])) - .where( - <<~SQL.squish - DATE_TRUNC('day', reports.created_at)::date = axis.period - SQL - ).to_sql + .where(daily_period(:reports)) + .to_sql end def params diff --git a/app/lib/admin/metrics/measure/instance_statuses_measure.rb b/app/lib/admin/metrics/measure/instance_statuses_measure.rb index 73dea4d5c48..e27da646c41 100644 --- a/app/lib/admin/metrics/measure/instance_statuses_measure.rb +++ b/app/lib/admin/metrics/measure/instance_statuses_measure.rb @@ -41,11 +41,8 @@ class Admin::Metrics::Measure::InstanceStatusesMeasure < Admin::Metrics::Measure statuses.id BETWEEN :earliest_status_id AND :latest_status_id SQL ) - .where( - <<~SQL.squish - date_trunc('day', statuses.created_at)::date = axis.period - SQL - ).to_sql + .where(daily_period(:statuses)) + .to_sql end def earliest_status_id diff --git a/app/lib/admin/metrics/measure/new_users_measure.rb b/app/lib/admin/metrics/measure/new_users_measure.rb index 66334cd11c8..f4a7e5b2931 100644 --- a/app/lib/admin/metrics/measure/new_users_measure.rb +++ b/app/lib/admin/metrics/measure/new_users_measure.rb @@ -24,10 +24,7 @@ class Admin::Metrics::Measure::NewUsersMeasure < Admin::Metrics::Measure::BaseMe def data_source_query User .select(:id) - .where( - <<~SQL.squish - DATE_TRUNC('day', users.created_at)::date = axis.period - SQL - ).to_sql + .where(daily_period(:users)) + .to_sql end end diff --git a/app/lib/admin/metrics/measure/query_helper.rb b/app/lib/admin/metrics/measure/query_helper.rb index 4145f3fa442..120c05f1bbc 100644 --- a/app/lib/admin/metrics/measure/query_helper.rb +++ b/app/lib/admin/metrics/measure/query_helper.rb @@ -33,6 +33,12 @@ module Admin::Metrics::Measure::QueryHelper SQL end + def daily_period(table, column = :created_at) + <<~SQL.squish + DATE_TRUNC('day', #{table}.#{column})::date = axis.period + SQL + end + def account_domain_sql(include_subdomains) if include_subdomains "accounts.domain IN (SELECT domain FROM instances WHERE reverse('.' || domain) LIKE reverse('.' || :domain::text))" diff --git a/app/lib/admin/metrics/measure/resolved_reports_measure.rb b/app/lib/admin/metrics/measure/resolved_reports_measure.rb index 5c7b1c320fd..a3b67ebc595 100644 --- a/app/lib/admin/metrics/measure/resolved_reports_measure.rb +++ b/app/lib/admin/metrics/measure/resolved_reports_measure.rb @@ -24,10 +24,7 @@ class Admin::Metrics::Measure::ResolvedReportsMeasure < Admin::Metrics::Measure: def data_source_query Report .select(:id) - .where( - <<~SQL.squish - DATE_TRUNC('day', reports.action_taken_at)::date = axis.period - SQL - ).to_sql + .where(daily_period(:reports, :action_taken_at)) + .to_sql end end diff --git a/app/lib/admin/metrics/measure/tag_servers_measure.rb b/app/lib/admin/metrics/measure/tag_servers_measure.rb index 5eca9813d5a..dfde12c8180 100644 --- a/app/lib/admin/metrics/measure/tag_servers_measure.rb +++ b/app/lib/admin/metrics/measure/tag_servers_measure.rb @@ -37,11 +37,8 @@ class Admin::Metrics::Measure::TagServersMeasure < Admin::Metrics::Measure::Base AND statuses.id BETWEEN :earliest_status_id AND :latest_status_id SQL ) - .where( - <<~SQL.squish - date_trunc('day', statuses.created_at)::date = axis.period - SQL - ).to_sql + .where(daily_period(:statuses)) + .to_sql end def earliest_status_id