|
1 | 1 | import { observable } from "@trpc/server/observable";
|
2 | 2 |
|
| 3 | +import type { Modify } from "@homarr/common/types"; |
| 4 | +import type { Integration } from "@homarr/db/schema/sqlite"; |
| 5 | +import type { IntegrationKindByCategory } from "@homarr/definitions"; |
3 | 6 | import { getIntegrationKindsByCategory } from "@homarr/definitions";
|
4 | 7 | import type { HealthMonitoring } from "@homarr/integrations";
|
5 | 8 | import { systemInfoRequestHandler } from "@homarr/request-handler/health-monitoring";
|
| 9 | +import { z } from "@homarr/validation"; |
6 | 10 |
|
7 | 11 | import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
8 | 12 | import { createTRPCRouter, publicProcedure } from "../../trpc";
|
9 | 13 |
|
10 | 14 | export const healthMonitoringRouter = createTRPCRouter({
|
11 | 15 | getHealthStatus: publicProcedure
|
| 16 | + .input(z.object({ pointCount: z.number().optional(), maxElements: z.number().optional() })) |
12 | 17 | .unstable_concat(createManyIntegrationMiddleware("query", ...getIntegrationKindsByCategory("healthMonitoring")))
|
13 |
| - .query(async ({ ctx }) => { |
| 18 | + .query(async ({ input: { pointCount = 1, maxElements = 32 }, ctx }) => { |
14 | 19 | return await Promise.all(
|
15 |
| - ctx.integrations.map(async (integration) => { |
16 |
| - const innerHandler = systemInfoRequestHandler.handler(integration, {}); |
17 |
| - const { data, timestamp } = await innerHandler.getCachedOrUpdatedDataAsync({ forceUpdate: false }); |
| 20 | + ctx.integrations.map(async (integrationWithSecrets) => { |
| 21 | + const innerHandler = systemInfoRequestHandler.handler(integrationWithSecrets, { maxElements, pointCount }); |
| 22 | + const healthInfo = await innerHandler.getCachedOrUpdatedDataAsync({ forceUpdate: false }); |
| 23 | + |
| 24 | + const { decryptedSecrets: _, ...integration } = integrationWithSecrets; |
18 | 25 |
|
19 | 26 | return {
|
20 |
| - integrationId: integration.id, |
21 |
| - integrationName: integration.name, |
22 |
| - healthInfo: data, |
23 |
| - updatedAt: timestamp, |
| 27 | + integration, |
| 28 | + healthInfo, |
24 | 29 | };
|
25 | 30 | }),
|
26 | 31 | );
|
27 | 32 | }),
|
28 | 33 |
|
29 | 34 | subscribeHealthStatus: publicProcedure
|
| 35 | + .input(z.object({ maxElements: z.number().optional() })) |
30 | 36 | .unstable_concat(createManyIntegrationMiddleware("query", ...getIntegrationKindsByCategory("healthMonitoring")))
|
31 |
| - .subscription(({ ctx }) => { |
32 |
| - return observable<{ integrationId: string; healthInfo: HealthMonitoring; timestamp: Date }>((emit) => { |
| 37 | + .subscription(({ ctx, input: { maxElements = 32 } }) => { |
| 38 | + return observable<{ |
| 39 | + integration: Modify<Integration, { kind: IntegrationKindByCategory<"healthMonitoring"> }>; |
| 40 | + healthInfo: { data: HealthMonitoring; timestamp: Date }; |
| 41 | + }>((emit) => { |
33 | 42 | const unsubscribes: (() => void)[] = [];
|
34 |
| - for (const integration of ctx.integrations) { |
35 |
| - const innerHandler = systemInfoRequestHandler.handler(integration, {}); |
36 |
| - const unsubscribe = innerHandler.subscribe((healthInfo) => { |
| 43 | + for (const integrationWithSecrets of ctx.integrations) { |
| 44 | + const innerHandler = systemInfoRequestHandler.handler(integrationWithSecrets, { maxElements, pointCount: 1 }); |
| 45 | + const { decryptedSecrets: _, ...integration } = integrationWithSecrets; |
| 46 | + const unsubscribe = innerHandler.subscribe((data) => { |
37 | 47 | emit.next({
|
38 |
| - integrationId: integration.id, |
39 |
| - healthInfo, |
40 |
| - timestamp: new Date(), |
| 48 | + integration, |
| 49 | + healthInfo: { data, timestamp: new Date() }, |
41 | 50 | });
|
42 | 51 | });
|
43 | 52 | unsubscribes.push(unsubscribe);
|
|
0 commit comments