feat: add telemetry event count

This commit is contained in:
moonrailgun 2024-04-14 15:47:55 +08:00
parent ac7b44e862
commit 8a3c93fff7
3 changed files with 38 additions and 6 deletions

View File

@ -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<CommonListProps> = React.memo((props) => {
})
}
>
<div className="flex w-full flex-col gap-1">
<div className="flex items-center">
<div className="flex items-center gap-2">
<div className="flex w-full items-center justify-between gap-1">
<div className="font-semibold">{item.title}</div>
</div>
</div>
{item.number && item.number > 0 && (
<span className="opacity-60">{item.number}</span>
)}
</div>
{item.content && (

View File

@ -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}`,
}));

View File

@ -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<Record<string, number>>((prev, item) => {
if (item.telemetryId) {
prev[item.telemetryId] = item._count;
}
return prev;
}, {});
}),
eventCount: workspaceProcedure
.meta(
buildTelemetryOpenapi({