Skip to content

Commit d142fee

Browse files
committed
refactor: create trash cache for optimization
1 parent f44eb35 commit d142fee

File tree

3 files changed

+74
-19
lines changed

3 files changed

+74
-19
lines changed

src/trash-guide.ts

+61-7
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,48 @@ import { getConfig } from "./config";
55
import { logger } from "./logger";
66
import { CFIDToConfigGroup, ConfigarrCF, QualityDefintionsRadarr, QualityDefintionsSonarr } from "./types/common.types";
77
import { ConfigCustomFormat, ConfigQualityProfile, ConfigQualityProfileItem } from "./types/config.types";
8-
import { TrashArrSupported, TrashCF, TrashQP, TrashQualityDefintion } from "./types/trashguide.types";
8+
import { TrashArrSupported, TrashCache, TrashCF, TrashQP, TrashQualityDefintion } from "./types/trashguide.types";
99
import { cloneGitRepo, loadJsonFile, mapImportCfToRequestCf, notEmpty, toCarrCF, trashRepoPaths } from "./util";
1010

1111
const DEFAULT_TRASH_GIT_URL = "https://github.com/TRaSH-Guides/Guides";
1212

13+
let cache: TrashCache;
14+
let cacheReady = false;
15+
16+
const createCache = async () => {
17+
logger.debug(`Creating TRaSH-Guides cache ...`);
18+
19+
const radarrCF = await loadTrashCFs("RADARR");
20+
const sonarrCF = await loadTrashCFs("SONARR");
21+
22+
const radarrQP = await loadQPFromTrash("RADARR");
23+
const sonarrQP = await loadQPFromTrash("SONARR");
24+
25+
const radarrQDMovie = await loadQualityDefinitionFromTrash("movie", "RADARR");
26+
const sonarrQDSeries = await loadQualityDefinitionFromTrash("series", "SONARR");
27+
const sonarrQDAnime = await loadQualityDefinitionFromTrash("anime", "SONARR");
28+
29+
cache = {
30+
SONARR: {
31+
qualityProfiles: sonarrQP,
32+
customFormats: sonarrCF,
33+
qualityDefinition: {
34+
anime: sonarrQDAnime,
35+
series: sonarrQDSeries,
36+
},
37+
},
38+
RADARR: {
39+
qualityProfiles: radarrQP,
40+
customFormats: radarrCF,
41+
qualityDefinition: {
42+
movie: radarrQDMovie,
43+
},
44+
},
45+
};
46+
47+
cacheReady = true;
48+
};
49+
1350
export const cloneTrashRepo = async () => {
1451
logger.info(`Checking TRaSH-Guides repo ...`);
1552

@@ -20,6 +57,7 @@ export const cloneTrashRepo = async () => {
2057

2158
const cloneResult = await cloneGitRepo(rootPath, gitUrl, revision);
2259
logger.info(`TRaSH-Guides repo: ref[${cloneResult.ref}], hash[${cloneResult.hash}], path[${cloneResult.localPath}]`);
60+
await createCache();
2361
};
2462

2563
export const loadTrashCFs = async (arrType: TrashArrSupported): Promise<CFIDToConfigGroup> => {
@@ -29,6 +67,10 @@ export const loadTrashCFs = async (arrType: TrashArrSupported): Promise<CFIDToCo
2967
return new Map();
3068
}
3169

70+
if (cacheReady) {
71+
return cache[arrType].customFormats;
72+
}
73+
3274
const carrIdToObject = new Map<string, { carrConfig: ConfigarrCF; requestConfig: MergedCustomFormatResource }>();
3375

3476
let pathForFiles: string;
@@ -54,7 +96,7 @@ export const loadTrashCFs = async (arrType: TrashArrSupported): Promise<CFIDToCo
5496
});
5597
}
5698

57-
logger.debug(`Trash CFs: ${carrIdToObject.size}`);
99+
logger.debug(`(${arrType}) Trash CFs: ${carrIdToObject.size}`);
58100

