2023-02-10 18:20:28 +01:00
|
|
|
import { Center, createStyles, Grid, Stack, Text, Title } from '@mantine/core';
|
2023-02-06 20:52:36 +01:00
|
|
|
import { IconUnlink } from '@tabler/icons';
|
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';
|
2023-02-10 18:20:28 +01:00
|
|
|
import { DashDotInfo } from './DashDotCompactNetwork';
|
2022-12-18 21:21:23 +01:00
|
|
|
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-02-10 18:20:28 +01:00
|
|
|
url: {
|
|
|
|
|
type: 'text',
|
|
|
|
|
defaultValue: '',
|
2022-12-18 21:21:23 +01:00
|
|
|
},
|
2023-02-10 18:20:28 +01:00
|
|
|
usePercentages: {
|
2022-12-18 21:21:23 +01:00
|
|
|
type: 'switch',
|
|
|
|
|
defaultValue: false,
|
|
|
|
|
},
|
2023-02-10 18:20:28 +01:00
|
|
|
columns: {
|
|
|
|
|
type: 'number',
|
|
|
|
|
defaultValue: 2,
|
2022-12-18 21:21:23 +01:00
|
|
|
},
|
2023-02-10 18:20:28 +01:00
|
|
|
graphHeight: {
|
|
|
|
|
type: 'number',
|
|
|
|
|
defaultValue: 115,
|
2023-01-31 20:12:03 +01:00
|
|
|
},
|
2023-02-10 18:20:28 +01:00
|
|
|
graphsOrder: {
|
|
|
|
|
type: 'draggable-list',
|
|
|
|
|
defaultValue: [
|
|
|
|
|
{
|
|
|
|
|
key: 'storage',
|
|
|
|
|
subValues: {
|
|
|
|
|
enabled: true,
|
|
|
|
|
compactView: true,
|
|
|
|
|
span: 2,
|
|
|
|
|
multiView: false,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
key: 'network',
|
|
|
|
|
subValues: {
|
|
|
|
|
enabled: true,
|
|
|
|
|
compactView: true,
|
|
|
|
|
span: 2,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
key: 'cpu',
|
|
|
|
|
subValues: {
|
|
|
|
|
enabled: true,
|
|
|
|
|
multiView: false,
|
|
|
|
|
span: 1,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
key: 'ram',
|
|
|
|
|
subValues: {
|
|
|
|
|
enabled: true,
|
|
|
|
|
span: 1,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
key: 'gpu',
|
|
|
|
|
subValues: {
|
|
|
|
|
enabled: false,
|
|
|
|
|
span: 1,
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-01-11 08:44:48 +09:00
|
|
|
],
|
2023-02-10 18:20:28 +01:00
|
|
|
items: {
|
|
|
|
|
cpu: {
|
|
|
|
|
enabled: {
|
|
|
|
|
type: 'switch',
|
|
|
|
|
},
|
|
|
|
|
span: {
|
|
|
|
|
type: 'number',
|
|
|
|
|
},
|
|
|
|
|
multiView: {
|
|
|
|
|
type: 'switch',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
storage: {
|
|
|
|
|
enabled: {
|
|
|
|
|
type: 'switch',
|
|
|
|
|
},
|
|
|
|
|
span: {
|
|
|
|
|
type: 'number',
|
|
|
|
|
},
|
|
|
|
|
compactView: {
|
|
|
|
|
type: 'switch',
|
|
|
|
|
},
|
|
|
|
|
multiView: {
|
|
|
|
|
type: 'switch',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
ram: {
|
|
|
|
|
enabled: {
|
|
|
|
|
type: 'switch',
|
|
|
|
|
},
|
|
|
|
|
span: {
|
|
|
|
|
type: 'number',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
network: {
|
|
|
|
|
enabled: {
|
|
|
|
|
type: 'switch',
|
|
|
|
|
},
|
|
|
|
|
span: {
|
|
|
|
|
type: 'number',
|
|
|
|
|
},
|
|
|
|
|
compactView: {
|
|
|
|
|
type: 'switch',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
gpu: {
|
|
|
|
|
enabled: {
|
|
|
|
|
type: 'switch',
|
|
|
|
|
},
|
|
|
|
|
span: {
|
|
|
|
|
type: 'number',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2022-12-18 21:21:23 +01:00
|
|
|
},
|
|
|
|
|
},
|
2022-12-18 21:50:08 +01:00
|
|
|
gridstack: {
|
2023-01-08 12:41:55 +09:00
|
|
|
minWidth: 2,
|
|
|
|
|
minHeight: 2,
|
2022-12-18 21:50:08 +01:00
|
|
|
maxWidth: 12,
|
|
|
|
|
maxHeight: 14,
|
|
|
|
|
},
|
2022-12-18 21:21:23 +01:00
|
|
|
component: DashDotTile,
|
|
|
|
|
});
|
|
|
|
|
|
2023-02-06 20:52:36 +01:00
|
|
|
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;
|
2023-02-06 20:52:36 +01:00
|
|
|
const locationProtocol = window.location.protocol;
|
|
|
|
|
const detectedProtocolDowngrade =
|
|
|
|
|
locationProtocol === 'https:' && dashDotUrl.toLowerCase().startsWith('http:');
|
2022-12-10 22:14:31 +01:00
|
|
|
|
2023-01-05 23:14:36 +09:00
|
|
|
const { data: info } = useDashDotInfo({
|
|
|
|
|
dashDotUrl,
|
2023-02-06 20:52:36 +01:00
|
|
|
enabled: !detectedProtocolDowngrade,
|
2023-01-05 23:14:36 +09:00
|
|
|
});
|
2022-12-10 22:14:31 +01:00
|
|
|
|
2023-02-06 20:52:36 +01:00
|
|
|
if (detectedProtocolDowngrade) {
|
|
|
|
|
return (
|
|
|
|
|
<Center h="100%">
|
|
|
|
|
<Stack spacing="xs" align="center">
|
|
|
|
|
<IconUnlink size={40} strokeWidth={1.2} />
|
|
|
|
|
<Title order={5}>{t('card.errors.protocolDowngrade.title')}</Title>
|
|
|
|
|
<Text align="center" size="sm">
|
2023-02-10 18:20:28 +01:00
|
|
|
{t('card.errors.protocolDowngrade.text')}
|
2023-02-06 20:52:36 +01:00
|
|
|
</Text>
|
|
|
|
|
</Stack>
|
|
|
|
|
</Center>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-10 18:20:28 +01:00
|
|
|
const { graphsOrder, usePercentages, columns, graphHeight } = widget.properties;
|
2022-12-10 22:14:31 +01:00
|
|
|
|
|
|
|
|
return (
|
2023-02-10 18:20:28 +01:00
|
|
|
<Stack spacing="xs">
|
|
|
|
|
<Title order={3}>{t('card.title')}</Title>
|
2022-12-10 22:14:31 +01:00
|
|
|
{!info && <p>{t('card.errors.noInformation')}</p>}
|
|
|
|
|
{info && (
|
|
|
|
|
<div className={classes.graphsContainer}>
|
2023-02-10 18:20:28 +01:00
|
|
|
<Grid grow gutter="sm" w="100%" columns={columns}>
|
|
|
|
|
{graphsOrder
|
|
|
|
|
.filter((g) => g.subValues.enabled)
|
|
|
|
|
.map((g) => (
|
|
|
|
|
<Grid.Col span={Math.min(columns, g.subValues.span)}>
|
|
|
|
|
<DashDotGraph
|
|
|
|
|
dashDotUrl={dashDotUrl}
|
|
|
|
|
info={info}
|
|
|
|
|
graph={g.key as any}
|
|
|
|
|
graphHeight={graphHeight}
|
|
|
|
|
isCompact={g.subValues.compactView ?? false}
|
|
|
|
|
multiView={g.subValues.multiView ?? false}
|
|
|
|
|
usePercentages={usePercentages}
|
|
|
|
|
/>
|
|
|
|
|
</Grid.Col>
|
|
|
|
|
))}
|
|
|
|
|
</Grid>
|
2022-12-10 22:14:31 +01:00
|
|
|
</div>
|
|
|
|
|
)}
|
2023-02-10 18:20:28 +01:00
|
|
|
</Stack>
|
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-02-06 20:52:36 +01:00
|
|
|
const useDashDotInfo = ({ dashDotUrl, enabled }: { dashDotUrl: string; enabled: boolean }) => {
|
2022-12-19 17:03:39 +01:00
|
|
|
const { name: configName } = useConfigContext();
|
2022-12-10 22:14:31 +01:00
|
|
|
return useQuery({
|
2023-01-06 11:19:07 +09:00
|
|
|
refetchInterval: 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),
|
2023-02-06 20:52:36 +01:00
|
|
|
enabled,
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-10 18:48:08 +01:00
|
|
|
export const useDashDotTileStyles = createStyles((theme) => ({
|
2022-12-10 22:14:31 +01:00
|
|
|
graphsContainer: {
|
2023-02-10 18:48:08 +01:00
|
|
|
marginRight: theme.spacing.sm * -1, // fix because margin collapses weirdly
|
2022-12-10 22:14:31 +01:00
|
|
|
},
|
|
|
|
|
}));
|
2022-12-18 21:21:23 +01:00
|
|
|
|
|
|
|
|
export default definition;
|