1
0

links.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. add(original_status.preview_card, status.account_id, at_time) if original_status.preview_card&.appropriate_for_trends?
  46. end
  47. def add(preview_card, account_id, at_time = Time.now.utc)
  48. preview_card.history.add(account_id, at_time)
  49. record_used_id(preview_card.id, at_time)
  50. end
  51. def query
  52. Query.new(key_prefix, klass)
  53. end
  54. def refresh(at_time = Time.now.utc)
  55. # First, recalculate scores for links that were trending previously. We split the queries
  56. # to avoid having to load all of the IDs into Ruby just to send them back into Postgres
  57. PreviewCard.where(id: PreviewCardTrend.select(:preview_card_id)).find_in_batches(batch_size: BATCH_SIZE) do |preview_cards|
  58. calculate_scores(preview_cards, at_time)
  59. end
  60. # Then, calculate scores for links that were used today. There are potentially some
  61. # duplicate items here that we might process one more time, but that should be fine
  62. PreviewCard.where(id: recently_used_ids(at_time)).find_in_batches(batch_size: BATCH_SIZE) do |preview_cards|
  63. calculate_scores(preview_cards, at_time)
  64. end
  65. # Now that all trends have up-to-date scores, and all the ones below the threshold have
  66. # been removed, we can recalculate their positions
  67. 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')
  68. end
  69. def request_review
  70. PreviewCardTrend.pluck('distinct language').flat_map do |language|
  71. score_at_threshold = PreviewCardTrend.where(language: language, allowed: true).order(rank: :desc).where('rank <= ?', options[:review_threshold]).first&.score || 0
  72. preview_card_trends = PreviewCardTrend.where(language: language, allowed: false).joins(:preview_card)
  73. preview_card_trends.filter_map do |trend|
  74. preview_card = trend.preview_card
  75. next unless trend.score > score_at_threshold && !preview_card.trendable? && preview_card.requires_review_notification?
  76. if preview_card.provider.nil?
  77. preview_card.provider = PreviewCardProvider.create(domain: preview_card.domain, requested_review_at: Time.now.utc)
  78. else
  79. preview_card.provider.touch(:requested_review_at)
  80. end
  81. preview_card
  82. end
  83. end
  84. end
  85. protected
  86. def key_prefix
  87. PREFIX
  88. end
  89. def klass
  90. PreviewCard
  91. end
  92. private
  93. def calculate_scores(preview_cards, at_time)
  94. items = preview_cards.map do |preview_card|
  95. expected = preview_card.history.get(at_time - 1.day).accounts.to_f
  96. expected = 1.0 if expected.zero?
  97. observed = preview_card.history.get(at_time).accounts.to_f
  98. max_time = preview_card.max_score_at
  99. max_score = preview_card.max_score
  100. max_score = 0 if max_time.nil? || max_time < (at_time - options[:max_score_cooldown])
  101. score = if expected > observed || observed < options[:threshold]
  102. 0
  103. else
  104. ((observed - expected)**2) / expected
  105. end
  106. if score > max_score
  107. max_score = score
  108. max_time = at_time
  109. # Not interested in triggering any callbacks for this
  110. preview_card.update_columns(max_score: max_score, max_score_at: max_time)
  111. end
  112. decaying_score = if max_score.zero? || !valid_locale?(preview_card.language)
  113. 0
  114. else
  115. max_score * (0.5**((at_time.to_f - max_time.to_f) / options[:max_score_halflife].to_f))
  116. end
  117. [decaying_score, preview_card]
  118. end
  119. to_insert = items.filter { |(score, _)| score >= options[:decay_threshold] }
  120. to_delete = items.filter { |(score, _)| score < options[:decay_threshold] }
  121. 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?
  122. PreviewCardTrend.where(preview_card_id: to_delete.map { |(_, preview_card)| preview_card.id }).delete_all if to_delete.any?
  123. end
  124. end