Skip to content

Instantly share code, notes, and snippets.

@twfarland
Created February 18, 2014 20:35
Show Gist options
  • Save twfarland/9079502 to your computer and use it in GitHub Desktop.
Save twfarland/9079502 to your computer and use it in GitHub Desktop.
Hot score function mysql implementation
/* hot score */
/* http://bibwild.wordpress.com/2012/05/08/reddit-story-ranking-algorithm/ */
drop function if exists hot;
delimiter $$
create function hot(upvotes int, downvotes int, created_time int unsigned) returns double
begin
declare s int;
declare ord double;
declare secs int;
declare sign int;
set s = upvotes - downvotes;
set ord = log10(greatest(abs(s), 1));
set secs = created_time - 1134028003;
if s > 0 then set sign = 1;
elseif s < 0 then set sign = -1;
else set sign = 0;
end if;
return round((ord * sign) + (secs / 45000), 7);
end $$
delimiter ;
@twfarland
Copy link
Author

To fix later: scores even / close to even are all the same:
hot(x, x+1, y) == hot(x+1, x, y) == hot(x, x, y)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment