From 5b8a4ae2c9edeeba0da8d14f5907735cc7b4b186 Mon Sep 17 00:00:00 2001 From: Rauno Tegelmann Date: Mon, 14 Oct 2024 12:06:12 +0300 Subject: [PATCH] fix: don't count duration from non-active libraries on user card (#267) --- .vscode/extensions.json | 3 +-- .vscode/settings.json | 11 ++++------- README.md | 2 +- src/utils/getUsersTop.ts | 42 ++++++++++++++++++++++++++++++---------- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 8ea8d9fe..7901b57b 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -4,7 +4,6 @@ "esbenp.prettier-vscode", "dbaeumer.vscode-eslint", "editorconfig.editorconfig", - "stylelint.vscode-stylelint", - "csstools.postcss" + "stylelint.vscode-stylelint" ] } diff --git a/.vscode/settings.json b/.vscode/settings.json index 8a568968..065971f0 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,7 +10,6 @@ "typescript", "typescriptreact" ], - "css.validate": false, "[css]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, @@ -20,15 +19,13 @@ "[typescript]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, - "prettier.documentSelectors": ["**/*.svg"], + "css.validate": false, "stylelint.packageManager": "pnpm", "stylelint.snippet": ["css", "postcss"], "stylelint.validate": ["css", "postcss"], - "typescript.tsdk": "node_modules/typescript/lib", + "prettier.documentSelectors": ["**/*.svg"], "files.associations": { - "*.css": "postcss" + "*.css": "tailwindcss" }, - "tailwindCSS.experimental.classRegex": [ - ["clsx\\(([^)]*)\\)", "(?:'|\"|`)([^']*)(?:'|\"|`)"] - ] + "typescript.tsdk": "node_modules/typescript/lib" } diff --git a/README.md b/README.md index c3f9ee3d..1bfa4d22 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ _Please note that supporting does not guarantee any support or future developmen To learn more about some of the tools used in this project, take a look at the following resources: -- [Tautulli API reference](https://github.com/Tautulli/Tautulli/wiki/Tautulli-API-Reference) +- [Tautulli API reference](https://docs.tautulli.com/extending-tautulli/api-reference) - [Overseerr API reference](https://api-docs.overseerr.dev) - [Next.js docs](https://nextjs.org/docs) - [NextAuth.js docs](https://next-auth.js.org/getting-started/introduction) diff --git a/src/utils/getUsersTop.ts b/src/utils/getUsersTop.ts index c74b3e1c..ba52027c 100644 --- a/src/utils/getUsersTop.ts +++ b/src/utils/getUsersTop.ts @@ -1,6 +1,7 @@ import { TautulliItem, TautulliItemRow } from '@/types/tautulli' import { fetchOverseerrStats, fetchOverseerrUserId } from './fetchOverseerr' import fetchTautulli, { + getLibraries, getLibrariesByType, getUsersCount, } from './fetchTautulli' @@ -29,18 +30,39 @@ export default async function getUsersTop( return null } - const usersRes = await fetchTautulli('get_home_stats', { - stat_id: 'top_users', - stats_count: allUsersCount, - stats_type: 'duration', - ...(after && before ? { after, before } : { time_range: period || '30' }), - }) - const users = usersRes?.response?.data?.rows + const activeLibraries = await getLibraries() + const userStats = await Promise.all( + activeLibraries.map((library) => + fetchTautulli('get_home_stats', { + stat_id: 'top_users', + stats_count: allUsersCount, + stats_type: 'duration', + section_id: library.section_id, + ...(after && before + ? { after, before } + : { time_range: period || '30' }), + }), + ), + ) + const combinedUserStats: { [userId: string]: TautulliItemRow } = {} - if (!users) { - return null - } + userStats.forEach((stat) => { + const users = stat?.response?.data?.rows + if (users) { + users.forEach((user) => { + if (combinedUserStats[user.user_id]) { + combinedUserStats[user.user_id].total_duration += user.total_duration + } else { + combinedUserStats[user.user_id] = { ...user } + } + }) + } + }) + + const users = Object.values(combinedUserStats).sort( + (a, b) => b.total_duration - a.total_duration, + ) const isAnonymousAccess = settings.general.isOutsideAccess && !loggedInUserId const listedUsers = isAnonymousAccess ? users.slice(0, numberOfUsers)