summaryrefslogtreecommitdiffhomepage
path: root/packages/console/core/src/subscription.ts
diff options
context:
space:
mode:
authorFrank <[email protected]>2026-02-24 04:45:39 -0500
committerFrank <[email protected]>2026-02-24 04:45:41 -0500
commitfb6d201ee03d73967c554394742be360e2ff782d (patch)
tree25cbda2e9ec598dfa5a5493f67cb432c5287d624 /packages/console/core/src/subscription.ts
parentcda2af2589ddef9265ca2db379ecd4ab556f6be8 (diff)
downloadopencode-fb6d201ee03d73967c554394742be360e2ff782d.tar.gz
opencode-fb6d201ee03d73967c554394742be360e2ff782d.zip
wip: zen lite
Diffstat (limited to 'packages/console/core/src/subscription.ts')
-rw-r--r--packages/console/core/src/subscription.ts40
1 files changed, 37 insertions, 3 deletions
diff --git a/packages/console/core/src/subscription.ts b/packages/console/core/src/subscription.ts
index ca3b17042..879f940e0 100644
--- a/packages/console/core/src/subscription.ts
+++ b/packages/console/core/src/subscription.ts
@@ -1,7 +1,7 @@
import { z } from "zod"
import { fn } from "./util/fn"
import { centsToMicroCents } from "./util/price"
-import { getWeekBounds } from "./util/date"
+import { getWeekBounds, getMonthlyBounds } from "./util/date"
export namespace Subscription {
export const analyzeRollingUsage = fn(
@@ -29,7 +29,7 @@ export namespace Subscription {
return {
status: "ok" as const,
resetInSec: Math.ceil((windowEnd.getTime() - now.getTime()) / 1000),
- usagePercent: Math.ceil(Math.min(100, (usage / rollingLimitInMicroCents) * 100)),
+ usagePercent: Math.floor(Math.min(100, (usage / rollingLimitInMicroCents) * 100)),
}
}
return {
@@ -61,7 +61,7 @@ export namespace Subscription {
return {
status: "ok" as const,
resetInSec: Math.ceil((week.end.getTime() - now.getTime()) / 1000),
- usagePercent: Math.ceil(Math.min(100, (usage / fixedLimitInMicroCents) * 100)),
+ usagePercent: Math.floor(Math.min(100, (usage / fixedLimitInMicroCents) * 100)),
}
}
@@ -72,4 +72,38 @@ export namespace Subscription {
}
},
)
+
+ export const analyzeMonthlyUsage = fn(
+ z.object({
+ limit: z.number().int(),
+ usage: z.number().int(),
+ timeUpdated: z.date(),
+ timeSubscribed: z.date(),
+ }),
+ ({ limit, usage, timeUpdated, timeSubscribed }) => {
+ const now = new Date()
+ const month = getMonthlyBounds(now, timeSubscribed)
+ const fixedLimitInMicroCents = centsToMicroCents(limit * 100)
+ if (timeUpdated < month.start) {
+ return {
+ status: "ok" as const,
+ resetInSec: Math.ceil((month.end.getTime() - now.getTime()) / 1000),
+ usagePercent: 0,
+ }
+ }
+ if (usage < fixedLimitInMicroCents) {
+ return {
+ status: "ok" as const,
+ resetInSec: Math.ceil((month.end.getTime() - now.getTime()) / 1000),
+ usagePercent: Math.floor(Math.min(100, (usage / fixedLimitInMicroCents) * 100)),
+ }
+ }
+
+ return {
+ status: "rate-limited" as const,
+ resetInSec: Math.ceil((month.end.getTime() - now.getTime()) / 1000),
+ usagePercent: 100,
+ }
+ },
+ )
}