2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-12-23 18:50:36 -05:00
|
|
|
class TopTopic < ActiveRecord::Base
|
|
|
|
belongs_to :topic
|
|
|
|
|
2014-08-01 16:24:48 -04:00
|
|
|
# The top topics we want to refresh often
|
|
|
|
def self.refresh_daily!
|
2023-01-12 15:03:26 -05:00
|
|
|
DistributedMutex.synchronize("update_top_topics", validity: 5.minutes) do
|
|
|
|
transaction do
|
|
|
|
remove_invisible_topics
|
|
|
|
add_new_visible_topics
|
2014-08-01 16:24:48 -04:00
|
|
|
|
2023-01-12 15:03:26 -05:00
|
|
|
update_counts_and_compute_scores_for(:daily)
|
|
|
|
end
|
2014-08-01 16:24:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# We don't have to refresh these as often
|
|
|
|
def self.refresh_older!
|
2023-01-12 15:03:26 -05:00
|
|
|
DistributedMutex.synchronize("update_top_topics", validity: 5.minutes) do
|
|
|
|
older_periods = periods - %i[daily all]
|
2014-08-01 16:24:48 -04:00
|
|
|
|
2023-01-12 15:03:26 -05:00
|
|
|
transaction { older_periods.each { |period| update_counts_and_compute_scores_for(period) } }
|
2015-03-06 02:00:29 -05:00
|
|
|
|
2023-01-12 15:03:26 -05:00
|
|
|
compute_top_score_for(:all)
|
|
|
|
end
|
2013-12-23 18:50:36 -05:00
|
|
|
end
|
|
|
|
|
2014-08-01 16:24:48 -04:00
|
|
|
def self.refresh!
|
2014-10-04 00:23:46 -04:00
|
|
|
refresh_daily!
|
|
|
|
refresh_older!
|
2014-08-01 16:24:48 -04:00
|
|
|
end
|
|
|
|
|
2014-08-18 21:58:21 -04:00
|
|
|
def self.periods
|
2015-07-28 12:31:31 -04:00
|
|
|
@@periods ||= %i[all yearly quarterly monthly weekly daily].freeze
|
2014-08-18 21:58:21 -04:00
|
|
|
end
|
2014-08-12 13:42:46 -04:00
|
|
|
|
2015-09-21 14:28:20 -04:00
|
|
|
def self.sorted_periods
|
2016-01-08 05:53:52 -05:00
|
|
|
ascending_periods ||= Enum.new(daily: 1, weekly: 2, monthly: 3, quarterly: 4, yearly: 5, all: 6)
|
2015-09-21 14:28:20 -04:00
|
|
|
end
|
|
|
|
|
2021-07-23 13:52:35 -04:00
|
|
|
def self.score_column_for_period(period)
|
|
|
|
TopTopic.validate_period(period)
|
|
|
|
"#{period}_score"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.validate_period(period)
|
2024-10-20 22:44:43 -04:00
|
|
|
@invalid_period_error ||=
|
|
|
|
Discourse::InvalidParameters.new("Invalid period. Valid periods are #{periods.join(", ")}")
|
|
|
|
|
|
|
|
raise @invalid_period_error if period.blank? || !periods.include?(period.to_sym)
|
|
|
|
rescue NoMethodError
|
|
|
|
raise @invalid_period_error
|
2021-07-23 13:52:35 -04:00
|
|
|
end
|
|
|
|
|
2016-11-23 21:26:39 -05:00
|
|
|
private
|
|
|
|
|
2014-08-18 21:58:21 -04:00
|
|
|
def self.sort_orders
|
2015-03-06 02:00:29 -05:00
|
|
|
@@sort_orders ||= %i[posts views likes op_likes].freeze
|
2014-08-18 21:58:21 -04:00
|
|
|
end
|
2014-08-12 13:42:46 -04:00
|
|
|
|
2014-08-18 21:58:21 -04:00
|
|
|
def self.update_counts_and_compute_scores_for(period)
|
2019-05-06 21:27:05 -04:00
|
|
|
sort_orders.each { |sort| TopTopic.public_send("update_#{sort}_count_for", period) }
|
2014-10-04 00:23:46 -04:00
|
|
|
compute_top_score_for(period)
|
2014-08-18 21:58:21 -04:00
|
|
|
end
|
2014-08-12 13:42:46 -04:00
|
|
|
|
2014-08-18 21:58:21 -04:00
|
|
|
def self.remove_invisible_topics
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec(
|
|
|
|
"WITH category_definition_topic_ids AS (
|
2014-08-12 13:42:46 -04:00
|
|
|
SELECT COALESCE(topic_id, 0) AS id FROM categories
|
|
|
|
), invisible_topic_ids AS (
|
|
|
|
SELECT id
|
|
|
|
FROM topics
|
|
|
|
WHERE deleted_at IS NOT NULL
|
|
|
|
OR NOT visible
|
|
|
|
OR archetype = :private_message
|
|
|
|
OR archived
|
|
|
|
OR id IN (SELECT id FROM category_definition_topic_ids)
|
|
|
|
)
|
|
|
|
DELETE FROM top_topics
|
|
|
|
WHERE topic_id IN (SELECT id FROM invisible_topic_ids)",
|
2014-08-18 21:58:21 -04:00
|
|
|
private_message: Archetype.private_message,
|
|
|
|
)
|
|
|
|
end
|
2014-08-12 13:42:46 -04:00
|
|
|
|
2014-08-18 21:58:21 -04:00
|
|
|
def self.add_new_visible_topics
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec(
|
|
|
|
"WITH category_definition_topic_ids AS (
|
2014-08-12 13:42:46 -04:00
|
|
|
SELECT COALESCE(topic_id, 0) AS id FROM categories
|
|
|
|
), visible_topics AS (
|
|
|
|
SELECT t.id
|
|
|
|
FROM topics t
|
|
|
|
LEFT JOIN top_topics tt ON t.id = tt.topic_id
|
|
|
|
WHERE t.deleted_at IS NULL
|
|
|
|
AND t.visible
|
|
|
|
AND t.archetype <> :private_message
|
|
|
|
AND NOT t.archived
|
|
|
|
AND t.id NOT IN (SELECT id FROM category_definition_topic_ids)
|
|
|
|
AND tt.topic_id IS NULL
|
2014-02-28 14:33:52 -05:00
|
|
|
)
|
2014-08-12 13:42:46 -04:00
|
|
|
INSERT INTO top_topics (topic_id)
|
|
|
|
SELECT id FROM visible_topics",
|
2014-08-18 21:58:21 -04:00
|
|
|
private_message: Archetype.private_message,
|
|
|
|
)
|
|
|
|
end
|
2014-02-28 14:33:52 -05:00
|
|
|
|
2014-08-18 21:58:21 -04:00
|
|
|
def self.update_posts_count_for(period)
|
|
|
|
sql =
|
|
|
|
"SELECT topic_id, GREATEST(COUNT(*), 1) AS count
|
2014-08-12 13:42:46 -04:00
|
|
|
FROM posts
|
|
|
|
WHERE created_at >= :from
|
|
|
|
AND deleted_at IS NULL
|
|
|
|
AND NOT hidden
|
|
|
|
AND post_type = #{Post.types[:regular]}
|
|
|
|
AND user_id <> #{Discourse.system_user.id}
|
|
|
|
GROUP BY topic_id"
|
|
|
|
|
2014-10-04 00:23:46 -04:00
|
|
|
update_top_topics(period, "posts", sql)
|
2014-08-18 21:58:21 -04:00
|
|
|
end
|
2013-12-23 18:50:36 -05:00
|
|
|
|
2014-08-18 21:58:21 -04:00
|
|
|
def self.update_views_count_for(period)
|
|
|
|
sql =
|
|
|
|
"SELECT topic_id, COUNT(*) AS count
|
2014-08-12 13:42:46 -04:00
|
|
|
FROM topic_views
|
|
|
|
WHERE viewed_at >= :from
|
|
|
|
GROUP BY topic_id"
|
2013-12-23 18:50:36 -05:00
|
|
|
|
2014-10-04 00:23:46 -04:00
|
|
|
update_top_topics(period, "views", sql)
|
2014-08-18 21:58:21 -04:00
|
|
|
end
|
2013-12-23 18:50:36 -05:00
|
|
|
|
2014-08-18 21:58:21 -04:00
|
|
|
def self.update_likes_count_for(period)
|
2015-03-06 02:00:29 -05:00
|
|
|
sql =
|
|
|
|
"SELECT topic_id, SUM(like_count) AS count
|
2014-08-12 13:42:46 -04:00
|
|
|
FROM posts
|
|
|
|
WHERE created_at >= :from
|
|
|
|
AND deleted_at IS NULL
|
|
|
|
AND NOT hidden
|
|
|
|
AND post_type = #{Post.types[:regular]}
|
|
|
|
GROUP BY topic_id"
|
2013-12-23 18:50:36 -05:00
|
|
|
|
2014-10-04 00:23:46 -04:00
|
|
|
update_top_topics(period, "likes", sql)
|
2014-08-18 21:58:21 -04:00
|
|
|
end
|
2013-12-23 18:50:36 -05:00
|
|
|
|
2015-03-06 02:00:29 -05:00
|
|
|
def self.update_op_likes_count_for(period)
|
|
|
|
sql =
|
|
|
|
"SELECT topic_id, like_count AS count
|
|
|
|
FROM posts
|
|
|
|
WHERE created_at >= :from
|
|
|
|
AND post_number = 1
|
|
|
|
AND deleted_at IS NULL
|
|
|
|
AND NOT hidden
|
|
|
|
AND post_type = #{Post.types[:regular]}"
|
|
|
|
|
|
|
|
update_top_topics(period, "op_likes", sql)
|
|
|
|
end
|
|
|
|
|
2014-08-18 21:58:21 -04:00
|
|
|
def self.compute_top_score_for(period)
|
2016-01-20 11:36:18 -05:00
|
|
|
log_views_multiplier = SiteSetting.top_topics_formula_log_views_multiplier.to_f
|
|
|
|
log_views_multiplier = 2 if log_views_multiplier == 0
|
|
|
|
|
|
|
|
first_post_likes_multiplier = SiteSetting.top_topics_formula_first_post_likes_multiplier.to_f
|
|
|
|
first_post_likes_multiplier = 0.5 if first_post_likes_multiplier == 0
|
|
|
|
|
|
|
|
least_likes_per_post_multiplier =
|
|
|
|
SiteSetting.top_topics_formula_least_likes_per_post_multiplier.to_f
|
|
|
|
least_likes_per_post_multiplier = 3 if least_likes_per_post_multiplier == 0
|
|
|
|
|
2015-03-06 02:00:29 -05:00
|
|
|
if period == :all
|
|
|
|
top_topics =
|
|
|
|
"(
|
|
|
|
SELECT t.like_count all_likes_count,
|
|
|
|
t.id topic_id,
|
|
|
|
t.posts_count all_posts_count,
|
|
|
|
p.like_count all_op_likes_count,
|
|
|
|
t.views all_views_count
|
|
|
|
FROM topics t
|
|
|
|
JOIN posts p ON p.topic_id = t.id AND p.post_number = 1
|
|
|
|
) as top_topics"
|
|
|
|
time_filter = "false"
|
|
|
|
else
|
|
|
|
top_topics = "top_topics"
|
|
|
|
time_filter = "topics.created_at < :from"
|
|
|
|
end
|
|
|
|
|
2018-06-19 02:13:14 -04:00
|
|
|
sql = <<~SQL
|
2014-08-12 13:42:46 -04:00
|
|
|
WITH top AS (
|
|
|
|
SELECT CASE
|
2015-03-06 02:00:29 -05:00
|
|
|
WHEN #{time_filter} THEN 0
|
2016-01-20 11:36:18 -05:00
|
|
|
ELSE log(GREATEST(#{period}_views_count, 1)) * #{log_views_multiplier} +
|
|
|
|
#{period}_op_likes_count * #{first_post_likes_multiplier} +
|
2015-03-06 02:00:29 -05:00
|
|
|
CASE WHEN #{period}_likes_count > 0 AND #{period}_posts_count > 0
|
|
|
|
THEN
|
2016-01-20 11:36:18 -05:00
|
|
|
LEAST(#{period}_likes_count / #{period}_posts_count, #{least_likes_per_post_multiplier})
|
2015-03-06 02:00:29 -05:00
|
|
|
ELSE 0
|
|
|
|
END +
|
|
|
|
CASE WHEN topics.posts_count < 10 THEN
|
|
|
|
0 - ((10 - topics.posts_count) / 20) * #{period}_op_likes_count
|
|
|
|
ELSE
|
|
|
|
10
|
|
|
|
END +
|
|
|
|
log(GREATEST(#{period}_posts_count, 1))
|
2014-08-12 13:42:46 -04:00
|
|
|
END AS score,
|
|
|
|
topic_id
|
2015-03-06 02:00:29 -05:00
|
|
|
FROM #{top_topics}
|
|
|
|
LEFT JOIN topics ON topics.id = top_topics.topic_id AND
|
|
|
|
topics.deleted_at IS NULL
|
2014-08-12 13:42:46 -04:00
|
|
|
)
|
|
|
|
UPDATE top_topics
|
|
|
|
SET #{period}_score = top.score
|
|
|
|
FROM top
|
|
|
|
WHERE top_topics.topic_id = top.topic_id
|
|
|
|
AND #{period}_score <> top.score
|
2014-08-18 21:58:21 -04:00
|
|
|
SQL
|
2014-08-12 13:42:46 -04:00
|
|
|
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec(sql, from: start_of(period))
|
2024-10-10 09:10:39 -04:00
|
|
|
|
|
|
|
DiscourseEvent.trigger(:top_score_computed, period: period)
|
2014-08-18 21:58:21 -04:00
|
|
|
end
|
2013-12-27 12:10:35 -05:00
|
|
|
|
2014-08-18 21:58:21 -04:00
|
|
|
def self.start_of(period)
|
|
|
|
case period
|
2015-07-28 12:31:31 -04:00
|
|
|
when :yearly
|
|
|
|
1.year.ago
|
|
|
|
when :monthly
|
|
|
|
1.month.ago
|
|
|
|
when :quarterly
|
|
|
|
3.months.ago
|
|
|
|
when :weekly
|
|
|
|
1.week.ago
|
|
|
|
when :daily
|
|
|
|
1.day.ago
|
2013-12-23 18:50:36 -05:00
|
|
|
end
|
2014-08-18 21:58:21 -04:00
|
|
|
end
|
2013-12-23 18:50:36 -05:00
|
|
|
|
2014-08-18 21:58:21 -04:00
|
|
|
def self.update_top_topics(period, sort, inner_join)
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec(
|
|
|
|
"UPDATE top_topics
|
2014-08-12 13:42:46 -04:00
|
|
|
SET #{period}_#{sort}_count = c.count
|
|
|
|
FROM top_topics tt
|
|
|
|
INNER JOIN (#{inner_join}) c ON tt.topic_id = c.topic_id
|
|
|
|
WHERE tt.topic_id = top_topics.topic_id
|
|
|
|
AND tt.#{period}_#{sort}_count <> c.count",
|
2014-08-18 21:58:21 -04:00
|
|
|
from: start_of(period),
|
|
|
|
)
|
|
|
|
end
|
2013-12-23 18:50:36 -05:00
|
|
|
end
|
2014-02-06 19:07:36 -05:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: top_topics
|
|
|
|
#
|
2015-07-28 12:31:31 -04:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# topic_id :integer
|
|
|
|
# yearly_posts_count :integer default(0), not null
|
|
|
|
# yearly_views_count :integer default(0), not null
|
|
|
|
# yearly_likes_count :integer default(0), not null
|
|
|
|
# monthly_posts_count :integer default(0), not null
|
|
|
|
# monthly_views_count :integer default(0), not null
|
|
|
|
# monthly_likes_count :integer default(0), not null
|
|
|
|
# weekly_posts_count :integer default(0), not null
|
|
|
|
# weekly_views_count :integer default(0), not null
|
|
|
|
# weekly_likes_count :integer default(0), not null
|
|
|
|
# daily_posts_count :integer default(0), not null
|
|
|
|
# daily_views_count :integer default(0), not null
|
|
|
|
# daily_likes_count :integer default(0), not null
|
|
|
|
# daily_score :float default(0.0)
|
|
|
|
# weekly_score :float default(0.0)
|
|
|
|
# monthly_score :float default(0.0)
|
|
|
|
# yearly_score :float default(0.0)
|
|
|
|
# all_score :float default(0.0)
|
|
|
|
# daily_op_likes_count :integer default(0), not null
|
|
|
|
# weekly_op_likes_count :integer default(0), not null
|
|
|
|
# monthly_op_likes_count :integer default(0), not null
|
|
|
|
# yearly_op_likes_count :integer default(0), not null
|
|
|
|
# quarterly_posts_count :integer default(0), not null
|
|
|
|
# quarterly_views_count :integer default(0), not null
|
|
|
|
# quarterly_likes_count :integer default(0), not null
|
|
|
|
# quarterly_score :float default(0.0)
|
|
|
|
# quarterly_op_likes_count :integer default(0), not null
|
2014-02-06 19:07:36 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2016-11-23 21:13:03 -05:00
|
|
|
# index_top_topics_on_all_score (all_score)
|
2015-07-28 12:31:31 -04:00
|
|
|
# index_top_topics_on_daily_likes_count (daily_likes_count)
|
|
|
|
# index_top_topics_on_daily_op_likes_count (daily_op_likes_count)
|
|
|
|
# index_top_topics_on_daily_posts_count (daily_posts_count)
|
2016-11-23 21:13:03 -05:00
|
|
|
# index_top_topics_on_daily_score (daily_score)
|
2015-07-28 12:31:31 -04:00
|
|
|
# index_top_topics_on_daily_views_count (daily_views_count)
|
|
|
|
# index_top_topics_on_monthly_likes_count (monthly_likes_count)
|
|
|
|
# index_top_topics_on_monthly_op_likes_count (monthly_op_likes_count)
|
|
|
|
# index_top_topics_on_monthly_posts_count (monthly_posts_count)
|
2016-11-23 21:13:03 -05:00
|
|
|
# index_top_topics_on_monthly_score (monthly_score)
|
2015-07-28 12:31:31 -04:00
|
|
|
# index_top_topics_on_monthly_views_count (monthly_views_count)
|
|
|
|
# index_top_topics_on_quarterly_likes_count (quarterly_likes_count)
|
|
|
|
# index_top_topics_on_quarterly_op_likes_count (quarterly_op_likes_count)
|
|
|
|
# index_top_topics_on_quarterly_posts_count (quarterly_posts_count)
|
|
|
|
# index_top_topics_on_quarterly_views_count (quarterly_views_count)
|
|
|
|
# index_top_topics_on_topic_id (topic_id) UNIQUE
|
|
|
|
# index_top_topics_on_weekly_likes_count (weekly_likes_count)
|
|
|
|
# index_top_topics_on_weekly_op_likes_count (weekly_op_likes_count)
|
|
|
|
# index_top_topics_on_weekly_posts_count (weekly_posts_count)
|
2016-11-23 21:13:03 -05:00
|
|
|
# index_top_topics_on_weekly_score (weekly_score)
|
2015-07-28 12:31:31 -04:00
|
|
|
# index_top_topics_on_weekly_views_count (weekly_views_count)
|
|
|
|
# index_top_topics_on_yearly_likes_count (yearly_likes_count)
|
|
|
|
# index_top_topics_on_yearly_op_likes_count (yearly_op_likes_count)
|
|
|
|
# index_top_topics_on_yearly_posts_count (yearly_posts_count)
|
2016-11-23 21:13:03 -05:00
|
|
|
# index_top_topics_on_yearly_score (yearly_score)
|
2015-07-28 12:31:31 -04:00
|
|
|
# index_top_topics_on_yearly_views_count (yearly_views_count)
|
2014-02-06 19:07:36 -05:00
|
|
|
#
|