GeezerVison is showing github.com — zen: stat worker · comio/opencode@005e64e
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: infra/console.ts +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -293,3 +293,12 @@ new sst.cloudflare.x.SolidStart("Console", { | 293 | 293 | }, | 294 | 294 | }, | 295 | 295 | }) | | 296 | + | | 297 | +//////////////// | | 298 | +// HELPERS | | 299 | +//////////////// | | 300 | + | | 301 | +export const stat = new sst.cloudflare.Worker("Stat", { | | 302 | + handler: "packages/console/function/src/stat.ts", | | 303 | + link: [database], | | 304 | +}) |
Collapse file Copy file name to clipboardExpand all lines: packages/console/core/sst-env.d.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -298,6 +298,7 @@ declare module "sst" { | 298 | 298 | "EnterpriseStorage": cloudflare.R2Bucket | 299 | 299 | "GatewayKv": cloudflare.KVNamespace | 300 | 300 | "LogProcessor": cloudflare.Service | | 301 | + "Stat": cloudflare.Service | 301 | 302 | "ZenData": cloudflare.R2Bucket | 302 | 303 | "ZenDataNew": cloudflare.R2Bucket | 303 | 304 | } | |
Collapse file Copy file name to clipboard +39Lines changed: 39 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { WorkerEntrypoint } from "cloudflare:workers" | | 2 | +import { and, Database, inArray } from "@opencode-ai/console-core/drizzle/index.js" | | 3 | +import { ModelTpsRateLimitTable } from "@opencode-ai/console-core/schema/ip.sql.js" | | 4 | + | | 5 | +type Result = Record<string, { qualify: number; unqualify: number }> | | 6 | + | | 7 | +export default class Stat extends WorkerEntrypoint { | | 8 | + async fetch() { | | 9 | + return new Response("Not Found", { status: 404 }) | | 10 | + } | | 11 | + | | 12 | + async getStats(ids: string[]): Promise<Result> { | | 13 | + if (ids.length === 0) return {} | | 14 | + | | 15 | + const toInterval = (date: Date) => | | 16 | + parseInt( | | 17 | + date | | 18 | + .toISOString() | | 19 | + .replace(/[^0-9]/g, "") | | 20 | + .substring(0, 12), | | 21 | + ) | | 22 | + const now = Date.now() | | 23 | + const intervals = Array.from({ length: 5 }, (_, i) => toInterval(new Date(now - i * 60 * 1000))) | | 24 | + | | 25 | + const rows = await Database.use((tx) => | | 26 | + tx | | 27 | + .select() | | 28 | + .from(ModelTpsRateLimitTable) | | 29 | + .where(and(inArray(ModelTpsRateLimitTable.id, ids), inArray(ModelTpsRateLimitTable.interval, intervals))), | | 30 | + ) | | 31 | + | | 32 | + const result: Result = Object.fromEntries(ids.map((id) => [id, { qualify: 0, unqualify: 0 }])) | | 33 | + for (const row of rows) { | | 34 | + result[row.id].qualify += row.qualify | | 35 | + result[row.id].unqualify += row.unqualify | | 36 | + } | | 37 | + return result | | 38 | + } | | 39 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/console/function/sst-env.d.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -298,6 +298,7 @@ declare module "sst" { | 298 | 298 | "EnterpriseStorage": cloudflare.R2Bucket | 299 | 299 | "GatewayKv": cloudflare.KVNamespace | 300 | 300 | "LogProcessor": cloudflare.Service | | 301 | + "Stat": cloudflare.Service | 301 | 302 | "ZenData": cloudflare.R2Bucket | 302 | 303 | "ZenDataNew": cloudflare.R2Bucket | 303 | 304 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/console/resource/sst-env.d.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -298,6 +298,7 @@ declare module "sst" { | 298 | 298 | "EnterpriseStorage": cloudflare.R2Bucket | 299 | 299 | "GatewayKv": cloudflare.KVNamespace | 300 | 300 | "LogProcessor": cloudflare.Service | | 301 | + "Stat": cloudflare.Service | 301 | 302 | "ZenData": cloudflare.R2Bucket | 302 | 303 | "ZenDataNew": cloudflare.R2Bucket | 303 | 304 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/enterprise/sst-env.d.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -298,6 +298,7 @@ declare module "sst" { | 298 | 298 | "EnterpriseStorage": cloudflare.R2Bucket | 299 | 299 | "GatewayKv": cloudflare.KVNamespace | 300 | 300 | "LogProcessor": cloudflare.Service | | 301 | + "Stat": cloudflare.Service | 301 | 302 | "ZenData": cloudflare.R2Bucket | 302 | 303 | "ZenDataNew": cloudflare.R2Bucket | 303 | 304 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/function/sst-env.d.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -298,6 +298,7 @@ declare module "sst" { | 298 | 298 | "EnterpriseStorage": cloudflare.R2Bucket | 299 | 299 | "GatewayKv": cloudflare.KVNamespace | 300 | 300 | "LogProcessor": cloudflare.Service | | 301 | + "Stat": cloudflare.Service | 301 | 302 | "ZenData": cloudflare.R2Bucket | 302 | 303 | "ZenDataNew": cloudflare.R2Bucket | 303 | 304 | } | |
Collapse file Copy file name to clipboardExpand all lines: sst-env.d.ts +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -153,6 +153,9 @@ declare module "sst" { | 153 | 153 | "type": "sst.sst.Linkable" | 154 | 154 | "value": string | 155 | 155 | } | | 156 | + "Stat": { | | 157 | + "type": "sst.cloudflare.Worker" | | 158 | + } | 156 | 159 | "Teams": { | 157 | 160 | "type": "sst.cloudflare.SolidStart" | 158 | 161 | "url": string | |
Collapse file Copy file name to clipboardExpand all lines: sst.config.ts +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,10 +19,14 @@ export default $config({ | 19 | 19 | }, | 20 | 20 | async run() { | 21 | 21 | await import("./infra/app.js") | 22 | | - await import("./infra/console.js") | | 22 | + const { stat } = await import("./infra/console.js") | 23 | 23 | await import("./infra/enterprise.js") | 24 | 24 | if ($app.stage === "production" || $app.stage === "vimtor") { | 25 | 25 | await import("./infra/monitoring.js") | 26 | 26 | } | | 27 | + | | 28 | + return { | | 29 | + STAT_WORKER_NAME: stat.nodes.worker.scriptName, | | 30 | + } | 27 | 31 | }, | 28 | 32 | }) |
|