Fix:Load year in review stats for playback sessions with null mediaMetadata

This commit is contained in:
advplyr 2024-12-02 17:23:25 -06:00
parent c03f18b90a
commit 84803cef82

View File

@ -5,7 +5,7 @@ const fsExtra = require('../../libs/fsExtra')
module.exports = { module.exports = {
/** /**
* *
* @param {number} year YYYY * @param {number} year YYYY
* @returns {Promise<PlaybackSession[]>} * @returns {Promise<PlaybackSession[]>}
*/ */
@ -22,7 +22,7 @@ module.exports = {
}, },
/** /**
* *
* @param {number} year YYYY * @param {number} year YYYY
* @returns {Promise<number>} * @returns {Promise<number>}
*/ */
@ -39,7 +39,7 @@ module.exports = {
}, },
/** /**
* *
* @param {number} year YYYY * @param {number} year YYYY
* @returns {Promise<import('../../models/Book')[]>} * @returns {Promise<import('../../models/Book')[]>}
*/ */
@ -63,7 +63,7 @@ module.exports = {
}, },
/** /**
* *
* @param {number} year YYYY * @param {number} year YYYY
*/ */
async getStatsForYear(year) { async getStatsForYear(year) {
@ -75,7 +75,7 @@ module.exports = {
for (const book of booksAdded) { for (const book of booksAdded) {
// Grab first 25 that have a cover // 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) booksWithCovers.push(book.libraryItem.id)
} }
if (book.duration && !isNaN(book.duration)) { if (book.duration && !isNaN(book.duration)) {
@ -95,45 +95,54 @@ module.exports = {
const listeningSessions = await this.getListeningSessionsForYear(year) const listeningSessions = await this.getListeningSessionsForYear(year)
let totalListeningTime = 0 let totalListeningTime = 0
for (const ls of listeningSessions) { 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) => { authors.forEach((au) => {
if (!authorListeningMap[au.name]) authorListeningMap[au.name] = 0 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) => { narrators.forEach((narrator) => {
if (!narratorListeningMap[narrator]) narratorListeningMap[narrator] = 0 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" // 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) => { genres.forEach((genre) => {
if (!genreListeningMap[genre]) genreListeningMap[genre] = 0 if (!genreListeningMap[genre]) genreListeningMap[genre] = 0
genreListeningMap[genre] += (ls.timeListening || 0) genreListeningMap[genre] += ls.timeListening || 0
}) })
} }
let topAuthors = null let topAuthors = null
topAuthors = Object.keys(authorListeningMap).map(authorName => ({ topAuthors = Object.keys(authorListeningMap)
name: authorName, .map((authorName) => ({
time: Math.round(authorListeningMap[authorName]) name: authorName,
})).sort((a, b) => b.time - a.time).slice(0, 3) time: Math.round(authorListeningMap[authorName])
}))
.sort((a, b) => b.time - a.time)
.slice(0, 3)
let topNarrators = null let topNarrators = null
topNarrators = Object.keys(narratorListeningMap).map(narratorName => ({ topNarrators = Object.keys(narratorListeningMap)
name: narratorName, .map((narratorName) => ({
time: Math.round(narratorListeningMap[narratorName]) name: narratorName,
})).sort((a, b) => b.time - a.time).slice(0, 3) time: Math.round(narratorListeningMap[narratorName])
}))
.sort((a, b) => b.time - a.time)
.slice(0, 3)
let topGenres = null let topGenres = null
topGenres = Object.keys(genreListeningMap).map(genre => ({ topGenres = Object.keys(genreListeningMap)
genre, .map((genre) => ({
time: Math.round(genreListeningMap[genre]) genre,
})).sort((a, b) => b.time - a.time).slice(0, 3) 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 // 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";`, { 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";`, {