Skip to content

Commit

Permalink
feat: add bot token usage analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
RaoHai committed Dec 25, 2024
1 parent a8c1e13 commit 2e4b284
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
6 changes: 3 additions & 3 deletions client/app/admin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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 { BotUsage, useAnalyze, useTopBots, useTopUsers } from '../hooks/useAnalyze';
import { maxBy, sortBy } from 'lodash';

export default function AdminPage() {
Expand All @@ -13,7 +13,7 @@ export default function AdminPage() {
const { data: topUsers = [] } = useTopUsers();

const chartProps = {
xField: d => new Date(d.usage_date),
xField: (d: BotUsage) => new Date(d.usage_date),
colorField: 'bot_name',
height: 400,
stack: true,
Expand All @@ -22,7 +22,7 @@ export default function AdminPage() {
scale: { color: { palette: 'tableau10' }},
axis: {
x: {
labelFormatter: x => dayjs(x).format('MM-DD'),
labelFormatter: (x: string) => dayjs(x).format('MM-DD'),
}
},
}
Expand Down
7 changes: 6 additions & 1 deletion client/app/hooks/useAnalyze.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { useQuery } from '@tanstack/react-query';
import { analyzeTokenUsage, analyzeTopBots, analyzeTopUsers } from '../services/TokensController';

export interface BotUsage {
bot_name: string;
total_tokens: number;
usage_date: Date;
}

export function useAnalyze() {
return useQuery({
Expand All @@ -11,7 +16,7 @@ export function useAnalyze() {
}

export function useTopBots() {
return useQuery<{ bot_name: string; total_tokens: number;}[]>({
return useQuery<BotUsage[]>({
queryKey: [`usage.top.bots`],
queryFn: async () => analyzeTopBots(),
retry: false,
Expand Down

0 comments on commit 2e4b284

Please sign in to comment.