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'; import { TelemetryPage } from './pages/Telemetry';
export const AppRoutes: React.FC = React.memo(() => { export const AppRoutes: React.FC = React.memo(() => {
const { info } = useUserStore(); const { info: userInfo } = useUserStore();
const { allowRegister } = useGlobalConfig(); const { allowRegister } = useGlobalConfig();
useInjectWebsiteScript(); useInjectWebsiteScript();
return ( return (
<Routes> <Routes>
{info ? ( {userInfo ? (
<Route element={<Layout />}> <Route element={<Layout />}>
<Route path="/dashboard" element={<DashboardPage />} /> <Route path="/dashboard" element={<DashboardPage />} />
<Route path="/monitor/*" element={<MonitorPage />} /> <Route path="/monitor/*" element={<MonitorPage />} />
@ -50,7 +50,7 @@ export const AppRoutes: React.FC = React.memo(() => {
<Route <Route
path="*" path="*"
element={ element={
<Navigate to={info ? '/dashboard' : '/login'} replace={true} /> <Navigate to={userInfo ? '/dashboard' : '/login'} replace={true} />
} }
/> />
</Routes> </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 { useNavigate } from 'react-router';
import { PageHeader } from '../PageHeader'; import { PageHeader } from '../PageHeader';
import { useEvent } from '../../hooks/useEvent'; import { useEvent } from '../../hooks/useEvent';
import { TelemetryCounter } from './TelemetryCounter';
type TelemetryInfo = AppRouterOutput['telemetry']['all'][number]; type TelemetryInfo = AppRouterOutput['telemetry']['all'][number];
@ -108,8 +109,20 @@ const TelemetryListTable: React.FC<{
dataIndex: 'name', dataIndex: 'name',
title: t('Name'), title: t('Name'),
}, },
{
dataIndex: 'id',
title: t('Count'),
align: 'center',
width: 130,
render: (id) => {
return <TelemetryCounter telemetryId={id} />;
},
},
{ {
key: 'action', key: 'action',
title: t('Actions'),
align: 'right',
width: 240,
render: (_, record) => { render: (_, record) => {
return ( return (
<div className="flex gap-2 justify-end"> <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="/monitor" label={t('Monitor')} />
<NavItem to="/website" label={t('Website')} /> <NavItem to="/website" label={t('Website')} />
<NavItem to="/servers" label={t('Servers')} /> <NavItem to="/servers" label={t('Servers')} />
{alphaMode && (
<NavItem to="/telemetry" label={t('Telemetry')} /> <NavItem to="/telemetry" label={t('Telemetry')} />
)}
<NavItem to="/settings" label={t('Settings')} /> <NavItem to="/settings" label={t('Settings')} />
</div> </div>

View File

@ -33,6 +33,31 @@ export const telemetryRouter = router({
return res; 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 upsert: workspaceOwnerProcedure
.meta( .meta(
buildTelemetryOpenapi({ buildTelemetryOpenapi({