fix: refine widget modernization

This commit is contained in:
Thomas Camlong authored and ajnart committed 2026-08-13 19:54:29 +02:00
1 parent 6f2cd7fc2b
commit 777bbd89a1
44 files changed
+259 -320

No files matched your search

@@ -40,34 +40,18 @@ export const toChartItem = (healthInfo: {
export const appendBoundedHistory = (history: ChartItem[], item: ChartItem, limit: number): ChartItem[] =>
[...history, item].slice(-limit);
export const boundHistoryByIntegration = (
historyByIntegration: Record<string, ChartItem[]>,
limit: number,
): Record<string, ChartItem[]> =>
Object.fromEntries(
Object.entries(historyByIntegration).map(([integrationId, history]) => [integrationId, history.slice(-limit)]),
);
export default function SystemResources({
integrationIds,
options,
width,
displayMode,
widgetStateRef,
}: WidgetComponentProps<"systemResources">) {
const { data = [], dataUpdatedAt } = clientApi.widget.healthMonitoring.getSystemHealthStatus.useQuery({
integrationIds,
});
const isAdvanced = displayMode === "advanced";
const integrationKey = useMemo(() => integrationIds.join("\u0000"), [integrationIds]);
const persistedState = widgetStateRef?.current?.systemResources as
| { integrationKey?: string; historyByIntegration?: Record<string, ChartItem[]> }
| undefined;
const [historyByIntegration, setHistoryByIntegration] = useState<Record<string, ChartItem[]>>(() =>
persistedState?.integrationKey === integrationKey && persistedState.historyByIntegration
? boundHistoryByIntegration(persistedState.historyByIntegration, ADVANCED_HISTORY_SIZE)
: {},
);
const [historyByIntegration, setHistoryByIntegration] = useState<Record<string, ChartItem[]>>({});
const previousIntegrationKey = useRef(integrationKey);
useEffect(() => {
@@ -94,17 +78,6 @@ export default function SystemResources({
);
}, [dataUpdatedAt, data]);
useEffect(() => {
if (!widgetStateRef) return;
widgetStateRef.current = {
...widgetStateRef.current,
systemResources: {
integrationKey,
historyByIntegration: boundHistoryByIntegration(historyByIntegration, ADVANCED_HISTORY_SIZE),
},
};
}, [historyByIntegration, integrationKey, widgetStateRef]);
const panelColumns = isAdvanced && width >= 960 && data.length > 1 ? 2 : 1;
const panelWidth = width / panelColumns;
const renderCharts = (entry: (typeof data)[number], availableWidth: number) => {
@@ -1,6 +1,6 @@
import { describe, expect, test } from "vitest";
import { appendBoundedHistory, boundHistoryByIntegration, toChartItem } from "./component";
import { appendBoundedHistory, toChartItem } from "./component";
describe("system resource history", () => {
test("averages all GPU values", () => {
@@ -18,12 +18,4 @@ describe("system resource history", () => {
const item = toChartItem({ cpuUtilization: 1, memUsedInBytes: 2, gpu: [], network: null });
expect(appendBoundedHistory([item, item], item, 2)).toHaveLength(2);
});
test("bounds restored history for every integration", () => {
const item = toChartItem({ cpuUtilization: 1, memUsedInBytes: 2, gpu: [], network: null });
expect(boundHistoryByIntegration({ first: [item, item, item], second: [item, item] }, 2)).toEqual({
first: [item, item],
second: [item, item],
});
});
});