feat(integrations): add beszel system usage integration
This commit is contained in:
28 files changed
+955
-2
No files matched your search
@@ -44,6 +44,7 @@ import * as speedtestTracker from "./speedtest-tracker";
|
||||
import * as stockPrice from "./stocks";
|
||||
import * as systemDisks from "./system-disks";
|
||||
import * as systemResources from "./system-resources";
|
||||
import * as systemUsage from "./system-usage";
|
||||
import * as timetable from "./timetable";
|
||||
import * as tracearr from "./tracearr";
|
||||
import * as umami from "./umami";
|
||||
@@ -86,6 +87,7 @@ export const widgetImports = {
|
||||
notifications,
|
||||
mediaReleases,
|
||||
systemResources,
|
||||
systemUsage,
|
||||
coolify,
|
||||
systemDisks,
|
||||
timetable,
|
||||
|
||||
@@ -0,0 +1,228 @@
|
||||
"use client";
|
||||
|
||||
import type { MantineColor } from "@mantine/core";
|
||||
import { Divider, Group, Stack, Text } from "@mantine/core";
|
||||
import {
|
||||
IconCpu,
|
||||
IconHourglass,
|
||||
IconNetwork,
|
||||
IconSquareChevronRight,
|
||||
IconThermometer,
|
||||
IconWifi,
|
||||
} from "@tabler/icons-react";
|
||||
|
||||
import { clientApi } from "@homarr/api/client";
|
||||
import { humanFileSize, objectEntries } from "@homarr/common";
|
||||
import type { System, SystemLoadStatus, SystemStatus } from "@homarr/integrations/types";
|
||||
import { useI18n } from "@homarr/translation/client";
|
||||
import type { TablerIcon } from "@homarr/ui";
|
||||
import { IconGpu, IconHardDrive, IconMemoryStick, IconWebsocket } from "@homarr/ui/icons";
|
||||
|
||||
import type { WidgetComponentProps } from "../definition";
|
||||
import { NoIntegrationSelectedError } from "../errors";
|
||||
import { NoSystemSelectedError } from "./errors/no-system-selected-error";
|
||||
import { Dot, Item, ProgressValue } from "./item";
|
||||
|
||||
export default function SystemUsage({ integrationIds, options, isEditMode }: WidgetComponentProps<"systemUsage">) {
|
||||
if (isEditMode) {
|
||||
return <SystemUsagePreview visibleItems={options.visibleItems} />;
|
||||
}
|
||||
|
||||
const integrationId = integrationIds.at(0);
|
||||
if (!integrationId) {
|
||||
throw new NoIntegrationSelectedError();
|
||||
}
|
||||
|
||||
if (!options.systemId) {
|
||||
throw new NoSystemSelectedError();
|
||||
}
|
||||
|
||||
return (
|
||||
<SystemUsageContent
|
||||
systemId={options.systemId.value}
|
||||
integrationId={integrationId}
|
||||
visibleItems={options.visibleItems}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
interface SystemUsagePreviewProps {
|
||||
visibleItems: (keyof typeof items)[];
|
||||
}
|
||||
|
||||
const progressItems = new Set<keyof typeof items>(["cpu", "memory", "disk", "gpu"]);
|
||||
|
||||
const SystemUsagePreview = ({ visibleItems }: SystemUsagePreviewProps) => {
|
||||
const t = useI18n();
|
||||
|
||||
return (
|
||||
<Stack gap="sm" p="sm">
|
||||
<Group gap="sm" align="center">
|
||||
<Dot color="green" />
|
||||
<Text fw="bold" size="sm">
|
||||
System Name
|
||||
</Text>
|
||||
</Group>
|
||||
<Divider />
|
||||
|
||||
{objectEntries(items).map(([key, item]) =>
|
||||
("hidden" in item && item.hidden({ usage: { gpuPercentage: null, temperature: null } } as System)) ||
|
||||
!visibleItems.includes(key) ? null : (
|
||||
<Item key={key} icon={item.icon} label={t(`widget.systemUsage.item.${key}.label`)}>
|
||||
{progressItems.has(key) ? <ProgressValue value={50} /> : <Text size="xs">—</Text>}
|
||||
</Item>
|
||||
),
|
||||
)}
|
||||
</Stack>
|
||||
);
|
||||
};
|
||||
|
||||
interface SystemUsageContentProps {
|
||||
systemId: string;
|
||||
integrationId: string;
|
||||
visibleItems: (keyof typeof items)[];
|
||||
}
|
||||
|
||||
const SystemUsageContent = ({ systemId, integrationId, visibleItems }: SystemUsageContentProps) => {
|
||||
const [systemDetails] = clientApi.widget.systemUsage.get.useSuspenseQuery(
|
||||
{ systemId, integrationId },
|
||||
{
|
||||
refetchOnMount: false,
|
||||
refetchOnWindowFocus: false,
|
||||
refetchOnReconnect: false,
|
||||
},
|
||||
);
|
||||
const utils = clientApi.useUtils();
|
||||
clientApi.widget.systemUsage.subscribe.useSubscription(
|
||||
{ systemId, integrationId },
|
||||
{
|
||||
onData(data) {
|
||||
utils.widget.systemUsage.get.setData({ systemId, integrationId }, data.system);
|
||||
},
|
||||
},
|
||||
);
|
||||
const t = useI18n();
|
||||
|
||||
return (
|
||||
<Stack gap="sm" p="sm">
|
||||
<Group gap="sm" align="center">
|
||||
<Dot color={systemStatusColors[systemDetails.status]} />
|
||||
<Text fw="bold" size="sm">
|
||||
{systemDetails.name}
|
||||
</Text>
|
||||
</Group>
|
||||
<Divider />
|
||||
|
||||
{objectEntries(items).map(([key, item]) =>
|
||||
("hidden" in item && item.hidden(systemDetails)) || !visibleItems.includes(key) ? null : (
|
||||
<Item key={key} icon={item.icon} label={t(`widget.systemUsage.item.${key}.label`)}>
|
||||
<item.component system={systemDetails} />
|
||||
</Item>
|
||||
),
|
||||
)}
|
||||
</Stack>
|
||||
);
|
||||
};
|
||||
|
||||
const systemStatusColors = {
|
||||
up: "green",
|
||||
down: "red",
|
||||
paused: "gray",
|
||||
pending: "yellow",
|
||||
} satisfies Record<SystemStatus, MantineColor>;
|
||||
|
||||
const loadStatusColors = {
|
||||
good: "green",
|
||||
warning: "yellow",
|
||||
critical: "red",
|
||||
unknown: "gray",
|
||||
} satisfies Record<SystemLoadStatus, MantineColor>;
|
||||
|
||||
const items = {
|
||||
cpu: {
|
||||
icon: IconCpu,
|
||||
component({ system }) {
|
||||
return <ProgressValue value={system.usage.cpuPercentage} />;
|
||||
},
|
||||
},
|
||||
memory: {
|
||||
icon: IconMemoryStick,
|
||||
component({ system }) {
|
||||
return <ProgressValue value={system.usage.memoryPercentage} />;
|
||||
},
|
||||
},
|
||||
disk: {
|
||||
icon: IconHardDrive,
|
||||
component({ system }) {
|
||||
return <ProgressValue value={system.usage.diskPercentage} />;
|
||||
},
|
||||
},
|
||||
gpu: {
|
||||
icon: IconGpu,
|
||||
component({ system }) {
|
||||
return <ProgressValue value={system.usage.gpuPercentage ?? 0} />;
|
||||
},
|
||||
hidden(system) {
|
||||
return system.usage.gpuPercentage === null;
|
||||
},
|
||||
},
|
||||
load: {
|
||||
icon: IconHourglass,
|
||||
component({ system }) {
|
||||
return (
|
||||
<Group gap="xs" align="center" wrap="nowrap">
|
||||
<Dot color={loadStatusColors[system.usage.load.status]} />
|
||||
<Text size="xs" style={{ whiteSpace: "nowrap" }}>
|
||||
{system.usage.load.averages.one}
|
||||
</Text>
|
||||
<Text size="xs" style={{ whiteSpace: "nowrap" }}>
|
||||
{system.usage.load.averages.five}
|
||||
</Text>
|
||||
<Text size="xs" style={{ whiteSpace: "nowrap" }}>
|
||||
{system.usage.load.averages.fifteen}
|
||||
</Text>
|
||||
</Group>
|
||||
);
|
||||
},
|
||||
},
|
||||
network: {
|
||||
icon: IconNetwork,
|
||||
component({ system }) {
|
||||
return (
|
||||
<Text size="xs" style={{ whiteSpace: "nowrap" }}>
|
||||
{humanFileSize(system.usage.networkBytes, "/s")}
|
||||
</Text>
|
||||
);
|
||||
},
|
||||
},
|
||||
temperature: {
|
||||
icon: IconThermometer,
|
||||
component({ system }) {
|
||||
return (
|
||||
<Text size="xs" style={{ whiteSpace: "nowrap" }}>
|
||||
{system.usage.temperature ?? "N/A"} °C
|
||||
</Text>
|
||||
);
|
||||
},
|
||||
hidden(system) {
|
||||
return system.usage.temperature === null;
|
||||
},
|
||||
},
|
||||
agent: {
|
||||
icon: IconWifi,
|
||||
component({ system }) {
|
||||
return (
|
||||
<Group gap="xs" align="center" wrap="nowrap">
|
||||
{system.agent.connectionType === "ssh" ? <IconSquareChevronRight size={14} stroke={1.5} /> : null}
|
||||
{system.agent.connectionType === "webSocket" ? <IconWebsocket size={14} stroke={1.5} /> : null}
|
||||
<Text size="xs" style={{ whiteSpace: "nowrap" }}>
|
||||
{system.agent.version}
|
||||
</Text>
|
||||
</Group>
|
||||
);
|
||||
},
|
||||
},
|
||||
} satisfies Record<
|
||||
string,
|
||||
{ icon: TablerIcon; component: (props: { system: System }) => React.ReactNode; hidden?: (system: System) => boolean }
|
||||
>;
|
||||
@@ -0,0 +1,19 @@
|
||||
import { IconServerOff } from "@tabler/icons-react";
|
||||
|
||||
import type { TranslationFunction } from "@homarr/translation";
|
||||
|
||||
import { ErrorBoundaryError } from "../../errors";
|
||||
|
||||
export class NoSystemSelectedError extends ErrorBoundaryError {
|
||||
constructor() {
|
||||
super("No system selected");
|
||||
}
|
||||
|
||||
public getErrorBoundaryData() {
|
||||
return {
|
||||
icon: IconServerOff,
|
||||
message: (t: TranslationFunction) => t("widget.systemUsage.error.noSystem"),
|
||||
showLogsLink: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { IconServer } from "@tabler/icons-react";
|
||||
|
||||
import { clientApi } from "@homarr/api/client";
|
||||
|
||||
import { getIntegrationKindsByCategory } from "@homarr/definitions";
|
||||
import { createWidgetDefinition } from "../definition";
|
||||
import { optionsBuilder } from "../options";
|
||||
|
||||
export const { definition, componentLoader } = createWidgetDefinition("systemUsage", {
|
||||
icon: IconServer,
|
||||
supportedIntegrations: getIntegrationKindsByCategory("systemUsage"),
|
||||
integrationsRequired: false,
|
||||
createOptions() {
|
||||
return optionsBuilder.from((factory) => ({
|
||||
systemId: factory.dynamicSelect({
|
||||
useOptions(_query, integrationIds) {
|
||||
const { data: systems, isPending } = clientApi.widget.systemUsage.listSystems.useQuery(
|
||||
{ integrationId: integrationIds[0] ?? "" },
|
||||
{
|
||||
enabled: integrationIds.length > 0,
|
||||
},
|
||||
);
|
||||
|
||||
return {
|
||||
isPending,
|
||||
options:
|
||||
systems?.map((system) => ({
|
||||
label: system.name,
|
||||
value: system.id,
|
||||
})) ?? [],
|
||||
};
|
||||
},
|
||||
}),
|
||||
visibleItems: factory.multiSelect({
|
||||
options: (["cpu", "memory", "disk", "gpu", "load", "network", "temperature", "agent"] as const).map((key) => ({
|
||||
value: key,
|
||||
label: (t) => t(`widget.systemUsage.item.${key}.label`),
|
||||
})),
|
||||
defaultValue: ["cpu", "memory", "disk", "gpu", "load", "network", "temperature", "agent"],
|
||||
}),
|
||||
}));
|
||||
},
|
||||
}).withDynamicImport(() => import("./component"));
|
||||
@@ -0,0 +1,46 @@
|
||||
import type { MantineColor } from "@mantine/core";
|
||||
import { Box, Flex, Group, Progress, Text } from "@mantine/core";
|
||||
|
||||
import type { TablerIcon } from "@homarr/ui";
|
||||
|
||||
export const ProgressValue = ({ value }: { value: number }) => (
|
||||
<Group gap="xs" align="center" w="100%" wrap="nowrap">
|
||||
<Text size="xs" style={{ whiteSpace: "nowrap" }}>
|
||||
{value}%
|
||||
</Text>
|
||||
<Progress value={value} color={progressColor(value)} w="100%" />
|
||||
</Group>
|
||||
);
|
||||
|
||||
const progressColor = (value: number): MantineColor => {
|
||||
if (value < 50) return "green";
|
||||
if (value < 75) return "yellow";
|
||||
return "red";
|
||||
};
|
||||
|
||||
interface DotProps {
|
||||
color: MantineColor;
|
||||
}
|
||||
|
||||
export const Dot = ({ color }: DotProps) => <Box style={{ borderRadius: "100%" }} bg={color} h={8} w={8}></Box>;
|
||||
|
||||
interface ItemProps {
|
||||
icon: TablerIcon;
|
||||
label: string;
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
export const Item = (props: ItemProps) => (
|
||||
<Group justify="space-between" align="center" wrap="nowrap" w="100%">
|
||||
<Group gap="xs" wrap="nowrap">
|
||||
<props.icon size={16} stroke={1.5} />
|
||||
<Text size="xs" style={{ whiteSpace: "nowrap" }}>
|
||||
{props.label}:
|
||||
</Text>
|
||||
</Group>
|
||||
|
||||
<Flex c="white" justify="end" maw="50%" w="100%">
|
||||
{props.children}
|
||||
</Flex>
|
||||
</Group>
|
||||
);
|
||||
Reference in new issue
Block a user