feat: 使用统计页面 + 简体中文界面优化
- 新增「我的」页面替代原「账户」页面 - 实现使用统计仪表盘(Tokens消耗、成本统计、缓存命中率) - 添加使用趋势图表(支持按天/周/月/全部筛选) - 添加模型消耗统计表格 - 优化简体中文翻译 - 修改导航栏标签和工具提示
This commit is contained in:
1 parent
34bb7b9a93
commit
376d10cbbb
7 files changed
+797
No files matched your search
@@ -0,0 +1 @@
|
||||
web_search = "disabled"
|
||||
@@ -0,0 +1,141 @@
|
||||
import { useMemo, useState } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
|
||||
type ModelStats = {
|
||||
requests: number
|
||||
tokensIn: number
|
||||
tokensOut: number
|
||||
cost: number
|
||||
}
|
||||
|
||||
type ModelStatsTableProps = {
|
||||
modelStats: Map<string, ModelStats>
|
||||
}
|
||||
|
||||
function formatNumber(num: number): string {
|
||||
if (num >= 1000000) {
|
||||
return (num / 1000000).toFixed(2) + "M"
|
||||
}
|
||||
if (num >= 1000) {
|
||||
return (num / 1000).toFixed(1) + "K"
|
||||
}
|
||||
return num.toLocaleString()
|
||||
}
|
||||
|
||||
function formatCost(cost: number): string {
|
||||
return "$" + cost.toFixed(4)
|
||||
}
|
||||
|
||||
export const ModelStatsTable = ({ modelStats }: ModelStatsTableProps) => {
|
||||
const { t } = useTranslation("settings")
|
||||
const [sortField, setSortField] = useState<"requests" | "tokens" | "cost">("tokens")
|
||||
const [sortDirection, setSortDirection] = useState<"asc" | "desc">("desc")
|
||||
|
||||
const sortedModels = useMemo(() => {
|
||||
const models = Array.from(modelStats.entries()).map(([name, stats]) => ({
|
||||
name,
|
||||
...stats,
|
||||
totalTokens: stats.tokensIn + stats.tokensOut,
|
||||
}))
|
||||
|
||||
models.sort((a, b) => {
|
||||
let aVal: number, bVal: number
|
||||
switch (sortField) {
|
||||
case "requests":
|
||||
aVal = a.requests
|
||||
bVal = b.requests
|
||||
break
|
||||
case "tokens":
|
||||
aVal = a.totalTokens
|
||||
bVal = b.totalTokens
|
||||
break
|
||||
case "cost":
|
||||
aVal = a.cost
|
||||
bVal = b.cost
|
||||
break
|
||||
default:
|
||||
aVal = a.totalTokens
|
||||
bVal = b.totalTokens
|
||||
}
|
||||
return sortDirection === "desc" ? bVal - aVal : aVal - bVal
|
||||
})
|
||||
|
||||
return models
|
||||
}, [modelStats, sortField, sortDirection])
|
||||
|
||||
const handleSort = (field: "requests" | "tokens" | "cost") => {
|
||||
if (sortField === field) {
|
||||
setSortDirection(sortDirection === "desc" ? "asc" : "desc")
|
||||
} else {
|
||||
setSortField(field)
|
||||
setSortDirection("desc")
|
||||
}
|
||||
}
|
||||
|
||||
const SortIcon = ({ field }: { field: string }) => {
|
||||
if (sortField !== field) return <span className="ml-1 text-[var(--vscode-descriptionForeground)]">↕</span>
|
||||
return <span className="ml-1 text-[var(--vscode-foreground)]">{sortDirection === "desc" ? "↓" : "↑"}</span>
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="bg-[var(--vscode-editorWidget-background)] border border-[var(--vscode-widget-border)] rounded-lg overflow-hidden">
|
||||
<h3 className="text-lg font-semibold text-[var(--vscode-foreground)] p-6 pb-4">{t("usageStats.modelStats")}</h3>
|
||||
|
||||
{sortedModels.length === 0 ? (
|
||||
<div className="text-center text-[var(--vscode-descriptionForeground)] py-8">{t("usageStats.noData")}</div>
|
||||
) : (
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full text-sm">
|
||||
<thead>
|
||||
<tr className="border-b border-[var(--vscode-widget-border)]">
|
||||
<th className="text-left p-4 text-[var(--vscode-descriptionForeground)] font-medium">
|
||||
{t("usageStats.tableHeaders.model")}
|
||||
</th>
|
||||
<th
|
||||
className="text-right p-4 text-[var(--vscode-descriptionForeground)] font-medium cursor-pointer hover:text-[var(--vscode-foreground)]"
|
||||
onClick={() => handleSort("requests")}>
|
||||
{t("usageStats.tableHeaders.requests")}
|
||||
<SortIcon field="requests" />
|
||||
</th>
|
||||
<th
|
||||
className="text-right p-4 text-[var(--vscode-descriptionForeground)] font-medium cursor-pointer hover:text-[var(--vscode-foreground)]"
|
||||
onClick={() => handleSort("tokens")}>
|
||||
{t("usageStats.tableHeaders.tokens")}
|
||||
<SortIcon field="tokens" />
|
||||
</th>
|
||||
<th
|
||||
className="text-right p-4 text-[var(--vscode-descriptionForeground)] font-medium cursor-pointer hover:text-[var(--vscode-foreground)]"
|
||||
onClick={() => handleSort("cost")}>
|
||||
{t("usageStats.tableHeaders.totalCost")}
|
||||
<SortIcon field="cost" />
|
||||
</th>
|
||||
<th className="text-right p-4 text-[var(--vscode-descriptionForeground)] font-medium">
|
||||
{t("usageStats.tableHeaders.avgCost")}
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{sortedModels.map((model) => (
|
||||
<tr
|
||||
className="border-b border-[var(--vscode-widget-border)] last:border-0 hover:bg-[var(--vscode-list-hoverBackground)]"
|
||||
key={model.name}>
|
||||
<td className="p-4 text-[var(--vscode-foreground)] font-medium">{model.name}</td>
|
||||
<td className="p-4 text-right text-[var(--vscode-foreground)]">{model.requests}</td>
|
||||
<td className="p-4 text-right text-[var(--vscode-foreground)]">
|
||||
{formatNumber(model.totalTokens)}
|
||||
</td>
|
||||
<td className="p-4 text-right text-[var(--vscode-foreground)]">{formatCost(model.cost)}</td>
|
||||
<td className="p-4 text-right text-[var(--vscode-foreground)]">
|
||||
{model.requests > 0 ? formatCost(model.cost / model.requests) : "$0.0000"}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default ModelStatsTable
|
||||
@@ -0,0 +1,232 @@
|
||||
import { HistoryItem } from "@shared/HistoryItem"
|
||||
import { useCallback, useMemo, useState } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { useExtensionState } from "@/context/ExtensionStateContext"
|
||||
import ViewHeader from "../common/ViewHeader"
|
||||
import { ModelStatsTable } from "./ModelStatsTable"
|
||||
import { UsageSummaryCard } from "./UsageSummaryCard"
|
||||
import { UsageTrendChart } from "./UsageTrendChart"
|
||||
|
||||
type UsageStatsViewProps = {
|
||||
onDone: () => void
|
||||
}
|
||||
|
||||
export type UsageStatsData = {
|
||||
totalTokensIn: number
|
||||
totalTokensOut: number
|
||||
totalCacheWrites: number
|
||||
totalCacheReads: number
|
||||
totalCost: number
|
||||
totalRequests: number
|
||||
modelStats: Map<
|
||||
string,
|
||||
{
|
||||
requests: number
|
||||
tokensIn: number
|
||||
tokensOut: number
|
||||
cost: number
|
||||
}
|
||||
>
|
||||
dailyStats: Map<
|
||||
string,
|
||||
{
|
||||
tokensIn: number
|
||||
tokensOut: number
|
||||
cacheWrites: number
|
||||
cacheReads: number
|
||||
cost: number
|
||||
}
|
||||
>
|
||||
}
|
||||
|
||||
function parseUsageFromHistoryItem(item: HistoryItem): {
|
||||
tokensIn: number
|
||||
tokensOut: number
|
||||
cacheWrites: number
|
||||
cacheReads: number
|
||||
cost: number
|
||||
modelId: string
|
||||
timestamp: number
|
||||
} {
|
||||
return {
|
||||
tokensIn: item.tokensIn || 0,
|
||||
tokensOut: item.tokensOut || 0,
|
||||
cacheWrites: item.cacheWrites || 0,
|
||||
cacheReads: item.cacheReads || 0,
|
||||
cost: item.totalCost || 0,
|
||||
modelId: item.modelId || "unknown",
|
||||
timestamp: item.ts,
|
||||
}
|
||||
}
|
||||
|
||||
function getDateKey(timestamp: number): string {
|
||||
const date = new Date(timestamp)
|
||||
return `${date.getMonth() + 1}/${date.getDate()}`
|
||||
}
|
||||
|
||||
function getHourKey(timestamp: number): string {
|
||||
const date = new Date(timestamp)
|
||||
return `${date.getHours().toString().padStart(2, "0")}:00`
|
||||
}
|
||||
|
||||
export const UsageStatsView = ({ onDone }: UsageStatsViewProps) => {
|
||||
const { t } = useTranslation("settings")
|
||||
const { environment, taskHistory = [] } = useExtensionState()
|
||||
|
||||
const [timeRange, setTimeRange] = useState<"today" | "week" | "month" | "all">("all")
|
||||
|
||||
const statsData = useMemo((): UsageStatsData => {
|
||||
const now = Date.now()
|
||||
let cutoffTime = 0
|
||||
|
||||
switch (timeRange) {
|
||||
case "today":
|
||||
cutoffTime = new Date().setHours(0, 0, 0, 0)
|
||||
break
|
||||
case "week":
|
||||
cutoffTime = now - 7 * 24 * 60 * 60 * 1000
|
||||
break
|
||||
case "month":
|
||||
cutoffTime = now - 30 * 24 * 60 * 60 * 1000
|
||||
break
|
||||
default:
|
||||
cutoffTime = 0
|
||||
}
|
||||
|
||||
const filteredItems = taskHistory.filter((item: HistoryItem) => item.ts >= cutoffTime)
|
||||
|
||||
let totalTokensIn = 0
|
||||
let totalTokensOut = 0
|
||||
let totalCacheWrites = 0
|
||||
let totalCacheReads = 0
|
||||
let totalCost = 0
|
||||
const totalRequests = filteredItems.length
|
||||
|
||||
const modelStats = new Map<
|
||||
string,
|
||||
{
|
||||
requests: number
|
||||
tokensIn: number
|
||||
tokensOut: number
|
||||
cost: number
|
||||
}
|
||||
>()
|
||||
|
||||
const dailyStats = new Map<
|
||||
string,
|
||||
{
|
||||
tokensIn: number
|
||||
tokensOut: number
|
||||
cacheWrites: number
|
||||
cacheReads: number
|
||||
cost: number
|
||||
}
|
||||
>()
|
||||
|
||||
filteredItems.forEach((item: HistoryItem) => {
|
||||
const usage = parseUsageFromHistoryItem(item)
|
||||
|
||||
totalTokensIn += usage.tokensIn
|
||||
totalTokensOut += usage.tokensOut
|
||||
totalCacheWrites += usage.cacheWrites
|
||||
totalCacheReads += usage.cacheReads
|
||||
totalCost += usage.cost
|
||||
|
||||
// Model stats
|
||||
const modelKey = usage.modelId
|
||||
if (!modelStats.has(modelKey)) {
|
||||
modelStats.set(modelKey, {
|
||||
requests: 0,
|
||||
tokensIn: 0,
|
||||
tokensOut: 0,
|
||||
cost: 0,
|
||||
})
|
||||
}
|
||||
const modelData = modelStats.get(modelKey)!
|
||||
modelData.requests++
|
||||
modelData.tokensIn += usage.tokensIn
|
||||
modelData.tokensOut += usage.tokensOut
|
||||
modelData.cost += usage.cost
|
||||
|
||||
// Daily stats - use different keys based on time range
|
||||
let statKey: string
|
||||
if (timeRange === "today") {
|
||||
statKey = getHourKey(usage.timestamp)
|
||||
} else {
|
||||
statKey = getDateKey(usage.timestamp)
|
||||
}
|
||||
|
||||
if (!dailyStats.has(statKey)) {
|
||||
dailyStats.set(statKey, {
|
||||
tokensIn: 0,
|
||||
tokensOut: 0,
|
||||
cacheWrites: 0,
|
||||
cacheReads: 0,
|
||||
cost: 0,
|
||||
})
|
||||
}
|
||||
const dayData = dailyStats.get(statKey)!
|
||||
dayData.tokensIn += usage.tokensIn
|
||||
dayData.tokensOut += usage.tokensOut
|
||||
dayData.cacheWrites += usage.cacheWrites
|
||||
dayData.cacheReads += usage.cacheReads
|
||||
dayData.cost += usage.cost
|
||||
})
|
||||
|
||||
return {
|
||||
totalTokensIn,
|
||||
totalTokensOut,
|
||||
totalCacheWrites,
|
||||
totalCacheReads,
|
||||
totalCost,
|
||||
totalRequests,
|
||||
modelStats,
|
||||
dailyStats,
|
||||
}
|
||||
}, [taskHistory, timeRange])
|
||||
|
||||
const handleTimeRangeChange = useCallback((range: "today" | "week" | "month" | "all") => {
|
||||
setTimeRange(range)
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<div className="fixed inset-0 flex flex-col overflow-hidden">
|
||||
<ViewHeader environment={environment} onDone={onDone} title={t("usageStats.title")} />
|
||||
<div className="flex-1 overflow-y-auto px-5 pb-6">
|
||||
{/* Time Range Selector */}
|
||||
<div className="flex gap-2 mb-6">
|
||||
{(["today", "week", "month", "all"] as const).map((range) => (
|
||||
<button
|
||||
className={`px-3 py-1.5 rounded text-sm ${
|
||||
timeRange === range
|
||||
? "bg-[var(--vscode-button-background)] text-[var(--vscode-button-foreground)]"
|
||||
: "bg-[var(--vscode-input-background)] text-[var(--vscode-foreground)] hover:bg-[var(--vscode-inputOption-activeBackground)]"
|
||||
}`}
|
||||
key={range}
|
||||
onClick={() => handleTimeRangeChange(range)}>
|
||||
{t(`usageStats.timeRange.${range}`)}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* Summary Cards */}
|
||||
<UsageSummaryCard
|
||||
totalCacheReads={statsData.totalCacheReads}
|
||||
totalCost={statsData.totalCost}
|
||||
totalRequests={statsData.totalRequests}
|
||||
totalTokens={statsData.totalTokensIn + statsData.totalTokensOut}
|
||||
totalTokensIn={statsData.totalTokensIn}
|
||||
totalTokensOut={statsData.totalTokensOut}
|
||||
/>
|
||||
|
||||
{/* Usage Trend Chart */}
|
||||
<UsageTrendChart dailyStats={statsData.dailyStats} timeRange={timeRange} />
|
||||
|
||||
{/* Model Statistics Table */}
|
||||
<ModelStatsTable modelStats={statsData.modelStats} />
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default UsageStatsView
|
||||
@@ -0,0 +1,99 @@
|
||||
import { useTranslation } from "react-i18next"
|
||||
|
||||
type UsageSummaryCardProps = {
|
||||
totalTokens: number
|
||||
totalTokensIn: number
|
||||
totalTokensOut: number
|
||||
totalCacheReads: number
|
||||
totalCost: number
|
||||
totalRequests: number
|
||||
}
|
||||
|
||||
function formatNumber(num: number): string {
|
||||
if (num >= 1000000) {
|
||||
return (num / 1000000).toFixed(2) + "M"
|
||||
}
|
||||
if (num >= 1000) {
|
||||
return (num / 1000).toFixed(1) + "K"
|
||||
}
|
||||
return num.toLocaleString()
|
||||
}
|
||||
|
||||
function formatCost(cost: number): string {
|
||||
return "$" + cost.toFixed(4)
|
||||
}
|
||||
|
||||
export const UsageSummaryCard = ({
|
||||
totalTokens,
|
||||
totalTokensIn,
|
||||
totalTokensOut,
|
||||
totalCacheReads,
|
||||
totalCost,
|
||||
totalRequests,
|
||||
}: UsageSummaryCardProps) => {
|
||||
const { t } = useTranslation("settings")
|
||||
|
||||
// Calculate cache hit rate based on input tokens
|
||||
const cacheHitRate = totalTokensIn > 0 ? (totalCacheReads / totalTokensIn) * 100 : 0
|
||||
|
||||
return (
|
||||
<div className="bg-[var(--vscode-editorWidget-background)] border border-[var(--vscode-widget-border)] rounded-lg p-6 mb-6">
|
||||
{/* Top row: Total tokens + Total requests + Total cost */}
|
||||
<div className="flex items-center gap-4 mb-4">
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="w-10 h-10 rounded-full bg-[var(--vscode-primediffEditor-insertedTextBackground)] flex items-center justify-center">
|
||||
<span className="text-lg">⚡</span>
|
||||
</div>
|
||||
<div>
|
||||
<div className="text-sm text-[var(--vscode-descriptionForeground)]">{t("usageStats.tokensConsumed")}</div>
|
||||
<div className="text-3xl font-bold text-[var(--vscode-foreground)]">{formatNumber(totalTokens)}</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="ml-auto flex items-center gap-6">
|
||||
<div className="text-right">
|
||||
<div className="text-sm text-[var(--vscode-descriptionForeground)]">{t("usageStats.totalRequests")}</div>
|
||||
<div className="text-xl font-semibold text-[var(--vscode-foreground)]">{totalRequests}</div>
|
||||
</div>
|
||||
{/* Divider line */}
|
||||
<div className="h-10 w-px bg-[var(--vscode-widget-border)] opacity-50" />
|
||||
<div className="text-right">
|
||||
<div className="text-sm text-[var(--vscode-descriptionForeground)]">{t("usageStats.totalCost")}</div>
|
||||
<div className="text-xl font-semibold text-[var(--vscode-foreground)]">{formatCost(totalCost)}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Middle row: Input / Output / Cache Hits - three columns */}
|
||||
<div className="grid grid-cols-3 gap-4 mb-4">
|
||||
<div className="bg-[var(--vscode-editor-background)] rounded-lg p-4">
|
||||
<div className="text-sm text-[var(--vscode-descriptionForeground)] mb-1">↓ {t("usageStats.input")}</div>
|
||||
<div className="text-xl font-semibold text-[var(--vscode-foreground)]">{formatNumber(totalTokensIn)}</div>
|
||||
</div>
|
||||
<div className="bg-[var(--vscode-editor-background)] rounded-lg p-4">
|
||||
<div className="text-sm text-[var(--vscode-descriptionForeground)] mb-1">↑ {t("usageStats.output")}</div>
|
||||
<div className="text-xl font-semibold text-[var(--vscode-foreground)]">{formatNumber(totalTokensOut)}</div>
|
||||
</div>
|
||||
<div className="bg-[var(--vscode-editor-background)] rounded-lg p-4">
|
||||
<div className="text-sm text-[var(--vscode-descriptionForeground)] mb-1">🎯 {t("usageStats.cacheHits")}</div>
|
||||
<div className="text-xl font-semibold text-[var(--vscode-foreground)]">{formatNumber(totalCacheReads)}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Cache hit rate bar */}
|
||||
<div>
|
||||
<div className="flex justify-between text-sm mb-1">
|
||||
<span className="text-[var(--vscode-descriptionForeground)]">{t("usageStats.cacheHitRate")}</span>
|
||||
<span className="text-[var(--vscode-foreground)] font-medium">{cacheHitRate.toFixed(1)}%</span>
|
||||
</div>
|
||||
<div className="h-2.5 bg-[var(--vscode-editor-background)] rounded-full overflow-hidden">
|
||||
<div
|
||||
className="h-full bg-green-500 transition-all duration-300"
|
||||
style={{ width: `${Math.min(cacheHitRate, 100)}%` }}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default UsageSummaryCard
|
||||
@@ -0,0 +1,320 @@
|
||||
import { useMemo } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
|
||||
type DailyStats = {
|
||||
tokensIn: number
|
||||
tokensOut: number
|
||||
cacheWrites: number
|
||||
cacheReads: number
|
||||
cost: number
|
||||
}
|
||||
|
||||
type UsageTrendChartProps = {
|
||||
dailyStats: Map<string, DailyStats>
|
||||
timeRange: "today" | "week" | "month" | "all"
|
||||
}
|
||||
|
||||
function formatNumber(num: number): string {
|
||||
if (num >= 1000000) {
|
||||
return (num / 1000000).toFixed(1) + "M"
|
||||
}
|
||||
if (num >= 1000) {
|
||||
return (num / 1000).toFixed(1) + "K"
|
||||
}
|
||||
return num.toString()
|
||||
}
|
||||
|
||||
// Generate X-axis labels based on time range
|
||||
function generateXAxisLabels(timeRange: "today" | "week" | "month" | "all"): string[] {
|
||||
const now = new Date()
|
||||
|
||||
switch (timeRange) {
|
||||
case "today": {
|
||||
// 12 hour marks, every 2 hours
|
||||
const labels: string[] = []
|
||||
for (let i = 0; i < 12; i++) {
|
||||
const hour = i * 2
|
||||
labels.push(`${hour.toString().padStart(2, "0")}:00`)
|
||||
}
|
||||
return labels
|
||||
}
|
||||
case "week": {
|
||||
// 7 days, one mark per day
|
||||
const labels: string[] = []
|
||||
for (let i = 6; i >= 0; i--) {
|
||||
const date = new Date(now)
|
||||
date.setDate(date.getDate() - i)
|
||||
labels.push(`${date.getMonth() + 1}/${date.getDate()}`)
|
||||
}
|
||||
return labels
|
||||
}
|
||||
case "month":
|
||||
case "all": {
|
||||
// 10 marks, every 3 days
|
||||
const labels: string[] = []
|
||||
for (let i = 9; i >= 0; i--) {
|
||||
const date = new Date(now)
|
||||
date.setDate(date.getDate() - i * 3)
|
||||
labels.push(`${date.getMonth() + 1}/${date.getDate()}`)
|
||||
}
|
||||
return labels
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get the number of tick positions for the chart
|
||||
function getTickCount(timeRange: "today" | "week" | "month" | "all"): number {
|
||||
switch (timeRange) {
|
||||
case "today":
|
||||
return 12
|
||||
case "week":
|
||||
return 7
|
||||
case "month":
|
||||
case "all":
|
||||
return 10
|
||||
}
|
||||
}
|
||||
|
||||
export const UsageTrendChart = ({ dailyStats, timeRange }: UsageTrendChartProps) => {
|
||||
const { t } = useTranslation("settings")
|
||||
|
||||
const xAxisLabels = useMemo(() => generateXAxisLabels(timeRange), [timeRange])
|
||||
const tickCount = useMemo(() => getTickCount(timeRange), [timeRange])
|
||||
|
||||
const chartData = useMemo(() => {
|
||||
const entries = Array.from(dailyStats.entries())
|
||||
if (entries.length === 0) return []
|
||||
|
||||
// Sort by date
|
||||
entries.sort((a, b) => {
|
||||
// Handle hour format for "today"
|
||||
if (timeRange === "today") {
|
||||
return a[0].localeCompare(b[0])
|
||||
}
|
||||
const [aMonth, aDay] = a[0].split("/").map(Number)
|
||||
const [bMonth, bDay] = b[0].split("/").map(Number)
|
||||
if (aMonth !== bMonth) return aMonth - bMonth
|
||||
return aDay - bDay
|
||||
})
|
||||
|
||||
return entries.map(([date, stats]) => ({
|
||||
date,
|
||||
...stats,
|
||||
}))
|
||||
}, [dailyStats, timeRange])
|
||||
|
||||
const maxValue = useMemo(() => {
|
||||
if (chartData.length === 0) return 1000000
|
||||
return Math.max(...chartData.map((d) => d.tokensIn + d.tokensOut + d.cacheReads)) * 1.1 || 1000000
|
||||
}, [chartData])
|
||||
|
||||
const maxCost = useMemo(() => {
|
||||
if (chartData.length === 0) return 1
|
||||
return Math.max(...chartData.map((d) => d.cost)) * 1.1 || 1
|
||||
}, [chartData])
|
||||
|
||||
if (chartData.length === 0) {
|
||||
return (
|
||||
<div className="bg-[var(--vscode-editorWidget-background)] border border-[var(--vscode-widget-border)] rounded-lg p-6 mb-6">
|
||||
<h3 className="text-lg font-semibold text-[var(--vscode-foreground)] mb-4">{t("usageStats.usageTrend")}</h3>
|
||||
<div className="text-center text-[var(--vscode-descriptionForeground)] py-8">{t("usageStats.noData")}</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="bg-[var(--vscode-editorWidget-background)] border border-[var(--vscode-widget-border)] rounded-lg p-6 mb-6">
|
||||
<h3 className="text-lg font-semibold text-[var(--vscode-foreground)] mb-4">{t("usageStats.usageTrend")}</h3>
|
||||
|
||||
<div className="w-full" style={{ height: "220px" }}>
|
||||
<svg className="w-full h-full" preserveAspectRatio="xMidYMid meet" viewBox="0 0 500 200">
|
||||
{/* Chart area: x from 50 to 450, y from 10 to 150 */}
|
||||
{/* Grid lines */}
|
||||
{[0.25, 0.5, 0.75, 1].map((ratio) => (
|
||||
<line
|
||||
key={`grid-${ratio}`}
|
||||
stroke="var(--vscode-widget-border, #444)"
|
||||
strokeDasharray="4,4"
|
||||
strokeWidth="0.5"
|
||||
x1="50"
|
||||
x2="450"
|
||||
y1={150 - ratio * 140}
|
||||
y2={150 - ratio * 140}
|
||||
/>
|
||||
))}
|
||||
|
||||
{/* Y-axis labels (tokens) */}
|
||||
<text fill="var(--vscode-descriptionForeground)" fontSize="9" textAnchor="end" x="45" y="12">
|
||||
{formatNumber(maxValue)}
|
||||
</text>
|
||||
<text fill="var(--vscode-descriptionForeground)" fontSize="9" textAnchor="end" x="45" y="47">
|
||||
{formatNumber(maxValue * 0.75)}
|
||||
</text>
|
||||
<text fill="var(--vscode-descriptionForeground)" fontSize="9" textAnchor="end" x="45" y="82">
|
||||
{formatNumber(maxValue * 0.5)}
|
||||
</text>
|
||||
<text fill="var(--vscode-descriptionForeground)" fontSize="9" textAnchor="end" x="45" y="117">
|
||||
{formatNumber(maxValue * 0.25)}
|
||||
</text>
|
||||
<text fill="var(--vscode-descriptionForeground)" fontSize="9" textAnchor="end" x="45" y="152">
|
||||
0
|
||||
</text>
|
||||
|
||||
{/* Y-axis labels (cost) */}
|
||||
<text fill="var(--vscode-descriptionForeground)" fontSize="9" textAnchor="start" x="455" y="12">
|
||||
${maxCost.toFixed(2)}
|
||||
</text>
|
||||
<text fill="var(--vscode-descriptionForeground)" fontSize="9" textAnchor="start" x="455" y="47">
|
||||
${(maxCost * 0.75).toFixed(2)}
|
||||
</text>
|
||||
<text fill="var(--vscode-descriptionForeground)" fontSize="9" textAnchor="start" x="455" y="82">
|
||||
${(maxCost * 0.5).toFixed(2)}
|
||||
</text>
|
||||
<text fill="var(--vscode-descriptionForeground)" fontSize="9" textAnchor="start" x="455" y="117">
|
||||
${(maxCost * 0.25).toFixed(2)}
|
||||
</text>
|
||||
<text fill="var(--vscode-descriptionForeground)" fontSize="9" textAnchor="start" x="455" y="152">
|
||||
$0
|
||||
</text>
|
||||
|
||||
{/* Axes */}
|
||||
<line stroke="var(--vscode-widget-border, #444)" strokeWidth="1" x1="50" x2="450" y1="150" y2="150" />
|
||||
<line stroke="var(--vscode-widget-border, #444)" strokeWidth="1" x1="50" x2="50" y1="10" y2="150" />
|
||||
|
||||
{/* X-axis tick marks and labels */}
|
||||
{xAxisLabels.map((label, i) => {
|
||||
const x = 50 + (i / (xAxisLabels.length - 1)) * 400
|
||||
return (
|
||||
<g key={`tick-${i}`}>
|
||||
<line
|
||||
stroke="var(--vscode-widget-border, #444)"
|
||||
strokeWidth="1"
|
||||
x1={x}
|
||||
x2={x}
|
||||
y1="150"
|
||||
y2="155"
|
||||
/>
|
||||
<text fill="var(--vscode-descriptionForeground)" fontSize="8" textAnchor="middle" x={x} y="168">
|
||||
{label}
|
||||
</text>
|
||||
</g>
|
||||
)
|
||||
})}
|
||||
|
||||
{/* Data lines */}
|
||||
{chartData.map((d, i) => {
|
||||
// Find the closest tick position for this data point
|
||||
const x = 50 + (i / (chartData.length - 1 || 1)) * 400
|
||||
const inputY = 150 - (d.tokensIn / maxValue) * 140
|
||||
const outputY = 150 - (d.tokensOut / maxValue) * 140
|
||||
const cacheReadsY = 150 - (d.cacheReads / maxValue) * 140
|
||||
const costY = 150 - (d.cost / maxCost) * 140
|
||||
|
||||
return (
|
||||
<g key={i}>
|
||||
{/* Input tokens line */}
|
||||
{i > 0 &&
|
||||
(() => {
|
||||
const prevX = 50 + ((i - 1) / (chartData.length - 1)) * 400
|
||||
const prevInputY = 150 - (chartData[i - 1].tokensIn / maxValue) * 140
|
||||
return (
|
||||
<line
|
||||
fill="none"
|
||||
stroke="#3b82f6"
|
||||
strokeWidth="2"
|
||||
x1={prevX}
|
||||
x2={x}
|
||||
y1={prevInputY}
|
||||
y2={inputY}
|
||||
/>
|
||||
)
|
||||
})()}
|
||||
<circle cx={x} cy={inputY} fill="#3b82f6" r="3" />
|
||||
|
||||
{/* Output tokens line */}
|
||||
{i > 0 &&
|
||||
(() => {
|
||||
const prevX = 50 + ((i - 1) / (chartData.length - 1)) * 400
|
||||
const prevOutputY = 150 - (chartData[i - 1].tokensOut / maxValue) * 140
|
||||
return (
|
||||
<line
|
||||
fill="none"
|
||||
stroke="#22c55e"
|
||||
strokeWidth="2"
|
||||
x1={prevX}
|
||||
x2={x}
|
||||
y1={prevOutputY}
|
||||
y2={outputY}
|
||||
/>
|
||||
)
|
||||
})()}
|
||||
<circle cx={x} cy={outputY} fill="#22c55e" r="3" />
|
||||
|
||||
{/* Cache reads line */}
|
||||
{i > 0 &&
|
||||
(() => {
|
||||
const prevX = 50 + ((i - 1) / (chartData.length - 1)) * 400
|
||||
const prevCacheReadsY = 150 - (chartData[i - 1].cacheReads / maxValue) * 140
|
||||
return (
|
||||
<line
|
||||
fill="none"
|
||||
stroke="#8b5cf6"
|
||||
strokeDasharray="6,3"
|
||||
strokeWidth="2"
|
||||
x1={prevX}
|
||||
x2={x}
|
||||
y1={prevCacheReadsY}
|
||||
y2={cacheReadsY}
|
||||
/>
|
||||
)
|
||||
})()}
|
||||
<circle cx={x} cy={cacheReadsY} fill="#8b5cf6" r="3" />
|
||||
|
||||
{/* Cost line */}
|
||||
{i > 0 &&
|
||||
(() => {
|
||||
const prevX = 50 + ((i - 1) / (chartData.length - 1)) * 400
|
||||
const prevCostY = 150 - (chartData[i - 1].cost / maxCost) * 140
|
||||
return (
|
||||
<line
|
||||
fill="none"
|
||||
stroke="#ef4444"
|
||||
strokeWidth="2"
|
||||
x1={prevX}
|
||||
x2={x}
|
||||
y1={prevCostY}
|
||||
y2={costY}
|
||||
/>
|
||||
)
|
||||
})()}
|
||||
<circle cx={x} cy={costY} fill="#ef4444" r="3" />
|
||||
</g>
|
||||
)
|
||||
})}
|
||||
</svg>
|
||||
</div>
|
||||
|
||||
{/* Legend */}
|
||||
<div className="flex flex-wrap justify-center gap-4 mt-4 text-sm">
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="w-4 h-0.5 bg-[#3b82f6]" />
|
||||
<span className="text-[var(--vscode-foreground)]">{t("usageStats.input")}</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="w-4 h-0.5 bg-[#22c55e]" />
|
||||
<span className="text-[var(--vscode-foreground)]">{t("usageStats.output")}</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="w-4 h-0.5 bg-[#8b5cf6]" style={{ borderTop: "2px dashed #8b5cf6", height: 0 }} />
|
||||
<span className="text-[var(--vscode-foreground)]">{t("usageStats.cacheHits")}</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="w-4 h-0.5 bg-[#ef4444]" />
|
||||
<span className="text-[var(--vscode-foreground)]">{t("usageStats.cost")}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default UsageTrendChart
|
||||
@@ -0,0 +1,4 @@
|
||||
export { ModelStatsTable } from "./ModelStatsTable"
|
||||
export { UsageStatsView } from "./UsageStatsView"
|
||||
export { UsageSummaryCard } from "./UsageSummaryCard"
|
||||
export { UsageTrendChart } from "./UsageTrendChart"
|
||||
Reference in new issue
Block a user