From 84803cef82226ca3382dc9a76cc5a42292720c76 Mon Sep 17 00:00:00 2001 From: advplyr Date: Mon, 2 Dec 2024 17:23:25 -0600 Subject: [PATCH] Fix:Load year in review stats for playback sessions with null mediaMetadata --- server/utils/queries/adminStats.js | 57 +++++++++++++++++------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/server/utils/queries/adminStats.js b/server/utils/queries/adminStats.js index 0c490de4..9d7f572a 100644 --- a/server/utils/queries/adminStats.js +++ b/server/utils/queries/adminStats.js @@ -5,7 +5,7 @@ const fsExtra = require('../../libs/fsExtra') module.exports = { /** - * + * * @param {number} year YYYY * @returns {Promise} */ @@ -22,7 +22,7 @@ module.exports = { }, /** - * + * * @param {number} year YYYY * @returns {Promise} */ @@ -39,7 +39,7 @@ module.exports = { }, /** - * + * * @param {number} year YYYY * @returns {Promise} */ @@ -63,7 +63,7 @@ module.exports = { }, /** - * + * * @param {number} year YYYY */ async getStatsForYear(year) { @@ -75,7 +75,7 @@ module.exports = { for (const book of booksAdded) { // Grab first 25 that have a cover - if (book.coverPath && !booksWithCovers.includes(book.libraryItem.id) && booksWithCovers.length < 25 && await fsExtra.pathExists(book.coverPath)) { + if (book.coverPath && !booksWithCovers.includes(book.libraryItem.id) && booksWithCovers.length < 25 && (await fsExtra.pathExists(book.coverPath))) { booksWithCovers.push(book.libraryItem.id) } if (book.duration && !isNaN(book.duration)) { @@ -95,45 +95,54 @@ module.exports = { const listeningSessions = await this.getListeningSessionsForYear(year) let totalListeningTime = 0 for (const ls of listeningSessions) { - totalListeningTime += (ls.timeListening || 0) + totalListeningTime += ls.timeListening || 0 - const authors = ls.mediaMetadata.authors || [] + const authors = ls.mediaMetadata?.authors || [] authors.forEach((au) => { if (!authorListeningMap[au.name]) authorListeningMap[au.name] = 0 - authorListeningMap[au.name] += (ls.timeListening || 0) + authorListeningMap[au.name] += ls.timeListening || 0 }) - const narrators = ls.mediaMetadata.narrators || [] + const narrators = ls.mediaMetadata?.narrators || [] narrators.forEach((narrator) => { if (!narratorListeningMap[narrator]) narratorListeningMap[narrator] = 0 - narratorListeningMap[narrator] += (ls.timeListening || 0) + narratorListeningMap[narrator] += ls.timeListening || 0 }) // Filter out bad genres like "audiobook" and "audio book" - const genres = (ls.mediaMetadata.genres || []).filter(g => g && !g.toLowerCase().includes('audiobook') && !g.toLowerCase().includes('audio book')) + const genres = (ls.mediaMetadata?.genres || []).filter((g) => g && !g.toLowerCase().includes('audiobook') && !g.toLowerCase().includes('audio book')) genres.forEach((genre) => { if (!genreListeningMap[genre]) genreListeningMap[genre] = 0 - genreListeningMap[genre] += (ls.timeListening || 0) + genreListeningMap[genre] += ls.timeListening || 0 }) } let topAuthors = null - topAuthors = Object.keys(authorListeningMap).map(authorName => ({ - name: authorName, - time: Math.round(authorListeningMap[authorName]) - })).sort((a, b) => b.time - a.time).slice(0, 3) + topAuthors = Object.keys(authorListeningMap) + .map((authorName) => ({ + name: authorName, + time: Math.round(authorListeningMap[authorName]) + })) + .sort((a, b) => b.time - a.time) + .slice(0, 3) let topNarrators = null - topNarrators = Object.keys(narratorListeningMap).map(narratorName => ({ - name: narratorName, - time: Math.round(narratorListeningMap[narratorName]) - })).sort((a, b) => b.time - a.time).slice(0, 3) + topNarrators = Object.keys(narratorListeningMap) + .map((narratorName) => ({ + name: narratorName, + time: Math.round(narratorListeningMap[narratorName]) + })) + .sort((a, b) => b.time - a.time) + .slice(0, 3) let topGenres = null - topGenres = Object.keys(genreListeningMap).map(genre => ({ - genre, - time: Math.round(genreListeningMap[genre]) - })).sort((a, b) => b.time - a.time).slice(0, 3) + topGenres = Object.keys(genreListeningMap) + .map((genre) => ({ + genre, + time: Math.round(genreListeningMap[genre]) + })) + .sort((a, b) => b.time - a.time) + .slice(0, 3) // Stats for total books, size and duration for everything added this year or earlier const [totalStatResultsRow] = await Database.sequelize.query(`SELECT SUM(li.size) AS totalSize, SUM(b.duration) AS totalDuration, COUNT(*) AS totalItems FROM libraryItems li, books b WHERE b.id = li.mediaId AND li.mediaType = 'book' AND li.createdAt < ":nextYear-01-01";`, {