links.rb 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # frozen_string_literal: true
  2. class Trends::Links < Trends::Base
  3. PREFIX = 'trending_links'
  4. self.default_options = {
  5. threshold: 5,
  6. review_threshold: 3,
  7. max_score_cooldown: 2.days.freeze,
  8. max_score_halflife: 8.hours.freeze,
  9. decay_threshold: 1,
  10. }
  11. class Query < Trends::Query
  12. def filtered_for!(account)
  13. @account = account
  14. self
  15. end
  16. def filtered_for(account)
  17. clone.filtered_for!(account)
  18. end
  19. def to_arel
  20. scope = PreviewCard.joins(:trend).reorder(score: :desc)
  21. scope = scope.reorder(language_order_clause.desc, score: :desc) if preferred_languages.present?
  22. scope = scope.merge(PreviewCardTrend.allowed) if @allowed
  23. scope = scope.offset(@offset) if @offset.present?
  24. scope = scope.limit(@limit) if @limit.present?
  25. scope
  26. end
  27. private
  28. def language_order_clause
  29. Arel::Nodes::Case.new.when(PreviewCardTrend.arel_table[:language].in(preferred_languages)).then(1).else(0)
  30. end
  31. def preferred_languages
  32. if @account&.chosen_languages.present?
  33. @account.chosen_languages
  34. else
  35. @locale
  36. end
  37. end
  38. end
  39. def register(status, at_time = Time.now.utc)
  40. original_status = status.proper
  41. return unless (original_status.public_visibility? && status.public_visibility?) &&
  42. !(original_status.account.silenced? || status.account.silenced?) &&
  43. !(original_status.spoiler_text? || original_status.sensitive?)
  44. original_status.preview_cards.each do |preview_card|
  45. add(preview_card, status.account_id, at_time) if preview_card.appropriate_for_trends?
  46. end
  47. end
  48. def add(preview_card, account_id, at_time = Time.now.utc)
  49. preview_card.history.add(account_id, at_time)
  50. record_used_id(preview_card.id, at_time)
  51. end
  52. def query
  53. Query.new(key_prefix, klass)
  54. end
  55. def refresh(at_time = Time.now.utc)
  56. preview_cards = PreviewCard.where(id: (recently_used_ids(at_time) + PreviewCardTrend.pluck(:preview_card_id)).uniq)
  57. calculate_scores(preview_cards, at_time)
  58. end
  59. def request_review
  60. PreviewCardTrend.pluck('distinct language').flat_map do |language|
  61. score_at_threshold = PreviewCardTrend.where(language: language, allowed: true).order(rank: :desc).where('rank <= ?', options[:review_threshold]).first&.score || 0
  62. preview_card_trends = PreviewCardTrend.where(language: language, allowed: false).joins(:preview_card)
  63. preview_card_trends.filter_map do |trend|
  64. preview_card = trend.preview_card
  65. next unless trend.score > score_at_threshold && !preview_card.trendable? && preview_card.requires_review_notification?
  66. if preview_card.provider.nil?
  67. preview_card.provider = PreviewCardProvider.create(domain: preview_card.domain, requested_review_at: Time.now.utc)
  68. else
  69. preview_card.provider.touch(:requested_review_at)
  70. end
  71. preview_card
  72. end
  73. end
  74. end
  75. protected
  76. def key_prefix
  77. PREFIX
  78. end
  79. def klass
  80. PreviewCard
  81. end
  82. private
  83. def calculate_scores(preview_cards, at_time)
  84. items = preview_cards.map do |preview_card|
  85. expected = preview_card.history.get(at_time - 1.day).accounts.to_f
  86. expected = 1.0 if expected.zero?
  87. observed = preview_card.history.get(at_time).accounts.to_f
  88. max_time = preview_card.max_score_at
  89. max_score = preview_card.max_score
  90. max_score = 0 if max_time.nil? || max_time < (at_time - options[:max_score_cooldown])
  91. score = begin
  92. if expected > observed || observed < options[:threshold]
  93. 0
  94. else
  95. ((observed - expected)**2) / expected
  96. end
  97. end
  98. if score > max_score
  99. max_score = score
  100. max_time = at_time
  101. # Not interested in triggering any callbacks for this
  102. preview_card.update_columns(max_score: max_score, max_score_at: max_time)
  103. end
  104. decaying_score = begin
  105. if max_score.zero? || !valid_locale?(preview_card.language)
  106. 0
  107. else
  108. max_score * (0.5**((at_time.to_f - max_time.to_f) / options[:max_score_halflife].to_f))
  109. end
  110. end
  111. [decaying_score, preview_card]
  112. end
  113. to_insert = items.filter { |(score, _)| score >= options[:decay_threshold] }
  114. to_delete = items.filter { |(score, _)| score < options[:decay_threshold] }
  115. PreviewCardTrend.transaction do
  116. 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?
  117. PreviewCardTrend.where(preview_card_id: to_delete.map { |(_, preview_card)| preview_card.id }).delete_all if to_delete.any?
  118. 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')
  119. end
  120. end
  121. end