0
我正在使用数学算法来计算'whats hot'排序类型。它基本上会产生一个很长的数字浮点数,它考虑到时间以及给定时间范围内的投票数。这个数学转换看起来正确吗? (ruby - > mysql)
我让MySQL处理这个问题,以减少我的服务器上的负载,我想确保我不会错过任何东西。这部分对我的应用程序非常重要。
下面是它应该如何工作的描述:http://amix.dk/blog/post/19588
数量:1134028003仅仅是定义的秒数为应用程序的发布DAT任意数,这样的数字在零开始,随着时间的推移,他们会慢慢成长,成长壮大。
这就是我现在的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
你应该为它写测试。测试一般值和角落案例。 – 2012-07-10 18:57:49