perf: optimize widget integration requests
This commit is contained in:
1 parent
f65969053a
commit
dcef36ffa7
14 files changed
+517
-48
No files matched your search
@@ -23,9 +23,11 @@ export class NzbGetIntegration extends Integration implements IDownloadClientInt
|
||||
|
||||
public async getClientJobsAndStatusAsync(input: { limit: number }): Promise<DownloadClientJobsAndStatus> {
|
||||
const type = "usenet";
|
||||
const queue = await this.nzbGetApiCallAsync("listgroups");
|
||||
const history = await this.nzbGetApiCallAsync("history");
|
||||
const nzbGetStatus = await this.nzbGetApiCallAsync("status");
|
||||
const [queue, history, nzbGetStatus] = await Promise.all([
|
||||
this.nzbGetApiCallAsync("listgroups"),
|
||||
this.nzbGetApiCallAsync("history"),
|
||||
this.nzbGetApiCallAsync("status"),
|
||||
]);
|
||||
const status: DownloadClientStatus = {
|
||||
paused: nzbGetStatus.DownloadPaused,
|
||||
rates: { down: nzbGetStatus.DownloadRate },
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
// @vitest-environment node
|
||||
|
||||
import { Response } from "undici";
|
||||
import { beforeEach, describe, expect, test, vi } from "vitest";
|
||||
|
||||
vi.hoisted(() => {
|
||||
process.env.SKIP_ENV_VALIDATION = "true";
|
||||
process.env.SECRET_ENCRYPTION_KEY = "ff3f4f7ce30e870c9630de9e5d244ffa81101a24ed0dfe5f064beb53a7e684f1";
|
||||
});
|
||||
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/core/infrastructure/http";
|
||||
|
||||
import type { IntegrationSecret } from "../../../base/types";
|
||||
import { NzbGetIntegration } from "../nzbget-integration";
|
||||
|
||||
vi.mock("@homarr/core/infrastructure/http", () => ({
|
||||
fetchWithTrustedCertificatesAsync: vi.fn(),
|
||||
}));
|
||||
|
||||
const mockFetch = vi.mocked(fetchWithTrustedCertificatesAsync);
|
||||
const secrets: IntegrationSecret[] = [
|
||||
{ kind: "username", value: "test-user" },
|
||||
{ kind: "password", value: "test-password" },
|
||||
];
|
||||
|
||||
const createIntegration = () =>
|
||||
new NzbGetIntegration({
|
||||
id: "test-nzbget",
|
||||
name: "Test NZBGet",
|
||||
url: "https://nzbget.example.com",
|
||||
externalUrl: null,
|
||||
decryptedSecrets: secrets,
|
||||
});
|
||||
|
||||
describe("NzbGetIntegration.getClientJobsAndStatusAsync", () => {
|
||||
beforeEach(() => {
|
||||
mockFetch.mockReset();
|
||||
});
|
||||
|
||||
test("starts queue, history, and status requests together", async () => {
|
||||
const releaseRequests = Promise.withResolvers<void>();
|
||||
const startedMethods: string[] = [];
|
||||
const results = {
|
||||
listgroups: [],
|
||||
history: [],
|
||||
status: { DownloadPaused: false, DownloadRate: 0 },
|
||||
} as const;
|
||||
|
||||
mockFetch.mockImplementation(async (_url, init) => {
|
||||
const method = (JSON.parse(String(init?.body)) as { method: keyof typeof results }).method;
|
||||
startedMethods.push(method);
|
||||
await releaseRequests.promise;
|
||||
return new Response(JSON.stringify({ result: results[method] }), {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
});
|
||||
});
|
||||
|
||||
const resultPromise = createIntegration().getClientJobsAndStatusAsync({ limit: 10 });
|
||||
|
||||
try {
|
||||
await vi.waitFor(() => {
|
||||
expect(startedMethods).toEqual(expect.arrayContaining(["listgroups", "history", "status"]));
|
||||
expect(startedMethods).toHaveLength(3);
|
||||
});
|
||||
} finally {
|
||||
releaseRequests.resolve();
|
||||
}
|
||||
|
||||
await expect(resultPromise).resolves.toStrictEqual({
|
||||
status: { paused: false, rates: { down: 0 }, types: ["usenet"] },
|
||||
items: [],
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -25,12 +25,16 @@ export class SabnzbdIntegration extends Integration implements IDownloadClientIn
|
||||
|
||||
public async getClientJobsAndStatusAsync(input: { limit: number }): Promise<DownloadClientJobsAndStatus> {
|
||||
const type = "usenet";
|
||||
const { queue } = await queueSchema.parseAsync(
|
||||
await this.sabNzbApiCallAsync("queue", { limit: input.limit.toString() }),
|
||||
);
|
||||
const { history } = await historySchema.parseAsync(
|
||||
await this.sabNzbApiCallAsync("history", { limit: input.limit.toString() }),
|
||||
);
|
||||
const [queueResult, historyResult] = await Promise.all([
|
||||
this.sabNzbApiCallAsync("queue", { limit: input.limit.toString() }).then((result) =>
|
||||
queueSchema.parseAsync(result),
|
||||
),
|
||||
this.sabNzbApiCallAsync("history", { limit: input.limit.toString() }).then((result) =>
|
||||
historySchema.parseAsync(result),
|
||||
),
|
||||
]);
|
||||
const { queue } = queueResult;
|
||||
const { history } = historyResult;
|
||||
const status: DownloadClientStatus = {
|
||||
paused: queue.paused,
|
||||
rates: { down: Math.floor(Number(queue.kbpersec) * 1024) }, //Actually rounded kiBps ()
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
// @vitest-environment node
|
||||
|
||||
import { Response } from "undici";
|
||||
import { beforeEach, describe, expect, test, vi } from "vitest";
|
||||
|
||||
vi.hoisted(() => {
|
||||
process.env.SKIP_ENV_VALIDATION = "true";
|
||||
process.env.SECRET_ENCRYPTION_KEY = "ff3f4f7ce30e870c9630de9e5d244ffa81101a24ed0dfe5f064beb53a7e684f1";
|
||||
});
|
||||
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/core/infrastructure/http";
|
||||
|
||||
import type { IntegrationSecret } from "../../../base/types";
|
||||
import { SabnzbdIntegration } from "../sabnzbd-integration";
|
||||
|
||||
vi.mock("@homarr/core/infrastructure/http", () => ({
|
||||
fetchWithTrustedCertificatesAsync: vi.fn(),
|
||||
}));
|
||||
|
||||
const mockFetch = vi.mocked(fetchWithTrustedCertificatesAsync);
|
||||
const secrets: IntegrationSecret[] = [{ kind: "apiKey", value: "test-api-key" }];
|
||||
|
||||
const createIntegration = () =>
|
||||
new SabnzbdIntegration({
|
||||
id: "test-sabnzbd",
|
||||
name: "Test SABnzbd",
|
||||
url: "https://sabnzbd.example.com",
|
||||
externalUrl: null,
|
||||
decryptedSecrets: secrets,
|
||||
});
|
||||
|
||||
describe("SabnzbdIntegration.getClientJobsAndStatusAsync", () => {
|
||||
beforeEach(() => {
|
||||
mockFetch.mockReset();
|
||||
});
|
||||
|
||||
test("starts queue and history requests together", async () => {
|
||||
const releaseRequests = Promise.withResolvers<void>();
|
||||
const startedModes: string[] = [];
|
||||
const results = {
|
||||
queue: { queue: { paused: false, kbpersec: "0", slots: [] } },
|
||||
history: { history: { slots: [] } },
|
||||
} as const;
|
||||
|
||||
mockFetch.mockImplementation(async (url) => {
|
||||
const mode = new URL(String(url)).searchParams.get("mode") as keyof typeof results;
|
||||
startedModes.push(mode);
|
||||
await releaseRequests.promise;
|
||||
return new Response(JSON.stringify(results[mode]), {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
});
|
||||
});
|
||||
|
||||
const resultPromise = createIntegration().getClientJobsAndStatusAsync({ limit: 10 });
|
||||
|
||||
try {
|
||||
await vi.waitFor(() => {
|
||||
expect(startedModes).toEqual(expect.arrayContaining(["queue", "history"]));
|
||||
expect(startedModes).toHaveLength(2);
|
||||
});
|
||||
} finally {
|
||||
releaseRequests.resolve();
|
||||
}
|
||||
|
||||
await expect(resultPromise).resolves.toStrictEqual({
|
||||
status: { paused: false, rates: { down: 0 }, types: ["usenet"] },
|
||||
items: [],
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user