Skip to content

Instantly share code, notes, and snippets.

@zmitton
Last active May 10, 2020 11:50
Show Gist options
  • Save zmitton/895bfa750f097278289eb626a24a22c3 to your computer and use it in GitHub Desktop.
Save zmitton/895bfa750f097278289eb626a24a22c3 to your computer and use it in GitHub Desktop.
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
}
}
@zmitton
Copy link
Author

zmitton commented May 9, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment