From 8a3c93fff71a42a48041af1008f701e9868982c7 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sun, 14 Apr 2024 15:47:55 +0800 Subject: [PATCH] feat: add telemetry event count --- src/client/components/CommonList.tsx | 13 +++++++------ src/client/routes/telemetry.tsx | 4 ++++ src/server/trpc/routers/telemetry.ts | 27 +++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/client/components/CommonList.tsx b/src/client/components/CommonList.tsx index 3f35c83..0c4b30f 100644 --- a/src/client/components/CommonList.tsx +++ b/src/client/components/CommonList.tsx @@ -11,6 +11,7 @@ import { Empty } from 'antd'; export interface CommonListItem { id: string; title: string; + number?: number; content?: React.ReactNode; tags?: string[]; href: string; @@ -84,12 +85,12 @@ export const CommonList: React.FC = React.memo((props) => { }) } > -
-
-
-
{item.title}
-
-
+
+
{item.title}
+ + {item.number && item.number > 0 && ( + {item.number} + )}
{item.content && ( diff --git a/src/client/routes/telemetry.tsx b/src/client/routes/telemetry.tsx index 7523261..4e77d8a 100644 --- a/src/client/routes/telemetry.tsx +++ b/src/client/routes/telemetry.tsx @@ -27,6 +27,9 @@ function TelemetryComponent() { const { data = [] } = trpc.telemetry.all.useQuery({ workspaceId, }); + const { data: allEventCount = {} } = trpc.telemetry.allEventCount.useQuery({ + workspaceId, + }); const navigate = useNavigate(); const pathname = useRouterState({ select: (state) => state.location.pathname, @@ -35,6 +38,7 @@ function TelemetryComponent() { const items = data.map((item) => ({ id: item.id, title: item.name, + number: allEventCount[item.id] ?? 0, href: `/telemetry/${item.id}`, })); diff --git a/src/server/trpc/routers/telemetry.ts b/src/server/trpc/routers/telemetry.ts index bf74631..5324a9f 100644 --- a/src/server/trpc/routers/telemetry.ts +++ b/src/server/trpc/routers/telemetry.ts @@ -78,6 +78,33 @@ export const telemetryRouter = router({ return res; }), + allEventCount: workspaceProcedure + .meta( + buildTelemetryOpenapi({ + method: 'GET', + path: '/allEventCount', + }) + ) + .output(z.record(z.string(), z.number())) + .query(async ({ input }) => { + const { workspaceId } = input; + + const res = await prisma.telemetryEvent.groupBy({ + by: ['telemetryId'], + where: { + workspaceId, + }, + _count: true, + }); + + return res.reduce>((prev, item) => { + if (item.telemetryId) { + prev[item.telemetryId] = item._count; + } + + return prev; + }, {}); + }), eventCount: workspaceProcedure .meta( buildTelemetryOpenapi({