mirror of
https://github.com/fallenbagel/jellyseerr.git
synced 2025-12-23 18:29:19 -05:00
fix(jellyfin-scan): reduce jellyfin API calls during recently added scan (#2205)
* fix(jellyfin scanner): reduce jellyfin API calls during recently added scan Significantly reduce number of API calls, addressing CPU spikes on Jellyfin 10.10+ servers.- Move getSeasons() call outside the seasons loop (N calls to 1)- Request MediaSources via getEpisodes() field parameter instead of individual getItemData() calls per episode (N calls to 1 per season) Performance improvements (tested on library with 12 TV shows):- Scan duration: 43.7s to 9.1s - Peak CPU: 277% to 115% - CPU spike duration: 36s to 2s Functionality is unchanged, all availability statuses identicalbefore and after. * fix: add getEpisodes overloads to remove unsafe type assertion * refactor(jellyfin): use generics instead of overloads --------- Co-authored-by: patrick-acland <patrick.acland@kraken.tech>
This commit is contained in:
@@ -112,6 +112,10 @@ export interface JellyfinLibraryItemExtended extends JellyfinLibraryItem {
|
|||||||
DateCreated?: string;
|
DateCreated?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type EpisodeReturn<T> = T extends { includeMediaInfo: true }
|
||||||
|
? JellyfinLibraryItemExtended[]
|
||||||
|
: JellyfinLibraryItem[];
|
||||||
|
|
||||||
export interface JellyfinItemsReponse {
|
export interface JellyfinItemsReponse {
|
||||||
Items: JellyfinLibraryItemExtended[];
|
Items: JellyfinLibraryItemExtended[];
|
||||||
TotalRecordCount: number;
|
TotalRecordCount: number;
|
||||||
@@ -415,13 +419,22 @@ class JellyfinAPI extends ExternalAPI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getEpisodes(
|
public async getEpisodes<
|
||||||
|
T extends { includeMediaInfo?: boolean } | undefined = undefined
|
||||||
|
>(
|
||||||
seriesID: string,
|
seriesID: string,
|
||||||
seasonID: string
|
seasonID: string,
|
||||||
): Promise<JellyfinLibraryItem[]> {
|
options?: T
|
||||||
|
): Promise<EpisodeReturn<T>> {
|
||||||
try {
|
try {
|
||||||
const episodeResponse = await this.get<any>(
|
const episodeResponse = await this.get<any>(
|
||||||
`/Shows/${seriesID}/Episodes?seasonId=${seasonID}`
|
`/Shows/${seriesID}/Episodes`,
|
||||||
|
{
|
||||||
|
params: {
|
||||||
|
seasonId: seasonID,
|
||||||
|
...(options?.includeMediaInfo && { fields: 'MediaSources' }),
|
||||||
|
},
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
return episodeResponse.Items.filter(
|
return episodeResponse.Items.filter(
|
||||||
|
|||||||
@@ -374,9 +374,10 @@ class JellyfinScanner {
|
|||||||
) ?? []
|
) ?? []
|
||||||
).length;
|
).length;
|
||||||
|
|
||||||
|
const jellyfinSeasons = await this.jfClient.getSeasons(Id);
|
||||||
|
|
||||||
for (const season of seasons) {
|
for (const season of seasons) {
|
||||||
const JellyfinSeasons = await this.jfClient.getSeasons(Id);
|
const matchedJellyfinSeason = jellyfinSeasons.find((md) => {
|
||||||
const matchedJellyfinSeason = JellyfinSeasons.find((md) => {
|
|
||||||
if (tvdbSeasonFromAnidb) {
|
if (tvdbSeasonFromAnidb) {
|
||||||
// In AniDB we don't have the concept of seasons,
|
// In AniDB we don't have the concept of seasons,
|
||||||
// we have multiple shows with only Season 1 (and sometimes a season with index 0 for specials).
|
// we have multiple shows with only Season 1 (and sometimes a season with index 0 for specials).
|
||||||
@@ -397,38 +398,52 @@ class JellyfinScanner {
|
|||||||
|
|
||||||
// Check if we found the matching season and it has all the available episodes
|
// Check if we found the matching season and it has all the available episodes
|
||||||
if (matchedJellyfinSeason) {
|
if (matchedJellyfinSeason) {
|
||||||
// If we have a matched Jellyfin season, get its children metadata so we can check details
|
|
||||||
const episodes = await this.jfClient.getEpisodes(
|
|
||||||
Id,
|
|
||||||
matchedJellyfinSeason.Id
|
|
||||||
);
|
|
||||||
|
|
||||||
//Get count of episodes that are HD and 4K
|
|
||||||
let totalStandard = 0;
|
let totalStandard = 0;
|
||||||
let total4k = 0;
|
let total4k = 0;
|
||||||
|
|
||||||
//use for loop to make sure this loop _completes_ in full
|
if (!this.enable4kShow) {
|
||||||
//before the next section
|
const episodes = await this.jfClient.getEpisodes(
|
||||||
for (const episode of episodes) {
|
Id,
|
||||||
let episodeCount = 1;
|
matchedJellyfinSeason.Id
|
||||||
|
);
|
||||||
|
|
||||||
// count number of combined episodes
|
for (const episode of episodes) {
|
||||||
if (
|
let episodeCount = 1;
|
||||||
episode.IndexNumber !== undefined &&
|
|
||||||
episode.IndexNumberEnd !== undefined
|
// count number of combined episodes
|
||||||
) {
|
if (
|
||||||
episodeCount =
|
episode.IndexNumber !== undefined &&
|
||||||
episode.IndexNumberEnd - episode.IndexNumber + 1;
|
episode.IndexNumberEnd !== undefined
|
||||||
}
|
) {
|
||||||
|
episodeCount =
|
||||||
|
episode.IndexNumberEnd - episode.IndexNumber + 1;
|
||||||
|
}
|
||||||
|
|
||||||
if (!this.enable4kShow) {
|
|
||||||
totalStandard += episodeCount;
|
totalStandard += episodeCount;
|
||||||
} else {
|
}
|
||||||
const ExtendedEpisodeData = await this.jfClient.getItemData(
|
} else {
|
||||||
episode.Id
|
// 4K detection enabled - request media info to check resolution
|
||||||
);
|
const episodes = await this.jfClient.getEpisodes(
|
||||||
|
Id,
|
||||||
|
matchedJellyfinSeason.Id,
|
||||||
|
{ includeMediaInfo: true }
|
||||||
|
);
|
||||||
|
|
||||||
ExtendedEpisodeData?.MediaSources?.some((MediaSource) => {
|
for (const episode of episodes) {
|
||||||
|
let episodeCount = 1;
|
||||||
|
|
||||||
|
// count number of combined episodes
|
||||||
|
if (
|
||||||
|
episode.IndexNumber !== undefined &&
|
||||||
|
episode.IndexNumberEnd !== undefined
|
||||||
|
) {
|
||||||
|
episodeCount =
|
||||||
|
episode.IndexNumberEnd - episode.IndexNumber + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// MediaSources field is included in response when includeMediaInfo is true
|
||||||
|
// We iterate all MediaSources to detect if episode has both standard AND 4K versions
|
||||||
|
episode.MediaSources?.some((MediaSource) => {
|
||||||
return MediaSource.MediaStreams.some((MediaStream) => {
|
return MediaSource.MediaStreams.some((MediaStream) => {
|
||||||
if (MediaStream.Type === 'Video') {
|
if (MediaStream.Type === 'Video') {
|
||||||
if ((MediaStream.Width ?? 0) >= 2000) {
|
if ((MediaStream.Width ?? 0) >= 2000) {
|
||||||
|
|||||||
Reference in New Issue
Block a user