"""Canonical metric + asset metadata shared by API and UI.""" from __future__ import annotations from typing import Dict, List METRIC_CATALOG: Dict[str, Dict[str, object]] = { "cpu_pct": {"label": "CPU utilization", "unit": "%", "kind": "util", "max": 100}, "mem_pct": {"label": "Memory utilization", "unit": "%", "kind": "util", "max": 100}, "storage_pct": {"label": "Storage utilization", "unit": "%", "kind": "util", "max": 100}, "license_pct": {"label": "License consumption", "unit": "%", "kind": "util", "max": 100}, "load_pct": {"label": "Load", "unit": "%", "kind": "util", "max": 100}, "storage_used_gb": {"label": "Storage used", "unit": "GB", "kind": "gauge"}, "storage_total_gb": {"label": "Storage capacity", "unit": "GB", "kind": "capacity"}, "cpu_cores": {"label": "CPU cores", "unit": "cores", "kind": "capacity"}, "mem_gb": {"label": "Memory", "unit": "GB", "kind": "capacity"}, "net_rx_mbps": {"label": "Network in", "unit": "Mbps", "kind": "throughput"}, "net_tx_mbps": {"label": "Network out", "unit": "Mbps", "kind": "throughput"}, "iops": {"label": "IOPS", "unit": "IOPS", "kind": "throughput"}, "latency_ms": {"label": "Latency", "unit": "ms", "kind": "latency"}, "power_w": {"label": "Power draw", "unit": "W", "kind": "power"}, "temp_c": {"label": "Temperature", "unit": "°C", "kind": "temperature"}, "uptime_days": {"label": "Uptime", "unit": "days", "kind": "counter"}, "vm_count": {"label": "Guests", "unit": "VMs", "kind": "counter"}, "index_gb": {"label": "Index size", "unit": "GB", "kind": "gauge"}, "battery_pct": {"label": "Battery", "unit": "%", "kind": "gauge", "max": 100}, } UTIL_METRICS = ["cpu_pct", "mem_pct", "storage_pct", "license_pct", "load_pct"] ASSET_TYPES: Dict[str, Dict[str, object]] = { "hypervisor": {"label": "Hypervisor", "icon": "server", "primary": "cpu_pct", "utils": ["cpu_pct", "mem_pct"]}, "storage": {"label": "Storage", "icon": "database", "primary": "storage_pct", "utils": ["storage_pct"]}, "nas": {"label": "NAS", "icon": "database", "primary": "storage_pct", "utils": ["storage_pct", "cpu_pct", "mem_pct", "load_pct"]}, "server": {"label": "Server", "icon": "server", "primary": "cpu_pct", "utils": ["cpu_pct", "mem_pct", "storage_pct"]}, "appliance": {"label": "Appliance", "icon": "box", "primary": "license_pct", "utils": ["cpu_pct", "mem_pct", "license_pct"]}, "network": {"label": "Network", "icon": "network", "primary": "load_pct", "utils": ["load_pct"]}, "power": {"label": "Power", "icon": "bolt", "primary": "load_pct", "utils": ["load_pct"]}, "cluster": {"label": "Cluster", "icon": "layers", "primary": "cpu_pct", "utils": ["cpu_pct", "mem_pct", "storage_pct"]}, } STATUS_BANDS = [ (0, 59, "healthy", "Comfortable headroom"), (60, 79, "watch", "Monitor closely"), (80, 89, "tight", "Limited headroom"), (90, 101, "critical", "Capacity exhausted soon"), ] def utilization_for_type(asset_type: str) -> List[str]: spec = ASSET_TYPES.get(asset_type, ASSET_TYPES["server"]) return list(spec["utils"]) # type: ignore[arg-type] def primary_for_type(asset_type: str) -> str: return str(ASSET_TYPES.get(asset_type, ASSET_TYPES["server"])["primary"]) def metric_meta(metric: str) -> Dict[str, object]: return METRIC_CATALOG.get(metric, {"label": metric, "unit": "", "kind": "other"}) def status_for_pressure(pressure: int) -> str: for low, high, key, _ in STATUS_BANDS: if low <= pressure <= high: return key return "critical" def status_label(pressure: int) -> str: for low, high, _, label in STATUS_BANDS: if low <= pressure <= high: return label return "Capacity exhausted soon" def meta() -> Dict[str, object]: return { "metrics": METRIC_CATALOG, "asset_types": ASSET_TYPES, "util_metrics": UTIL_METRICS, "status_bands": [ {"min": low, "max": high, "key": key, "label": label} for low, high, key, label in STATUS_BANDS ], }