Exclude inactive hosts from capacity history
CI / checks (push) Successful in 6m13s

This commit is contained in:
2026-10-14 19:31:26 -04:00
parent be7f6c77c1
commit 428df7f03a
3 changed files with 87 additions and 5 deletions
+6 -1
View File
@@ -1,5 +1,6 @@
import { db } from '../db/index.js';
import type { CapacityHistory, CapacityQuery, CapacityRange } from '../../capacity.js';
import type { HostMode } from '../../hosts.js';
import { readTemperatureHistory } from './temperatures.js';
const windows: Record<CapacityRange, { duration: number; bucketMs: number }> = {
@@ -16,13 +17,14 @@ export function readCapacityHistory(query: CapacityQuery, now = Date.now()): Cap
const count = duration / bucketMs;
const hosts = handle
.prepare(
`SELECT id,name,created_at FROM ec_hosts
`SELECT id,name,created_at,mode FROM ec_hosts
WHERE deleted_at IS NULL ${query.hostId ? 'AND id=?' : ''} ORDER BY id`
)
.all(...(query.hostId ? [query.hostId] : [])) as {
id: string;
name: string;
created_at: string;
mode: HostMode;
}[];
// The primary key bounds each host scan to the selected interval. Aggregate
// before crossing the worker boundary; do not return raw heartbeat rows.
@@ -44,6 +46,9 @@ export function readCapacityHistory(query: CapacityQuery, now = Date.now()): Cap
expectedHosts: 0
}));
for (const host of hosts) {
// Current mode determines available capacity; keep inactive hosts in
// the separate temperature history for diagnostics.
if (host.mode !== 'active') continue;
const createdAt = Date.parse(host.created_at);
for (const point of points) {
if (createdAt <= Math.min(now, point.at + bucketMs)) point.expectedHosts++;