zen: stat worker

This commit is contained in:
Frank
2026-05-12 21:37:34 -04:00
parent 8310e7df70
commit 005e64e7d8
9 changed files with 61 additions and 1 deletions

View File

@@ -298,6 +298,7 @@ declare module "sst" {
"EnterpriseStorage": cloudflare.R2Bucket
"GatewayKv": cloudflare.KVNamespace
"LogProcessor": cloudflare.Service
"Stat": cloudflare.Service
"ZenData": cloudflare.R2Bucket
"ZenDataNew": cloudflare.R2Bucket
}

View File

@@ -0,0 +1,39 @@
import { WorkerEntrypoint } from "cloudflare:workers"
import { and, Database, inArray } from "@opencode-ai/console-core/drizzle/index.js"
import { ModelTpsRateLimitTable } from "@opencode-ai/console-core/schema/ip.sql.js"
type Result = Record<string, { qualify: number; unqualify: number }>
export default class Stat extends WorkerEntrypoint {
async fetch() {
return new Response("Not Found", { status: 404 })
}
async getStats(ids: string[]): Promise<Result> {
if (ids.length === 0) return {}
const toInterval = (date: Date) =>
parseInt(
date
.toISOString()
.replace(/[^0-9]/g, "")
.substring(0, 12),
)
const now = Date.now()
const intervals = Array.from({ length: 5 }, (_, i) => toInterval(new Date(now - i * 60 * 1000)))
const rows = await Database.use((tx) =>
tx
.select()
.from(ModelTpsRateLimitTable)
.where(and(inArray(ModelTpsRateLimitTable.id, ids), inArray(ModelTpsRateLimitTable.interval, intervals))),
)
const result: Result = Object.fromEntries(ids.map((id) => [id, { qualify: 0, unqualify: 0 }]))
for (const row of rows) {
result[row.id].qualify += row.qualify
result[row.id].unqualify += row.unqualify
}
return result
}
}

View File

@@ -298,6 +298,7 @@ declare module "sst" {
"EnterpriseStorage": cloudflare.R2Bucket
"GatewayKv": cloudflare.KVNamespace
"LogProcessor": cloudflare.Service
"Stat": cloudflare.Service
"ZenData": cloudflare.R2Bucket
"ZenDataNew": cloudflare.R2Bucket
}

View File

@@ -298,6 +298,7 @@ declare module "sst" {
"EnterpriseStorage": cloudflare.R2Bucket
"GatewayKv": cloudflare.KVNamespace
"LogProcessor": cloudflare.Service
"Stat": cloudflare.Service
"ZenData": cloudflare.R2Bucket
"ZenDataNew": cloudflare.R2Bucket
}