From 92179cbbb22176a131ee710014a9440a12595b63 Mon Sep 17 00:00:00 2001 From: Henry Dollman Date: Tue, 15 Oct 2024 21:59:05 -0400 Subject: [PATCH] show active alerts in dashboard --- beszel/site/src/components/routes/home.tsx | 62 ++++++++++++++++++++- beszel/site/src/components/table-alerts.tsx | 2 +- beszel/site/src/components/ui/alert.tsx | 59 ++++++++++++++++++++ beszel/site/src/components/ui/icons.tsx | 7 +-- beszel/site/src/lib/utils.ts | 32 ++++++++++- beszel/site/src/types.d.ts | 2 + 6 files changed, 154 insertions(+), 10 deletions(-) create mode 100644 beszel/site/src/components/ui/alert.tsx diff --git a/beszel/site/src/components/routes/home.tsx b/beszel/site/src/components/routes/home.tsx index b6a7483..7f0245f 100644 --- a/beszel/site/src/components/routes/home.tsx +++ b/beszel/site/src/components/routes/home.tsx @@ -1,18 +1,33 @@ -import { Suspense, lazy, useEffect, useState } from 'react' +import { Suspense, lazy, useEffect, useMemo, useState } from 'react' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '../ui/card' import { $alerts, $hubVersion, $systems, pb } from '@/lib/stores' import { useStore } from '@nanostores/react' import { GithubIcon } from 'lucide-react' import { Separator } from '../ui/separator' -import { updateRecordList, updateSystemList } from '@/lib/utils' +import { alertInfo, updateRecordList, updateSystemList } from '@/lib/utils' import { AlertRecord, SystemRecord } from '@/types' import { Input } from '../ui/input' +import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert' +import { Link } from '../router' const SystemsTable = lazy(() => import('../systems-table/systems-table')) export default function () { const hubVersion = useStore($hubVersion) const [filter, setFilter] = useState() + const alerts = useStore($alerts) + const systems = useStore($systems) + + const activeAlerts = useMemo(() => { + if (!systems.length) { + return [] + } + const activeAlerts = alerts.filter((alert) => alert.triggered && alert.name in alertInfo) + for (const alert of activeAlerts) { + alert.sysname = systems.find((system) => system.id === alert.system)?.name + } + return activeAlerts + }, [alerts]) useEffect(() => { document.title = 'Dashboard / Beszel' @@ -24,17 +39,57 @@ export default function () { pb.collection('systems').subscribe('*', (e) => { updateRecordList(e, $systems) }) + // todo: add toast if new triggered alert comes in pb.collection('alerts').subscribe('*', (e) => { updateRecordList(e, $alerts) }) return () => { pb.collection('systems').unsubscribe('*') - pb.collection('alerts').unsubscribe('*') + // pb.collection('alerts').unsubscribe('*') } }, []) return ( <> + {/* show active alerts */} + {activeAlerts.length > 0 && ( + + +
+ Active Alerts +
+
+ + {activeAlerts.length > 0 && ( +
+ {activeAlerts.map((alert) => { + const a = alertInfo[alert.name as keyof typeof alertInfo] + return ( + + + + {alert.sysname} {a.name} + + + Exceeds {alert.value} + {a.unit} threshold in last {alert.min} min + + + + ) + })} +
+ )} +
+
+ )}
@@ -61,6 +116,7 @@ export default function () { + {hubVersion && (
}>