2012-09-27 13:13:55 -07:00
|
|
|
// "secret" server code to recalculate scores
|
2012-09-11 15:32:25 +10:00
|
|
|
|
2012-12-24 10:59:13 +01:00
|
|
|
var updateScore = function (collection, id, forceUpdate) {
|
|
|
|
var forceUpdate = typeof forceUpdate !== 'undefined' ? forceUpdate : false;
|
|
|
|
// For performance reasons, the database is only updated if the difference between the old score and the new score
|
|
|
|
// is meaningful enough. To find out, we calculate the "power" of a single vote after n days.
|
|
|
|
// We assume that after n days, a single vote will not be powerful enough to affect posts' ranking order.
|
|
|
|
// Note: sites whose posts regularly get a lot of votes can afford to use a lower n.
|
|
|
|
|
|
|
|
// n = number of days after which a single vote will not have a big enough effect to trigger a score update
|
|
|
|
// and posts can become inactive
|
|
|
|
var n = 30;
|
|
|
|
// x = score increase amount of a single vote after n days (for n=100, x=0.000040295)
|
|
|
|
var x = 1/Math.pow(n*24+2,1.3);
|
2012-09-27 13:13:55 -07:00
|
|
|
var object = collection.findOne(id);
|
2012-09-12 08:53:13 +09:00
|
|
|
|
2013-01-13 12:34:29 +09:00
|
|
|
// use submitted timestamp if available, else (for pending posts) calculate score using createdAt
|
|
|
|
var age = object.submitted || object.createdAt;
|
|
|
|
|
2012-09-27 13:13:55 -07:00
|
|
|
// use baseScore if defined, if not just use the number of votes
|
|
|
|
// note: for transition period, also use votes if there are more votes than baseScore
|
2012-12-23 17:25:03 +01:00
|
|
|
// var baseScore = Math.max(object.votes || 0, object.baseScore || 0);
|
|
|
|
var baseScore = object.baseScore;
|
2012-09-17 12:51:35 +09:00
|
|
|
|
2012-09-27 13:13:55 -07:00
|
|
|
// now multiply by 'age' exponentiated
|
|
|
|
// FIXME: timezones <-- set by server or is getTime() ok?
|
2013-01-14 10:53:36 +09:00
|
|
|
var ageInHours = (new Date().getTime() - age) / (60 * 60 * 1000);
|
2012-09-20 10:59:53 +10:00
|
|
|
|
2012-12-23 17:25:03 +01:00
|
|
|
// HN algorithm
|
2012-09-27 13:13:55 -07:00
|
|
|
var newScore = baseScore / Math.pow(ageInHours + 2, 1.3);
|
|
|
|
|
2013-01-14 10:53:36 +09:00
|
|
|
// console.log('newScore: '+newScore);
|
|
|
|
|
2012-12-23 17:25:03 +01:00
|
|
|
// Note: before the first time updateScore runs on a new item, its score will be at 0
|
|
|
|
var scoreDiff = Math.abs(object.score - newScore);
|
2012-12-07 09:31:38 +09:00
|
|
|
|
2012-12-23 17:25:03 +01:00
|
|
|
// console.log('updating score | scoreDiff:'+scoreDiff);
|
2012-12-24 10:59:13 +01:00
|
|
|
collection.update(id, {$set: {inactive: false}});
|
2012-12-23 17:25:03 +01:00
|
|
|
|
2012-12-24 10:59:13 +01:00
|
|
|
// only update database if difference is larger than x to avoid unnecessary updates
|
|
|
|
if (forceUpdate || scoreDiff > x){
|
2012-12-31 18:18:46 +01:00
|
|
|
// console.log('updating: '+object.headline)
|
2012-12-23 17:25:03 +01:00
|
|
|
collection.update(id, {$set: {score: newScore}});
|
|
|
|
return 1;
|
2012-12-24 10:59:13 +01:00
|
|
|
}else if(ageInHours > n*24){
|
|
|
|
// only set a post as inactive if it's older than n days
|
|
|
|
collection.update(id, {$set: {inactive: true}});
|
2012-12-23 17:25:03 +01:00
|
|
|
}
|
|
|
|
return 0;
|
2012-09-27 13:13:55 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Meteor.startup(function () {
|
2012-11-21 12:30:45 +09:00
|
|
|
var scoreInterval = getSetting("scoreUpdateInterval") || 30;
|
2012-10-22 14:00:47 +09:00
|
|
|
if(scoreInterval>0){
|
2012-12-24 16:21:56 +01:00
|
|
|
|
|
|
|
// active items get updated every N seconds
|
2012-10-22 14:00:47 +09:00
|
|
|
intervalId=Meteor.setInterval(function () {
|
2012-12-23 17:25:03 +01:00
|
|
|
var updatedPosts = 0;
|
|
|
|
var updatedComments = 0;
|
2012-10-23 12:24:38 +09:00
|
|
|
// console.log('tick ('+scoreInterval+')');
|
2012-12-24 10:59:13 +01:00
|
|
|
Posts.find({'inactive': {$ne : true}}).forEach(function (post) {
|
2012-12-23 17:25:03 +01:00
|
|
|
updatedPosts += updateScore(Posts, post._id);
|
|
|
|
});
|
2012-12-24 10:59:13 +01:00
|
|
|
Comments.find({'inactive': {$ne : true}}).forEach(function (comment) {
|
2012-12-23 17:25:03 +01:00
|
|
|
updatedComments += updateScore(Comments, comment._id);
|
|
|
|
});
|
2012-12-31 18:18:46 +01:00
|
|
|
// console.log("Updated "+updatedPosts+"/"+Posts.find().count()+" Posts")
|
|
|
|
// console.log("Updated "+updatedComments+"/"+Comments.find().count()+" Comments")
|
2012-10-22 14:00:47 +09:00
|
|
|
}, scoreInterval * 1000);
|
2012-12-24 16:21:56 +01:00
|
|
|
|
|
|
|
// inactive items get updated every hour
|
|
|
|
inactiveIntervalId=Meteor.setInterval(function () {
|
2013-01-11 11:17:24 +09:00
|
|
|
var updatedPosts = 0;
|
|
|
|
var updatedComments = 0;
|
2012-12-24 16:21:56 +01:00
|
|
|
Posts.find({'inactive': true}).forEach(function (post) {
|
|
|
|
updatedPosts += updateScore(Posts, post._id);
|
|
|
|
});
|
|
|
|
Comments.find({'inactive': true}).forEach(function (comment) {
|
|
|
|
updatedComments += updateScore(Comments, comment._id);
|
|
|
|
});
|
|
|
|
}, 3600 * 1000);
|
|
|
|
|
2012-10-22 14:00:47 +09:00
|
|
|
}
|
2012-09-27 13:13:55 -07:00
|
|
|
});
|