2013-02-05 13:16:51 -06:00
|
|
|
class ScoreCalculator
|
|
|
|
|
2013-02-25 10:42:20 -06:00
|
|
|
def self.default_score_weights
|
2013-02-05 13:16:51 -06:00
|
|
|
{
|
|
|
|
reply_count: 5,
|
2013-05-27 11:45:10 -05:00
|
|
|
like_score: 15,
|
2013-02-05 13:16:51 -06:00
|
|
|
incoming_link_count: 5,
|
|
|
|
bookmark_count: 2,
|
|
|
|
avg_time: 0.05,
|
|
|
|
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
|
2013-02-25 10:42:20 -06:00
|
|
|
def calculate
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
# First update the scores of the posts
|
|
|
|
exec_sql(post_score_sql, @weightings)
|
|
|
|
|
2013-03-22 14:43:57 -05:00
|
|
|
# Update the percent rankings of the posts
|
|
|
|
exec_sql("UPDATE posts SET percent_rank = x.percent_rank
|
|
|
|
FROM (SELECT id, percent_rank()
|
|
|
|
OVER (PARTITION BY topic_id ORDER BY SCORE DESC) as percent_rank
|
|
|
|
FROM posts) AS x
|
2013-09-30 01:59:16 -05:00
|
|
|
WHERE x.id = posts.id AND
|
|
|
|
(posts.percent_rank IS NULL OR x.percent_rank <> posts.percent_rank)")
|
2013-03-22 14:43:57 -05:00
|
|
|
|
|
|
|
|
2013-03-28 12:02:59 -05:00
|
|
|
# Update the topics
|
|
|
|
exec_sql "UPDATE topics AS t
|
|
|
|
SET has_best_of = (t.like_count >= :likes_required AND
|
|
|
|
t.posts_count >= :posts_required AND
|
2013-03-29 11:54:16 -05:00
|
|
|
x.max_score >= :score_required),
|
2013-03-28 12:02:59 -05:00
|
|
|
score = x.avg_score
|
|
|
|
FROM (SELECT p.topic_id,
|
2013-03-29 11:54:16 -05:00
|
|
|
MAX(p.score) AS max_score,
|
2013-03-28 12:02:59 -05:00
|
|
|
AVG(p.score) AS avg_score
|
|
|
|
FROM posts AS p
|
|
|
|
GROUP BY p.topic_id) AS x
|
2013-09-30 01:59:16 -05:00
|
|
|
WHERE x.topic_id = t.id AND
|
|
|
|
(
|
|
|
|
(t.score <> x.avg_score OR t.score IS NULL) OR
|
|
|
|
(t.has_best_of IS NULL OR t.has_best_of <> (
|
|
|
|
t.like_count >= :likes_required AND
|
|
|
|
t.posts_count >= :posts_required AND
|
|
|
|
x.max_score >= :score_required
|
|
|
|
))
|
|
|
|
)
|
|
|
|
",
|
2013-03-28 12:02:59 -05:00
|
|
|
likes_required: SiteSetting.best_of_likes_required,
|
|
|
|
posts_required: SiteSetting.best_of_posts_required,
|
|
|
|
score_required: SiteSetting.best_of_score_threshold
|
2013-02-25 10:42:20 -06:00
|
|
|
|
2013-03-28 12:02:59 -05:00
|
|
|
# Update percentage rank of topics
|
|
|
|
exec_sql("UPDATE topics SET percent_rank = x.percent_rank
|
|
|
|
FROM (SELECT id, percent_rank()
|
|
|
|
OVER (ORDER BY SCORE DESC) as percent_rank
|
|
|
|
FROM topics) AS x
|
2013-09-30 01:59:16 -05:00
|
|
|
WHERE x.id = topics.id AND (topics.percent_rank <> x.percent_rank OR topics.percent_rank IS NULL)")
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-03-22 14:43:57 -05:00
|
|
|
def exec_sql(sql, params=nil)
|
2013-02-05 13:16:51 -06:00
|
|
|
ActiveRecord::Base.exec_sql(sql, params)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Generate a SQL statement to update the scores of all posts
|
|
|
|
def post_score_sql
|
2013-10-01 10:11:13 -05:00
|
|
|
components = []
|
|
|
|
@weightings.keys.each { |k| components << "COALESCE(#{k.to_s}, 0) * :#{k.to_s}" }
|
|
|
|
components = components.join(" + ")
|
|
|
|
|
|
|
|
"UPDATE posts SET score = x.score
|
|
|
|
FROM (SELECT id, #{components} as score FROM posts) AS x
|
|
|
|
WHERE x.id = posts.id AND (posts.score IS NULL OR x.score <> posts.score)"
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
end
|