chore: refine diffing
This commit is contained in:
parent
0d6eb8f272
commit
1f3af8e72d
1 changed files with 20 additions and 19 deletions
|
@ -55,23 +55,28 @@ const titleCase = (string) => {
|
||||||
|
|
||||||
const diffTracks = (cache, tracks) => {
|
const diffTracks = (cache, tracks) => {
|
||||||
const trackCompareSet = Object.values(tracks)
|
const trackCompareSet = Object.values(tracks)
|
||||||
const cacheCompareSet = Object.values(cache).sort((a, b) => a.time - b.time)
|
const cacheCompareSet = _.orderBy(Object.values(cache), ['time'], ['desc'])
|
||||||
const diffedTracks = {}
|
const diffedTracks = {}
|
||||||
|
|
||||||
const TIMER_CEILING = 3780000 // 63 minutes — an hour plus a 3-minute buffer
|
const getTracksOneHour = (tracks) => {
|
||||||
const tracksOneHour = []
|
const TIMER_CEILING = 3600000 // 1 hour
|
||||||
let trackIndex = 0
|
const tracksOneHour = []
|
||||||
let trackTimer = 0
|
let trackIndex = 0
|
||||||
|
let trackTimer = 0
|
||||||
|
|
||||||
while (trackTimer < TIMER_CEILING) {
|
while (trackTimer < TIMER_CEILING) {
|
||||||
trackTimer = trackTimer + parseInt(trackCompareSet[trackIndex].duration)
|
trackTimer = trackTimer + parseInt(tracks[trackIndex].duration)
|
||||||
tracksOneHour.push(trackCompareSet[trackIndex])
|
tracksOneHour.push(tracks[trackIndex])
|
||||||
trackIndex++
|
trackIndex++
|
||||||
|
}
|
||||||
|
|
||||||
|
return tracksOneHour
|
||||||
}
|
}
|
||||||
const comparedTracks = _.differenceWith(
|
|
||||||
tracksOneHour,
|
const tracksOneHour = getTracksOneHour(trackCompareSet)
|
||||||
cacheCompareSet.slice(-tracksOneHour.length),
|
const cachedTracksOneHour = getTracksOneHour(cacheCompareSet)
|
||||||
(a, b) => _.isEqual(a.id, b.id)
|
const comparedTracks = _.differenceWith(tracksOneHour, cachedTracksOneHour, (a, b) =>
|
||||||
|
_.isEqual(a.id, b.id)
|
||||||
)
|
)
|
||||||
|
|
||||||
for (let i = 0; i < comparedTracks.length; i++)
|
for (let i = 0; i < comparedTracks.length; i++)
|
||||||
|
@ -215,12 +220,8 @@ module.exports = async function () {
|
||||||
...diffedTracks,
|
...diffedTracks,
|
||||||
}
|
}
|
||||||
charts = deriveCharts(updatedCache)
|
charts = deriveCharts(updatedCache)
|
||||||
charts.artists = Object.values(charts.artists)
|
charts.artists = _.orderBy(Object.values(charts.artists), ['plays'], ['desc']).splice(0, 8)
|
||||||
.sort((a, b) => b.plays - a.plays)
|
charts.albums = _.orderBy(Object.values(charts.albums), ['plays'], ['desc']).splice(0, 8)
|
||||||
.splice(0, 8)
|
|
||||||
charts.albums = Object.values(charts.albums)
|
|
||||||
.sort((a, b) => b.plays - a.plays)
|
|
||||||
.splice(0, 8)
|
|
||||||
|
|
||||||
if (!_.isEmpty(diffedTracks)) {
|
if (!_.isEmpty(diffedTracks)) {
|
||||||
await client.send(
|
await client.send(
|
||||||
|
|
Reference in a new issue