I’m using a math algorithm to calculate a ‘whats hot’ sorting type. It basically generates a long many digit float number which takes into account time, and the number of votes in a given time span.
I am letting mysql handle this to reduce load on my server, and I want to make sure I’m not missing anything. This part is super important on my app.
Here’s a description of how its supposed to work: http://amix.dk/blog/post/19588
The number: 1134028003 is just an arbitrary number that defines the number in seconds for the release dat of the app, that way the numbers start at zero and as time goes on they slowly grow and grow and grow.
Here’s what I have now for 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
This is my instance method, to be used only for testing purposes. id like to make sure this is right as well. it seems to be rounding too soon and im not sure why
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
Looks like the linked formula, except you are using a double
IFinstead ofSIGN().