Skip to content

Commit f249d36

Browse files
committed
ui: add dashboard stats card
1 parent b822ef7 commit f249d36

File tree

1 file changed

+80
-3
lines changed

1 file changed

+80
-3
lines changed
Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,87 @@
1-
import React from 'react';
1+
import React, { useEffect } from 'react';
2+
import {
3+
Group, Paper, SimpleGrid, Text,
4+
} from '@mantine/core';
5+
import {
6+
IconBalloon, IconChecklist, IconMailCheck, IconMailForward, IconPrinter, IconSend2, IconUser, IconUserCheck, IconUserX,
7+
} from '@tabler/icons-react';
8+
import { useQuery } from '@tanstack/react-query';
9+
10+
export function StatsCard({ title, value, Icon }) {
11+
return (
12+
<Paper withBorder p="md" radius="md" key={title}>
13+
<Group justify="space-between">
14+
<Text size="xs" c="dimmed">
15+
{title}
16+
</Text>
17+
<Icon size="1.4rem" stroke={1.5} />
18+
</Group>
19+
20+
<Group align="flex-end" gap="xs" mt={25}>
21+
<Text>{value}</Text>
22+
</Group>
23+
</Paper>
24+
);
25+
}
226

327
export default function Dashboard() {
28+
const query = useQuery({
29+
queryKey: ['metrics'],
30+
queryFn: () => fetch('/metrics', {
31+
headers: { 'Accept': 'application/json' },
32+
}).then((res) => res.json()),
33+
refetchInterval: 30000,
34+
});
35+
36+
const [machinesOnline, setMachinesOnline] = React.useState(0);
37+
const [machinesOffline, setMachinesOffline] = React.useState(0);
38+
const [printTaskNew, setPrintTasksNew] = React.useState(0);
39+
const [printTaskSent, setPrintTasksSent] = React.useState(0);
40+
const [printTaskDone, setPrintTasksDone] = React.useState(0);
41+
const [balloonTaskNew, setBalloonTasksNew] = React.useState(0);
42+
const [balloonTaskSent, setBalloonTasksSent] = React.useState(0);
43+
const [balloonTaskDone, setBalloonTasksDone] = React.useState(0);
44+
45+
useEffect(() => {
46+
if (!query.data) return;
47+
const machines: { online: number, offline: number } = { online: 0, offline: 0 };
48+
for (const metric of query.data.find((d) => d.name === 'xcpc_machinecount').values) {
49+
machines[metric.labels.status] = metric.value;
50+
}
51+
setMachinesOnline(machines.online);
52+
setMachinesOffline(machines.offline);
53+
const printTasks: { new: number, sent: number, done: number } = { new: 0, sent: 0, done: 0 };
54+
for (const metric of query.data.find((d) => d.name === 'xcpc_printcount').values) {
55+
printTasks[metric.labels.status] = metric.value;
56+
}
57+
setPrintTasksNew(printTasks.new);
58+
setPrintTasksSent(printTasks.sent);
59+
setPrintTasksDone(printTasks.done);
60+
const balloonTasks: { new: number, sent: number, done: number } = { new: 0, sent: 0, done: 0 };
61+
for (const metric of query.data.find((d) => d.name === 'xcpc_ballooncount').values) {
62+
balloonTasks[metric.labels.status] = metric.value;
63+
}
64+
setBalloonTasksNew(balloonTasks.new);
65+
setBalloonTasksSent(balloonTasks.sent);
66+
setBalloonTasksDone(balloonTasks.done);
67+
}, [query.data]);
468
return (
569
<div>
6-
<h1>Dashboard</h1>
7-
<p>Dashboard</p>
70+
<SimpleGrid cols={{ base: 1, xs: 2, md: 3 }} m="lg">
71+
<StatsCard title="Machines Online" value={machinesOnline} Icon={IconUserCheck} />
72+
<StatsCard title="Machines Offline" value={machinesOffline} Icon={IconUserX} />
73+
<StatsCard title="Machines Total" value={machinesOnline + machinesOffline} Icon={IconUser} />
74+
</SimpleGrid>
75+
<SimpleGrid cols={{ base: 1, xs: 2, md: 3 }} m="lg">
76+
<StatsCard title="Print Tasks New" value={printTaskNew} Icon={IconPrinter} />
77+
<StatsCard title="Print Tasks Sent" value={printTaskSent} Icon={IconMailForward} />
78+
<StatsCard title="Print Tasks Done" value={printTaskDone} Icon={IconMailCheck} />
79+
</SimpleGrid>
80+
<SimpleGrid cols={{ base: 1, xs: 2, md: 3 }} m="lg">
81+
<StatsCard title="Balloon Tasks New" value={balloonTaskNew} Icon={IconBalloon} />
82+
<StatsCard title="Balloon Tasks Sent" value={balloonTaskSent} Icon={IconSend2} />
83+
<StatsCard title="Balloon Tasks Done" value={balloonTaskDone} Icon={IconChecklist} />
84+
</SimpleGrid>
885
</div>
986
);
1087
}

0 commit comments

Comments
 (0)