fix(beszel): leverage TanStack Query for stale-while-revalidate caching (#6055)

This commit is contained in:
Thomas authored and GitHub committed 2026-06-22 17:17:09 +02:00
1 parent ea9dd716f0
commit bbc4589c9e
6 files changed
+24 -24

No files matched your search

+11 -16
View File
@@ -75,7 +75,7 @@ export const beszelSystemsRequestHandler = createCachedIntegrationRequestHandler
}); });
return enriched; return enriched;
}, },
cacheDuration: dayjs.duration(5, "seconds"), cacheDuration: dayjs.duration(1, "second"),
fallbackToStaleOnError: true, fallbackToStaleOnError: true,
retry: { attempts: 2, delayMs: 500 }, retry: { attempts: 2, delayMs: 500 },
queryKey: "beszelSystems", queryKey: "beszelSystems",
@@ -104,19 +104,19 @@ export const beszelAlertsRequestHandler = createCachedIntegrationRequestHandler<
}); });
return { alerts, history }; return { alerts, history };
}, },
cacheDuration: dayjs.duration(15, "seconds"), cacheDuration: dayjs.duration(1, "second"),
fallbackToStaleOnError: true, fallbackToStaleOnError: true,
retry: { attempts: 2, delayMs: 500 }, retry: { attempts: 2, delayMs: 500 },
queryKey: "beszelAlerts", queryKey: "beszelAlerts",
}); });
const timePeriodConfig: Record<string, { type: string; perPage: number; cacheSeconds: number }> = { const timePeriodConfig: Record<string, { type: string; perPage: number }> = {
"1m": { type: "1m", perPage: 60, cacheSeconds: 5 }, "1m": { type: "1m", perPage: 60 },
"1h": { type: "1m", perPage: 60, cacheSeconds: 60 }, "1h": { type: "1m", perPage: 60 },
"12h": { type: "10m", perPage: 72, cacheSeconds: 300 }, "12h": { type: "10m", perPage: 72 },
"24h": { type: "20m", perPage: 72, cacheSeconds: 600 }, "24h": { type: "20m", perPage: 72 },
"1w": { type: "480m", perPage: 21, cacheSeconds: 1800 }, "1w": { type: "480m", perPage: 21 },
"30d": { type: "480m", perPage: 90, cacheSeconds: 3600 }, "30d": { type: "480m", perPage: 90 },
}; };
export interface BeszelStatsData { export interface BeszelStatsData {
@@ -131,7 +131,7 @@ export const beszelStatsRequestHandler = createCachedIntegrationRequestHandler<
>({ >({
async requestAsync(integration, input) { async requestAsync(integration, input) {
const start = performance.now(); const start = performance.now();
const config = timePeriodConfig[input.timePeriod] ?? { type: "1m", perPage: 60, cacheSeconds: 60 }; const config = timePeriodConfig[input.timePeriod] ?? { type: "1m", perPage: 60 };
const instance = await createIntegrationAsync(integration); const instance = await createIntegrationAsync(integration);
const systemStats = await instance.getSystemStatsAsync(input.systemId, config.type, config.perPage); const systemStats = await instance.getSystemStatsAsync(input.systemId, config.type, config.perPage);
const containerStats = input.includeDocker const containerStats = input.includeDocker
@@ -147,13 +147,8 @@ export const beszelStatsRequestHandler = createCachedIntegrationRequestHandler<
}); });
return { systemStats, containerStats }; return { systemStats, containerStats };
}, },
cacheDuration: dayjs.duration(60, "seconds"), cacheDuration: dayjs.duration(1, "second"),
fallbackToStaleOnError: true, fallbackToStaleOnError: true,
retry: { attempts: 2, delayMs: 500 }, retry: { attempts: 2, delayMs: 500 },
queryKey: "beszelStats", queryKey: "beszelStats",
cacheDurationForInput(input) {
const config = timePeriodConfig[input.timePeriod];
if (!config) return undefined;
return dayjs.duration(config.cacheSeconds, "seconds");
},
}); });
@@ -46,8 +46,9 @@ export default function BeszelAlertsWidget({
error: alertsError, error: alertsError,
isPending, isPending,
} = clientApi.widget.beszel.getAlerts.useQuery(alertsInput, { } = clientApi.widget.beszel.getAlerts.useQuery(alertsInput, {
staleTime: 15_000, staleTime: 10_000,
refetchInterval: 15_000, gcTime: 48 * 60 * 60 * 1000,
refetchInterval: 10_000,
retry: false, retry: false,
}); });
const utils = clientApi.useUtils(); const utils = clientApi.useUtils();
@@ -371,7 +371,7 @@ export default function BeszelSystemGridWidget({
isPending, isPending,
} = clientApi.widget.beszel.getSystems.useQuery( } = clientApi.widget.beszel.getSystems.useQuery(
{ integrationIds }, { integrationIds },
{ staleTime: 5_000, refetchInterval: 5_000, retry: false }, { staleTime: 10_000, gcTime: 48 * 60 * 60 * 1000, refetchInterval: 10_000, retry: false },
); );
useBeszelSystemsSubscription(integrationIds, !isEditMode); useBeszelSystemsSubscription(integrationIds, !isEditMode);
@@ -67,7 +67,10 @@ export default function BeszelSystemStatsWidget({
data: systemsResult = [], data: systemsResult = [],
isPending: systemsPending, isPending: systemsPending,
error: systemsError, error: systemsError,
} = clientApi.widget.beszel.getSystems.useQuery({ integrationIds }, { staleTime: 30 * 1000, retry: false }); } = clientApi.widget.beszel.getSystems.useQuery(
{ integrationIds },
{ staleTime: 10_000, gcTime: 48 * 60 * 60 * 1000, retry: false },
);
const systems = useMemo( const systems = useMemo(
() => systemsResult.flatMap((r) => r.systems.map((s) => ({ value: s.id, label: s.name }))), () => systemsResult.flatMap((r) => r.systems.map((s) => ({ value: s.id, label: s.name }))),
@@ -100,8 +103,9 @@ export default function BeszelSystemStatsWidget({
includeDocker: showDocker, includeDocker: showDocker,
}, },
{ {
staleTime: 30 * 1000, staleTime: 10_000,
refetchInterval: isLive ? false : 5_000, gcTime: 48 * 60 * 60 * 1000,
refetchInterval: isLive ? false : 10_000,
enabled: !isLive && systemReady && selectedSystem !== "", enabled: !isLive && systemReady && selectedSystem !== "",
retry: false, retry: false,
}, },
@@ -76,7 +76,7 @@ export default function BeszelSystemTableWidget({
isPending, isPending,
} = clientApi.widget.beszel.getSystems.useQuery( } = clientApi.widget.beszel.getSystems.useQuery(
{ integrationIds }, { integrationIds },
{ staleTime: 5_000, refetchInterval: 5_000, retry: false }, { staleTime: 10_000, gcTime: 48 * 60 * 60 * 1000, refetchInterval: 10_000, retry: false },
); );
const size = getSizeConfig(width); const size = getSizeConfig(width);
@@ -43,7 +43,7 @@ export const BeszelSystemStatsModal = createModal<BeszelSystemStatsModalProps>((
timePeriod, timePeriod,
includeDocker: false, includeDocker: false,
}, },
{ staleTime: 30_000 }, { staleTime: 10_000, gcTime: 48 * 60 * 60 * 1000, refetchInterval: 10_000 },
); );
const mappers = useMemo( const mappers = useMemo(