Add trending links (#16917)
* Add trending links * Add overriding specific links trendability * Add link type to preview cards and only trend articles Change trends review notifications from being sent every 5 minutes to being sent every 2 hours Change threshold from 5 unique accounts to 15 unique accounts * Fix tests
This commit is contained in:
		@@ -7,9 +7,14 @@ class Admin::Metrics::Dimension
 | 
			
		||||
    servers: Admin::Metrics::Dimension::ServersDimension,
 | 
			
		||||
    space_usage: Admin::Metrics::Dimension::SpaceUsageDimension,
 | 
			
		||||
    software_versions: Admin::Metrics::Dimension::SoftwareVersionsDimension,
 | 
			
		||||
    tag_servers: Admin::Metrics::Dimension::TagServersDimension,
 | 
			
		||||
    tag_languages: Admin::Metrics::Dimension::TagLanguagesDimension,
 | 
			
		||||
  }.freeze
 | 
			
		||||
 | 
			
		||||
  def self.retrieve(dimension_keys, start_at, end_at, limit)
 | 
			
		||||
    Array(dimension_keys).map { |key| DIMENSIONS[key.to_sym]&.new(start_at, end_at, limit) }.compact
 | 
			
		||||
  def self.retrieve(dimension_keys, start_at, end_at, limit, params)
 | 
			
		||||
    Array(dimension_keys).map do |key|
 | 
			
		||||
      klass = DIMENSIONS[key.to_sym]
 | 
			
		||||
      klass&.new(start_at, end_at, limit, klass.with_params? ? params.require(key.to_sym) : nil)
 | 
			
		||||
    end.compact
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
@@ -1,10 +1,15 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class Admin::Metrics::Dimension::BaseDimension
 | 
			
		||||
  def initialize(start_at, end_at, limit)
 | 
			
		||||
  def self.with_params?
 | 
			
		||||
    false
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  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
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def key
 | 
			
		||||
@@ -26,6 +31,10 @@ class Admin::Metrics::Dimension::BaseDimension
 | 
			
		||||
  protected
 | 
			
		||||
 | 
			
		||||
  def time_period
 | 
			
		||||
    (@start_at...@end_at)
 | 
			
		||||
    (@start_at..@end_at)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def params
 | 
			
		||||
    raise NotImplementedError
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,8 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class Admin::Metrics::Dimension::LanguagesDimension < Admin::Metrics::Dimension::BaseDimension
 | 
			
		||||
  include LanguagesHelper
 | 
			
		||||
 | 
			
		||||
  def key
 | 
			
		||||
    'languages'
 | 
			
		||||
  end
 | 
			
		||||
@@ -18,6 +20,6 @@ class Admin::Metrics::Dimension::LanguagesDimension < Admin::Metrics::Dimension:
 | 
			
		||||
 | 
			
		||||
    rows = ActiveRecord::Base.connection.select_all(sql, nil, [[nil, @start_at], [nil, @end_at], [nil, @limit]])
 | 
			
		||||
 | 
			
		||||
    rows.map { |row| { key: row['locale'], human_key: SettingsHelper::HUMAN_LOCALES[row['locale'].to_sym], value: row['value'].to_s } }
 | 
			
		||||
    rows.map { |row| { key: row['locale'], human_key: human_locale(row['locale']), value: row['value'].to_s } }
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										36
									
								
								app/lib/admin/metrics/dimension/tag_languages_dimension.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								app/lib/admin/metrics/dimension/tag_languages_dimension.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class Admin::Metrics::Dimension::TagLanguagesDimension < Admin::Metrics::Dimension::BaseDimension
 | 
			
		||||
  include LanguagesHelper
 | 
			
		||||
 | 
			
		||||
  def self.with_params?
 | 
			
		||||
    true
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def key
 | 
			
		||||
    'tag_languages'
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def data
 | 
			
		||||
    sql = <<-SQL.squish
 | 
			
		||||
      SELECT COALESCE(statuses.language, 'und') AS language, count(*) AS value
 | 
			
		||||
      FROM statuses
 | 
			
		||||
      INNER JOIN statuses_tags ON statuses_tags.status_id = statuses.id
 | 
			
		||||
      WHERE statuses_tags.tag_id = $1
 | 
			
		||||
        AND statuses.id BETWEEN $2 AND $3
 | 
			
		||||
      GROUP BY COALESCE(statuses.language, 'und')
 | 
			
		||||
      ORDER BY count(*) DESC
 | 
			
		||||
      LIMIT $4
 | 
			
		||||
    SQL
 | 
			
		||||
 | 
			
		||||
    rows = ActiveRecord::Base.connection.select_all(sql, nil, [[nil, params[:id]], [nil, Mastodon::Snowflake.id_at(@start_at, with_random: false)], [nil, Mastodon::Snowflake.id_at(@end_at, with_random: false)], [nil, @limit]])
 | 
			
		||||
 | 
			
		||||
    rows.map { |row| { key: row['language'], human_key: human_locale(row['language']), value: row['value'].to_s } }
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  private
 | 
			
		||||
 | 
			
		||||
  def params
 | 
			
		||||
    @params.permit(:id)
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										35
									
								
								app/lib/admin/metrics/dimension/tag_servers_dimension.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								app/lib/admin/metrics/dimension/tag_servers_dimension.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class Admin::Metrics::Dimension::TagServersDimension < Admin::Metrics::Dimension::BaseDimension
 | 
			
		||||
  def self.with_params?
 | 
			
		||||
    true
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def key
 | 
			
		||||
    'tag_servers'
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def data
 | 
			
		||||
    sql = <<-SQL.squish
 | 
			
		||||
      SELECT accounts.domain, count(*) AS value
 | 
			
		||||
      FROM statuses
 | 
			
		||||
      INNER JOIN accounts ON accounts.id = statuses.account_id
 | 
			
		||||
      INNER JOIN statuses_tags ON statuses_tags.status_id = statuses.id
 | 
			
		||||
      WHERE statuses_tags.tag_id = $1
 | 
			
		||||
        AND statuses.id BETWEEN $2 AND $3
 | 
			
		||||
      GROUP BY accounts.domain
 | 
			
		||||
      ORDER BY count(*) DESC
 | 
			
		||||
      LIMIT $4
 | 
			
		||||
    SQL
 | 
			
		||||
 | 
			
		||||
    rows = ActiveRecord::Base.connection.select_all(sql, nil, [[nil, params[:id]], [nil, Mastodon::Snowflake.id_at(@start_at, with_random: false)], [nil, Mastodon::Snowflake.id_at(@end_at, with_random: false)], [nil, @limit]])
 | 
			
		||||
 | 
			
		||||
    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
 | 
			
		||||
