feat: add telemetry event count

This commit is contained in:
moonrailgun 2024-02-23 00:07:30 +08:00
parent 355690eb75
commit e79c4b4819
5 changed files with 61 additions and 4 deletions

View File

@ -22,14 +22,14 @@ import { StatusPage } from './pages/Status';
import { TelemetryPage } from './pages/Telemetry';
export const AppRoutes: React.FC = React.memo(() => {
const { info } = useUserStore();
const { info: userInfo } = useUserStore();
const { allowRegister } = useGlobalConfig();
useInjectWebsiteScript();
return (
<Routes>
{info ? (
{userInfo ? (
<Route element={<Layout />}>
<Route path="/dashboard" element={<DashboardPage />} />
<Route path="/monitor/*" element={<MonitorPage />} />
@ -50,7 +50,7 @@ export const AppRoutes: React.FC = React.memo(() => {
<Route
path="*"
element={
<Navigate to={info ? '/dashboard' : '/login'} replace={true} />
<Navigate to={userInfo ? '/dashboard' : '/login'} replace={true} />
}
/>
</Routes>

View File

@ -0,0 +1,17 @@
import React from 'react';
import { trpc } from '../../api/trpc';
import { useCurrentWorkspaceId } from '../../store/user';
import { formatNumber } from '../../utils/common';
export const TelemetryCounter: React.FC<{
telemetryId: string;
}> = React.memo((props) => {
const workspaceId = useCurrentWorkspaceId();
const { data = 0 } = trpc.telemetry.eventCount.useQuery({
workspaceId,
telemetryId: props.telemetryId,
});
return <span>{formatNumber(data)}</span>;
});
TelemetryCounter.displayName = 'TelemetryCounter';

View File

@ -12,6 +12,7 @@ import {
import { useNavigate } from 'react-router';
import { PageHeader } from '../PageHeader';
import { useEvent } from '../../hooks/useEvent';
import { TelemetryCounter } from './TelemetryCounter';
type TelemetryInfo = AppRouterOutput['telemetry']['all'][number];
@ -108,8 +109,20 @@ const TelemetryListTable: React.FC<{
dataIndex: 'name',
title: t('Name'),
},
{
dataIndex: 'id',
title: t('Count'),
align: 'center',
width: 130,
render: (id) => {
return <TelemetryCounter telemetryId={id} />;
},
},
{
key: 'action',
title: t('Actions'),
align: 'right',
width: 240,
render: (_, record) => {
return (
<div className="flex gap-2 justify-end">

View File

@ -157,7 +157,9 @@ export const Layout: React.FC = React.memo(() => {
<NavItem to="/monitor" label={t('Monitor')} />
<NavItem to="/website" label={t('Website')} />
<NavItem to="/servers" label={t('Servers')} />
<NavItem to="/telemetry" label={t('Telemetry')} />
{alphaMode && (
<NavItem to="/telemetry" label={t('Telemetry')} />
)}
<NavItem to="/settings" label={t('Settings')} />
</div>

View File

@ -33,6 +33,31 @@ export const telemetryRouter = router({
return res;
}),
eventCount: workspaceProcedure
.meta(
buildTelemetryOpenapi({
method: 'GET',
path: '/eventCount',
})
)
.input(
z.object({
telemetryId: z.string(),
})
)
.output(z.number())
.query(async ({ input }) => {
const { workspaceId, telemetryId } = input;
const count = await prisma.telemetryEvent.count({
where: {
workspaceId,
telemetryId,
},
});
return count;
}),
upsert: workspaceOwnerProcedure
.meta(
buildTelemetryOpenapi({