"use client"; import { useQuery } from "@tanstack/react-query"; import { aiUsageAnalyticsQueries } from "@app/lib/queries"; import type { AiUsageAnalyticsFilters } from "@app/lib/queries"; import { Card, CardContent, CardHeader } from "@app/components/ui/card"; import { InfoSection, InfoSectionContent, InfoSections, InfoSectionTitle } from "@app/components/InfoSection"; import { ToggleableTrendChart } from "./ToggleableTrendChart"; import { TopEntitiesList, type TopEntity } from "./TopEntitiesList"; import { SERIES_COLORS, buildSeriesFromData, compactNumberFormatter, formatCost } from "./shared"; type OverviewTabProps = { orgId: string; filters: AiUsageAnalyticsFilters; }; const TOKEN_TYPE_LABELS: Record = { promptTokens: "Prompt", cacheReadTokens: "Cache read", cacheWriteTokens: "Cache write", completionTokens: "Completion", reasoningTokens: "Reasoning" }; export function OverviewTab(props: OverviewTabProps) { const { data, isLoading } = useQuery( aiUsageAnalyticsQueries.overview({ orgId: props.orgId, filters: props.filters }) ); const requestsSeries = [ { key: "requests", label: "Requests", color: SERIES_COLORS[0] } ]; const tokensSeries = Object.keys(TOKEN_TYPE_LABELS).map((key, i) => ({ key, label: TOKEN_TYPE_LABELS[key], color: SERIES_COLORS[i % SERIES_COLORS.length] })); const costSeries = [ { key: "cost", label: "Cost", color: SERIES_COLORS[0] } ]; const modelCostSeries = buildSeriesFromData( data?.modelCostPerDay ?? [], (key) => key ); const modelTokensSeries = buildSeriesFromData( data?.modelTokensPerDay ?? [], (key) => key ); const topModels: TopEntity[] = (data?.topModels ?? []).map((m) => ({ key: m.model, label: m.model, requests: m.requests, totalTokens: m.totalTokens, costUsd: m.costUsd })); return (
Total requests {data ? compactNumberFormatter.format( data.totalRequests ) : "--"} Total tokens {data ? compactNumberFormatter.format( data.totalTokens ) : "--"} Total cost {data ? formatCost(data.totalCost) : "--"} Estimated {data ? `${Math.round(data.estimatedPercent)}%` : "--"}

Request volume

Token usage

Cost

formatCost(v)} />

Model cost

formatCost(v)} />

Model tokens

Top models

); }