end
 | 
			
		||||
@@ -7,9 +7,15 @@ class Admin::Metrics::Measure
 | 
			
		||||
    interactions: Admin::Metrics::Measure::InteractionsMeasure,
 | 
			
		||||
    opened_reports: Admin::Metrics::Measure::OpenedReportsMeasure,
 | 
			
		||||
    resolved_reports: Admin::Metrics::Measure::ResolvedReportsMeasure,
 | 
			
		||||
    tag_accounts: Admin::Metrics::Measure::TagAccountsMeasure,
 | 
			
		||||
    tag_uses: Admin::Metrics::Measure::TagUsesMeasure,
 | 
			
		||||
    tag_servers: Admin::Metrics::Measure::TagServersMeasure,
 | 
			
		||||
  }.freeze
 | 
			
		||||
 | 
			
		||||
  def self.retrieve(measure_keys, start_at, end_at)
 | 
			
		||||
    Array(measure_keys).map { |key| MEASURES[key.to_sym]&.new(start_at, end_at) }.compact
 | 
			
		||||
  def self.retrieve(measure_keys, start_at, end_at, params)
 | 
			
		||||
    Array(measure_keys).map do |key|
 | 
			
		||||
      klass = MEASURES[key.to_sym]
 | 
			
		||||
      klass&.new(start_at, end_at, klass.with_params? ? params.require(key.to_sym) : nil)
 | 
			
		||||
    end.compact
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
@@ -24,10 +24,10 @@ class Admin::Metrics::Measure::ActiveUsersMeasure < Admin::Metrics::Measure::Bas
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def time_period
 | 
			
		||||
    (@start_at.to_date...@end_at.to_date)
 | 
			
		||||
    (@start_at.to_date..@end_at.to_date)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def previous_time_period
 | 
			
		||||
    ((@start_at.to_date - length_of_period)...(@end_at.to_date - length_of_period))
 | 
			
		||||
    ((@start_at.to_date - length_of_period)..(@end_at.to_date - length_of_period))
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
@@ -1,9 +1,14 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class Admin::Metrics::Measure::BaseMeasure
 | 
			
		||||
  def initialize(start_at, end_at)
 | 
			
		||||
  def self.with_params?
 | 
			
		||||
    false
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def initialize(start_at, end_at, params)
 | 
			
		||||
    @start_at = start_at&.to_datetime
 | 
			
		||||
    @end_at   = end_at&.to_datetime
 | 
			
		||||
    @params   = params
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def key
 | 
			
		||||
