From ac07ebcce8ab40fff812680bd4489e5d69b50880 Mon Sep 17 00:00:00 2001 From: "raoha.rh" Date: Tue, 24 Dec 2024 17:58:15 +0800 Subject: [PATCH] feat: add bot token usage analyzer --- client/app/admin/page.tsx | 103 +++++++++++++++++++++++++++++++++ client/app/hooks/useAnalyze.ts | 27 +++++++++ 2 files changed, 130 insertions(+) create mode 100644 client/app/admin/page.tsx create mode 100644 client/app/hooks/useAnalyze.ts diff --git a/client/app/admin/page.tsx b/client/app/admin/page.tsx new file mode 100644 index 00000000..f1e4936e --- /dev/null +++ b/client/app/admin/page.tsx @@ -0,0 +1,103 @@ +'use client'; + +import React from 'react'; +import dayjs from 'dayjs'; +import { Column } from '@ant-design/charts'; +import { Card, Progress } from '@nextui-org/react'; +import { useAnalyze, useTopBots, useTopUsers } from '../hooks/useAnalyze'; +import { maxBy, sortBy } from 'lodash'; + +export default function AdminPage() { + const { data = [], isLoading } = useAnalyze(); + const { data: topBots = [] } = useTopBots(); + const { data: topUsers = [] } = useTopUsers(); + + const chartProps = { + xField: d => new Date(d.usage_date), + colorField: 'bot_name', + height: 400, + stack: true, + legend: false, + sort: { by: 'x' }, + scale: { color: { palette: 'tableau10' }}, + axis: { + x: { + labelFormatter: x => dayjs(x).format('MM-DD'), + } + }, + } + + return ( +
+ {/* Header */} +
+

Usage: Tokens

+
+ + {/* Charts Section */} +
+
+ +

Input Tokens

+
+ +
+
+ + +

Output Tokens

+
+ +
+
+
+
+ +

Top Bots

+
+ {topBots.map(record => { + return + })} +
+
+ + +

Top Users

+
+ {topUsers.map(record => { + return + })} +
+
+
+
+ +
+ ); +} diff --git a/client/app/hooks/useAnalyze.ts b/client/app/hooks/useAnalyze.ts new file mode 100644 index 00000000..372fdbe4 --- /dev/null +++ b/client/app/hooks/useAnalyze.ts @@ -0,0 +1,27 @@ +import { useQuery } from '@tanstack/react-query'; +import { analyzeTokenUsage, analyzeTopBots, analyzeTopUsers } from '../services/TokensController'; + + +export function useAnalyze() { + return useQuery({ + queryKey: [`usage.analyze`], + queryFn: async () => analyzeTokenUsage(), + retry: false, + }); +} + +export function useTopBots() { + return useQuery<{ bot_name: string; total_tokens: number;}[]>({ + queryKey: [`usage.top.bots`], + queryFn: async () => analyzeTopBots(), + retry: false, + }); +} + +export function useTopUsers() { + return useQuery<{ user_name: string; total_tokens: number;}[]>({ + queryKey: [`usage.top.users`], + queryFn: async () => analyzeTopUsers(), + retry: false, + }); +}