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
@@ -0,0 +1,60 @@
|
||||
import { beforeEach, describe, expect, test, vi } from "vitest";
|
||||
|
||||
import type { MediaRequestStats } from "@homarr/integrations/types";
|
||||
|
||||
import { mediaRequestStatsRequestHandler } from "./media-request-stats";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
createIntegrationAsync: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@homarr/integrations", () => ({
|
||||
createIntegrationAsync: mocks.createIntegrationAsync,
|
||||
}));
|
||||
|
||||
const integration = {
|
||||
id: "media-request-integration",
|
||||
kind: "overseerr",
|
||||
} as Parameters<typeof mediaRequestStatsRequestHandler.handler>[0];
|
||||
|
||||
describe("mediaRequestStatsRequestHandler", () => {
|
||||
beforeEach(() => {
|
||||
mediaRequestStatsRequestHandler.invalidateCache();
|
||||
mocks.createIntegrationAsync.mockReset();
|
||||
});
|
||||
|
||||
test("starts independent upstream requests together", async () => {
|
||||
const requestsStarted = Promise.withResolvers<void>();
|
||||
const expected = {
|
||||
stats: {
|
||||
total: 8,
|
||||
movie: 5,
|
||||
tv: 3,
|
||||
pending: 1,
|
||||
approved: 2,
|
||||
declined: 1,
|
||||
processing: 1,
|
||||
available: 3,
|
||||
},
|
||||
users: [{ id: 1, displayName: "User", avatar: "/avatar.png", requestCount: 4, link: "/users/1" }],
|
||||
} satisfies MediaRequestStats;
|
||||
const getStatsAsync = vi.fn(async () => {
|
||||
await requestsStarted.promise;
|
||||
return expected.stats;
|
||||
});
|
||||
const getUsersAsync = vi.fn(async () => {
|
||||
await requestsStarted.promise;
|
||||
return expected.users;
|
||||
});
|
||||
mocks.createIntegrationAsync.mockResolvedValue({ getStatsAsync, getUsersAsync });
|
||||
|
||||
const resultPromise = mediaRequestStatsRequestHandler.handler(integration, {}).getDataAsync();
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(getStatsAsync).toHaveBeenCalledOnce();
|
||||
expect(getUsersAsync).toHaveBeenCalledOnce();
|
||||
});
|
||||
requestsStarted.resolve();
|
||||
await expect(resultPromise).resolves.toMatchObject({ data: expected });
|
||||
});
|
||||
});
|
||||
@@ -11,9 +11,13 @@ export const mediaRequestStatsRequestHandler = createIntegrationRequestHandler<
|
||||
>({
|
||||
async requestAsync(integration, _input) {
|
||||
const integrationInstance = await createIntegrationAsync(integration);
|
||||
const [stats, users] = await Promise.all([
|
||||
integrationInstance.getStatsAsync(),
|
||||
integrationInstance.getUsersAsync(),
|
||||
]);
|
||||
return {
|
||||
stats: await integrationInstance.getStatsAsync(),
|
||||
users: await integrationInstance.getUsersAsync(),
|
||||
stats,
|
||||
users,
|
||||
};
|
||||
},
|
||||
cacheTtlMs: 60_000,
|
||||
|
||||
Reference in new issue
Block a user