@@ -33,14 +38,18 @@ class Admin::Metrics::Measure::BaseMeasure
 | 
			
		||||
  protected
 | 
			
		||||
 | 
			
		||||
  def time_period
 | 
			
		||||
    (@start_at...@end_at)
 | 
			
		||||
    (@start_at..@end_at)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def previous_time_period
 | 
			
		||||
    ((@start_at - length_of_period)...(@end_at - length_of_period))
 | 
			
		||||
    ((@start_at - length_of_period)..(@end_at - length_of_period))
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def length_of_period
 | 
			
		||||
    @length_of_period ||= @end_at - @start_at
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def params
 | 
			
		||||
    raise NotImplementedError
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
@@ -24,10 +24,10 @@ class Admin::Metrics::Measure::InteractionsMeasure < Admin::Metrics::Measure::Ba
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def time_period
 | 
			
		||||
    (@start_at.to_date...@end_at.to_date)
 | 
			
		||||
    (@start_at.to_date..@end_at.to_date)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def previous_time_period
 | 
			
		||||
    ((@start_at.to_date - length_of_period)...(@end_at.to_date - length_of_period))
 | 
			
		||||
    ((@start_at.to_date - length_of_period)..(@end_at.to_date - length_of_period))
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										41
									
								
								app/lib/admin/metrics/measure/tag_accounts_measure.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								app/lib/admin/metrics/measure/tag_accounts_measure.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,41 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class Admin::Metrics::Measure::TagAccountsMeasure < Admin::Metrics::Measure::BaseMeasure
 | 
			
		||||
  def self.with_params?
 | 
			
		||||
    true
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def key
 | 
			
		||||
    'tag_accounts'
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def total
 | 
			
		||||
    tag.history.aggregate(time_period).accounts
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def previous_total
 | 
			
		||||
    tag.history.aggregate(previous_time_period).accounts
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def data
 | 
			
		||||
    time_period.map { |date| { date: date.to_time(:utc).iso8601, value: tag.history.get(date).accounts.to_s } }
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  protected
 | 
			
		||||
 | 
			
		||||
  def tag
 | 
			
		||||
    @tag ||= Tag.find(params[:id])
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def time_period
 | 
			
		||||
    (@start_at.to_date..@end_at.to_date)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def previous_time_period
 | 
			
		||||
    ((@start_at.to_date - length_of_period)..(@end_at.to_date - length_of_period))
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def params
 | 
			
		||||
    @params.permit(:id)
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										47
									
								
								app/lib/admin/metrics/measure/tag_servers_measure.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								app/lib/admin/metrics/measure/tag_servers_measure.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,47 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class Admin::Metrics::Measure::TagServersMeasure < Admin::Metrics::Measure::BaseMeasure
 | 
			
		||||
  def self.with_params?
 | 
			
		||||
    true
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def key
 | 
			
		||||
    'tag_servers'
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def total
 | 
			
		||||
    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
 | 
			
		||||
 | 
			
		||||
  def previous_total
 | 
			
		||||
    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
 | 
			
		||||
 | 
			
		||||
  def data
 | 
			
		||||
    sql = <<-SQL.squish
 | 
			
		||||
      SELECT axis.*, (
 | 
			
		||||
        SELECT count(*) AS value
 | 
			
		||||
        FROM statuses
 | 
			
		||||
        WHERE statuses.id BETWEEN $1 AND $2
 | 
			
		||||
          AND date_trunc('day', statuses.created_at)::date = axis.day
 | 
			
		||||
      )
 | 
			
		||||
      FROM (
 | 
			
		||||
        SELECT generate_series(date_trunc('day', $3::timestamp)::date, date_trunc('day', $4::timestamp)::date, ('1 day')::interval) AS day
 | 
			
		||||
      ) as axis
 | 
			
		||||
    SQL
 | 
			
		||||
 | 
			
		||||
    rows = ActiveRecord::Base.connection.select_all(sql, nil, [[nil, Mastodon::Snowflake.id_at(@start_at, with_random: false)], [nil, Mastodon::Snowflake.id_at(@end_at, with_random: false)], [nil, @start_at], [nil, @end_at]])
 | 
			
		||||
 | 
			
		||||
    rows.map { |row| { date: row['day'], value: row['value'].to_s } }
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  protected
 | 
			
		||||
 | 
			
		||||
  def tag
 | 
			
		||||
    @tag ||= Tag.find(params[:id])
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def params
 | 
			
		||||
    @params.permit(:id)
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										41
									
								
								app/lib/admin/metrics/measure/tag_uses_measure.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								app/lib/admin/metrics/measure/tag_uses_measure.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,41 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class Admin::Metrics::Measure::TagUsesMeasure < Admin::Metrics::Measure::BaseMeasure
 | 
			
		||||
  def self.with_params?
 | 
			
		||||
    true
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def key
 | 
			
		||||
    'tag_uses'
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def total
 | 
			
		||||
    tag.history.aggregate(time_period).uses
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def previous_total
 | 
			
		||||
    tag.history.aggregate(previous_time_period).uses
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def data
 | 
			
		||||
    time_period.map { |date| { date: date.to_time(:utc).iso8601, value: tag.history.get(date).uses.to_s } }
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  protected
 | 
			
		||||
 | 
			
		||||
  def tag
 | 
			
		||||
    @tag ||= Tag.find(params[:id])
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def time_period
 | 
			
		||||
    (@start_at.to_date..@end_at.to_date)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def previous_time_period
 | 
			
		||||
    ((@start_at.to_date - length_of_period)..(@end_at.to_date - length_of_period))
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def params
 | 
			
		||||
    @params.permit(:id)
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
		Reference in New Issue
	
	Block a user