GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(console): backport usage reset boundary fix to dev (#47267) · koji/opencode@475b408

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/console/app/src/routes/zen/util
    • handler.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/console/app/src/routes/zen/util/handler.ts‎

Copy file name to clipboardExpand all lines: packages/console/app/src/routes/zen/util/handler.ts
+26-6Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,8 @@ export async function handler(
10781078
authInfo = authInfo!
10791079

10801080
const cost = centsToMicroCents(totalCostInCent)
1081+
// Keep period bounds and persisted timestamps on one snapshot when a queued write crosses a reset boundary.
1082+
const trackedAt = new Date()
10811083

10821084
// For hot workspaces, batch balance/usage updates through Redis to avoid
10831085
// row-level lock contention on BillingTable/UserTable. Returns the amount
@@ -1118,19 +1120,25 @@ export async function handler(
11181120
if (billingSource === "subscription") {
11191121
const plan = authInfo.billing.subscription!.plan
11201122
const black = BlackData.getLimits({ plan })
1121-
const week = getWeekBounds(new Date())
1123+
const week = getWeekBounds(trackedAt)
11221124
const rollingWindowSeconds = black.rollingWindow * 3600
11231125
return [
11241126
db
11251127
.update(SubscriptionTable)
11261128
.set({
11271129
fixedUsage: sql`
11281130
CASE
1131+
WHEN ${SubscriptionTable.timeFixedUpdated} >= ${week.end} THEN ${SubscriptionTable.fixedUsage}
11291132
WHEN ${SubscriptionTable.timeFixedUpdated} >= ${week.start} THEN ${SubscriptionTable.fixedUsage} + ${cost}
11301133
ELSE ${cost}
11311134
END
11321135
`,
1133-
timeFixedUpdated: sql`now()`,
1136+
timeFixedUpdated: sql`
1137+
CASE
1138+
WHEN ${SubscriptionTable.timeFixedUpdated} > ${trackedAt} THEN ${SubscriptionTable.timeFixedUpdated}
1139+
ELSE ${trackedAt}
1140+
END
1141+
`,
11341142
rollingUsage: sql`
11351143
CASE
11361144
WHEN UNIX_TIMESTAMP(${SubscriptionTable.timeRollingUpdated}) >= UNIX_TIMESTAMP(now()) - ${rollingWindowSeconds} THEN ${SubscriptionTable.rollingUsage} + ${cost}
@@ -1154,8 +1162,8 @@ export async function handler(
11541162
}
11551163
if (billingSource === "lite") {
11561164
const lite = LiteData.getLimits()
1157-
const week = getWeekBounds(new Date())
1158-
const month = getMonthlyBounds(new Date(), authInfo.lite!.timeCreated)
1165+
const week = getWeekBounds(trackedAt)
1166+
const month = getMonthlyBounds(trackedAt, authInfo.lite!.timeCreated)
11591167
const rollingWindowSeconds = lite.rollingWindow * 3600
11601168
const quotaCost = Math.round(cost * modelInfo.costMultiplier)
11611169
return [
@@ -1164,18 +1172,30 @@ export async function handler(
11641172
.set({
11651173
monthlyUsage: sql`
11661174
CASE
1175+
WHEN ${LiteTable.timeMonthlyUpdated} >= ${month.end} THEN ${LiteTable.monthlyUsage}
11671176
WHEN ${LiteTable.timeMonthlyUpdated} >= ${month.start} THEN ${LiteTable.monthlyUsage} + ${quotaCost}
11681177
ELSE ${quotaCost}
11691178
END
11701179
`,
1171-
timeMonthlyUpdated: sql`now()`,
1180+
timeMonthlyUpdated: sql`
1181+
CASE
1182+
WHEN ${LiteTable.timeMonthlyUpdated} > ${trackedAt} THEN ${LiteTable.timeMonthlyUpdated}
1183+
ELSE ${trackedAt}
1184+
END
1185+
`,
11721186
weeklyUsage: sql`
11731187
CASE
1188+
WHEN ${LiteTable.timeWeeklyUpdated} >= ${week.end} THEN ${LiteTable.weeklyUsage}
11741189
WHEN ${LiteTable.timeWeeklyUpdated} >= ${week.start} THEN ${LiteTable.weeklyUsage} + ${quotaCost}
11751190
ELSE ${quotaCost}
11761191
END
11771192
`,
1178-
timeWeeklyUpdated: sql`now()`,
1193+
timeWeeklyUpdated: sql`
1194+
CASE
1195+
WHEN ${LiteTable.timeWeeklyUpdated} > ${trackedAt} THEN ${LiteTable.timeWeeklyUpdated}
1196+
ELSE ${trackedAt}
1197+
END
1198+
`,
11791199
rollingUsage: sql`
11801200
CASE
11811201
WHEN UNIX_TIMESTAMP(${LiteTable.timeRollingUpdated}) >= UNIX_TIMESTAMP(now()) - ${rollingWindowSeconds} THEN ${LiteTable.rollingUsage} + ${quotaCost}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.