feat: add monitor overview

This commit is contained in:
moonrailgun 2023-10-17 20:26:28 +08:00
parent 0b2f4b7e62
commit 1a9daaddfe
4 changed files with 112 additions and 5 deletions

View File

@ -0,0 +1,62 @@
import React from 'react';
import { trpc } from '../../api/trpc';
import { useCurrentWorkspaceId } from '../../store/user';
import clsx from 'clsx';
import dayjs from 'dayjs';
import { Card, Empty } from 'antd';
import { useNavigate } from 'react-router';
interface MonitorEventListProps {
monitorId?: string;
}
export const MonitorEventList: React.FC<MonitorEventListProps> = React.memo(
(props) => {
const workspaceId = useCurrentWorkspaceId();
const { data = [], isLoading } = trpc.monitor.events.useQuery({
workspaceId,
monitorId: props.monitorId,
});
const navigate = useNavigate();
if (isLoading === false && data.length === 0) {
return <Empty />;
}
return (
<div className="space-y-4 py-2">
{data.map((item) => (
<Card
key={item.id}
className="min-h-[60px]"
hoverable={true}
onClick={() => {
navigate(`/monitor/${item.monitorId}`);
}}
>
<div className="flex items-center w-full gap-2">
<div
className={clsx(
'min-w-[62px] py-1 px-2 rounded-full text-white inline-block text-center',
item.type === 'UP'
? 'bg-green-400'
: item.type === 'DOWN'
? 'bg-red-500'
: 'bg-amber-400'
)}
>
{item.type}
</div>
<div className="text-black text-opacity-60">
{dayjs(item.createdAt).format('YYYY-MM-DD HH:mm:ss')}
</div>
<div>{item.message}</div>
</div>
</Card>
))}
</div>
);
}
);
MonitorEventList.displayName = 'MonitorEventList';

View File

@ -1,10 +1,8 @@
import { Card } from 'antd';
import clsx from 'clsx'; import clsx from 'clsx';
import React, { useMemo, useState } from 'react'; import React, { useMemo, useState } from 'react';
import { useLocation, useNavigate, useParams } from 'react-router'; import { useNavigate } from 'react-router';
import { trpc } from '../../api/trpc'; import { trpc } from '../../api/trpc';
import { useCurrentWorkspaceId } from '../../store/user'; import { useCurrentWorkspaceId } from '../../store/user';
import { HealthBar } from '../HealthBar';
import { NoWorkspaceTip } from '../NoWorkspaceTip'; import { NoWorkspaceTip } from '../NoWorkspaceTip';
import { MonitorHealthBar } from './MonitorHealthBar'; import { MonitorHealthBar } from './MonitorHealthBar';
@ -80,7 +78,6 @@ export const MonitorList: React.FC = React.memo(() => {
upPercent === 100 ? 'bg-green-400' : 'bg-amber-400' upPercent === 100 ? 'bg-green-400' : 'bg-amber-400'
)} )}
> >
{/* {monitor.monthOnlineRate * 100}% */}
{upPercent}% {upPercent}%
</span> </span>
</div> </div>

View File

@ -1,6 +1,33 @@
import React from 'react'; import React from 'react';
import { useCurrentWorkspaceId } from '../../store/user';
import { trpc } from '../../api/trpc';
import { Card } from 'antd';
import { MonitorEventList } from '../../components/monitor/MonitorEventList';
export const MonitorOverview: React.FC = React.memo(() => { export const MonitorOverview: React.FC = React.memo(() => {
return <div>Overview</div>; const currentWorkspaceId = useCurrentWorkspaceId()!;
const { data: monitors = [] } = trpc.monitor.all.useQuery({
workspaceId: currentWorkspaceId,
});
return (
<div className="px-2">
<div className="grid gap-4 grid-cols-2">
<Card hoverable={true}>
<div>Monitors</div>
<div className="text-2xl font-semibold">{monitors.length}</div>
</Card>
<Card hoverable={true}>
<div>Available</div>
<div className="text-2xl font-semibold">
{monitors.filter((m) => m.active).length}
</div>
</Card>
</div>
<div>
<MonitorEventList />
</div>
</div>
);
}); });
MonitorOverview.displayName = 'MonitorOverview'; MonitorOverview.displayName = 'MonitorOverview';

View File

@ -193,4 +193,25 @@ export const monitorRouter = router({
recent30DayOfflineCount, recent30DayOfflineCount,
}; };
}), }),
events: workspaceProcedure
.input(
z.object({
monitorId: z.string().cuid2().optional(),
})
)
.query(async ({ input }) => {
const { monitorId } = input;
const list = await prisma.monitorEvent.findMany({
where: {
monitorId,
},
orderBy: {
createdAt: 'desc',
},
take: 20,
});
return list;
}),
}); });