only run scoreUpdate if score difference is large enough

This commit is contained in:
Sacha Greif 2012-12-23 17:25:03 +01:00
parent 40a02737cf
commit 5718c66a2a

View file

@ -5,18 +5,27 @@ var updateScore = function (collection, id) {
// 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
var baseScore = Math.max(object.votes || 0, object.baseScore || 0);
// var baseScore = Math.max(object.votes || 0, object.baseScore || 0);
var baseScore = object.baseScore;
// now multiply by 'age' exponentiated
// FIXME: timezones <-- set by server or is getTime() ok?
var ageInHours = (new Date().getTime() - object.submitted) / (60 * 60 * 1000);
// HN algorithm (same as Bindle)
// HN algorithm
var newScore = baseScore / Math.pow(ageInHours + 2, 1.3);
collection.update(id, {$set: {score: newScore}});
// Note: before the first time updateScore runs on a new item, its score will be at 0
var scoreDiff = Math.abs(object.score - newScore);
// console.log('old score: '+object.baseScore+' | new score: '+newScore+' | score diff: '+Math.abs(newScore-object.baseScore));
// console.log('updating score | scoreDiff:'+scoreDiff);
// only update database if difference is larger than a given value to avoid extra work
if (scoreDiff > 0.00001){
collection.update(id, {$set: {score: newScore}});
return 1;
}
return 0;
};
Meteor.startup(function () {
@ -24,9 +33,17 @@ Meteor.startup(function () {
// recalculate scores every N seconds
if(scoreInterval>0){
intervalId=Meteor.setInterval(function () {
var updatedPosts = 0;
var updatedComments = 0;
// console.log('tick ('+scoreInterval+')');
Posts.find().forEach(function (post) { updateScore(Posts, post._id); });
Comments.find().forEach(function (comment) { updateScore(Comments, comment._id); });
Posts.find().forEach(function (post) {
updatedPosts += updateScore(Posts, post._id);
});
Comments.find().forEach(function (comment) {
updatedComments += updateScore(Comments, comment._id);
});
console.log("Updated "+updatedPosts+"/"+Posts.find().count()+" Posts")
console.log("Updated "+updatedComments+"/"+Comments.find().count()+" Comments")
}, scoreInterval * 1000);
}
});