2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
class ScoreCalculator
|
2013-02-26 00:42:20 +08:00
|
|
|
def self.default_score_weights
|
2013-02-06 03:16:51 +08:00
|
|
|
{ reply_count: 5, like_score: 15, incoming_link_count: 5, bookmark_count: 2, reads: 0.2 }
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(weightings = nil)
|
|
|
|
@weightings = weightings || ScoreCalculator.default_score_weights
|
|
|
|
end
|
|
|
|
|
|
|
|
# Calculate the score for all posts based on the weightings
|
2016-07-22 07:48:26 +08:00
|
|
|
def calculate(opts = nil)
|
|
|
|
update_posts_score(opts)
|
|
|
|
update_posts_rank(opts)
|
|
|
|
update_topics_rank(opts)
|
2014-02-27 08:45:20 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-07-22 07:48:26 +08:00
|
|
|
def update_posts_score(opts)
|
2016-07-08 10:54:03 +08:00
|
|
|
limit = 20_000
|
|
|
|
|
2014-02-27 08:45:20 +08:00
|
|
|
components = []
|
2016-07-22 07:48:26 +08:00
|
|
|
@weightings.each_key { |k| components << "COALESCE(posts.#{k}, 0) * :#{k}" }
|
2014-02-27 08:45:20 +08:00
|
|
|
components = components.join(" + ")
|
|
|
|
|
2018-06-20 15:48:02 +08:00
|
|
|
builder = DB.build <<SQL
|
2016-07-08 10:54:03 +08:00
|
|
|
UPDATE posts p
|
|
|
|
SET score = x.score
|
|
|
|
FROM (
|
2016-07-22 07:48:26 +08:00
|
|
|
SELECT posts.id, #{components} as score FROM posts
|
|
|
|
join topics on posts.topic_id = topics.id
|
2016-07-08 10:54:03 +08:00
|
|
|
/*where*/
|
|
|
|
limit #{limit}
|
|
|
|
) AS x
|
|
|
|
WHERE x.id = p.id
|
|
|
|
SQL
|
2014-02-27 08:45:20 +08:00
|
|
|
|
2016-07-22 07:48:26 +08:00
|
|
|
builder.where("posts.score IS NULL OR posts.score <> #{components}", @weightings)
|
2014-02-27 08:45:20 +08:00
|
|
|
|
2016-07-22 07:48:26 +08:00
|
|
|
filter_topics(builder, opts)
|
2014-02-27 08:45:20 +08:00
|
|
|
|
2018-06-20 15:48:02 +08:00
|
|
|
while builder.exec == limit
|
2016-07-08 10:54:03 +08:00
|
|
|
end
|
2014-02-27 08:45:20 +08:00
|
|
|
end
|
|
|
|
|
2016-07-22 07:48:26 +08:00
|
|
|
def update_posts_rank(opts)
|
2016-07-08 10:54:03 +08:00
|
|
|
limit = 20_000
|
|
|
|
|
2018-06-20 15:48:02 +08:00
|
|
|
builder = DB.build <<~SQL
|
|
|
|
UPDATE posts
|
|
|
|
SET percent_rank = X.percent_rank
|
|
|
|
FROM (
|
|
|
|
SELECT posts.id, Y.percent_rank
|
|
|
|
FROM posts
|
|
|
|
JOIN (
|
|
|
|
SELECT id, percent_rank()
|
|
|
|
OVER (PARTITION BY topic_id ORDER BY SCORE DESC) as percent_rank
|
|
|
|
FROM posts
|
|
|
|
) Y ON Y.id = posts.id
|
|
|
|
JOIN topics ON posts.topic_id = topics.id
|
|
|
|
/*where*/
|
|
|
|
LIMIT #{limit}
|
|
|
|
) AS X
|
|
|
|
WHERE posts.id = X.id
|
|
|
|
SQL
|
2016-07-08 10:54:03 +08:00
|
|
|
|
|
|
|
builder.where("posts.percent_rank IS NULL OR Y.percent_rank <> posts.percent_rank")
|
2013-03-23 03:43:57 +08:00
|
|
|
|
2016-07-22 07:48:26 +08:00
|
|
|
filter_topics(builder, opts)
|
2014-02-27 08:45:20 +08:00
|
|
|
|
2018-06-20 15:48:02 +08:00
|
|
|
while builder.exec == limit
|
2016-07-08 10:54:03 +08:00
|
|
|
end
|
2014-02-27 08:45:20 +08:00
|
|
|
end
|
|
|
|
|
2016-07-22 07:48:26 +08:00
|
|
|
def update_topics_rank(opts)
|
2018-06-20 15:48:02 +08:00
|
|
|
builder = DB.build <<~SQL
|
|
|
|
UPDATE topics AS topics
|
|
|
|
SET has_summary = (topics.like_count >= :likes_required AND
|
|
|
|
topics.posts_count >= :posts_required AND
|
|
|
|
x.max_score >= :score_required),
|
|
|
|
score = x.avg_score
|
|
|
|
FROM (SELECT p.topic_id,
|
|
|
|
MAX(p.score) AS max_score,
|
|
|
|
AVG(p.score) AS avg_score
|
|
|
|
FROM posts AS p
|
|
|
|
GROUP BY p.topic_id) AS x
|
|
|
|
/*where*/
|
|
|
|
SQL
|
|
|
|
|
|
|
|
defaults = {
|
|
|
|
likes_required: SiteSetting.summary_likes_required,
|
|
|
|
posts_required: SiteSetting.summary_posts_required,
|
|
|
|
score_required: SiteSetting.summary_score_threshold,
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.where(<<~SQL, defaults)
|
|
|
|
x.topic_id = topics.id AND
|
|
|
|
(
|
|
|
|
(topics.score <> x.avg_score OR topics.score IS NULL) OR
|
|
|
|
(topics.has_summary IS NULL OR topics.has_summary <> (
|
|
|
|
topics.like_count >= :likes_required AND
|
|
|
|
topics.posts_count >= :posts_required AND
|
|
|
|
x.max_score >= :score_required
|
|
|
|
))
|
|
|
|
)
|
|
|
|
SQL
|
2014-02-27 08:45:20 +08:00
|
|
|
|
2016-07-22 07:48:26 +08:00
|
|
|
filter_topics(builder, opts)
|
2014-02-27 08:45:20 +08:00
|
|
|
|
|
|
|
builder.exec
|
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2016-07-22 07:48:26 +08:00
|
|
|
def filter_topics(builder, opts)
|
|
|
|
return builder unless opts
|
|
|
|
|
|
|
|
if min_topic_age = opts[:min_topic_age]
|
|
|
|
builder.where("topics.bumped_at > :bumped_at ", bumped_at: min_topic_age)
|
|
|
|
end
|
|
|
|
if max_topic_length = opts[:max_topic_length]
|
|
|
|
builder.where("topics.posts_count < :max_topic_length", max_topic_length: max_topic_length)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2014-02-27 08:45:20 +08:00
|
|
|
|
|
|
|
builder
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|