New data model update stats page and routes, update users page

This commit is contained in:
advplyr 2022-03-13 17:33:50 -05:00
parent 4bdef893af
commit be1e1e7ba0
7 changed files with 62 additions and 62 deletions

View file

@ -370,11 +370,11 @@ class ApiController {
// User audiobook progress attach book details
if (json.audiobooks && Object.keys(json.audiobooks).length) {
for (const audiobookId in json.audiobooks) {
var audiobook = this.db.audiobooks.find(ab => ab.id === audiobookId)
if (!audiobook) {
Logger.error('[ApiController] Audiobook not found for users progress ' + audiobookId)
var libraryItem = this.db.libraryItems.find(li => li.id === audiobookId)
if (!libraryItem) {
Logger.error('[ApiController] Library item not found for users progress ' + audiobookId)
} else {
json.audiobooks[audiobookId].book = audiobook.book.toJSON()
json.audiobooks[audiobookId].media = libraryItem.media.toJSONExpanded()
}
}
}

View file

@ -411,19 +411,19 @@ class LibraryController {
}
async stats(req, res) {
var audiobooksInLibrary = this.db.audiobooks.filter(ab => ab.libraryId === req.library.id)
var libraryItems = req.libraryItems
var authorsWithCount = libraryHelpers.getAuthorsWithCount(audiobooksInLibrary)
var genresWithCount = libraryHelpers.getGenresWithCount(audiobooksInLibrary)
var abDurationStats = libraryHelpers.getAudiobookDurationStats(audiobooksInLibrary)
var authorsWithCount = libraryHelpers.getAuthorsWithCount(libraryItems)
var genresWithCount = libraryHelpers.getGenresWithCount(libraryItems)
var durationStats = libraryHelpers.getItemDurationStats(libraryItems)
var stats = {
totalBooks: audiobooksInLibrary.length,
totalItems: libraryItems.length,
totalAuthors: Object.keys(authorsWithCount).length,
totalGenres: Object.keys(genresWithCount).length,
totalDuration: abDurationStats.totalDuration,
longestAudiobooks: abDurationStats.longstAudiobooks,
numAudioTracks: abDurationStats.numAudioTracks,
totalSize: libraryHelpers.getAudiobooksTotalSize(audiobooksInLibrary),
totalDuration: durationStats.totalDuration,
longestItems: durationStats.longestItems,
numAudioTracks: durationStats.numAudioTracks,
totalSize: libraryHelpers.getLibraryItemsTotalSize(libraryItems),
authorsWithCount,
genresWithCount
}

View file

@ -244,10 +244,10 @@ module.exports = {
return seriesSortedByAddedAt.slice(0, limit)
},
getGenresWithCount(audiobooks) {
getGenresWithCount(libraryItems) {
var genresMap = {}
audiobooks.forEach((ab) => {
var genres = ab.book.genres || []
libraryItems.forEach((li) => {
var genres = li.media.metadata.genres || []
genres.forEach((genre) => {
if (genresMap[genre]) genresMap[genre].count++
else
@ -260,15 +260,15 @@ module.exports = {
return Object.values(genresMap).sort((a, b) => b.count - a.count)
},
getAuthorsWithCount(audiobooks) {
getAuthorsWithCount(libraryItems) {
var authorsMap = {}
audiobooks.forEach((ab) => {
var authors = ab.book.authorFL ? ab.book.authorFL.split(', ') : []
libraryItems.forEach((li) => {
var authors = li.media.metadata.authors || []
authors.forEach((author) => {
if (authorsMap[author]) authorsMap[author].count++
if (authorsMap[author.id]) authorsMap[author.id].count++
else
authorsMap[author] = {
author,
authorsMap[author.id] = {
author: author.name,
count: 1
}
})
@ -276,26 +276,26 @@ module.exports = {
return Object.values(authorsMap).sort((a, b) => b.count - a.count)
},
getAudiobookDurationStats(audiobooks) {
var sorted = sort(audiobooks).desc(a => a.duration)
var top10 = sorted.slice(0, 10).map(ab => ({ title: ab.book.title, duration: ab.duration })).filter(ab => ab.duration > 0)
getItemDurationStats(libraryItems) {
var sorted = sort(libraryItems).desc(li => li.media.duration)
var top10 = sorted.slice(0, 10).map(li => ({ title: li.media.metadata.title, duration: li.media.duration })).filter(i => i.duration > 0)
var totalDuration = 0
var numAudioTracks = 0
audiobooks.forEach((ab) => {
totalDuration += ab.duration
numAudioTracks += ab.tracks.length
libraryItems.forEach((li) => {
totalDuration += li.media.duration
numAudioTracks += (li.media.tracks || []).length
})
return {
totalDuration,
numAudioTracks,
longstAudiobooks: top10
longestItems: top10
}
},
getAudiobooksTotalSize(audiobooks) {
getLibraryItemsTotalSize(libraryItems) {
var totalSize = 0
audiobooks.forEach((ab) => {
totalSize += ab.size
libraryItems.forEach((li) => {
totalSize += li.media.size
})
return totalSize
},