links.rb 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # frozen_string_literal: true
  2. class Trends::Links < Trends::Base
  3. PREFIX = 'trending_links'
  4. BATCH_SIZE = 100
  5. self.default_options = {
  6. threshold: 5,
  7. review_threshold: 3,
  8. max_score_cooldown: 2.days.freeze,
  9. max_score_halflife: 8.hours.freeze,
  10. decay_threshold: 1,
  11. }
  12. class Query < Trends::Query
  13. def filtered_for!(account)
  14. @account = account
  15. self
  16. end
  17. def filtered_for(account)
  18. clone.filtered_for!(account)
  19. end
  20. def to_arel
  21. scope = PreviewCard.joins(:trend).reorder(score: :desc)
  22. scope = scope.reorder(language_order_clause.desc, score: :desc) if preferred_languages.present?
  23. scope = scope.merge(PreviewCardTrend.allowed) if @allowed
  24. scope = scope.offset(@offset) if @offset.present?
  25. scope = scope.limit(@limit) if @limit.present?
  26. scope
  27. end
  28. private
  29. def language_order_clause
  30. Arel::Nodes::Case.new.when(PreviewCardTrend.arel_table[:language].in(preferred_languages)).then(1).else(0)
  31. end
  32. def preferred_languages
  33. if @account&.chosen_languages.present?
  34. @account.chosen_languages
  35. else
  36. @locale
  37. end
  38. end
  39. end
  40. def register(status, at_time = Time.now.utc)
  41. original_status = status.proper
  42. return unless (original_status.public_visibility? && status.public_visibility?) &&
  43. !(original_status.account.silenced? || status.account.silenced?) &&
  44. !(original_status.spoiler_text? || original_status.sensitive?)
  45. original_status.preview_cards.each do |preview_card|
  46. add(preview_card, status.account_id, at_time) if preview_card.appropriate_for_trends?
  47. end
  48. end
  49. def add(preview_card, account_id, at_time = Time.now.utc)
  50. preview_card.history.add(account_id, at_time)
  51. record_used_id(preview_card.id, at_time)
  52. end
  53. def query
  54. Query.new(key_prefix, klass)
  55. end
  56. def refresh(at_time = Time.now.utc)
  57. # First, recalculate scores for links that were trending previously. We split the queries
  58. # to avoid having to load all of the IDs into Ruby just to send them back into Postgres
  59. PreviewCard.where(id: PreviewCardTrend.select(:preview_card_id)).find_in_batches(batch_size: BATCH_SIZE) do |preview_cards|
  60. calculate_scores(preview_cards, at_time)
  61. end
  62. # Then, calculate scores for links that were used today. There are potentially some
  63. # duplicate items here that we might process one more time, but that should be fine
  64. PreviewCard.where(id: recently_used_ids(at_time)).find_in_batches(batch_size: BATCH_SIZE) do |preview_cards|
  65. calculate_scores(preview_cards, at_time)
  66. end
  67. # Now that all trends have up-to-date scores, and all the ones below the threshold have
  68. # been removed, we can recalculate their positions
  69. PreviewCardTrend.connection.exec_update('UPDATE preview_card_trends SET rank = t0.calculated_rank FROM (SELECT id, row_number() OVER w AS calculated_rank FROM preview_card_trends WINDOW w AS (PARTITION BY language ORDER BY score DESC)) t0 WHERE preview_card_trends.id = t0.id')
  70. end
  71. def request_review
  72. PreviewCardTrend.pluck('distinct language').flat_map do |language|
  73. score_at_threshold = PreviewCardTrend.where(language: language, allowed: true).order(rank: :desc).where('rank <= ?', options[:review_threshold]).first&.score || 0
  74. preview_card_trends = PreviewCardTrend.where(language: language, allowed: false).joins(:preview_card)
  75. preview_card_trends.filter_map do |trend|
  76. preview_card = trend.preview_card
  77. next unless trend.score > score_at_threshold && !preview_card.trendable? && preview_card.requires_review_notification?
  78. if preview_card.provider.nil?
  79. preview_card.provider = PreviewCardProvider.create(domain: preview_card.domain, requested_review_at: Time.now.utc)
  80. else
  81. preview_card.provider.touch(:requested_review_at)
  82. end
  83. preview_card
  84. end
  85. end
  86. end
  87. protected
  88. def key_prefix
  89. PREFIX
  90. end
  91. def klass
  92. PreviewCard
  93. end
  94. private
  95. def calculate_scores(preview_cards, at_time)
  96. items = preview_cards.map do |preview_card|
  97. expected = preview_card.history.get(at_time - 1.day).accounts.to_f
  98. expected = 1.0 if expected.zero?
  99. observed = preview_card.history.get(at_time).accounts.to_f
  100. max_time = preview_card.max_score_at
  101. max_score = preview_card.max_score
  102. max_score = 0 if max_time.nil? || max_time < (at_time - options[:max_score_cooldown])
  103. score = if expected > observed || observed < options[:threshold]
  104. 0
  105. else
  106. ((observed - expected)**2) / expected
  107. end
  108. if score > max_score
  109. max_score = score
  110. max_time = at_time
  111. # Not interested in triggering any callbacks for this
  112. preview_card.update_columns(max_score: max_score, max_score_at: max_time)
  113. end
  114. decaying_score = if max_score.zero? || !valid_locale?(preview_card.language)
  115. 0
  116. else
  117. max_score * (0.5**((at_time.to_f - max_time.to_f) / options[:max_score_halflife].to_f))
  118. end
  119. [decaying_score, preview_card]
  120. end
  121. to_insert = items.filter { |(score, _)| score >= options[:decay_threshold] }
  122. to_delete = items.filter { |(score, _)| score < options[:decay_threshold] }
  123. PreviewCardTrend.upsert_all(to_insert.map { |(score, preview_card)| { preview_card_id: preview_card.id, score: score, language: preview_card.language, allowed: preview_card.trendable? || false } }, unique_by: :preview_card_id) if to_insert.any?
  124. PreviewCardTrend.where(preview_card_id: to_delete.map { |(_, preview_card)| preview_card.id }).delete_all if to_delete.any?
  125. end
  126. end