mirror of
https://github.com/apricote/Listory.git
synced 2026-01-13 13:11:02 +00:00
fix(api): improve performance of all time listens report
The current algorithm is CPU intensive and blocks the event loop for multiple seconds in my deployment. This is not acceptable, as other requests can not be answered during that time. I do not have time to fully fix the issue here, but I did implement an optimization for ALL_TIME reports: Before, the all time report was generated for every timeFrame since 1970, which iterated over the listens many hundred times. We can instead only start the interval at the day of the first listen, and therefore skip 50+ years of calculations.
This commit is contained in:
parent
325d695eda
commit
99a4593774
1 changed files with 11 additions and 0 deletions
|
|
@ -10,6 +10,7 @@ import {
|
|||
isSameMonth,
|
||||
isSameWeek,
|
||||
isSameYear,
|
||||
min,
|
||||
parseJSON,
|
||||
startOfDay,
|
||||
sub,
|
||||
|
|
@ -77,6 +78,16 @@ export class ReportsService {
|
|||
const interval = this.getIntervalFromPreset(timePreset);
|
||||
const { eachOfInterval, isSame } = timeframeToDateFns[timeFrame];
|
||||
|
||||
// Optimize performance for ALL_TIME by skipping eachOfInterval for time
|
||||
// between 1970 and first listen
|
||||
if (timePreset.timePreset === TimePreset.ALL_TIME) {
|
||||
let firstListen = min(listens.map(({ playedAt }) => playedAt));
|
||||
interval.start = startOfDay(firstListen);
|
||||
}
|
||||
|
||||
// TODO: This code blocks the eventloop for multiple seconds if running for
|
||||
// a large interval and with many listens. Refactor to make this more
|
||||
// efficient or make pauses for event loop.
|
||||
const reportItems = eachOfInterval(interval).map((date) => {
|
||||
const count = listens.filter((listen) =>
|
||||
isSame(date, listen.playedAt)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue