perf(backend): reduce projection for playlist total duration calculation; fewer fields fetched

This commit is contained in:
Geert Rademakes 2025-08-08 09:18:48 +02:00
parent 940469ba52
commit 31a420cf5c

View File

@ -153,8 +153,7 @@ router.get('/playlist/*', async (req: Request, res: Response) => {
const totalPages = Math.ceil(totalSongs / limit); const totalPages = Math.ceil(totalSongs / limit);
// Calculate total duration for the entire playlist // Calculate total duration for the entire playlist
const allPlaylistSongs = await Song.find({ id: { $in: trackIds } }).lean(); const totalDuration = (await Song.find({ id: { $in: trackIds } }, { totalTime: 1 }).lean()).reduce((total, song: any) => {
const totalDuration = allPlaylistSongs.reduce((total, song: any) => {
if (!song.totalTime) return total; if (!song.totalTime) return total;
const totalTimeStr = String(song.totalTime); const totalTimeStr = String(song.totalTime);
const seconds = Math.floor(Number(totalTimeStr) / (totalTimeStr.length > 4 ? 1000 : 1)); const seconds = Math.floor(Number(totalTimeStr) / (totalTimeStr.length > 4 ? 1000 : 1));