feat: support every widget in the demo dashboard (#6696)
This commit is contained in:
1 parent
40e34181e7
commit
a56ec5d06a
42 files changed
+939
-163
No files matched your search
@@ -1,4 +1,6 @@
|
||||
import { getIntegrationKindsByCategory } from "@homarr/definitions";
|
||||
import type { IntegrationKind } from "@homarr/definitions";
|
||||
import { mockWidgetData } from "@homarr/integrations";
|
||||
import {
|
||||
firewallCpuRequestHandler,
|
||||
firewallInterfacesRequestHandler,
|
||||
@@ -10,29 +12,53 @@ import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries, toPublicIntegrationError } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
const firewallMiddleware = createManyIntegrationMiddleware("query", ...getIntegrationKindsByCategory("firewall"));
|
||||
const firewallMiddleware = createManyIntegrationMiddleware(
|
||||
"query",
|
||||
...getIntegrationKindsByCategory("firewall"),
|
||||
"mock",
|
||||
);
|
||||
|
||||
const queryFirewall = <
|
||||
THandler extends {
|
||||
handler: (integration: any, input: any) => { getDataAsync: () => Promise<{ data: any; timestamp: Date }> };
|
||||
interface FirewallResult<TSummary> {
|
||||
integration: { id: string; name: string; kind: IntegrationKind; updatedAt: Date };
|
||||
summary: TSummary;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
const queryFirewall = <TSummary>(
|
||||
handler: {
|
||||
handler: (
|
||||
integration: any,
|
||||
input: Record<string, never>,
|
||||
) => { getDataAsync: () => Promise<{ data: TSummary; timestamp: Date }> };
|
||||
},
|
||||
>(
|
||||
handler: THandler,
|
||||
createFallback: () => Awaited<ReturnType<ReturnType<THandler["handler"]>["getDataAsync"]>>["data"],
|
||||
createFallback: () => TSummary,
|
||||
getMockData: () => TSummary,
|
||||
) =>
|
||||
publicProcedure.concat(firewallMiddleware).query(async ({ ctx }) =>
|
||||
settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const { data, timestamp } = await handler.handler(integration, {}).getDataAsync();
|
||||
async (integration): Promise<FirewallResult<TSummary>> => {
|
||||
if (integration.kind === "mock") {
|
||||
return {
|
||||
integration: {
|
||||
id: integration.id,
|
||||
name: integration.name,
|
||||
kind: integration.kind,
|
||||
updatedAt: new Date(mockWidgetData.timestamp),
|
||||
},
|
||||
summary: getMockData(),
|
||||
error: undefined,
|
||||
};
|
||||
}
|
||||
const { data, timestamp } = await handler.handler({ ...integration, kind: "opnsense" }, {}).getDataAsync();
|
||||
return {
|
||||
integration: { id: integration.id, name: integration.name, kind: integration.kind, updatedAt: timestamp },
|
||||
summary: data as Awaited<ReturnType<ReturnType<THandler["handler"]>["getDataAsync"]>>["data"],
|
||||
error: undefined as string | undefined,
|
||||
summary: data,
|
||||
error: undefined,
|
||||
};
|
||||
},
|
||||
{
|
||||
fallback: (integration, error) => ({
|
||||
fallback: (integration, error): FirewallResult<TSummary> => ({
|
||||
integration: { id: integration.id, name: integration.name, kind: integration.kind, updatedAt: new Date(0) },
|
||||
summary: createFallback(),
|
||||
error: toPublicIntegrationError(error),
|
||||
@@ -43,8 +69,24 @@ const queryFirewall = <
|
||||
);
|
||||
|
||||
export const firewallRouter = createTRPCRouter({
|
||||
getFirewallCpuStatus: queryFirewall(firewallCpuRequestHandler, () => ({ total: 0 })),
|
||||
getFirewallInterfacesStatus: queryFirewall(firewallInterfacesRequestHandler, () => []),
|
||||
getFirewallVersionStatus: queryFirewall(firewallVersionRequestHandler, () => ({ version: "Unknown" })),
|
||||
getFirewallMemoryStatus: queryFirewall(firewallMemoryRequestHandler, () => ({ used: 0, total: 0, percent: 0 })),
|
||||
getFirewallCpuStatus: queryFirewall(
|
||||
firewallCpuRequestHandler,
|
||||
() => ({ total: 0 }),
|
||||
() => mockWidgetData.firewallCpu,
|
||||
),
|
||||
getFirewallInterfacesStatus: queryFirewall(
|
||||
firewallInterfacesRequestHandler,
|
||||
() => [],
|
||||
() => mockWidgetData.firewallInterfaces,
|
||||
),
|
||||
getFirewallVersionStatus: queryFirewall(
|
||||
firewallVersionRequestHandler,
|
||||
() => ({ version: "Unknown" }),
|
||||
() => mockWidgetData.firewallVersion,
|
||||
),
|
||||
getFirewallMemoryStatus: queryFirewall(
|
||||
firewallMemoryRequestHandler,
|
||||
() => ({ used: 0, total: 0, percent: 0 }),
|
||||
() => mockWidgetData.firewallMemory,
|
||||
),
|
||||
});
|
||||
Reference in new issue
Block a user