style: auto-fix lint and format
This commit is contained in:
1 parent
e22eee271c
commit
680cbc69d8
5 files changed
+46
-90
No files matched your search
@@ -10,7 +10,8 @@ export const beszelSystemStatsWidget: WidgetDefinition = {
|
|||||||
items: [
|
items: [
|
||||||
{
|
{
|
||||||
name: "Time period",
|
name: "Time period",
|
||||||
description: "The time range for chart data. '1 Minute' enables live streaming via PocketBase SSE (requires Beszel agent >= 0.13.0)",
|
description:
|
||||||
|
"The time range for chart data. '1 Minute' enables live streaming via PocketBase SSE (requires Beszel agent >= 0.13.0)",
|
||||||
values: {
|
values: {
|
||||||
type: "select",
|
type: "select",
|
||||||
options: ["1 Minute", "1 Hour", "12 Hours", "24 Hours", "1 Week", "30 Days"],
|
options: ["1 Minute", "1 Hour", "12 Hours", "24 Hours", "1 Week", "30 Days"],
|
||||||
|
|||||||
@@ -8,10 +8,7 @@ import {
|
|||||||
beszelStatsRequestHandler,
|
beszelStatsRequestHandler,
|
||||||
beszelSystemsRequestHandler,
|
beszelSystemsRequestHandler,
|
||||||
} from "@homarr/request-handler/beszel";
|
} from "@homarr/request-handler/beszel";
|
||||||
import type {
|
import type { BeszelAlertsData, BeszelSystemRow } from "@homarr/request-handler/beszel";
|
||||||
BeszelAlertsData,
|
|
||||||
BeszelSystemRow,
|
|
||||||
} from "@homarr/request-handler/beszel";
|
|
||||||
|
|
||||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||||
@@ -22,14 +19,10 @@ export const beszelRouter = createTRPCRouter({
|
|||||||
.query(async ({ ctx }) => {
|
.query(async ({ ctx }) => {
|
||||||
const results = await Promise.all(
|
const results = await Promise.all(
|
||||||
ctx.integrations.map(async (integration) => {
|
ctx.integrations.map(async (integration) => {
|
||||||
const innerHandler = beszelSystemsRequestHandler.handler(
|
const innerHandler = beszelSystemsRequestHandler.handler(integration, {});
|
||||||
integration,
|
const { data, timestamp } = await innerHandler.getCachedOrUpdatedDataAsync({
|
||||||
{},
|
forceUpdate: false,
|
||||||
);
|
});
|
||||||
const { data, timestamp } =
|
|
||||||
await innerHandler.getCachedOrUpdatedDataAsync({
|
|
||||||
forceUpdate: false,
|
|
||||||
});
|
|
||||||
return {
|
return {
|
||||||
integrationId: integration.id,
|
integrationId: integration.id,
|
||||||
integrationName: integration.name,
|
integrationName: integration.name,
|
||||||
@@ -51,10 +44,7 @@ export const beszelRouter = createTRPCRouter({
|
|||||||
timestamp: Date;
|
timestamp: Date;
|
||||||
}>((emit) => {
|
}>((emit) => {
|
||||||
const unsubscribes = ctx.integrations.map((integration) => {
|
const unsubscribes = ctx.integrations.map((integration) => {
|
||||||
const innerHandler = beszelSystemsRequestHandler.handler(
|
const innerHandler = beszelSystemsRequestHandler.handler(integration, {});
|
||||||
integration,
|
|
||||||
{},
|
|
||||||
);
|
|
||||||
return innerHandler.subscribe((systems) => {
|
return innerHandler.subscribe((systems) => {
|
||||||
emit.next({
|
emit.next({
|
||||||
integrationId: integration.id,
|
integrationId: integration.id,
|
||||||
@@ -80,17 +70,11 @@ export const beszelRouter = createTRPCRouter({
|
|||||||
.query(async ({ ctx, input }) => {
|
.query(async ({ ctx, input }) => {
|
||||||
const results = await Promise.all(
|
const results = await Promise.all(
|
||||||
ctx.integrations.map(async (integration) => {
|
ctx.integrations.map(async (integration) => {
|
||||||
const alertsHandler = beszelAlertsRequestHandler.handler(
|
const alertsHandler = beszelAlertsRequestHandler.handler(integration, {
|
||||||
integration,
|
includeHistory: input.includeHistory,
|
||||||
{
|
maxHistoryItems: input.maxHistoryItems,
|
||||||
includeHistory: input.includeHistory,
|
});
|
||||||
maxHistoryItems: input.maxHistoryItems,
|
const systemsHandler = beszelSystemsRequestHandler.handler(integration, {});
|
||||||
},
|
|
||||||
);
|
|
||||||
const systemsHandler = beszelSystemsRequestHandler.handler(
|
|
||||||
integration,
|
|
||||||
{},
|
|
||||||
);
|
|
||||||
const [alertsResult, systemsResult] = await Promise.all([
|
const [alertsResult, systemsResult] = await Promise.all([
|
||||||
alertsHandler.getCachedOrUpdatedDataAsync({ forceUpdate: false }),
|
alertsHandler.getCachedOrUpdatedDataAsync({ forceUpdate: false }),
|
||||||
systemsHandler.getCachedOrUpdatedDataAsync({ forceUpdate: false }),
|
systemsHandler.getCachedOrUpdatedDataAsync({ forceUpdate: false }),
|
||||||
@@ -161,8 +145,7 @@ export const beszelRouter = createTRPCRouter({
|
|||||||
timePeriod: input.timePeriod,
|
timePeriod: input.timePeriod,
|
||||||
includeDocker: input.includeDocker,
|
includeDocker: input.includeDocker,
|
||||||
});
|
});
|
||||||
const { data, timestamp } =
|
const { data, timestamp } = await innerHandler.getCachedOrUpdatedDataAsync({ forceUpdate: false });
|
||||||
await innerHandler.getCachedOrUpdatedDataAsync({ forceUpdate: false });
|
|
||||||
return { integrationId: integration.id, ...data, updatedAt: timestamp };
|
return { integrationId: integration.id, ...data, updatedAt: timestamp };
|
||||||
}),
|
}),
|
||||||
|
|
||||||
@@ -187,11 +170,7 @@ export const beszelRouter = createTRPCRouter({
|
|||||||
void (async () => {
|
void (async () => {
|
||||||
try {
|
try {
|
||||||
const instance = await createIntegrationAsync(integration);
|
const instance = await createIntegrationAsync(integration);
|
||||||
await instance.subscribeRealtimeMetrics(
|
await instance.subscribeRealtimeMetrics(input.systemId, (data) => emit.next(data), abortController.signal);
|
||||||
input.systemId,
|
|
||||||
(data) => emit.next(data),
|
|
||||||
abortController.signal,
|
|
||||||
);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (!abortController.signal.aborted) {
|
if (!abortController.signal.aborted) {
|
||||||
emit.error(error instanceof Error ? error : new Error(String(error)));
|
emit.error(error instanceof Error ? error : new Error(String(error)));
|
||||||
|
|||||||
@@ -298,7 +298,9 @@ export class BeszelIntegration extends Integration {
|
|||||||
updated: new Date().toISOString(),
|
updated: new Date().toISOString(),
|
||||||
};
|
};
|
||||||
const containerStats: BeszelContainerStatsRecord | null =
|
const containerStats: BeszelContainerStatsRecord | null =
|
||||||
parsed.container && Array.isArray(parsed.container) && (parsed.container as BeszelContainerStats[]).length > 0
|
parsed.container &&
|
||||||
|
Array.isArray(parsed.container) &&
|
||||||
|
(parsed.container as BeszelContainerStats[]).length > 0
|
||||||
? {
|
? {
|
||||||
id: "",
|
id: "",
|
||||||
system: systemId,
|
system: systemId,
|
||||||
|
|||||||
@@ -24,10 +24,7 @@ export type { BeszelSystemRow } from "@homarr/integrations/types";
|
|||||||
* bb=bandwidth(bytes/s), b=bandwidth(Mbps legacy), dt=disk temp, bat=battery,
|
* bb=bandwidth(bytes/s), b=bandwidth(Mbps legacy), dt=disk temp, bat=battery,
|
||||||
* sv=[running,total] services, h=hostname, m=CPU model, c=cores, ct=threads
|
* sv=[running,total] services, h=hostname, m=CPU model, c=cores, ct=threads
|
||||||
*/
|
*/
|
||||||
function mapToSystemRow(
|
function mapToSystemRow(system: BeszelSystem, details: BeszelSystemDetails | null): BeszelSystemRow {
|
||||||
system: BeszelSystem,
|
|
||||||
details: BeszelSystemDetails | null,
|
|
||||||
): BeszelSystemRow {
|
|
||||||
const info = system.info;
|
const info = system.info;
|
||||||
return {
|
return {
|
||||||
id: system.id,
|
id: system.id,
|
||||||
@@ -53,28 +50,25 @@ function mapToSystemRow(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export const beszelSystemsRequestHandler =
|
export const beszelSystemsRequestHandler = createCachedIntegrationRequestHandler<
|
||||||
createCachedIntegrationRequestHandler<
|
BeszelSystemRow[],
|
||||||
BeszelSystemRow[],
|
"beszel" | "mock",
|
||||||
"beszel" | "mock",
|
Record<string, never>
|
||||||
Record<string, never>
|
>({
|
||||||
>({
|
async requestAsync(integration) {
|
||||||
async requestAsync(integration) {
|
const instance = await createIntegrationAsync(integration);
|
||||||
const instance = await createIntegrationAsync(integration);
|
const systems = await instance.getSystemsAsync();
|
||||||
const systems = await instance.getSystemsAsync();
|
const enriched = await Promise.all(
|
||||||
const enriched = await Promise.all(
|
systems.map(async (system) => {
|
||||||
systems.map(async (system) => {
|
const details = await instance.getSystemDetailsAsync(system.id).catch(() => null);
|
||||||
const details = await instance
|
return mapToSystemRow(system, details);
|
||||||
.getSystemDetailsAsync(system.id)
|
}),
|
||||||
.catch(() => null);
|
);
|
||||||
return mapToSystemRow(system, details);
|
return enriched;
|
||||||
}),
|
},
|
||||||
);
|
cacheDuration: dayjs.duration(5, "seconds"),
|
||||||
return enriched;
|
queryKey: "beszelSystems",
|
||||||
},
|
});
|
||||||
cacheDuration: dayjs.duration(5, "seconds"),
|
|
||||||
queryKey: "beszelSystems",
|
|
||||||
});
|
|
||||||
|
|
||||||
export interface BeszelAlertsData {
|
export interface BeszelAlertsData {
|
||||||
alerts: BeszelAlert[];
|
alerts: BeszelAlert[];
|
||||||
@@ -89,9 +83,7 @@ export const beszelAlertsRequestHandler = createCachedIntegrationRequestHandler<
|
|||||||
async requestAsync(integration, input) {
|
async requestAsync(integration, input) {
|
||||||
const instance = await createIntegrationAsync(integration);
|
const instance = await createIntegrationAsync(integration);
|
||||||
const alerts = await instance.getAlertsAsync();
|
const alerts = await instance.getAlertsAsync();
|
||||||
const history = input.includeHistory
|
const history = input.includeHistory ? await instance.getAlertHistoryAsync(undefined, input.maxHistoryItems) : [];
|
||||||
? await instance.getAlertHistoryAsync(undefined, input.maxHistoryItems)
|
|
||||||
: [];
|
|
||||||
return { alerts, history };
|
return { alerts, history };
|
||||||
},
|
},
|
||||||
cacheDuration: dayjs.duration(15, "seconds"),
|
cacheDuration: dayjs.duration(15, "seconds"),
|
||||||
@@ -120,15 +112,9 @@ export const beszelStatsRequestHandler = createCachedIntegrationRequestHandler<
|
|||||||
async requestAsync(integration, input) {
|
async requestAsync(integration, input) {
|
||||||
const config = timePeriodConfig[input.timePeriod] ?? { type: "1m", perPage: 60, cacheSeconds: 60 };
|
const config = timePeriodConfig[input.timePeriod] ?? { type: "1m", perPage: 60, cacheSeconds: 60 };
|
||||||
const instance = await createIntegrationAsync(integration);
|
const instance = await createIntegrationAsync(integration);
|
||||||
const systemStats = await instance.getSystemStatsAsync(
|
const systemStats = await instance.getSystemStatsAsync(input.systemId, config.type, config.perPage);
|
||||||
input.systemId,
|
|
||||||
config.type,
|
|
||||||
config.perPage,
|
|
||||||
);
|
|
||||||
const containerStats = input.includeDocker
|
const containerStats = input.includeDocker
|
||||||
? await instance
|
? await instance.getContainerStatsAsync(input.systemId, config.type, config.perPage).catch(() => [])
|
||||||
.getContainerStatsAsync(input.systemId, config.type, config.perPage)
|
|
||||||
.catch(() => [])
|
|
||||||
: [];
|
: [];
|
||||||
return { systemStats, containerStats };
|
return { systemStats, containerStats };
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -6,14 +6,10 @@ import type { AreaChartProps } from "@mantine/charts";
|
|||||||
import { AreaChart } from "@mantine/charts";
|
import { AreaChart } from "@mantine/charts";
|
||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
|
|
||||||
import type {
|
import type { BeszelContainerStatsRecord, BeszelSystemStatsRecord } from "@homarr/integrations/types";
|
||||||
BeszelContainerStatsRecord,
|
|
||||||
BeszelSystemStatsRecord,
|
|
||||||
} from "@homarr/integrations/types";
|
|
||||||
|
|
||||||
const formatTime = (timestamp: string) => dayjs(timestamp).format("HH:mm");
|
const formatTime = (timestamp: string) => dayjs(timestamp).format("HH:mm");
|
||||||
const formatTimeLive = (timestamp: string) =>
|
const formatTimeLive = (timestamp: string) => dayjs(timestamp).format("HH:mm:ss");
|
||||||
dayjs(timestamp).format("HH:mm:ss");
|
|
||||||
|
|
||||||
const yAxisBase = { tickMargin: 0, tick: { fontSize: 10 } } as const;
|
const yAxisBase = { tickMargin: 0, tick: { fontSize: 10 } } as const;
|
||||||
|
|
||||||
@@ -39,10 +35,7 @@ export const ChartPanel = ({ title, subtitle, children }: ChartPanelProps) => (
|
|||||||
</Stack>
|
</Stack>
|
||||||
);
|
);
|
||||||
|
|
||||||
type BeszelAreaChartProps = Omit<
|
type BeszelAreaChartProps = Omit<AreaChartProps, "dataKey" | "curveType" | "withDots" | "withXAxis" | "withYAxis"> & {
|
||||||
AreaChartProps,
|
|
||||||
"dataKey" | "curveType" | "withDots" | "withXAxis" | "withYAxis"
|
|
||||||
> & {
|
|
||||||
yAxisFormatter: (value: number) => string;
|
yAxisFormatter: (value: number) => string;
|
||||||
yAxisDomain?: [number, string];
|
yAxisDomain?: [number, string];
|
||||||
};
|
};
|
||||||
@@ -96,10 +89,7 @@ export const useSystemChartData = (
|
|||||||
}));
|
}));
|
||||||
}, [systemStats, mapFn, live]);
|
}, [systemStats, mapFn, live]);
|
||||||
|
|
||||||
export const useContainerNames = (
|
export const useContainerNames = (containerStats: BeszelContainerStatsRecord[] | undefined, max = 15) =>
|
||||||
containerStats: BeszelContainerStatsRecord[] | undefined,
|
|
||||||
max = 15,
|
|
||||||
) =>
|
|
||||||
useMemo(() => {
|
useMemo(() => {
|
||||||
if (!containerStats?.length) return [];
|
if (!containerStats?.length) return [];
|
||||||
const names = new Set<string>();
|
const names = new Set<string>();
|
||||||
@@ -111,9 +101,7 @@ export const useContainerNames = (
|
|||||||
return [...names].slice(0, max);
|
return [...names].slice(0, max);
|
||||||
}, [containerStats, max]);
|
}, [containerStats, max]);
|
||||||
|
|
||||||
type ContainerExtractor = (
|
type ContainerExtractor = (container: BeszelContainerStatsRecord["stats"][number] | undefined) => number;
|
||||||
container: BeszelContainerStatsRecord["stats"][number] | undefined,
|
|
||||||
) => number;
|
|
||||||
|
|
||||||
// c = CPU (%), m = memory (MB), b = bandwidth [sent,recv] (bytes/s), ns/nr = legacy net (bytes/s)
|
// c = CPU (%), m = memory (MB), b = bandwidth [sent,recv] (bytes/s), ns/nr = legacy net (bytes/s)
|
||||||
const MB = 1024 * 1024;
|
const MB = 1024 * 1024;
|
||||||
|
|||||||
Reference in new issue
Block a user