Skip to content

Commit b336001

Browse files
committed
Log active count by model
1 parent 6a0acad commit b336001

3 files changed

Lines changed: 30 additions & 14 deletions

File tree

web/src/server/free-session/__tests__/admission.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function makeAdmissionDeps(overrides: Partial<AdmissionDeps> = {}): AdmissionDep
1616
calls,
1717
sweepExpired: async () => 0,
1818
queueDepth: async () => 0,
19-
activeCount: async () => 0,
19+
activeCountsByModel: async () => ({}),
2020
getFleetHealth: async () => ({}),
2121
admitFromQueue: async ({ health }) => {
2222
calls.admit += 1

web/src/server/free-session/admission.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import {
77
isWaitingRoomEnabled,
88
} from './config'
99
import { getFleetHealth } from './fireworks-health'
10-
import { activeCount, admitFromQueue, queueDepth, sweepExpired } from './store'
10+
import {
11+
activeCountsByModel,
12+
admitFromQueue,
13+
queueDepth,
14+
sweepExpired,
15+
} from './store'
1116

1217
import type { FireworksHealth, FleetHealth } from './fireworks-health'
1318

@@ -16,7 +21,7 @@ import { logger } from '@/util/logger'
1621
export interface AdmissionDeps {
1722
sweepExpired: (now: Date, graceMs: number) => Promise<number>
1823
queueDepth: (params: { model: string }) => Promise<number>
19-
activeCount: () => Promise<number>
24+
activeCountsByModel: () => Promise<Record<string, number>>
2025
admitFromQueue: (params: {
2126
model: string
2227
sessionLengthMs: number
@@ -35,7 +40,7 @@ export interface AdmissionDeps {
3540
const defaultDeps: AdmissionDeps = {
3641
sweepExpired,
3742
queueDepth,
38-
activeCount,
43+
activeCountsByModel,
3944
admitFromQueue,
4045
// FREEBUFF_DEV_FORCE_ADMIT lets local `dev:freebuff` drive the full
4146
// waiting-room → admitted → ended flow without a real upstream. Returning
@@ -58,7 +63,9 @@ export interface AdmissionTickResult {
5863
admitted: number
5964
/** Per-model queue depth at the end of the tick. */
6065
queueDepthByModel: Record<string, number>
61-
activeCount: number
66+
/** Per-model active-session count at the end of the tick. Models with no
67+
* active sessions are omitted. */
68+
activeCountByModel: Record<string, number>
6269
skipped: FireworksHealth | null
6370
}
6471

@@ -106,7 +113,7 @@ export async function runAdmissionTick(
106113
}),
107114
)
108115

109-
const active = await deps.activeCount()
116+
const activeCountByModel = await deps.activeCountsByModel()
110117
const totalAdmitted = perModel.reduce((s, r) => s + r.admittedCount, 0)
111118
const queueDepthByModel = Object.fromEntries(
112119
perModel.map((r) => [r.model, r.depth]),
@@ -117,7 +124,7 @@ export async function runAdmissionTick(
117124
expired,
118125
admitted: totalAdmitted,
119126
queueDepthByModel,
120-
activeCount: active,
127+
activeCountByModel,
121128
skipped,
122129
}
123130
}
@@ -130,16 +137,16 @@ function runTick() {
130137
inFlight = true
131138
runAdmissionTick()
132139
.then((result) => {
133-
// Emit every tick so queueDepth/activeCount form a continuous time-series
134-
// that can be charted over time. metric=freebuff_waiting_room makes it
135-
// filterable in the log aggregator.
140+
// Emit every tick so per-model queue depth and active counts form a
141+
// continuous time-series that can be charted over time.
142+
// metric=freebuff_waiting_room makes it filterable in the log aggregator.
136143
logger.info(
137144
{
138145
metric: 'freebuff_waiting_room',
139146
admitted: result.admitted,
140147
expired: result.expired,
141148
queueDepthByModel: result.queueDepthByModel,
142-
activeCount: result.activeCount,
149+
activeCountByModel: result.activeCountByModel,
143150
skipped: result.skipped,
144151
},
145152
'[FreeSessionAdmission] tick',

web/src/server/free-session/store.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,21 @@ export async function queueDepthsByModel(): Promise<Record<string, number>> {
176176
return out
177177
}
178178

179-
export async function activeCount(): Promise<number> {
179+
/**
180+
* Single-query read of active-row counts bucketed by model. Mirrors
181+
* `queueDepthsByModel` so the admission tick can log per-model utilization
182+
* alongside per-model queue depth. Models with no active sessions are absent
183+
* from the map; callers should default missing keys to 0.
184+
*/
185+
export async function activeCountsByModel(): Promise<Record<string, number>> {
180186
const rows = await db
181-
.select({ n: count() })
187+
.select({ model: schema.freeSession.model, n: count() })
182188
.from(schema.freeSession)
183189
.where(eq(schema.freeSession.status, 'active'))
184-
return Number(rows[0]?.n ?? 0)
190+
.groupBy(schema.freeSession.model)
191+
const out: Record<string, number> = {}
192+
for (const row of rows) out[row.model] = Number(row.n)
193+
return out
185194
}
186195

187196
export async function queuePositionFor(params: {

0 commit comments

Comments
 (0)