feat: persistent track history
This commit is contained in:
1 parent
4ce16a20ad
commit
4680aaeba6
6 files changed
+112
-44
No files matched your search
@@ -12,6 +12,11 @@ import {
|
||||
putUser,
|
||||
getQuery,
|
||||
setQuery,
|
||||
putPrevious,
|
||||
getPrevious,
|
||||
countPrevious,
|
||||
timeValid,
|
||||
allPrevious,
|
||||
} from './mongo.js'
|
||||
import redis from './redis.js'
|
||||
|
||||
@@ -32,10 +37,40 @@ function getTrackRank(tid) {
|
||||
else return rank + 1
|
||||
}
|
||||
|
||||
function parseTop(results) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
for (let tid in results) {
|
||||
let track = await getTrack(tid)
|
||||
|
||||
if (results[tid] < 0) delete results[tid] // remove negative for negative points
|
||||
if (track.banned) delete results[tid] // remove banned
|
||||
|
||||
let count = await countPrevious(tid, timeValid()) // count previous plays
|
||||
|
||||
if (count > 0) results[tid] = parseInt(results[tid] / (count + 1)) // halve points for every previous playback
|
||||
}
|
||||
|
||||
const tids = Object.keys(results).sort(function (a, b) {
|
||||
return results[b] - results[a]
|
||||
})
|
||||
|
||||
resolve(tids)
|
||||
})
|
||||
}
|
||||
|
||||
function broadcast(cats) {
|
||||
wsBroadcast(cats.map((cat) => ({ cat, ...current[cat] })))
|
||||
}
|
||||
|
||||
export { getTrackRank, parseTop }
|
||||
|
||||
const maxPrevious = 20,
|
||||
staleTop = 3e3
|
||||
|
||||
var topList = await countAllVotes().then((results) => parseTop(results)),
|
||||
lastTop = -1,
|
||||
votesSince = 0
|
||||
|
||||
const current = {
|
||||
next: { tid: null },
|
||||
status: {
|
||||
@@ -46,16 +81,12 @@ const current = {
|
||||
timestamp: new Date(),
|
||||
},
|
||||
previous: {
|
||||
combo: [],
|
||||
combo: (await allPrevious())
|
||||
.slice(0, maxPrevious)
|
||||
.map(({ tid, timestamp }) => [tid, timestamp]),
|
||||
},
|
||||
}
|
||||
|
||||
const maxPrevious = 20
|
||||
|
||||
var topList = [],
|
||||
lastTop = -1,
|
||||
votesSince = 0
|
||||
|
||||
const db = {
|
||||
/**
|
||||
* Methods for Track data
|
||||
@@ -146,6 +177,7 @@ const db = {
|
||||
get list() {
|
||||
return topList
|
||||
},
|
||||
|
||||
/**
|
||||
* Last date topList was updated
|
||||
* @type {Number} Date().now
|
||||
@@ -154,9 +186,6 @@ const db = {
|
||||
return lastTop
|
||||
},
|
||||
|
||||
get changed() {
|
||||
return
|
||||
},
|
||||
/**
|
||||
* Get current Top tracks
|
||||
* @returns {Promise<{}>} Array of Track ids and Fresh flag
|
||||
@@ -165,14 +194,10 @@ const db = {
|
||||
return new Promise((resolve, reject) => {
|
||||
let now = new Date().getTime()
|
||||
|
||||
if (now - lastTop > 60e3 || votesSince >= 2) {
|
||||
// update if stale
|
||||
if (now - lastTop > staleTop || votesSince >= 2) {
|
||||
countAllVotes()
|
||||
.then((top) => {
|
||||
return Promise.all(top.map((tid) => getTrack(tid)))
|
||||
})
|
||||
.then((top) => {
|
||||
return top.filter(({ banned }) => !banned).map(({ id }) => id)
|
||||
})
|
||||
.then((results) => parseTop(results))
|
||||
.then((top) => {
|
||||
topList = top // update the list
|
||||
lastTop = now // update list timestamp
|
||||
@@ -227,17 +252,16 @@ const db = {
|
||||
},
|
||||
|
||||
set status(chunk) {
|
||||
// console.log(current.status.tid, chunk?.tid)
|
||||
if (current.status.tid != chunk?.tid && current.status.tid) {
|
||||
// skip duplicates
|
||||
let last = current.previous.combo[0] ?? [null]
|
||||
|
||||
if (last[0] !== current.status.tid) {
|
||||
// add new [tid, date]
|
||||
current.previous.combo.unshift([
|
||||
current.status.tid,
|
||||
current.status.timestamp,
|
||||
])
|
||||
const { tid, timestamp } = current.status
|
||||
|
||||
current.previous.combo.unshift([tid, timestamp])
|
||||
|
||||
putPrevious(tid, timestamp)
|
||||
|
||||
// pop on overfill
|
||||
if (current.previous.combo.length > maxPrevious)
|
||||
@@ -300,6 +324,11 @@ const db = {
|
||||
})
|
||||
},
|
||||
},
|
||||
previous: {
|
||||
put: putPrevious,
|
||||
get: getPrevious,
|
||||
count: countPrevious,
|
||||
},
|
||||
}
|
||||
|
||||
export default db
|
||||
@@ -43,16 +43,16 @@ http.on('listening', function () {
|
||||
console.info(`http: listening on ${process.env.HTTP_PORT}`)
|
||||
})
|
||||
|
||||
setInterval(async () => {
|
||||
let { top, fresh } = await db.top.get()
|
||||
|
||||
if (fresh) {
|
||||
db.current.update({
|
||||
cat: 'top',
|
||||
content: {
|
||||
tids: top,
|
||||
timestamp: db.top.lastCount,
|
||||
},
|
||||
})
|
||||
}
|
||||
setInterval(() => {
|
||||
db.top.get().then(({ top, fresh }) => {
|
||||
if (fresh) {
|
||||
db.current.update({
|
||||
cat: 'top',
|
||||
content: {
|
||||
tids: top,
|
||||
timestamp: db.top.lastCount,
|
||||
},
|
||||
})
|
||||
}
|
||||
})
|
||||
}, 2e3)
|
||||
@@ -0,0 +1,17 @@
|
||||
import mongoose from 'mongoose'
|
||||
|
||||
const prev = new mongoose.Schema(
|
||||
{
|
||||
tid: String,
|
||||
timestamp: Date,
|
||||
},
|
||||
{
|
||||
collection: 'previous',
|
||||
timestamps: { createdAt: 'createdAt', updatedAt: 'updatedAt' },
|
||||
}
|
||||
)
|
||||
|
||||
const Prev = mongoose.model('Prev', prev)
|
||||
|
||||
export default Prev
|
||||
export { Prev, prev }
|
||||
@@ -4,6 +4,7 @@ import TrackModel from './models/Track.js'
|
||||
import Vote from './models/Vote.js'
|
||||
import User from './models/User.js'
|
||||
import Search from './models/Search.js'
|
||||
import Prev from './models/Previous.js'
|
||||
|
||||
const {
|
||||
DB_HOST,
|
||||
@@ -92,7 +93,7 @@ const timeValid = (set) => {
|
||||
let now = set ?? new Date(),
|
||||
period = process.env.TOP_TIME_VALUE
|
||||
|
||||
if (now.getDay() == 6) {
|
||||
if (now.getDay() == 1) {
|
||||
let saturday = new Date(new Date().setDate(now.getDate() - 2))
|
||||
|
||||
saturday.setHours(0, 0, 0, 0)
|
||||
@@ -236,11 +237,7 @@ function countAllVotes(from) {
|
||||
results[tid] += value
|
||||
}
|
||||
|
||||
const keysSorted = Object.keys(results).sort(function (a, b) {
|
||||
return results[b] - results[a]
|
||||
})
|
||||
|
||||
resolve(keysSorted)
|
||||
resolve(results)
|
||||
})
|
||||
.catch((err) => reject(err))
|
||||
})
|
||||
@@ -317,3 +314,28 @@ function setQuery(data) {
|
||||
}
|
||||
|
||||
export { getQuery, setQuery }
|
||||
|
||||
// #### Search #### #### #### #### #### #### #### ####
|
||||
|
||||
function getPrevious(tid, from = timeValid(), to = new Date()) {
|
||||
return Prev.findOne({ tid, createdAt: { $gt: from, $lt: to } })
|
||||
}
|
||||
|
||||
function allPrevious(from = timeValid(), to = new Date()) {
|
||||
return Prev.find({ createdAt: { $gt: from, $lt: to } }).sort({
|
||||
createdAt: -1,
|
||||
})
|
||||
}
|
||||
|
||||
function countPrevious(tid, from = timeValid(), to = new Date()) {
|
||||
return Prev.countDocuments({
|
||||
tid,
|
||||
createdAt: { $gt: from, $lt: to ?? new Date() },
|
||||
})
|
||||
}
|
||||
|
||||
function putPrevious(tid, timestamp) {
|
||||
return Prev.create({ tid, timestamp })
|
||||
}
|
||||
|
||||
export { getPrevious, allPrevious, countPrevious, putPrevious }
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
import express from 'express'
|
||||
|
||||
import db from '../db.js'
|
||||
import db, { parseTop } from '../db.js'
|
||||
import { countAllVotes } from '../mongo.js'
|
||||
|
||||
const router = express.Router()
|
||||
@@ -95,7 +95,7 @@ router.get(
|
||||
morning.setHours(8, 0, 0, 0)
|
||||
|
||||
countAllVotes(morning).then((tids) => {
|
||||
res.json(tids)
|
||||
parseTop(tids).then((tids) => res.json(tids))
|
||||
})
|
||||
} else if ((mode = 'rolling')) {
|
||||
db.top.get().then(({ top }) => {
|
||||
|
||||
Reference in new issue
Block a user