我正在使用数学算法来计算“热门”排序类型。它基本上会生成一个长的多位浮点数,其中考虑了时间和给定时间跨度内的投票数。
我让 mysql 处理这个以减少我的服务器上的负载,并且我想确保我没有遗漏任何东西。这部分在我的应用程序中非常重要。
这是它应该如何工作的描述:http: //amix.dk/blog/post/19588
数字:1134028003 只是一个任意数字,它以秒为单位定义应用程序发布日期的数字,这样数字从零开始,随着时间的推移,它们会慢慢增长、增长和增长。
这是我现在为 mysql 提供的内容:
def self.with_hot_ranking
select("resources.*, (
round(
log10(greatest(abs(resources.score),1)) +
if(resources.score > 0, 1, if(resources.score < 0, -1, 0)) *
(UNIX_TIMESTAMP(resources.created_at)-1134028003) /
45000.0
, 7)
) hot_ranking")
end
这是我的实例方法,仅用于测试目的。我想确保这也是正确的。它似乎太快了,我不知道为什么
def hot_ranking
# to sort by hot_ranking use the class method with_hot_ranking instead i.e.
# Resource.with_hot_ranking.order('hot_ranking DESC')
s = self.score
order = Math.log10([s.abs, 1].max)
sign = s <=> 0
seconds = epoch_seconds(self.created_at).to_i - 1134028003
(order + sign * seconds / 45000).round_to(7).to_f
end