%PDF- %PDF-
| Direktori : /home2/rezakur1/ai.rezakurnia.com/zh-router-frontend/src/components/dashboard/ |
| Current File : /home2/rezakur1/ai.rezakurnia.com/zh-router-frontend/src/components/dashboard/UsageChart.tsx |
import {
Area,
AreaChart,
CartesianGrid,
ResponsiveContainer,
Tooltip,
XAxis,
YAxis,
} from "recharts";
import type { DailyUsage } from "@/types/api";
import { formatCompact, formatShortDate } from "@/lib/format";
function ChartTooltip({ active, payload }: any) {
if (!active || !payload?.length) return null;
const row = payload[0].payload as DailyUsage;
return (
<div className="rounded-xl border border-white/[0.08] bg-ink-900 px-3.5 py-2.5 text-xs shadow-modal">
<p className="mb-1.5 font-semibold text-white">
{formatShortDate(row.date)}
</p>
<div className="space-y-1">
<p className="flex justify-between gap-6 text-slate-400">
Requests{" "}
<span className="font-semibold tabular-nums text-white">
{row.requests}
</span>
</p>
<p className="flex justify-between gap-6 text-slate-400">
Input token{" "}
<span className="font-semibold tabular-nums text-white">
{formatCompact(row.input_tokens)}
</span>
</p>
<p className="flex justify-between gap-6 text-slate-400">
Cached token{" "}
<span className="font-semibold tabular-nums text-white">
{formatCompact(row.cached_tokens)}
</span>
</p>
<p className="flex justify-between gap-6 text-slate-400">
Output token{" "}
<span className="font-semibold tabular-nums text-white">
{formatCompact(row.output_tokens)}
</span>
</p>
</div>
</div>
);
}
const legendDot = (color: string) => (
<span
className="inline-block h-2 w-2 rounded-full"
style={{ backgroundColor: color }}
/>
);
export default function UsageChart({ data }: { data: DailyUsage[] }) {
if (data.length === 0) {
return (
<div className="flex h-64 items-center justify-center text-sm text-slate-500">
Belum ada data penggunaan pada rentang ini.
</div>
);
}
return (
<div>
<div className="mb-4 flex items-center gap-5 text-xs text-slate-400">
<span className="flex items-center gap-2">
{legendDot("#3b82f6")} Requests
</span>
<span className="flex items-center gap-2">
{legendDot("#60a5fa")} Output token
</span>
</div>
<div className="h-64 w-full">
<ResponsiveContainer width="100%" height="100%">
<AreaChart data={data} margin={{ top: 4, right: 4, left: -12, bottom: 0 }}>
<defs>
<linearGradient id="gRequests" x1="0" y1="0" x2="0" y2="1">
<stop offset="0%" stopColor="#3b82f6" stopOpacity={0.28} />
<stop offset="100%" stopColor="#3b82f6" stopOpacity={0} />
</linearGradient>
<linearGradient id="gTokens" x1="0" y1="0" x2="0" y2="1">
<stop offset="0%" stopColor="#60a5fa" stopOpacity={0.18} />
<stop offset="100%" stopColor="#60a5fa" stopOpacity={0} />
</linearGradient>
</defs>
<CartesianGrid
strokeDasharray="3 3"
stroke="rgba(255,255,255,0.05)"
vertical={false}
/>
<XAxis
dataKey="date"
tickFormatter={formatShortDate}
tick={{ fill: "#475569", fontSize: 11 }}
axisLine={false}
tickLine={false}
minTickGap={24}
/>
<YAxis
tick={{ fill: "#475569", fontSize: 11 }}
tickFormatter={formatCompact}
axisLine={false}
tickLine={false}
/>
<Tooltip
content={<ChartTooltip />}
cursor={{ stroke: "rgba(255,255,255,0.1)" }}
/>
<Area
type="monotone"
dataKey="requests"
stroke="#3b82f6"
strokeWidth={2}
fill="url(#gRequests)"
name="Requests"
/>
<Area
type="monotone"
dataKey="output_tokens"
stroke="#60a5fa"
strokeWidth={1.5}
fill="url(#gTokens)"
name="Output token"
/>
</AreaChart>
</ResponsiveContainer>
</div>
</div>
);
}