mirror of
https://github.com/vale981/Vulcan
synced 2025-03-06 10:01:40 -05:00
make scoring client-side too for latency compensation
This commit is contained in:
parent
2fbb506096
commit
9b441c1048
4 changed files with 47 additions and 51 deletions
|
@ -4,10 +4,8 @@
|
|||
<ul class="post-actions">
|
||||
<li class="post-share desktop"><a href="#" class="share-link"><i class="icon-share"></i><span class="action">Share</span></a>
|
||||
<div class="share-options hidden">
|
||||
{{#constant}}
|
||||
<div data-url="{{#if url}}{{url}}{{else}}{{current_domain}}/posts/{{_id}}{{/if}}" data-text="{{headline}}" class="share-replace">
|
||||
</div>
|
||||
{{/constant}}
|
||||
</div>
|
||||
</li>
|
||||
<li class="post-discuss">
|
||||
|
|
45
lib/scoring.js
Normal file
45
lib/scoring.js
Normal file
|
@ -0,0 +1,45 @@
|
|||
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);
|
||||
// time decay factor
|
||||
var f = 1.3;
|
||||
|
||||
var object = collection.findOne(id);
|
||||
|
||||
// use submitted timestamp if available, else (for pending posts) calculate score using createdAt
|
||||
var age = object.submitted || object.createdAt;
|
||||
|
||||
// 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 = object.baseScore;
|
||||
|
||||
// now multiply by 'age' exponentiated
|
||||
// FIXME: timezones <-- set by server or is getTime() ok?
|
||||
var ageInHours = (new Date().getTime() - age) / (60 * 60 * 1000);
|
||||
|
||||
// HN algorithm
|
||||
var newScore = baseScore / Math.pow(ageInHours + 2, f);
|
||||
|
||||
// Note: before the first time updateScore runs on a new item, its score will be at 0
|
||||
var scoreDiff = Math.abs(object.score - newScore);
|
||||
|
||||
// only update database if difference is larger than x to avoid unnecessary updates
|
||||
if (forceUpdate || scoreDiff > x){
|
||||
collection.update(id, {$set: {score: newScore, inactive: false}});
|
||||
return 1;
|
||||
}else if(ageInHours > n*24){
|
||||
// only set a post as inactive if it's older than n days
|
||||
collection.update(id, {$set: {inactive: true}});
|
||||
}
|
||||
return 0;
|
||||
};
|
|
@ -42,8 +42,8 @@
|
|||
$inc: {votes: vote, baseScore: votePower},
|
||||
$set: {inactive: false}
|
||||
});
|
||||
if(!this.isSimulation)
|
||||
updateScore(collection, id, true);
|
||||
|
||||
updateScore(collection, id, true);
|
||||
|
||||
// Karma
|
||||
// user's posts and comments do not impact his own karma:
|
||||
|
|
|
@ -1,50 +1,3 @@
|
|||
// "secret" server code to recalculate scores
|
||||
|
||||
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);
|
||||
// time decay factor
|
||||
var f = 1.3;
|
||||
|
||||
var object = collection.findOne(id);
|
||||
|
||||
// use submitted timestamp if available, else (for pending posts) calculate score using createdAt
|
||||
var age = object.submitted || object.createdAt;
|
||||
|
||||
// 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 = object.baseScore;
|
||||
|
||||
// now multiply by 'age' exponentiated
|
||||
// FIXME: timezones <-- set by server or is getTime() ok?
|
||||
var ageInHours = (new Date().getTime() - age) / (60 * 60 * 1000);
|
||||
|
||||
// HN algorithm
|
||||
var newScore = baseScore / Math.pow(ageInHours + 2, f);
|
||||
|
||||
// Note: before the first time updateScore runs on a new item, its score will be at 0
|
||||
var scoreDiff = Math.abs(object.score - newScore);
|
||||
|
||||
// only update database if difference is larger than x to avoid unnecessary updates
|
||||
if (forceUpdate || scoreDiff > x){
|
||||
collection.update(id, {$set: {score: newScore, inactive: false}});
|
||||
return 1;
|
||||
}else if(ageInHours > n*24){
|
||||
// only set a post as inactive if it's older than n days
|
||||
collection.update(id, {$set: {inactive: true}});
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
|
||||
Meteor.startup(function () {
|
||||
var scoreInterval = getSetting("scoreUpdateInterval") || 30;
|
||||
|
|
Loading…
Add table
Reference in a new issue