|
| 1 | +// Pure grouping + filtering helpers for the /hosts fleet view. Kept |
| 2 | +// separate from HostsListPage so the behavior is unit-testable in |
| 3 | +// isolation (the page wiring stays a thin shell over these functions). |
| 4 | +// Spec: frontend-hosts-list v1.7.0 C-10 (grouping) + C-11 (filters). |
| 5 | + |
| 6 | +import type { DevHost, MonitoringBand } from './host-view-model'; |
| 7 | + |
| 8 | +// ─── Grouping ──────────────────────────────────────────────────────────── |
| 9 | + |
| 10 | +// Drop 'team': a host has no team/owner field (see api-hosts HostListItem), |
| 11 | +// so only None/Status/OS are backed by real data. |
| 12 | +export type GroupKey = 'none' | 'status' | 'os'; |
| 13 | + |
| 14 | +export interface HostGroup { |
| 15 | + key: string; |
| 16 | + label: string; |
| 17 | + hosts: DevHost[]; |
| 18 | +} |
| 19 | + |
| 20 | +// Worst-first, mirroring the page's default down-first ordering so the |
| 21 | +// most actionable groups surface at the top. |
| 22 | +const STATUS_ORDER: MonitoringBand[] = [ |
| 23 | + 'critical', |
| 24 | + 'down', |
| 25 | + 'degraded', |
| 26 | + 'online', |
| 27 | + 'maintenance', |
| 28 | + 'unknown', |
| 29 | +]; |
| 30 | + |
| 31 | +const STATUS_LABEL: Record<MonitoringBand, string> = { |
| 32 | + online: 'Online', |
| 33 | + degraded: 'Degraded', |
| 34 | + critical: 'Critical', |
| 35 | + down: 'Down', |
| 36 | + maintenance: 'Maintenance', |
| 37 | + unknown: 'Unknown', |
| 38 | +}; |
| 39 | + |
| 40 | +export function statusLabel(band: MonitoringBand): string { |
| 41 | + return STATUS_LABEL[band] ?? 'Unknown'; |
| 42 | +} |
| 43 | + |
| 44 | +// groupHosts partitions the (already sorted/filtered) host list into |
| 45 | +// labelled sections. group='none' returns a single anonymous group so the |
| 46 | +// renderer can treat both paths uniformly. Empty groups are omitted. |
| 47 | +export function groupHosts(hosts: DevHost[], group: GroupKey): HostGroup[] { |
| 48 | + if (group === 'none') { |
| 49 | + return [{ key: 'none', label: '', hosts }]; |
| 50 | + } |
| 51 | + if (group === 'status') { |
| 52 | + return STATUS_ORDER.map((band) => ({ |
| 53 | + key: band, |
| 54 | + label: statusLabel(band), |
| 55 | + hosts: hosts.filter((h) => h.monitoring === band), |
| 56 | + })).filter((g) => g.hosts.length > 0); |
| 57 | + } |
| 58 | + // group === 'os' — alphabetical, with the catch-all "Unknown" last. |
| 59 | + const byOs = new Map<string, DevHost[]>(); |
| 60 | + for (const h of hosts) { |
| 61 | + const key = h.os || 'Unknown'; |
| 62 | + (byOs.get(key) ?? byOs.set(key, []).get(key)!).push(h); |
| 63 | + } |
| 64 | + return [...byOs.entries()] |
| 65 | + .sort(([a], [b]) => { |
| 66 | + if (a === 'Unknown') return 1; |
| 67 | + if (b === 'Unknown') return -1; |
| 68 | + return a.localeCompare(b); |
| 69 | + }) |
| 70 | + .map(([key, hs]) => ({ key, label: key, hosts: hs })); |
| 71 | +} |
| 72 | + |
| 73 | +// ─── Filtering ─────────────────────────────────────────────────────────── |
| 74 | + |
| 75 | +export type TierFilter = 'crit' | 'warn' | 'ok' | 'none'; |
| 76 | + |
| 77 | +export interface HostFilters { |
| 78 | + status: string[]; // MonitoringBand values |
| 79 | + os: string[]; // osDisplayLabel values (host.os) |
| 80 | + tier: string[]; // TierFilter values |
| 81 | +} |
| 82 | + |
| 83 | +const TIER_LABEL: Record<TierFilter, string> = { |
| 84 | + crit: 'Critical (<40%)', |
| 85 | + warn: 'Warning (40-80%)', |
| 86 | + ok: 'Compliant (>=80%)', |
| 87 | + none: 'No scan data', |
| 88 | +}; |
| 89 | + |
| 90 | +export function tierLabel(t: TierFilter): string { |
| 91 | + return TIER_LABEL[t] ?? t; |
| 92 | +} |
| 93 | + |
| 94 | +// hostComplianceTier buckets a host's compliance score. A never-scanned |
| 95 | +// host (compliance null) is its own 'none' bucket rather than 'crit', so |
| 96 | +// "no data" and "actually failing" stay distinguishable in the filter. |
| 97 | +export function hostComplianceTier(h: DevHost): TierFilter { |
| 98 | + if (h.compliance == null) return 'none'; |
| 99 | + if (h.compliance < 40) return 'crit'; |
| 100 | + if (h.compliance < 80) return 'warn'; |
| 101 | + return 'ok'; |
| 102 | +} |
| 103 | + |
| 104 | +function csv(v: string | undefined): string[] { |
| 105 | + if (!v) return []; |
| 106 | + return v |
| 107 | + .split(',') |
| 108 | + .map((s) => s.trim()) |
| 109 | + .filter(Boolean); |
| 110 | +} |
| 111 | + |
| 112 | +export function parseHostFilters(search: { |
| 113 | + status?: string; |
| 114 | + os?: string; |
| 115 | + tier?: string; |
| 116 | +}): HostFilters { |
| 117 | + return { status: csv(search.status), os: csv(search.os), tier: csv(search.tier) }; |
| 118 | +} |
| 119 | + |
| 120 | +// applyHostFilters keeps a host only when it matches EVERY active |
| 121 | +// dimension (AND across dimensions, OR within a dimension). An empty |
| 122 | +// dimension imposes no constraint. |
| 123 | +export function applyHostFilters(hosts: DevHost[], f: HostFilters): DevHost[] { |
| 124 | + return hosts.filter((h) => { |
| 125 | + if (f.status.length && !f.status.includes(h.monitoring)) return false; |
| 126 | + if (f.os.length && !f.os.includes(h.os || 'Unknown')) return false; |
| 127 | + if (f.tier.length && !f.tier.includes(hostComplianceTier(h))) return false; |
| 128 | + return true; |
| 129 | + }); |
| 130 | +} |
| 131 | + |
| 132 | +export function activeFilterCount(f: HostFilters): number { |
| 133 | + return f.status.length + f.os.length + f.tier.length; |
| 134 | +} |
0 commit comments