feat: API endpoint /stats/getViewsByLibraryType

This commit is contained in:
Zlendy
2025-04-19 19:56:28 +02:00
parent dba9d5a7c6
commit fc8c5fa233
2 changed files with 76 additions and 0 deletions

View File

@@ -516,6 +516,35 @@ router.get("/getViewsByHour", async (req, res) => {
}
});
router.get("/getViewsByLibraryType", async (req, res) => {
try {
const { days = 30 } = req.query;
const { rows } = await db.query(`
SELECT COALESCE(i."Type", 'Other') AS type, COUNT(a."NowPlayingItemId") AS count
FROM jf_playback_activity a LEFT JOIN jf_library_items i ON i."Id" = a."NowPlayingItemId"
WHERE a."ActivityDateInserted" BETWEEN NOW() - CAST($1 || ' days' as INTERVAL) AND NOW()
GROUP BY i."Type"
`, [days]);
const supportedTypes = new Set(["Audio", "Movie", "Series", "Other"]);
const reorganizedData = {};
rows.forEach((item) => {
const { type, count } = item;
if (!supportedTypes.has(type)) return;
reorganizedData[type] = count;
});
res.send(reorganizedData);
} catch (error) {
console.log(error);
res.status(503);
res.send(error);
}
});
router.get("/getGenreUserStats", async (req, res) => {
try {
const { size = 50, page = 1, userid } = req.query;

View File

@@ -3644,6 +3644,53 @@
}
}
},
"/stats/getViewsByLibraryType": {
"get": {
"tags": [
"Stats"
],
"description": "",
"parameters": [
{
"name": "authorization",
"in": "header",
"type": "string"
},
{
"name": "x-api-token",
"in": "header",
"type": "string"
},
{
"name": "req",
"in": "query",
"type": "string"
},
{
"name": "days",
"in": "query",
"type": "string"
}
],
"responses": {
"200": {
"description": "OK"
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
},
"404": {
"description": "Not Found"
},
"503": {
"description": "Service Unavailable"
}
}
}
},
"/stats/getGenreUserStats": {
"get": {
"tags": [