🌳
pt0/deployF/testsF/fetchDeployStatsPerWindowAI.mts
3import { wrapDbEnv } from '../wrapDbEnvF.mts'
6const defaultTimeoutMs = 10_000
8export const fetchDeployStatsPerWindow = async (opts: {
9 ep?: string
10 appConfig?: any
11 actions?: string[]
12 timeoutMs?: number
13 queryFnc: (db: any, startTs: string, endTs: string | undefined) => Promise<number>
14}): Promise<DeployStatsRow[] | null> => {
15 const { ep, appConfig, actions = ['apply'], timeoutMs = defaultTimeoutMs, queryFnc } = opts
16 if (!appConfig?.envConf?.dbSecrets) return null
18 let rows = readLedger().filter(r => actions.includes(r.action || ''))
19 if (ep) {
20 const epRel = toPtRelPath(ep)
21 rows = rows.filter(r => r.ep === epRel)
22 }
24 const applyRows = rows
25 .filter(r => r.ts && r.gitSha)
26 .sort((a, b) => a.ts!.localeCompare(b.ts!))
27 .slice(-30)
29 if (!applyRows.length) return null
31 try {
32 let capturedResults: DeployStatsRow[] | null = null
33 await Promise.race([
34 wrapDbEnv({ appConfig, reuseConn: false }, async ({ db }) => {
35 const results: DeployStatsRow[] = []
36 for (let i = 0; i < applyRows.length; i++) {
37 const row = applyRows[i]
38 const startTs = row.ts!
39 const endTs = i < applyRows.length - 1 ? applyRows[i + 1]!.ts! : undefined
40 const value = await queryFnc(db, startTs, endTs)
41 results.push({ gitSha: row.gitSha!, value })
42 }
43 capturedResults = results
44 }),
45 new Promise<null>(resolve => setTimeout(() => resolve(null), timeoutMs)),
46 ])
47 return capturedResults
48 } catch {
49 return null
50 }