sync
This commit is contained in:
@@ -1,17 +1,16 @@
|
|||||||
import { and, Database, inArray } from "@opencode-ai/console-core/drizzle/index.js"
|
import { and, Database, inArray } from "@opencode-ai/console-core/drizzle/index.js"
|
||||||
import { ModelTpsRateLimitTable } from "@opencode-ai/console-core/schema/ip.sql.js"
|
import { ModelTpsRateLimitTable } from "@opencode-ai/console-core/schema/ip.sql.js"
|
||||||
|
|
||||||
type Entry = { provider: string; model: string; tps: number }
|
type Result = Record<string, Record<number, { qualify: number; unqualify: number }>>
|
||||||
type Result = Record<string, { qualify: number; unqualify: number }>
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
async fetch(request: Request) {
|
async fetch(request: Request) {
|
||||||
if (request.method !== "POST") return new Response("Method Not Allowed", { status: 405 })
|
if (request.method !== "POST") return new Response("Method Not Allowed", { status: 405 })
|
||||||
|
|
||||||
const entries = (await request.json()) as Entry[]
|
const body = (await request.json()) as { ids: string[] }
|
||||||
if (!Array.isArray(entries) || entries.length === 0) return Response.json({} satisfies Result)
|
const ids = body.ids
|
||||||
|
|
||||||
const ids = entries.map((e) => `${e.provider}/${e.model}/${e.tps}`)
|
if (ids.length === 0) return Response.json({} satisfies Result)
|
||||||
|
|
||||||
const toInterval = (date: Date) =>
|
const toInterval = (date: Date) =>
|
||||||
parseInt(
|
parseInt(
|
||||||
@@ -21,19 +20,23 @@ export default {
|
|||||||
.substring(0, 12),
|
.substring(0, 12),
|
||||||
)
|
)
|
||||||
const now = Date.now()
|
const now = Date.now()
|
||||||
const intervals = Array.from({ length: 5 }, (_, i) => toInterval(new Date(now - i * 60 * 1000)))
|
const intervals = Array.from({ length: 30 }, (_, i) => toInterval(new Date(now - i * 60 * 1000)))
|
||||||
|
|
||||||
const rows = await Database.use((tx) =>
|
const rows = await Database.use((tx) =>
|
||||||
tx
|
tx
|
||||||
.select()
|
.select()
|
||||||
.from(ModelTpsRateLimitTable)
|
.from(ModelTpsRateLimitTable)
|
||||||
.where(and(inArray(ModelTpsRateLimitTable.id, ids), inArray(ModelTpsRateLimitTable.interval, intervals))),
|
.where(and(inArray(ModelTpsRateLimitTable.id, body.ids), inArray(ModelTpsRateLimitTable.interval, intervals))),
|
||||||
)
|
)
|
||||||
|
|
||||||
const result: Result = Object.fromEntries(ids.map((id) => [id, { qualify: 0, unqualify: 0 }]))
|
const result: Result = Object.fromEntries(
|
||||||
|
body.ids.map((id) => [
|
||||||
|
id,
|
||||||
|
Object.fromEntries(intervals.map((interval) => [interval, { qualify: 0, unqualify: 0 }])),
|
||||||
|
]),
|
||||||
|
)
|
||||||
for (const row of rows) {
|
for (const row of rows) {
|
||||||
result[row.id].qualify += row.qualify
|
result[row.id][row.interval] = { qualify: row.qualify, unqualify: row.unqualify }
|
||||||
result[row.id].unqualify += row.unqualify
|
|
||||||
}
|
}
|
||||||
return Response.json(result)
|
return Response.json(result)
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user