mirror of
https://github.com/anomalyco/opencode.git
synced 2026-08-21 22:23:29 +00:00
refactor(console): simplify go usage response
This commit is contained in:
parent
561afb401a
commit
d470434746
1 changed files with 33 additions and 19 deletions
|
|
@ -31,7 +31,6 @@ export async function GET(input: APIEvent) {
|
|||
const auth = await Database.use((tx) =>
|
||||
tx
|
||||
.select({
|
||||
lite: BillingTable.lite,
|
||||
userID: KeyTable.userID,
|
||||
workspaceID: KeyTable.workspaceID,
|
||||
})
|
||||
|
|
@ -115,24 +114,31 @@ export async function GET(input: APIEvent) {
|
|||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
useBalance: auth.lite?.useBalance ?? false,
|
||||
rollingUsage: Subscription.analyzeRollingUsage({
|
||||
limit: limits.rollingLimit,
|
||||
window: limits.rollingWindow,
|
||||
usage: row.rollingUsage ?? 0,
|
||||
timeUpdated: row.timeRollingUpdated ?? new Date(),
|
||||
}),
|
||||
weeklyUsage: Subscription.analyzeWeeklyUsage({
|
||||
limit: limits.weeklyLimit,
|
||||
usage: row.weeklyUsage ?? 0,
|
||||
timeUpdated: row.timeWeeklyUpdated ?? new Date(),
|
||||
}),
|
||||
monthlyUsage: Subscription.analyzeMonthlyUsage({
|
||||
limit: limits.monthlyLimit,
|
||||
usage: row.monthlyUsage ?? 0,
|
||||
timeUpdated: row.timeMonthlyUpdated ?? new Date(),
|
||||
timeSubscribed: row.timeCreated,
|
||||
}),
|
||||
usage: {
|
||||
rolling: formatUsage(
|
||||
Subscription.analyzeRollingUsage({
|
||||
limit: limits.rollingLimit,
|
||||
window: limits.rollingWindow,
|
||||
usage: row.rollingUsage ?? 0,
|
||||
timeUpdated: row.timeRollingUpdated ?? new Date(),
|
||||
}),
|
||||
),
|
||||
weekly: formatUsage(
|
||||
Subscription.analyzeWeeklyUsage({
|
||||
limit: limits.weeklyLimit,
|
||||
usage: row.weeklyUsage ?? 0,
|
||||
timeUpdated: row.timeWeeklyUpdated ?? new Date(),
|
||||
}),
|
||||
),
|
||||
monthly: formatUsage(
|
||||
Subscription.analyzeMonthlyUsage({
|
||||
limit: limits.monthlyLimit,
|
||||
usage: row.monthlyUsage ?? 0,
|
||||
timeUpdated: row.timeMonthlyUpdated ?? new Date(),
|
||||
timeSubscribed: row.timeCreated,
|
||||
}),
|
||||
),
|
||||
},
|
||||
}),
|
||||
{
|
||||
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(),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue