summaryrefslogtreecommitdiffhomepage
path: root/packages/frontend/src/lib/components/KeyUsage.svelte
blob: 7f0453e4ee25d1ba54c807f478a2003ae4618220 (plain)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<script lang="ts">
	import type { KeyInfo, KeyUsageData, UsageBucket } from "../types.js";

	const {
		keys = [],
		apiBase = "",
	}: {
		keys?: KeyInfo[];
		apiBase?: string;
	} = $props();

	interface KeyUsageEntry {
		keyId: string;
		provider: string;
		data: KeyUsageData | null;
		error: string | null;
	}

	let entries = $state<KeyUsageEntry[]>([]);
	let loading = $state(true);

	async function fetchAll() {
		loading = true;
		const results: KeyUsageEntry[] = [];

		for (const key of keys) {
			try {
				const res = await fetch(
					`${apiBase}/models/key-usage?keyId=${encodeURIComponent(key.id)}`,
				);
				if (!res.ok) {
					const data = await res.json().catch(() => ({}));
					results.push({
						keyId: key.id,
						provider: key.provider,
						data: null,
						error: data.error ?? `HTTP ${res.status}`,
					});
				} else {
					results.push({
						keyId: key.id,
						provider: key.provider,
						data: await res.json(),
						error: null,
					});
				}
			} catch (e) {
				results.push({
					keyId: key.id,
					provider: key.provider,
					data: null,
					error: e instanceof Error ? e.message : "Failed to fetch",
				});
			}
		}

		entries = results;
		loading = false;
	}

	$effect(() => {
		fetchAll();
	});

	function progressClass(utilization: number): string {
		if (utilization > 0.8) return "progress-error";
		if (utilization >= 0.5) return "progress-warning";
		return "progress-success";
	}

	function formatDate(ts: number): string {
		return new Date(ts).toLocaleString();
	}

	function hasBucketData(bucket: UsageBucket | undefined): boolean {
		return bucket !== undefined && bucket.utilization !== undefined;
	}
</script>

