- Fix scrambled text when typing in Base URL fields: debounced input hook now ignores echo round-trips of its own saves and defers unexpected external updates during an edit grace period - Serialize API configuration saves through a queue so rapid successive updates are applied in order on the backend - Fix Base URL input collapsing to tiny width: Radix TooltipTrigger now renders a full-width block wrapper instead of the default shrink-to-fit button - Trim Base URL values only on commit instead of every keystroke to prevent cursor jumps - Skip redundant save on mount and flush uncommitted edits on unmount - Bump version to 4.3.1, update CHANGELOG and README
278 lines
8.9 KiB
Plaintext
278 lines
8.9 KiB
Plaintext
import { useMemo } from "react"
|
|
import { useTranslation } from "react-i18next"
|
|
import type { UsageTimeRange } from "@shared/usage-stats"
|
|
|
|
type TrendBucket = {
|
|
key: string
|
|
label: string
|
|
tokensIn: number
|
|
tokensOut: number
|
|
cacheWrites: number
|
|
cacheReads: number
|
|
cost: number
|
|
}
|
|
|
|
type UsageTrendChartProps = {
|
|
trend: TrendBucket[]
|
|
timeRange: UsageTimeRange
|
|
isLoading?: boolean
|
|
}
|
|
|
|
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()
|
|
}
|
|
|
|
export const UsageTrendChart = ({ trend, timeRange, isLoading }: UsageTrendChartProps) => {
|
|
const { t } = useTranslation("settings")
|
|
|
|
const chartData = trend
|
|
|
|
// Pick calendar-aligned tick labels so the x-axis matches real dates/hours
|
|
const xAxisLabels = useMemo(() => {
|
|
if (chartData.length === 0) {
|
|
return []
|
|
}
|
|
if (timeRange === "today") {
|
|
// every 2 hours
|
|
return chartData.filter((_, i) => i % 2 === 0).map((d) => d.label)
|
|
}
|
|
const total = chartData.length
|
|
if (total <= 7) {
|
|
return chartData.map((d) => d.label)
|
|
}
|
|
// ~10 evenly spaced calendar ticks
|
|
const step = Math.max(1, Math.ceil(total / 10))
|
|
const labels: string[] = []
|
|
for (let i = 0; i < total; i += step) {
|
|
labels.push(chartData[i].label)
|
|
}
|
|
return labels
|
|
}, [chartData, 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 (isLoading || 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">
|
|
{isLoading ? t("usageStats.loading") : 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 (calendar-aligned) */}
|
|
{xAxisLabels.map((label, i) => {
|
|
const step = timeRange === "today" ? 2 : Math.max(1, Math.ceil(chartData.length / 10))
|
|
const dataIndex = timeRange === "today" ? i * 2 : i * step
|
|
if (dataIndex >= chartData.length) {
|
|
return null
|
|
}
|
|
const x = 50 + (dataIndex / (chartData.length - 1)) * 400
|
|
return (
|
|
<g key={`tick-${label}-${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 - each data point sits exactly on its calendar bucket */}
|
|
{chartData.map((d, i) => {
|
|
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={d.key}>
|
|
{/* 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 |