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 type { HealthMonitoring } from "@homarr/integrations" ;
4
- import type { ProxmoxClusterInfo } from "@homarr/integrations/types" ;
5
- import { clusterInfoRequestHandler , systemInfoRequestHandler } from "@homarr/request-handler/health-monitoring" ;
7
+ import { systemInfoRequestHandler } from "@homarr/request-handler/health-monitoring" ;
6
8
7
- import { createManyIntegrationMiddleware , createOneIntegrationMiddleware } from "../../middlewares/integration" ;
9
+ import { createManyIntegrationMiddleware } from "../../middlewares/integration" ;
8
10
import { createTRPCRouter , publicProcedure } from "../../trpc" ;
11
+ import { z } from "zod" ;
9
12
10
13
export const healthMonitoringRouter = createTRPCRouter ( {
11
- getSystemHealthStatus : publicProcedure
12
- . unstable_concat ( createManyIntegrationMiddleware ( "query" , "openmediavault" , "dashDot" ) )
13
- . query ( async ( { ctx } ) => {
14
+ getHealthStatus : publicProcedure
15
+ . input ( z . object ( { pointCount : z . number ( ) . optional ( ) , maxElements : z . number ( ) . optional ( ) } ) )
16
+ . unstable_concat ( createManyIntegrationMiddleware ( "query" , "openmediavault" , "dashDot" , "proxmox" ) )
17
+ . query ( async ( { input : { pointCount = 1 , maxElements = 32 } , ctx } ) => {
14
18
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 } ) ;
19
+ ctx . integrations . map ( async ( integrationWithSecrets ) => {
20
+ const innerHandler = systemInfoRequestHandler . handler ( integrationWithSecrets , { maxElements, pointCount } ) ;
21
+ const healthInfo = await innerHandler . getCachedOrUpdatedDataAsync ( { forceUpdate : false } ) ;
22
+
23
+ const { decryptedSecrets : _ , ...integration } = integrationWithSecrets ;
18
24
19
25
return {
20
- integrationId : integration . id ,
21
- integrationName : integration . name ,
22
- healthInfo : data ,
23
- updatedAt : timestamp ,
26
+ integration,
27
+ healthInfo,
24
28
} ;
25
29
} ) ,
26
30
) ;
27
31
} ) ,
28
- subscribeSystemHealthStatus : publicProcedure
29
- . unstable_concat ( createManyIntegrationMiddleware ( "query" , "openmediavault" , "dashDot" ) )
30
- . subscription ( ( { ctx } ) => {
31
- return observable < { integrationId : string ; healthInfo : HealthMonitoring ; timestamp : Date } > ( ( emit ) => {
32
+
33
+ subscribeHealthStatus : publicProcedure
34
+ . input ( z . object ( { maxElements : z . number ( ) . optional ( ) } ) )
35
+ . unstable_concat ( createManyIntegrationMiddleware ( "query" , "openmediavault" , "dashDot" , "proxmox" ) )
36
+ . subscription ( ( { ctx, input : { maxElements = 32 } } ) => {
37
+ return observable < {
38
+ integration : Modify < Integration , { kind : IntegrationKindByCategory < "healthMonitoring" > } > ;
39
+ healthInfo : { data : HealthMonitoring ; timestamp : Date } ;
40
+ } > ( ( emit ) => {
32
41
const unsubscribes : ( ( ) => void ) [ ] = [ ] ;
33
- for ( const integration of ctx . integrations ) {
34
- const innerHandler = systemInfoRequestHandler . handler ( integration , { } ) ;
35
- const unsubscribe = innerHandler . subscribe ( ( healthInfo ) => {
42
+ for ( const integrationWithSecrets of ctx . integrations ) {
43
+ const innerHandler = systemInfoRequestHandler . handler ( integrationWithSecrets , { maxElements, pointCount : 1 } ) ;
44
+ const { decryptedSecrets : _ , ...integration } = integrationWithSecrets ;
45
+ const unsubscribe = innerHandler . subscribe ( ( data ) => {
36
46
emit . next ( {
37
- integrationId : integration . id ,
38
- healthInfo,
39
- timestamp : new Date ( ) ,
47
+ integration,
48
+ healthInfo : { data, timestamp : new Date ( ) } ,
40
49
} ) ;
41
50
} ) ;
42
51
unsubscribes . push ( unsubscribe ) ;
@@ -48,26 +57,4 @@ export const healthMonitoringRouter = createTRPCRouter({
48
57
} ;
49
58
} ) ;
50
59
} ) ,
51
- getClusterHealthStatus : publicProcedure
52
- . unstable_concat ( createOneIntegrationMiddleware ( "query" , "proxmox" ) )
53
- . query ( async ( { ctx } ) => {
54
- const innerHandler = clusterInfoRequestHandler . handler ( ctx . integration , { } ) ;
55
- const { data } = await innerHandler . getCachedOrUpdatedDataAsync ( { forceUpdate : false } ) ;
56
- return data ;
57
- } ) ,
58
- subscribeClusterHealthStatus : publicProcedure
59
- . unstable_concat ( createOneIntegrationMiddleware ( "query" , "proxmox" ) )
60
- . subscription ( ( { ctx } ) => {
61
- return observable < ProxmoxClusterInfo > ( ( emit ) => {
62
- const unsubscribes : ( ( ) => void ) [ ] = [ ] ;
63
- const innerHandler = clusterInfoRequestHandler . handler ( ctx . integration , { } ) ;
64
- const unsubscribe = innerHandler . subscribe ( ( healthInfo ) => {
65
- emit . next ( healthInfo ) ;
66
- } ) ;
67
- unsubscribes . push ( unsubscribe ) ;
68
- return ( ) => {
69
- unsubscribe ( ) ;
70
- } ;
71
- } ) ;
72
- } ) ,
73
60
} ) ;
0 commit comments