From 5718c66a2a64752b695dcccd2c9559d41b5981db Mon Sep 17 00:00:00 2001 From: Sacha Greif Date: Sun, 23 Dec 2012 17:25:03 +0100 Subject: [PATCH] only run scoreUpdate if score difference is large enough --- server/scoring.js | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/server/scoring.js b/server/scoring.js index 8703bd659..5de93a32c 100644 --- a/server/scoring.js +++ b/server/scoring.js @@ -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); } });