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:
Julian Tölle 2022-06-19 20:35:30 +02:00
parent 325d695eda
commit 99a4593774

View file

@ -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)