Add host capacity sample viewer with filtering and pagination

This commit is contained in:
2026-10-04 22:22:01 -04:00
parent 148c74063b
commit 9c6d7cc347
7 changed files with 190 additions and 1 deletions
+22 -1
View File
@@ -1,6 +1,6 @@
import { db } from '../db/index.js';
import { ensureHostSchema } from './schema.js';
import type { CapacityCalibration, HostInventory, ResourceAllowance } from '../../hosts.js';
import type { CapacityCalibration, CapacitySample, CapacitySamplePage, HostInventory, ResourceAllowance } from '../../hosts.js';
const BOOTSTRAP_CPU = 0.5;
const BOOTSTRAP_MEMORY = 1024 ** 3;
@@ -14,6 +14,27 @@ interface AllowanceRow {
}
function database() { const handle = db(); ensureHostSchema(handle); return handle; }
/** Read retained minute peaks without contacting agents or recalculating allowances. */
export function capacitySamples(hostId: string, options: { scope: 'host' | 'fleet'; phase: 'all' | Phase; page: number }): CapacitySamplePage {
const handle = database();
const clauses = ['s.bucket >= ?'];
const values: (string | number)[] = [new Date(Date.now() - WINDOW_MS).toISOString()];
if (options.scope === 'host') { clauses.push('s.host_id = ?'); values.push(hostId); }
if (options.phase !== 'all') { clauses.push('s.phase = ?'); values.push(options.phase); }
const where = clauses.join(' AND ');
const total = (handle.prepare(`SELECT COUNT(*) AS count FROM ec_instance_resource_samples s WHERE ${where}`).get(...values) as { count: number }).count;
const pageSize = 50;
const page = Math.min(options.page, Math.max(1, Math.ceil(total / pageSize)));
const samples = handle.prepare(`SELECT s.host_id AS hostId, h.name AS hostName, s.instance_id AS instanceId,
i.server, s.generation, s.phase, s.bucket AS timestamp, s.cpu_cores AS cpuCores, s.memory_bytes AS memoryBytes
FROM ec_instance_resource_samples s
LEFT JOIN ec_hosts h ON h.id=s.host_id
LEFT JOIN native_instances i ON i.id=s.instance_id AND i.host_id=s.host_id
WHERE ${where} ORDER BY s.bucket DESC, s.host_id, s.instance_id, s.generation, s.phase LIMIT ? OFFSET ?`)
.all(...values, pageSize, (page - 1) * pageSize) as CapacitySample[];
return { samples, total, page, pageSize };
}
export function validateInstanceResources(inventory: HostInventory): void {
if (inventory.instanceResourcesError !== undefined && (typeof inventory.instanceResourcesError !== 'string'
|| inventory.instanceResourcesError.length > 500)) throw new Error('Invalid instance resource sampling error.');