<div class="flex flex-col gap-3">
	{#if keys.length === 0}
		<p class="text-xs text-base-content/50">No keys available.</p>
	{:else if loading}
		<div class="flex items-center gap-2 py-2">
			<span class="loading loading-spinner loading-sm"></span>
			<span class="text-xs text-base-content/50">Loading usage data...</span>
		</div>
	{:else}
		<div class="flex flex-col gap-3 max-h-96 overflow-y-auto">
			{#each entries as entry (entry.keyId)}
				<div
					class="bg-base-200 rounded-lg p-2"
				>
					<div class="flex items-center gap-1.5 mb-1.5">
						<span class="text-xs font-semibold">{entry.keyId}</span>
						<span class="badge badge-xs badge-ghost">{entry.provider}</span>
					</div>

					{#if entry.error}
						<div role="alert" class="text-xs text-error/80">{entry.error}</div>

					{:else if !entry.data}
						<p class="text-xs text-base-content/50">No data.</p>

					{:else if entry.data.provider === "anthropic"}
						<!-- Render each Claude account -->
						{#if entry.data.accounts}
							{#each entry.data.accounts as acct (acct.source)}
								<div class="flex flex-col gap-1 pl-1">
									<div class="flex items-center gap-1">
										<span class="text-xs font-medium">{acct.label}</span>
										{#if acct.subscriptionType}
											<span class="badge badge-xs">{acct.subscriptionType}</span>
										{/if}
									</div>

									{#if acct.error}
										<p class="text-xs text-error/70">{acct.error}</p>
									{/if}

									{#if hasBucketData(acct.fiveHour)}
										{@const b = acct.fiveHour!}
										{@const u = b.utilization ?? 0}
										{@const p = Math.round(u * 100)}
										<div class="flex flex-col gap-0.5">
											<div class="flex items-center justify-between">
												<span class="text-xs text-base-content/50">5-Hour</span>
												<span class="text-xs font-mono">{p}%</span>
											</div>
											<progress class="progress w-full h-2 {progressClass(u)}" value={p} max="100"></progress>
											{#if b.resetsAt}
												<span class="text-xs text-base-content/40">Resets: {formatDate(b.resetsAt)}</span>
											{/if}
										</div>
									{/if}

									{#if hasBucketData(acct.sevenDay)}
										{@const b = acct.sevenDay!}
										{@const u = b.utilization ?? 0}
										{@const p = Math.round(u * 100)}
										<div class="flex flex-col gap-0.5">
											<div class="flex items-center justify-between">
												<span class="text-xs text-base-content/50">Weekly</span>
												<span class="text-xs font-mono">{p}%</span>
											</div>
											<progress class="progress w-full h-2 {progressClass(u)}" value={p} max="100"></progress>
											{#if b.resetsAt}
												<span class="text-xs text-base-content/40">Resets: {formatDate(b.resetsAt)}</span>
											{/if}
										</div>
									{/if}
								</div>
							{/each}
						{/if}

					{:else if entry.data.provider === "opencode-go"}
						{#if entry.data.unavailable}
							<p class="text-xs text-base-content/70">Check console for usage.</p>
							{#if entry.data.consoleUrl}
								<a href={entry.data.consoleUrl} target="_blank" rel="noopener noreferrer" class="link link-primary text-xs">
									Open console
								</a>
							{/if}
						{:else}
							{#if hasBucketData(entry.data.fiveHour)}
								{@const b = entry.data.fiveHour!}
								{@const u = b.utilization ?? 0}
								{@const p = Math.round(u * 100)}
								<div class="flex flex-col gap-0.5">
									<div class="flex items-center justify-between">
										<span class="text-xs text-base-content/50">5-Hour</span>
										<span class="text-xs font-mono">{p}%</span>
									</div>
									<progress class="progress w-full h-2 {progressClass(u)}" value={p} max="100"></progress>
									{#if b.resetsAt}
										<span class="text-xs text-base-content/40">Resets: {formatDate(b.resetsAt)}</span>
									{/if}
								</div>
							{/if}
							{#if hasBucketData(entry.data.weekly)}
								{@const b = entry.data.weekly!}
								{@const u = b.utilization ?? 0}
								{@const p = Math.round(u * 100)}
								<div class="flex flex-col gap-0.5">
									<div class="flex items-center justify-between">
										<span class="text-xs text-base-content/50">Weekly</span>
										<span class="text-xs font-mono">{p}%</span>
									</div>
									<progress class="progress w-full h-2 {progressClass(u)}" value={p} max="100"></progress>
									{#if b.resetsAt}
										<span class="text-xs text-base-content/40">Resets: {formatDate(b.resetsAt)}</span>
									{/if}
								</div>
							{/if}
							{#if hasBucketData(entry.data.monthly)}
								{@const b = entry.data.monthly!}
								{@const u = b.utilization ?? 0}
								{@const p = Math.round(u * 100)}
								<div class="flex flex-col gap-0.5">
									<div class="flex items-center justify-between">
										<span class="text-xs text-base-content/50">Monthly</span>
										<span class="text-xs font-mono">{p}%</span>
									</div>
									<progress class="progress w-full h-2 {progressClass(u)}" value={p} max="100"></progress>
									{#if b.resetsAt}
										<span class="text-xs text-base-content/40">Resets: {formatDate(b.resetsAt)}</span>
									{/if}
								</div>
							{/if}
						{/if}

					{:else if entry.data.provider === "github-copilot"}
						{@const p = Math.round(entry.data.percentUsed ?? 0)}
						<div class="flex flex-col gap-0.5 pl-1">
							<div class="flex items-center justify-between">
								<span class="text-xs text-base-content/50">
									{#if entry.data.tokensConsumed !== undefined && entry.data.tokensRemaining !== undefined}
										{entry.data.tokensConsumed.toLocaleString()} / {(entry.data.tokensConsumed + entry.data.tokensRemaining).toLocaleString()} tokens
									{:else if entry.data.plan}
										{entry.data.plan}
									{:else}
										Usage
									{/if}
								</span>
								<span class="text-xs font-mono">{p}%</span>
							</div>
							<progress class="progress w-full h-2 {progressClass(p / 100)}" value={p} max="100"></progress>
							{#if entry.data.resetAt}
								<span class="text-xs text-base-content/40">Resets: {formatDate(entry.data.resetAt)}</span>
							{/if}
						</div>
					{/if}
				</div>
			{/each}
		</div>
	{/if}
</div>