From 99a4593774b648ac3b76389c51523ceadf0898aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20T=C3=B6lle?= Date: Sun, 19 Jun 2022 20:35:30 +0200 Subject: [PATCH] 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. --- src/reports/reports.service.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/reports/reports.service.ts b/src/reports/reports.service.ts index ad11aee..6c009d5 100644 --- a/src/reports/reports.service.ts +++ b/src/reports/reports.service.ts @@ -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)