|
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 | +} |
2 | 26 |
|
3 | 27 | 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]); |
4 | 68 | return (
|
5 | 69 | <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> |
8 | 85 | </div>
|
9 | 86 | );
|
10 | 87 | }
|
0 commit comments