mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-02-01 00:18:14 +01:00
Merge pull request #2896 from CoffeeKnyte/master
Split the author call in the library stats page to 2 lighter functions
This commit is contained in:
commit
fd22a6f51d
@ -605,12 +605,12 @@ class LibraryController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (req.library.isBook) {
|
if (req.library.isBook) {
|
||||||
const authors = await authorFilters.getAuthorsWithCount(req.library.id)
|
const authors = await authorFilters.getAuthorsWithCount(req.library.id, 10)
|
||||||
const genres = await libraryItemsBookFilters.getGenresWithCount(req.library.id)
|
const genres = await libraryItemsBookFilters.getGenresWithCount(req.library.id)
|
||||||
const bookStats = await libraryItemsBookFilters.getBookLibraryStats(req.library.id)
|
const bookStats = await libraryItemsBookFilters.getBookLibraryStats(req.library.id)
|
||||||
const longestBooks = await libraryItemsBookFilters.getLongestBooks(req.library.id, 10)
|
const longestBooks = await libraryItemsBookFilters.getLongestBooks(req.library.id, 10)
|
||||||
|
|
||||||
stats.totalAuthors = authors.length
|
stats.totalAuthors = await authorFilters.getAuthorsTotalCount(req.library.id)
|
||||||
stats.authorsWithCount = authors
|
stats.authorsWithCount = authors
|
||||||
stats.totalGenres = genres.length
|
stats.totalGenres = genres.length
|
||||||
stats.genresWithCount = genres
|
stats.genresWithCount = genres
|
||||||
|
@ -2,35 +2,49 @@ const Sequelize = require('sequelize')
|
|||||||
const Database = require('../../Database')
|
const Database = require('../../Database')
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
/**
|
||||||
|
* Get authors total count
|
||||||
|
* @param {string} libraryId
|
||||||
|
* @returns {number} count
|
||||||
|
*/
|
||||||
|
async getAuthorsTotalCount(libraryId) {
|
||||||
|
const authorsCount = await Database.authorModel.count({
|
||||||
|
where: {
|
||||||
|
libraryId: libraryId
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return authorsCount;
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get authors with count of num books
|
* Get authors with count of num books
|
||||||
* @param {string} libraryId
|
* @param {string} libraryId
|
||||||
|
* @param {number} limit
|
||||||
* @returns {{id:string, name:string, count:number}}
|
* @returns {{id:string, name:string, count:number}}
|
||||||
*/
|
*/
|
||||||
async getAuthorsWithCount(libraryId) {
|
async getAuthorsWithCount(libraryId, limit) {
|
||||||
const authors = await Database.authorModel.findAll({
|
const authors = await Database.bookAuthorModel.findAll({
|
||||||
where: [
|
include: [{
|
||||||
{
|
model: Database.authorModel,
|
||||||
libraryId
|
as: 'author', // Use the correct alias as defined in your associations
|
||||||
},
|
attributes: ['name'],
|
||||||
Sequelize.where(Sequelize.literal('count'), {
|
where: {
|
||||||
[Sequelize.Op.gt]: 0
|
libraryId: libraryId
|
||||||
})
|
}
|
||||||
],
|
}],
|
||||||
attributes: [
|
attributes: [
|
||||||
'id',
|
'authorId',
|
||||||
'name',
|
[Sequelize.fn('COUNT', Sequelize.col('authorId')), 'count']
|
||||||
[Sequelize.literal('(SELECT count(*) FROM bookAuthors ba WHERE ba.authorId = author.id)'), 'count']
|
|
||||||
],
|
],
|
||||||
order: [
|
group: ['authorId', 'author.id'], // Include 'author.id' to satisfy GROUP BY with JOIN
|
||||||
['count', 'DESC']
|
order: [[Sequelize.literal('count'), 'DESC']],
|
||||||
]
|
limit: limit
|
||||||
})
|
})
|
||||||
return authors.map(au => {
|
return authors.map(au => {
|
||||||
return {
|
return {
|
||||||
id: au.id,
|
id: au.authorId,
|
||||||
name: au.name,
|
name: au.author.name,
|
||||||
count: au.dataValues.count
|
count: au.get('count') // Use get method to access aliased attributes
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user