File size: 6,520 Bytes
1798222
 
 
 
 
 
 
 
 
 
 
 
 
2b62b0c
1798222
 
 
 
2b62b0c
1798222
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { useState } from "react";
import {
  AreaChart,
  Area,
  XAxis,
  YAxis,
  CartesianGrid,
  Tooltip,
  ResponsiveContainer,
  Legend,
} from "recharts";
import { useQuery } from "@tanstack/react-query";
import { trpc } from "@/utils/trpc";
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@pram/ui/components/card";
import {
  Tabs,
  TabsList,
  TabsTrigger,
} from "@pram/ui/components/tabs";
import { MaterialIcon } from "@/components/ui/MaterialIcon";

type Period = "daily" | "weekly" | "monthly";

interface ChartDataPoint {
  date: string;
  label: string;
  totalTokens: number;
  jobCount: number;
}

function formatTokenValue(value: number): string {
  if (value >= 1_000_000) return `${(value / 1_000_000).toFixed(1)}M`;
  if (value >= 1_000) return `${(value / 1_000).toFixed(1)}K`;
  return value.toString();
}

export function TokenUsageChart() {
  const [period, setPeriod] = useState<Period>("daily");

  const { data, isLoading } = useQuery(
    trpc.ai.tokenUsageHistory.queryOptions({ period }),
  );

  const chartData: ChartDataPoint[] = (data ?? []).map((d) => ({
    date: d.date,
    label: d.label,
    totalTokens: d.totalTokens,
    jobCount: d.jobCount,
  }));

  const hasData = chartData.some((d) => d.totalTokens > 0 || d.jobCount > 0);

  return (
    <Card className="clay-shadow bg-[var(--pure-white)] border-2 border-[var(--oat-border)] rounded-[var(--radius-xl)]">
      <CardHeader>
        <div className="flex items-center justify-between">
          <div className="flex items-center gap-3">
            <MaterialIcon name="show_chart" className="text-xl" />
            <div>
              <CardTitle className="font-headline text-[var(--clay-black)]">
                Tren Penggunaan Token
              </CardTitle>
              <CardDescription className="text-[var(--warm-charcoal)]">
                {period === "daily" && "7 hari terakhir"}
                {period === "weekly" && "4 minggu terakhir"}
                {period === "monthly" && "6 bulan terakhir"}
              </CardDescription>
            </div>
          </div>
          <Tabs
            value={period}
            onValueChange={(v: string | null) => setPeriod((v as Period) ?? "daily")}
          >
            <TabsList variant="line" className="h-8">
              <TabsTrigger value="daily" className="text-xs px-3">Harian</TabsTrigger>
              <TabsTrigger value="weekly" className="text-xs px-3">Mingguan</TabsTrigger>
              <TabsTrigger value="monthly" className="text-xs px-3">Bulanan</TabsTrigger>
            </TabsList>
          </Tabs>
        </div>
      </CardHeader>
      <CardContent>
        {isLoading ? (
          <div className="h-64 bg-[var(--oat-light)] animate-pulse rounded-[var(--radius-lg)]" />
        ) : !hasData ? (
          <div className="h-64 flex flex-col items-center justify-center text-[var(--warm-charcoal)]">
            <MaterialIcon name="receipt_long" className="text-4xl text-[var(--warm-silver)] mb-3" />
            <p className="font-semibold">Belum ada data penggunaan</p>
            <p className="text-xs text-[var(--warm-silver)] mt-1">
              Generate soal untuk melihat statistik penggunaan token.
            </p>
          </div>
        ) : (
          <ResponsiveContainer width="100%" height={256}>
            <AreaChart data={chartData} margin={{ top: 5, right: 10, left: -10, bottom: 0 }}>
              <defs>
                <linearGradient id="tokenGradient" x1="0" y1="0" x2="0" y2="1">
                  <stop offset="5%" stopColor="var(--chart-1)" stopOpacity={0.3} />
                  <stop offset="95%" stopColor="var(--chart-1)" stopOpacity={0} />
                </linearGradient>
                <linearGradient id="jobGradient" x1="0" y1="0" x2="0" y2="1">
                  <stop offset="5%" stopColor="var(--chart-2)" stopOpacity={0.3} />
                  <stop offset="95%" stopColor="var(--chart-2)" stopOpacity={0} />
                </linearGradient>
              </defs>
              <CartesianGrid strokeDasharray="3 3" stroke="var(--oat-border)" />
              <XAxis
                dataKey="label"
                tick={{ fontSize: 12, fill: "var(--warm-charcoal)" }}
                axisLine={{ stroke: "var(--oat-border)" }}
                tickLine={false}
              />
              <YAxis
                yAxisId="left"
                tick={{ fontSize: 12, fill: "var(--warm-charcoal)" }}
                axisLine={false}
                tickLine={false}
                tickFormatter={formatTokenValue}
              />
              <YAxis
                yAxisId="right"
                orientation="right"
                tick={{ fontSize: 12, fill: "var(--warm-charcoal)" }}
                axisLine={false}
                tickLine={false}
              />
              <Tooltip
                contentStyle={{
                  backgroundColor: "var(--pure-white)",
                  border: "2px solid var(--oat-border)",
                  borderRadius: "var(--radius-lg)",
                  fontSize: 13,
                }}
                formatter={(value: unknown, name: unknown) => {
                  const num = typeof value === "number" ? value : 0;
                  const n = typeof name === "string" ? name : "";
                  if (n === "totalTokens") return [num.toLocaleString("id-ID"), "Token"];
                  if (n === "jobCount") return [num, "Job"];
                  return [num, n];
                }}
              />
              <Legend
                wrapperStyle={{ fontSize: 12, paddingTop: 8 }}
                formatter={(v: string) => {
                  if (v === "totalTokens") return "Token";
                  if (v === "jobCount") return "Job";
                  return v;
                }}
              />
              <Area
                yAxisId="left"
                type="monotone"
                dataKey="totalTokens"
                stroke="var(--chart-1)"
                strokeWidth={2}
                fill="url(#tokenGradient)"
                name="totalTokens"
              />
              <Area
                yAxisId="right"
                type="monotone"
                dataKey="jobCount"
                stroke="var(--chart-2)"
                strokeWidth={2}
                fill="url(#jobGradient)"
                name="jobCount"
              />
            </AreaChart>
          </ResponsiveContainer>
        )}
      </CardContent>
    </Card>
  );
}