59101
return carrIdToObject;
60102
};
@@ -65,6 +107,13 @@ export const loadQualityDefinitionFromTrash = async (
65107
): Promise<TrashQualityDefintion> => {
66108
let trashPath = arrType === "RADARR" ? trashRepoPaths.radarrQualitySize : trashRepoPaths.sonarrQualitySize;
67109

110+
if (cacheReady) {
111+
const cacheObject = cache[arrType].qualityDefinition as any;
112+
if (qdType in cacheObject) {
113+
return cacheObject[qdType];
114+
}
115+
}
116+
68117
switch (qdType) {
69118
case "anime":
70119
return loadJsonFile(path.resolve(`${trashPath}/anime.json`));
@@ -73,21 +122,26 @@ export const loadQualityDefinitionFromTrash = async (
73122
case "movie":
74123
return loadJsonFile(path.resolve(`${trashPath}/movie.json`));
75124
case "custom":
76-
throw new Error("Not implemented yet");
125+
throw new Error(`(${arrType}) Not implemented yet`);
77126
default:
78-
throw new Error(`Unknown QualityDefintion type: ${qdType}`);
127+
throw new Error(`(${arrType}) Unknown QualityDefintion type: ${qdType}`);
79128
}
80129
};
81130

82131
export const loadQPFromTrash = async (arrType: TrashArrSupported) => {
83132
let trashPath = arrType === "RADARR" ? trashRepoPaths.radarrQP : trashRepoPaths.sonarrQP;
133+
134+
if (cacheReady) {
135+
return cache[arrType].qualityProfiles;
136+
}
137+
84138
const map = new Map<string, TrashQP>();
85139

86140
try {
87141
const files = fs.readdirSync(`${trashPath}`).filter((fn) => fn.endsWith("json"));
88142

89143
if (files.length <= 0) {
90-
logger.info(`Not found any TRaSH-Guides QualityProfiles. Skipping.`);
144+
logger.info(`(${arrType}) Not found any TRaSH-Guides QualityProfiles. Skipping.`);
91145
}
92146

93147
for (const item of files) {
@@ -96,7 +150,7 @@ export const loadQPFromTrash = async (arrType: TrashArrSupported) => {
96150
map.set(importTrashQP.trash_id, importTrashQP);
97151
}
98152
} catch (err: any) {
99-
logger.warn("Failed loading TRaSH-Guides QualityProfiles. Continue without ...", err?.message);
153+
logger.warn(`(${arrType}) Failed loading TRaSH-Guides QualityProfiles. Continue without ...`, err?.message);
100154
}
101155

102156
// const localPath = getLocalTemplatePath();
@@ -105,7 +159,7 @@ export const loadQPFromTrash = async (arrType: TrashArrSupported) => {
105159
// fillMap(localPath);
106160
// }
107161

108-
logger.debug(`Found ${map.size} TRaSH-Guides QualityProfiles.`);
162+
logger.debug(`(${arrType}) Found ${map.size} TRaSH-Guides QualityProfiles.`);
109163
return map;
110164
};
111165

src/types/common.types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,4 @@ const ArrTypeConst = ["RADARR", "SONARR", "WHISPARR", "READARR"] as const;
8282
export type ArrType = (typeof ArrTypeConst)[number];
8383

8484
export type QualityDefintionsSonarr = "anime" | "series" | "custom";
85-
export type QualityDefintionsRadarr = "movie" | "custom";
85+
export type QualityDefintionsRadarr = "movie" | "sqp-streaming" | "sqp-uhd" | "custom";

src/types/trashguide.types.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ArrType, ImportCF } from "./common.types";
1+
import { ArrType, CFIDToConfigGroup, ImportCF } from "./common.types";
22

33
export type TrashQualityDefintionQuality = {
44
quality: string;
@@ -72,17 +72,18 @@ export type TrashNamingFile = {
7272

7373
export type TrashCache = {
7474
SONARR: {
75-
qualityProfiles: TrashQP[];
76-
customFormats: TrashCF[];
77-
naming: TrashNamingFile;
78-
cfGroups: { [key: string]: string[] };
79-
qualitySize: number;
75+
qualityProfiles: Map<string, TrashQP>;
76+
customFormats: CFIDToConfigGroup;
77+
qualityDefinition: {
78+
series: TrashQualityDefintion;
79+
anime: TrashQualityDefintion;
80+
};
8081
};
8182
RADARR: {
82-
qualityProfiles: TrashQP[];
83-
customFormats: TrashCF[];
84-
naming: TrashNamingFile;
85-
cfGroups: { [key: string]: string[] };
86-
qualitySize: number;
83+
qualityProfiles: Map<string, TrashQP>;
84+
customFormats: CFIDToConfigGroup;
85+
qualityDefinition: {
86+
movie: TrashQualityDefintion;
87+
};
8788
};
8889
};

0 commit comments

Comments
 (0)