Files
Pulse/frontend-modern/src/utils/patrolCapacityForecastPresentation.ts
rcourtman d45715d083 feat(ai): surface deterministic capacity forecasts as first-class finding signals
Capacity forecasts (days-to-full, current usage, trend) were previously
computed deterministically but only fed into LLM prompt text. They never
reached the finding as structured data, so the frontend could not render a
verified urgency signal and the model's speculation was the only thing the
operator saw.

This persists CapacityForecast on Finding (marshal-mirror pair) and stamps
it post-analysis via a service join (StampCapacityForecasts). The forecast
filter now also keeps stable-high (>=80%) pools so the deterministic "no
fill trend" reading wins over model speculation, and fixes isQuiet wrongly
treating stable (-1) trends as filling.

Frontend maps the forecast through the UnifiedFinding view model and renders
a deterministic urgency line (Filling up / Stable / days-to-full · % used)
in the expanded finding detail.

Note: forecasts only populate for resources whose usage is ingested into
metrics history (Proxmox/Ceph storage, nodes, guests). Agent-host storage
(unraid pools) is not yet ingested as a time-series and remains a follow-up
to activate the feature for those pools.

Refs lane: protection-posture-attention-queue
2026-06-27 22:03:28 +01:00

76 lines
2.3 KiB
TypeScript

import type { CapacityForecast } from '@/api/patrol';
export type CapacityForecastTone = 'critical' | 'warning' | 'info';
export interface CapacityForecastPresentation {
/** Short scannable direction: "Filling up", "Stable", "Declining". */
direction: string;
/** Urgency detail, e.g. "~5 days to full at +1.2%/day". */
detail: string;
/** Current utilization readout, e.g. "86% used". */
current: string;
tone: CapacityForecastTone;
}
function formatDays(days: number): string {
if (days <= 0) return 'now';
if (days === 1) return '1 day';
if (days < 7) return `${days} days`;
if (days < 14) return '~1 week';
if (days < 30) return `~${Math.round(days / 7)} weeks`;
const months = Math.round(days / 30);
if (months <= 1) return '~1 month';
if (months < 12) return `~${months} months`;
return 'over a year';
}
function formatRate(changePerDay: number): string {
const sign = changePerDay > 0 ? '+' : '';
return `${sign}${changePerDay.toFixed(changePerDay >= 1 || changePerDay <= -1 ? 1 : 2)}%/day`;
}
/**
* Turn a deterministic capacity forecast into a plain operator-facing urgency
* line. Returns null when no forecast applies so callers can skip rendering.
*
* This is the canonical projection for the capacity-forecast signal: the
* backend owns the computation (linear regression over utilization samples),
* this helper owns the operator vocabulary. Kept free of component concerns so
* it can be exercised directly in tests and reused across surfaces.
*/
export function presentCapacityForecast(
fc?: CapacityForecast | null,
): CapacityForecastPresentation | null {
if (!fc) return null;
const days = fc.days_to_full;
const change = fc.daily_change ?? 0;
const current = `${Math.round(fc.current_pct)}% used`;
if (days > 0) {
const tone: CapacityForecastTone = days <= 7 ? 'critical' : days <= 30 ? 'warning' : 'info';
return {
direction: 'Filling up',
detail: `${formatDays(days)} to full at ${formatRate(change)}`,
current,
tone,
};
}
if (change < -0.1) {
return {
direction: 'Declining',
detail: `usage falling ${formatRate(Math.abs(change))}`,
current,
tone: 'info',
};
}
return {
direction: 'Stable',
detail: 'no clear fill trend',
current,
tone: 'info',
};
}