From c570eca3932661e61c789c177bc89fb4852c8bde Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 11 Jun 2024 10:10:55 -0400 Subject: [PATCH] Use AR to build --- .../instance_media_attachments_measure.rb | 16 ++++++------ .../metrics/measure/tag_servers_measure.rb | 25 ++++++++++++------- 2 files changed, 25 insertions(+), 16 deletions(-) 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 03204329cd9..c6abb9a0006 100644 --- a/app/lib/admin/metrics/measure/instance_media_attachments_measure.rb +++ b/app/lib/admin/metrics/measure/instance_media_attachments_measure.rb @@ -41,13 +41,15 @@ class Admin::Metrics::Measure::InstanceMediaAttachmentsMeasure < Admin::Metrics: end def data_source_query - <<~SQL.squish - SELECT COALESCE(media_attachments.file_file_size, 0) + COALESCE(media_attachments.thumbnail_file_size, 0) AS size - FROM media_attachments - INNER JOIN accounts ON accounts.id = media_attachments.account_id - WHERE date_trunc('day', media_attachments.created_at)::date = axis.period - AND #{account_domain_sql(params[:include_subdomains])} - SQL + MediaAttachment + .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 end def select_target diff --git a/app/lib/admin/metrics/measure/tag_servers_measure.rb b/app/lib/admin/metrics/measure/tag_servers_measure.rb index 897067703c1..5eca9813d5a 100644 --- a/app/lib/admin/metrics/measure/tag_servers_measure.rb +++ b/app/lib/admin/metrics/measure/tag_servers_measure.rb @@ -26,15 +26,22 @@ class Admin::Metrics::Measure::TagServersMeasure < Admin::Metrics::Measure::Base end def data_source_query - <<~SQL.squish - SELECT DISTINCT accounts.domain - FROM statuses - INNER JOIN statuses_tags ON statuses.id = statuses_tags.status_id - INNER JOIN accounts ON statuses.account_id = accounts.id - WHERE statuses_tags.tag_id = :tag_id - AND statuses.id BETWEEN :earliest_status_id AND :latest_status_id - AND date_trunc('day', statuses.created_at)::date = axis.period - SQL + Status + .select('accounts.domain') + .distinct + .reorder(nil) + .joins(:tags, :account) + .where( + <<~SQL.squish + statuses_tags.tag_id = :tag_id + 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 end def earliest_status_id