updated controller

This commit is contained in:
Vito0912 2025-01-01 20:00:40 +01:00
parent 39d40d5ef3
commit 793ef0e9ec
No known key found for this signature in database
GPG Key ID: 29A3D509FE70B237

View File

@ -48,7 +48,7 @@ class LibraryController {
async allStats(req, res) {
try {
const allStats = [];
const combinedStats = { sizes: {}, top: {} }; // Clear structure for combined stats
const combinedStats = {};
let libraries = await Database.libraryModel.getAllWithFolders();
const librariesAccessible = req.user.permissions?.librariesAccessible || [];
@ -74,23 +74,26 @@ class LibraryController {
combinedStats[key] = (combinedStats[key] || 0) + value;
} else if (typeof value === "object") {
for (const [subKey, subValue] of Object.entries(value)) {
if (subValue['size'] !== undefined) {
for (const keyType of ['size', 'count', 'duration']) {
if (subValue[keyType] !== undefined) {
if (combinedStats[key] === undefined) combinedStats[key] = [];
// Insert the current value into the combined stats if its size is bigger than the ten biggest sizes
console.log(subValue['size'])
if (Object.entries(combinedStats[key]).length < 10) {
combinedStats[key].push(subValue);
// Sort the array of sizes
combinedStats[key].sort((a, b) => b["size"] - a["size"]);
combinedStats[key].sort((a, b) => b[keyType] - a[keyType]);
} else {
if (subValue['size'] > combinedStats[key][9]['size']) {
if (subValue[keyType] > combinedStats[key][9][keyType]) {
combinedStats[key].pop();
combinedStats[key].push(subValue);
combinedStats[key].sort((a, b) => b["size"] - a["size"]);
combinedStats[key].sort((a, b) => b[keyType] - a[keyType]);
}
}
break;
}
}
}
}
}
}