refactor(console): simplify go usage response

This commit is contained in:
vimtor
2026-08-11 23:18:43 +02:00
parent 561afb401a
commit d470434746
@@ -31,7 +31,6 @@ export async function GET(input: APIEvent) {
const auth = await Database.use((tx) => const auth = await Database.use((tx) =>
tx tx
.select({ .select({
lite: BillingTable.lite,
userID: KeyTable.userID, userID: KeyTable.userID,
workspaceID: KeyTable.workspaceID, workspaceID: KeyTable.workspaceID,
}) })
@@ -115,24 +114,31 @@ export async function GET(input: APIEvent) {
return new Response( return new Response(
JSON.stringify({ JSON.stringify({
useBalance: auth.lite?.useBalance ?? false, usage: {
rollingUsage: Subscription.analyzeRollingUsage({ rolling: formatUsage(
limit: limits.rollingLimit, Subscription.analyzeRollingUsage({
window: limits.rollingWindow, limit: limits.rollingLimit,
usage: row.rollingUsage ?? 0, window: limits.rollingWindow,
timeUpdated: row.timeRollingUpdated ?? new Date(), usage: row.rollingUsage ?? 0,
}), timeUpdated: row.timeRollingUpdated ?? new Date(),
weeklyUsage: Subscription.analyzeWeeklyUsage({ }),
limit: limits.weeklyLimit, ),
usage: row.weeklyUsage ?? 0, weekly: formatUsage(
timeUpdated: row.timeWeeklyUpdated ?? new Date(), Subscription.analyzeWeeklyUsage({
}), limit: limits.weeklyLimit,
monthlyUsage: Subscription.analyzeMonthlyUsage({ usage: row.weeklyUsage ?? 0,
limit: limits.monthlyLimit, timeUpdated: row.timeWeeklyUpdated ?? new Date(),
usage: row.monthlyUsage ?? 0, }),
timeUpdated: row.timeMonthlyUpdated ?? new Date(), ),
timeSubscribed: row.timeCreated, monthly: formatUsage(
}), Subscription.analyzeMonthlyUsage({
limit: limits.monthlyLimit,
usage: row.monthlyUsage ?? 0,
timeUpdated: row.timeMonthlyUpdated ?? new Date(),
timeSubscribed: row.timeCreated,
}),
),
},
}), }),
{ {
status: 200, status: 200,
@@ -142,3 +148,11 @@ export async function GET(input: APIEvent) {
}, },
) )
} }
function formatUsage(usage: { status: "ok" | "rate-limited"; resetInSec: number; usagePercent: number }) {
return {
status: usage.status,
percent: usage.usagePercent,
resetsAt: new Date(Date.now() + usage.resetInSec * 1000).toISOString(),
}
}