From 537edcf5066fee879be2506dab17e0e583ed9729 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Mon, 29 Jan 2024 19:56:13 +0800 Subject: [PATCH] refactor: change website stat endpoint to trpc --- src/client/api/model/website.ts | 56 ----------- .../components/website/WebsiteOverview.tsx | 34 +++---- src/server/model/_schema/filter.ts | 26 +++++ src/server/router/workspace.ts | 80 ---------------- src/server/trpc/routers/website.ts | 96 ++++++++++++++++++- 5 files changed, 136 insertions(+), 156 deletions(-) create mode 100644 src/server/model/_schema/filter.ts diff --git a/src/client/api/model/website.ts b/src/client/api/model/website.ts index fa11afa..c2ee9c1 100644 --- a/src/client/api/model/website.ts +++ b/src/client/api/model/website.ts @@ -63,59 +63,3 @@ export function useWorkspaceWebsitePageview( refetch, }; } - -export interface StatsItemType { - value: number; - change: number; -} -export async function getWorkspaceWebsiteStats( - workspaceId: string, - websiteId: string, - filter: Record -): Promise<{ - bounces: StatsItemType; - pageviews: StatsItemType; - totaltime: StatsItemType; - uniques: StatsItemType; -}> { - const { data } = await request.get( - `/api/workspace/${workspaceId}/website/${websiteId}/stats`, - { - params: { - ...filter, - }, - } - ); - - return data.stats; -} - -export function useWorkspaceWebsiteStats( - workspaceId: string, - websiteId: string, - startAt: number, - endAt: number, - unit: DateUnit -) { - const { - data: stats, - isLoading, - refetch, - } = useQuery( - ['websiteStats', { workspaceId, websiteId, startAt, endAt }], - () => { - return getWorkspaceWebsiteStats(workspaceId, websiteId, { - startAt, - endAt, - unit, - timezone: getUserTimezone(), - }); - } - ); - - return { - stats, - isLoading, - refetch, - }; -} diff --git a/src/client/components/website/WebsiteOverview.tsx b/src/client/components/website/WebsiteOverview.tsx index 2d9f77c..c3f0772 100644 --- a/src/client/components/website/WebsiteOverview.tsx +++ b/src/client/components/website/WebsiteOverview.tsx @@ -4,9 +4,7 @@ import { Column, ColumnConfig } from '@ant-design/charts'; import { SyncOutlined } from '@ant-design/icons'; import { DateFilter } from '../DateFilter'; import { - StatsItemType, useWorkspaceWebsitePageview, - useWorkspaceWebsiteStats, WebsiteInfo, } from '../../api/model/website'; import { @@ -23,6 +21,8 @@ import { WebsiteOnlineCount } from './WebsiteOnlineCount'; import { useGlobalRangeDate } from '../../hooks/useGlobalRangeDate'; import { MonitorHealthBar } from '../monitor/MonitorHealthBar'; import { useNavigate } from 'react-router'; +import { AppRouterOutput, trpc } from '../../api/trpc'; +import { getUserTimezone } from '../../api/model/user'; export const WebsiteOverview: React.FC<{ website: WebsiteInfo; @@ -47,16 +47,17 @@ export const WebsiteOverview: React.FC<{ ); const { - stats, + data: stats, isLoading: isLoadingStats, refetch: refetchStats, - } = useWorkspaceWebsiteStats( - website.workspaceId, - website.id, - startDate.unix() * 1000, - endDate.unix() * 1000, - unit - ); + } = trpc.website.stats.useQuery({ + workspaceId: website.workspaceId, + websiteId: website.id, + startAt: startDate.unix() * 1000, + endAt: endDate.unix() * 1000, + timezone: getUserTimezone(), + unit, + }); const handleRefresh = useEvent(async () => { refresh(); @@ -109,8 +110,8 @@ export const WebsiteOverview: React.FC<{
{actions}
-
- {stats && } +
+
{stats && }