refactor: reduce verbosity and dead code across new integrations
- Remove dead widget.audiobookshelf/navidrome i18n blocks (76 lines) - Simplify audio-stats router (remove duplicated kind casts) - Remove redundant defaultDisplayOptions in audio-stats component - Dedup fetch helpers in uptime-kuma (fetchOkAsync) and paperless-ngx (fetchAuthenticatedAsync) - Simplify navidrome asArray helper and getArtistCountAsync - Simplify uptime-kuma averageUptimePercent and status derivation
This commit is contained in:
6 files changed
+34
-173
No files matched your search
@@ -19,17 +19,8 @@ const emptyLibraryMessage = "Library not found or empty";
|
||||
|
||||
const subsonicAuthErrorCodes = new Set([40, 41]);
|
||||
|
||||
const asArray = <TValue>(value: TValue | TValue[] | undefined): TValue[] => {
|
||||
if (value === undefined) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
return [value];
|
||||
};
|
||||
const asArray = <TValue>(value: TValue | TValue[] | undefined): TValue[] =>
|
||||
[value].flat().filter((item): item is TValue => item !== undefined);
|
||||
|
||||
export class NavidromeIntegration extends Integration {
|
||||
protected async testingAsync(input: IntegrationTestingInput): Promise<TestingResult> {
|
||||
@@ -78,13 +69,10 @@ export class NavidromeIntegration extends Integration {
|
||||
|
||||
private async getArtistCountAsync(): Promise<number> {
|
||||
const response = await this.subsonicRequestAsync("/rest/getArtists.view", {}, { tolerateEmptyLibrary: true });
|
||||
let count = 0;
|
||||
|
||||
for (const index of asArray(response.artists?.index)) {
|
||||
count += asArray(index.artist).length;
|
||||
}
|
||||
|
||||
return count;
|
||||
return asArray(response.artists?.index).reduce(
|
||||
(count, index) => count + asArray(index.artist).length,
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
private async getAlbumAndSongCountsAsync(): Promise<{ albumCount: number; songCount: number }> {
|
||||
|
||||
@@ -39,29 +39,23 @@ export class PaperlessNgxIntegration extends Integration {
|
||||
};
|
||||
}
|
||||
|
||||
private async fetchStatisticsAsync() {
|
||||
const url = this.url("/api/statistics/");
|
||||
const response = await fetchWithTrustedCertificatesAsync(url, {
|
||||
private async fetchAuthenticatedAsync(path: `/${string}`) {
|
||||
const response = await fetchWithTrustedCertificatesAsync(this.url(path), {
|
||||
headers: this.getAuthHeaders(),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new ResponseError(response);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
private async fetchStatisticsAsync() {
|
||||
const response = await this.fetchAuthenticatedAsync("/api/statistics/");
|
||||
return paperlessNgxStatisticsSchema.parse(await response.json());
|
||||
}
|
||||
|
||||
private async fetchCountAsync(path: `/${string}`) {
|
||||
const url = this.url(path);
|
||||
const response = await fetchWithTrustedCertificatesAsync(url, {
|
||||
headers: this.getAuthHeaders(),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new ResponseError(response);
|
||||
}
|
||||
|
||||
const response = await this.fetchAuthenticatedAsync(path);
|
||||
return paperlessNgxPaginatedCountSchema.parse(await response.json()).count;
|
||||
}
|
||||
|
||||
|
||||
@@ -69,15 +69,9 @@ export class UptimeKumaIntegration extends Integration {
|
||||
const latestHeartbeat = heartbeats.at(-1);
|
||||
const uptimeKey = `${monitor.id}_24`;
|
||||
const uptimeRaw = heartbeat.uptimeList[uptimeKey];
|
||||
let uptimePercent24h: number | null = null;
|
||||
if (typeof uptimeRaw === "number") {
|
||||
uptimePercent24h = uptimeRaw * 100;
|
||||
}
|
||||
|
||||
let status: UptimeKumaMonitorCategory = "paused";
|
||||
if (latestHeartbeat) {
|
||||
status = uptimeKumaHeartbeatCategoryMap[latestHeartbeat.status] ?? "down";
|
||||
}
|
||||
const uptimePercent24h = typeof uptimeRaw === "number" ? uptimeRaw * 100 : null;
|
||||
const status: UptimeKumaMonitorCategory =
|
||||
(latestHeartbeat && (uptimeKumaHeartbeatCategoryMap[latestHeartbeat.status] ?? "down")) || "paused";
|
||||
|
||||
return {
|
||||
id: monitor.id,
|
||||
@@ -100,11 +94,8 @@ export class UptimeKumaIntegration extends Integration {
|
||||
.map((monitor) => monitor.uptimePercent24h)
|
||||
.filter((value): value is number => value !== null);
|
||||
|
||||
let averageUptimePercent = 0;
|
||||
if (uptimeValues.length > 0) {
|
||||
const totalUptime = uptimeValues.reduce((sum, value) => sum + value, 0);
|
||||
averageUptimePercent = totalUptime / uptimeValues.length;
|
||||
}
|
||||
const averageUptimePercent =
|
||||
uptimeValues.reduce((sum, value) => sum + value, 0) / Math.max(uptimeValues.length, 1);
|
||||
|
||||
return {
|
||||
totalMonitors: monitors.length,
|
||||
@@ -116,24 +107,20 @@ export class UptimeKumaIntegration extends Integration {
|
||||
};
|
||||
}
|
||||
|
||||
private async getStatusPageAsync() {
|
||||
const response = await fetchWithTrustedCertificatesAsync(this.statusPageUrl());
|
||||
|
||||
private async fetchOkAsync(url: string | URL) {
|
||||
const response = await fetchWithTrustedCertificatesAsync(url);
|
||||
if (!response.ok) {
|
||||
throw new ResponseError(response);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
private async getHeartbeatAsync() {
|
||||
const response = await fetchWithTrustedCertificatesAsync(this.heartbeatUrl());
|
||||
private getStatusPageAsync() {
|
||||
return this.fetchOkAsync(this.statusPageUrl());
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
throw new ResponseError(response);
|
||||
}
|
||||
|
||||
return response;
|
||||
private getHeartbeatAsync() {
|
||||
return this.fetchOkAsync(this.heartbeatUrl());
|
||||
}
|
||||
|
||||
private statusPageUrl() {
|
||||
|
||||
Reference in new issue
Block a user