Last active
May 10, 2020 11:50
-
-
Save zmitton/895bfa750f097278289eb626a24a22c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Post{ | |
constructor(){ | |
this.temp = Post.initTemp() // use large floating point number | |
this.updatedAt = Math.round(new Date() / 1000) //unix timestamp | |
//other init logic... | |
//save object | |
} | |
hotness(){ | |
let timeInterval = Math.round(new Date() / 1000) - this.updatedAt // in seconds | |
return this.temp * 2**(-1 * timeInterval / Post.halfLife()) | |
} | |
upVote(){ | |
this.temp = this.hotness() + 1 | |
//save object | |
} | |
downVote(){ | |
this.temp = this.hotness() - 1 | |
//save object | |
} | |
static halfLife(){ | |
return 60 * 60 * 24 // simple 1 day half-life (score gets cut in half after one day) | |
} | |
static initTemp(){ | |
// this constant can be tweaked so that new posts appear in the desired location (probably half way down the page) | |
return 5 // this means a new post is considered to be as hot as a post getting about 5 likes per day | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As for assembling hottest post, obviously you'd have to run the hotness function on the whole dataset, but i think a cron job that deletes everything that doesn't make the top 100 could take care of that for now.