- FastAPI app med 7 bundled module-typer (proxmox, esxi, truenas, splunk, linux, windows, http-json) - Multi-instance support: flere instanser af samme modultype med adskilte assets - Community/Enterprise licensing med admin enterprise-toggle - Capacity forecasting, trends, what-if simulator, alert policies - MCP server + LLM copilot integration - Dockerfile + rune.yaml til Yggdrasil
79 lines
1.8 KiB
Python
79 lines
1.8 KiB
Python
"""Small shared helpers."""
|
|
from __future__ import annotations
|
|
|
|
import math
|
|
import re
|
|
from datetime import datetime, timezone
|
|
from typing import Any, Optional
|
|
|
|
_UNITS = [
|
|
(1024 ** 4, "PB"),
|
|
(1024 ** 3, "TB"),
|
|
(1024 ** 2, "GB"),
|
|
(1024, "MB"),
|
|
]
|
|
|
|
|
|
def utcnow() -> datetime:
|
|
return datetime.now(timezone.utc)
|
|
|
|
|
|
def now_iso() -> str:
|
|
return utcnow().isoformat(timespec="seconds")
|
|
|
|
|
|
def epoch() -> int:
|
|
return int(utcnow().timestamp())
|
|
|
|
|
|
def iso_from_epoch(ts: Optional[int]) -> Optional[str]:
|
|
if ts is None:
|
|
return None
|
|
return datetime.fromtimestamp(int(ts), timezone.utc).isoformat(timespec="seconds")
|
|
|
|
|
|
def epoch_from_iso(value: Optional[str]) -> Optional[int]:
|
|
if not value:
|
|
return None
|
|
try:
|
|
return int(datetime.fromisoformat(value.replace("Z", "+00:00")).timestamp())
|
|
except (ValueError, TypeError):
|
|
return None
|
|
|
|
|
|
def clamp(value: float, low: float, high: float) -> float:
|
|
return max(low, min(high, value))
|
|
|
|
|
|
def finite(value: Any) -> Optional[float]:
|
|
try:
|
|
number = float(value)
|
|
except (TypeError, ValueError):
|
|
return None
|
|
if not math.isfinite(number) or abs(number) > 1e15:
|
|
return None
|
|
return number
|
|
|
|
|
|
def human_bytes(value: Optional[float]) -> str:
|
|
if value is None:
|
|
return "—"
|
|
number = float(value)
|
|
for factor, unit in _UNITS:
|
|
if abs(number) >= factor:
|
|
return f"{number / factor:.1f} {unit}"
|
|
return f"{number:.0f} B"
|
|
|
|
|
|
def slug(value: str) -> str:
|
|
return re.sub(r"[^a-z0-9]+", "-", value.lower()).strip("-")
|
|
|
|
|
|
def truncate(value: Any, length: int = 200) -> str:
|
|
text = str(value if value is not None else "")
|
|
return text[:length]
|
|
|
|
|
|
def pct(value: Optional[float]) -> str:
|
|
return "—" if value is None else f"{value:.0f}%"
|