2021-11-25 06:07:38 -06:00
# frozen_string_literal: true
class Admin :: Metrics :: Measure :: TagServersMeasure < Admin :: Metrics :: Measure :: BaseMeasure
2023-06-05 13:46:04 -05:00
include Admin :: Metrics :: Measure :: QueryHelper
2021-11-25 06:07:38 -06:00
def self . with_params?
true
end
def key
'tag_servers'
end
2022-02-22 08:27:08 -06:00
protected
def perform_total_query
2021-11-25 06:07:38 -06:00
tag . statuses . where ( 'statuses.id BETWEEN ? AND ?' , Mastodon :: Snowflake . id_at ( @start_at , with_random : false ) , Mastodon :: Snowflake . id_at ( @end_at , with_random : false ) ) . joins ( :account ) . count ( 'distinct accounts.domain' )
end
2022-02-22 08:27:08 -06:00
def perform_previous_total_query
2021-11-25 06:07:38 -06:00
tag . statuses . where ( 'statuses.id BETWEEN ? AND ?' , Mastodon :: Snowflake . id_at ( @start_at - length_of_period , with_random : false ) , Mastodon :: Snowflake . id_at ( @end_at - length_of_period , with_random : false ) ) . joins ( :account ) . count ( 'distinct accounts.domain' )
end
2023-06-05 13:46:04 -05:00
def sql_array
[ sql_query_string , { start_at : @start_at , end_at : @end_at , tag_id : tag . id , earliest_status_id : earliest_status_id , latest_status_id : latest_status_id } ]
end
def sql_query_string
<< ~ SQL . squish
2021-11-25 06:07:38 -06:00
SELECT axis . * , (
2021-11-28 16:13:07 -06:00
SELECT count ( distinct accounts . domain ) AS value
2021-11-25 06:07:38 -06:00
FROM statuses
2021-11-28 16:13:07 -06:00
INNER JOIN statuses_tags ON statuses . id = statuses_tags . status_id
INNER JOIN accounts ON statuses . account_id = accounts . id
2023-06-05 13:46:04 -05:00
WHERE statuses_tags . tag_id = :tag_id
AND statuses . id BETWEEN :earliest_status_id AND :latest_status_id
2021-11-25 06:07:38 -06:00
AND date_trunc ( 'day' , statuses . created_at ) :: date = axis . day
)
FROM (
2023-06-05 13:46:04 -05:00
SELECT generate_series ( date_trunc ( 'day' , :start_at :: timestamp ) :: date , date_trunc ( 'day' , :end_at :: timestamp ) :: date , ( '1 day' ) :: interval ) AS day
2021-11-25 06:07:38 -06:00
) as axis
SQL
2023-06-05 13:46:04 -05:00
end
2021-11-25 06:07:38 -06:00
2023-06-05 13:46:04 -05:00
def earliest_status_id
Mastodon :: Snowflake . id_at ( @start_at , with_random : false )
end
2021-11-25 06:07:38 -06:00
2023-06-05 13:46:04 -05:00
def latest_status_id
Mastodon :: Snowflake . id_at ( @end_at , with_random : false )
2021-11-25 06:07:38 -06:00
end
def tag
@tag || = Tag . find ( params [ :id ] )
end
def params
@params . permit ( :id )
end
end