From 086c8ee3dfa1d9650745ec625032f6ce4af4a738 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sat, 7 Oct 2023 19:32:46 +0800 Subject: [PATCH] feat: add others metrics in website detail --- src/client/api/trpc.ts | 2 +- .../components/website/MetricsTable.tsx | 9 ++- src/client/components/website/PagesTable.tsx | 12 --- src/client/pages/Website/Detail.tsx | 76 +++++++++++++++++-- src/server/model/website.ts | 2 +- src/server/trpc/routers/website.ts | 13 ++-- 6 files changed, 85 insertions(+), 29 deletions(-) delete mode 100644 src/client/components/website/PagesTable.tsx diff --git a/src/client/api/trpc.ts b/src/client/api/trpc.ts index c4f01dd..a84242b 100644 --- a/src/client/api/trpc.ts +++ b/src/client/api/trpc.ts @@ -3,7 +3,7 @@ import type { AppRouter } from '../../server/trpc/routers'; import { httpBatchLink, TRPCClientErrorLike } from '@trpc/client'; import { getJWT } from './auth'; import { message } from 'antd'; -import { inferRouterInputs, inferRouterOutputs } from '@trpc/server'; +import type { inferRouterInputs, inferRouterOutputs } from '@trpc/server'; export const trpc = createTRPCReact(); diff --git a/src/client/components/website/MetricsTable.tsx b/src/client/components/website/MetricsTable.tsx index f8efdae..7299249 100644 --- a/src/client/components/website/MetricsTable.tsx +++ b/src/client/components/website/MetricsTable.tsx @@ -1,11 +1,13 @@ import { Table } from 'antd'; import { ColumnsType } from 'antd/es/table/interface'; import React from 'react'; -import { trpc } from '../../api/trpc'; +import { RouterOutput, trpc } from '../../api/trpc'; import { useCurrentWorkspaceId } from '../../store/user'; import { sum } from 'lodash-es'; import millify from 'millify'; +type MetricsItemType = RouterOutput['website']['metrics'][number]; + interface MetricsTableProps { websiteId: string; title: [string, string]; @@ -35,10 +37,11 @@ export const MetricsTable: React.FC = React.memo((props) => { const total = sum(metrics.map((m) => m.y)); - const columns: ColumnsType<{ x: string; y: number }> = [ + const columns: ColumnsType = [ { title: title[0], dataIndex: 'x', + render: (val) => val ?? (None), }, { title: title[1], @@ -55,7 +58,7 @@ export const MetricsTable: React.FC = React.memo((props) => { lowercase: true, })} -
+
= React.memo((props) => { - return ; -}); -PagesTable.displayName = 'PagesTable'; diff --git a/src/client/pages/Website/Detail.tsx b/src/client/pages/Website/Detail.tsx index 19e9ed7..edb4fd0 100644 --- a/src/client/pages/Website/Detail.tsx +++ b/src/client/pages/Website/Detail.tsx @@ -6,7 +6,7 @@ import { trpc } from '../../api/trpc'; import { ErrorTip } from '../../components/ErrorTip'; import { Loading } from '../../components/Loading'; import { NotFoundTip } from '../../components/NotFoundTip'; -import { PagesTable } from '../../components/website/PagesTable'; +import { MetricsTable } from '../../components/website/MetricsTable'; import { WebsiteOverview } from '../../components/website/WebsiteOverview'; import { useCurrentWorkspaceId } from '../../store/user'; @@ -30,21 +30,83 @@ export const WebsiteDetail: React.FC = React.memo(() => { return ; } + const startAt = dayjs().subtract(1, 'day').unix() * 1000; + const endAt = dayjs().unix() * 1000; + return (
- - + - - right + + + + + + + + + + + + + + {/* Map */} + + + + + + + + + {/* Events */}
diff --git a/src/server/model/website.ts b/src/server/model/website.ts index 29b9699..30d9ff3 100644 --- a/src/server/model/website.ts +++ b/src/server/model/website.ts @@ -292,7 +292,7 @@ export async function getSessionMetrics( const includeCountry = column === 'city' || column === 'subdivision1'; return prisma.$queryRaw`select - ${column} x, + ${Prisma.sql([`"${column}"`])} x, count(distinct "WebsiteEvent"."sessionId") y ${includeCountry ? Prisma.sql([', country']) : Prisma.empty} from "WebsiteEvent" diff --git a/src/server/trpc/routers/website.ts b/src/server/trpc/routers/website.ts index 2c248fa..873e1c5 100644 --- a/src/server/trpc/routers/website.ts +++ b/src/server/trpc/routers/website.ts @@ -73,8 +73,8 @@ export const websiteRouter = router({ .output( z.array( z.object({ - x: z.string(), - y: z.bigint(), + x: z.string().nullable(), + y: z.number(), }) ) ) @@ -137,16 +137,19 @@ export const websiteRouter = router({ } } - return Object.values(combined); + return Object.values(combined).map((d) => ({ + x: d.x, + y: Number(d.y), + })); } - return data; + return data.map((d) => ({ x: d.x, y: Number(d.y) })); } if (EVENT_COLUMNS.includes(type)) { const data = await getPageviewMetrics(websiteId, column, filters); - return data; + return data.map((d) => ({ x: d.x, y: Number(d.y) })); } return [];