feat(beszel): wire live SSE streaming for 1-minute time period
Add "Live" option to time period selector that uses the existing subscribeSystemStats tRPC subscription (PocketBase SSE) instead of REST polling. A useLiveStats hook accumulates incoming data points in a 60-entry rolling buffer. Charts use HH:mm:ss timestamps in live mode.
This commit is contained in:
3 files changed
+62
-13
No files matched your search
@@ -0,0 +1,42 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useRef, useState } from "react";
|
||||
|
||||
import { clientApi } from "@homarr/api/client";
|
||||
import type { BeszelContainerStatsRecord, BeszelSystemStatsRecord } from "@homarr/integrations/types";
|
||||
|
||||
const MAX_BUFFER = 60;
|
||||
|
||||
interface LiveStatsData {
|
||||
systemStats: BeszelSystemStatsRecord[];
|
||||
containerStats: BeszelContainerStatsRecord[];
|
||||
}
|
||||
|
||||
export const useLiveStats = (
|
||||
integrationIds: string[],
|
||||
systemId: string,
|
||||
includeDocker: boolean,
|
||||
enabled: boolean,
|
||||
) => {
|
||||
const bufferRef = useRef<LiveStatsData>({ systemStats: [], containerStats: [] });
|
||||
const [data, setData] = useState<LiveStatsData | null>(null);
|
||||
|
||||
const onData = useCallback(
|
||||
(incoming: { stats: BeszelSystemStatsRecord; containerStats: BeszelContainerStatsRecord | null }) => {
|
||||
const buf = bufferRef.current;
|
||||
buf.systemStats = [...buf.systemStats, incoming.stats].slice(-MAX_BUFFER);
|
||||
if (incoming.containerStats) {
|
||||
buf.containerStats = [...buf.containerStats, incoming.containerStats].slice(-MAX_BUFFER);
|
||||
}
|
||||
setData({ ...buf });
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
clientApi.widget.beszel.subscribeSystemStats.useSubscription(
|
||||
{ integrationIds, systemId, includeDocker },
|
||||
{ enabled: enabled && systemId !== "", onData },
|
||||
);
|
||||
|
||||
return data;
|
||||
};
|
||||
Reference in new issue
Block a user