2022-12-17 00:28:46 +09:00
|
|
|
import { createStyles, Group, Title } from '@mantine/core';
|
2022-12-10 22:14:31 +01:00
|
|
|
import { useQuery } from '@tanstack/react-query';
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
import { useTranslation } from 'next-i18next';
|
2022-12-16 21:01:06 +01:00
|
|
|
import { useConfigContext } from '../../config/provider';
|
2022-12-18 21:21:23 +01:00
|
|
|
import { defineWidget } from '../helper';
|
|
|
|
|
import { IWidget } from '../widgets';
|
|
|
|
|
import { DashDotCompactNetwork, DashDotInfo } from './DashDotCompactNetwork';
|
|
|
|
|
import { DashDotCompactStorage } from './DashDotCompactStorage';
|
|
|
|
|
import { DashDotGraph } from './DashDotGraph';
|
|
|
|
|
|
|
|
|
|
const definition = defineWidget({
|
2022-12-18 22:58:00 +01:00
|
|
|
id: 'dashdot',
|
2022-12-18 21:21:23 +01:00
|
|
|
icon: 'https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/png/dashdot.png',
|
|
|
|
|
options: {
|
2023-01-05 23:14:36 +09:00
|
|
|
refreshInterval: {
|
|
|
|
|
type: 'slider',
|
|
|
|
|
defaultValue: 5,
|
|
|
|
|
min: 1,
|
|
|
|
|
max: 60,
|
|
|
|
|
step: 1,
|
|
|
|
|
},
|
2022-12-18 21:21:23 +01:00
|
|
|
cpuMultiView: {
|
|
|
|
|
type: 'switch',
|
|
|
|
|
defaultValue: false,
|
|
|
|
|
},
|
|
|
|
|
storageMultiView: {
|
|
|
|
|
type: 'switch',
|
|
|
|
|
defaultValue: false,
|
|
|
|
|
},
|
|
|
|
|
useCompactView: {
|
|
|
|
|
type: 'switch',
|
|
|
|
|
defaultValue: true,
|
|
|
|
|
},
|
|
|
|
|
graphs: {
|
|
|
|
|
type: 'multi-select',
|
|
|
|
|
defaultValue: ['cpu', 'memory'],
|
|
|
|
|
data: ['cpu', 'memory', 'storage', 'network', 'gpu'],
|
|
|
|
|
},
|
|
|
|
|
url: {
|
|
|
|
|
type: 'text',
|
|
|
|
|
defaultValue: '',
|
|
|
|
|
},
|
|
|
|
|
},
|
2022-12-18 21:50:08 +01:00
|
|
|
gridstack: {
|
|
|
|
|
minWidth: 4,
|
|
|
|
|
minHeight: 5,
|
|
|
|
|
maxWidth: 12,
|
|
|
|
|
maxHeight: 14,
|
|
|
|
|
},
|
2022-12-18 21:21:23 +01:00
|
|
|
component: DashDotTile,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export type IDashDotTile = IWidget<typeof definition['id'], typeof definition>;
|
2022-12-10 22:14:31 +01:00
|
|
|
|
2022-12-19 18:26:04 +01:00
|
|
|
interface DashDotTileProps {
|
|
|
|
|
widget: IDashDotTile;
|
2022-12-10 22:14:31 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-19 18:26:04 +01:00
|
|
|
function DashDotTile({ widget }: DashDotTileProps) {
|
2022-12-10 22:14:31 +01:00
|
|
|
const { classes } = useDashDotTileStyles();
|
|
|
|
|
const { t } = useTranslation('modules/dashdot');
|
|
|
|
|
|
2023-01-05 23:14:36 +09:00
|
|
|
const dashDotUrl = widget.properties.url;
|
2022-12-10 22:14:31 +01:00
|
|
|
|
2023-01-05 23:14:36 +09:00
|
|
|
const { data: info } = useDashDotInfo({
|
|
|
|
|
dashDotUrl,
|
|
|
|
|
refreshInterval: widget.properties.refreshInterval,
|
|
|
|
|
});
|
2022-12-10 22:14:31 +01:00
|
|
|
|
2022-12-19 17:03:39 +01:00
|
|
|
const graphs = widget?.properties.graphs.map((g) => ({
|
2022-12-10 22:14:31 +01:00
|
|
|
id: g,
|
2022-12-18 21:21:23 +01:00
|
|
|
name: t(`card.graphs.${g}.title`),
|
2022-12-10 22:14:31 +01:00
|
|
|
twoSpan: ['network', 'gpu'].includes(g),
|
|
|
|
|
isMultiView:
|
2022-12-19 17:03:39 +01:00
|
|
|
(g === 'cpu' && widget.properties.cpuMultiView) ||
|
|
|
|
|
(g === 'storage' && widget.properties.storageMultiView),
|
2022-12-10 22:14:31 +01:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
const heading = (
|
|
|
|
|
<Title order={3} mb="xs">
|
|
|
|
|
{t('card.title')}
|
|
|
|
|
</Title>
|
|
|
|
|
);
|
|
|
|
|
|
2022-12-19 17:03:39 +01:00
|
|
|
const isCompact = widget?.properties.useCompactView ?? false;
|
2022-12-10 22:14:31 +01:00
|
|
|
|
|
|
|
|
const isCompactStorageVisible = graphs?.some((g) => g.id === 'storage' && isCompact);
|
|
|
|
|
|
|
|
|
|
const isCompactNetworkVisible = graphs?.some((g) => g.id === 'network' && isCompact);
|
|
|
|
|
|
|
|
|
|
const displayedGraphs = graphs?.filter(
|
|
|
|
|
(g) => !isCompact || !['network', 'storage'].includes(g.id)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
2022-12-19 18:26:04 +01:00
|
|
|
<>
|
2022-12-10 22:14:31 +01:00
|
|
|
{heading}
|
|
|
|
|
{!info && <p>{t('card.errors.noInformation')}</p>}
|
|
|
|
|
{info && (
|
|
|
|
|
<div className={classes.graphsContainer}>
|
|
|
|
|
<Group position="apart" w="100%">
|
2022-12-14 10:32:16 +01:00
|
|
|
{isCompactStorageVisible && <DashDotCompactStorage info={info} />}
|
2022-12-10 22:14:31 +01:00
|
|
|
{isCompactNetworkVisible && <DashDotCompactNetwork info={info} />}
|
|
|
|
|
</Group>
|
|
|
|
|
<Group position="center" w="100%" className={classes.graphsWrapper}>
|
|
|
|
|
{displayedGraphs?.map((graph) => (
|
|
|
|
|
<DashDotGraph
|
|
|
|
|
key={graph.id}
|
|
|
|
|
graph={graph}
|
|
|
|
|
dashDotUrl={dashDotUrl}
|
|
|
|
|
isCompact={isCompact}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</Group>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2022-12-19 18:26:04 +01:00
|
|
|
</>
|
2022-12-10 22:14:31 +01:00
|
|
|
);
|
2022-12-18 21:21:23 +01:00
|
|
|
}
|
2022-12-10 22:14:31 +01:00
|
|
|
|
2023-01-05 23:14:36 +09:00
|
|
|
const useDashDotInfo = ({
|
|
|
|
|
dashDotUrl,
|
|
|
|
|
refreshInterval,
|
|
|
|
|
}: {
|
|
|
|
|
dashDotUrl: string;
|
|
|
|
|
refreshInterval: number;
|
|
|
|
|
}) => {
|
2022-12-19 17:03:39 +01:00
|
|
|
const { name: configName } = useConfigContext();
|
2022-12-10 22:14:31 +01:00
|
|
|
return useQuery({
|
2023-01-05 23:14:36 +09:00
|
|
|
refetchInterval: refreshInterval * 1000 ?? 50000,
|
2022-12-10 22:14:31 +01:00
|
|
|
queryKey: [
|
|
|
|
|
'dashdot/info',
|
|
|
|
|
{
|
2022-12-14 10:32:16 +01:00
|
|
|
configName,
|
2022-12-19 17:03:39 +01:00
|
|
|
dashDotUrl,
|
2022-12-10 22:14:31 +01:00
|
|
|
},
|
|
|
|
|
],
|
2022-12-14 10:32:16 +01:00
|
|
|
queryFn: () => fetchDashDotInfo(configName),
|
2022-12-10 22:14:31 +01:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-14 10:32:16 +01:00
|
|
|
const fetchDashDotInfo = async (configName: string | undefined) => {
|
|
|
|
|
if (!configName) return {} as DashDotInfo;
|
2022-12-10 22:14:31 +01:00
|
|
|
return (await (
|
2022-12-14 10:32:16 +01:00
|
|
|
await axios.get('/api/modules/dashdot/info', { params: { configName } })
|
2022-12-10 22:14:31 +01:00
|
|
|
).data) as DashDotInfo;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useDashDotTileStyles = createStyles(() => ({
|
|
|
|
|
graphsContainer: {
|
|
|
|
|
display: 'flex',
|
|
|
|
|
flexDirection: 'row',
|
|
|
|
|
flexWrap: 'wrap',
|
|
|
|
|
rowGap: 10,
|
|
|
|
|
columnGap: 10,
|
|
|
|
|
},
|
|
|
|
|
graphsWrapper: {
|
2022-12-17 00:28:46 +09:00
|
|
|
'& > *:nth-child(odd):last-child': {
|
2022-12-10 22:14:31 +01:00
|
|
|
width: '100% !important',
|
|
|
|
|
maxWidth: '100% !important',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}));
|
2022-12-18 21:21:23 +01:00
|
|
|
|
|
|
|
|
export default definition;
|