Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't count duration from non-active libraries on user card #267

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"esbenp.prettier-vscode",
"dbaeumer.vscode-eslint",
"editorconfig.editorconfig",
"stylelint.vscode-stylelint",
"csstools.postcss"
"stylelint.vscode-stylelint"
]
}
11 changes: 4 additions & 7 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"typescript",
"typescriptreact"
],
"css.validate": false,
"[css]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
Expand All @@ -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"
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
42 changes: 32 additions & 10 deletions src/utils/getUsersTop.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TautulliItem, TautulliItemRow } from '@/types/tautulli'
import { fetchOverseerrStats, fetchOverseerrUserId } from './fetchOverseerr'
import fetchTautulli, {
getLibraries,
getLibrariesByType,
getUsersCount,
} from './fetchTautulli'
Expand Down Expand Up @@ -29,18 +30,39 @@ export default async function getUsersTop(
return null
}

const usersRes = await fetchTautulli<TautulliItem>('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<TautulliItem>('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)
Expand Down