- 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
100 lines
3.2 KiB
Python
100 lines
3.2 KiB
Python
"""First-run demo seeding: 30 days of realistic hourly metrics.
|
|
|
|
Seeding only ever runs when the database has no assets (or the explicit reseed
|
|
endpoint is used), so real deployments are never mixed with demo data.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import time
|
|
from typing import Any, Dict, List
|
|
|
|
from . import config, db
|
|
from .demo_fleet import FLEET, demo_value
|
|
from .util import now_iso
|
|
|
|
SEED_VERSION = f"headroom-demo-v1-{config.SEED_DAYS}"
|
|
|
|
|
|
def is_seeded() -> bool:
|
|
if db.get_setting("seed_version") != SEED_VERSION:
|
|
return False
|
|
row = db.query_one("SELECT COUNT(*) AS n FROM assets")
|
|
return bool(row and row["n"] > 0)
|
|
|
|
|
|
def seed_if_empty() -> bool:
|
|
if not config.SEED_DEMO:
|
|
return False
|
|
if is_seeded():
|
|
return False
|
|
existing = db.query_one("SELECT COUNT(*) AS n FROM assets")
|
|
if existing and existing["n"] > 0:
|
|
return False
|
|
seed()
|
|
return True
|
|
|
|
|
|
def seed() -> None:
|
|
now = int(time.time())
|
|
now_hour = now - (now % 3600)
|
|
start = now_hour - config.SEED_DAYS * 86400
|
|
timestamp = now_iso()
|
|
|
|
asset_rows = []
|
|
sample_rows = []
|
|
for module_id, specs in FLEET.items():
|
|
for spec in specs:
|
|
external_id = str(spec["external_id"])
|
|
asset_id = f"{module_id}::{external_id}"
|
|
asset_rows.append(
|
|
(
|
|
asset_id,
|
|
module_id,
|
|
external_id,
|
|
spec["name"],
|
|
spec["type"],
|
|
spec.get("cluster", ""),
|
|
spec.get("site", ""),
|
|
json.dumps(spec.get("tags", [])),
|
|
json.dumps(spec.get("meta", {})),
|
|
json.dumps(spec.get("capacity", {})),
|
|
timestamp,
|
|
timestamp,
|
|
1,
|
|
)
|
|
)
|
|
metric_names = list(spec["metrics"].keys())
|
|
for ts in range(start, now_hour + 1, 3600):
|
|
for metric in metric_names:
|
|
sample_rows.append(
|
|
(asset_id, metric, ts, demo_value(spec, metric, ts, now_hour))
|
|
)
|
|
|
|
with db.db() as conn:
|
|
conn.executemany(
|
|
"INSERT OR REPLACE INTO assets(id, module_id, external_id, name, type, cluster, site, "
|
|
"tags, meta, capacity, first_seen, last_seen, active) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)",
|
|
asset_rows,
|
|
)
|
|
conn.executemany(
|
|
"INSERT OR REPLACE INTO samples(asset_id, metric, ts, value) VALUES(?,?,?,?)",
|
|
sample_rows,
|
|
)
|
|
conn.execute(
|
|
"INSERT INTO settings(key, value) VALUES('seed_version', ?) "
|
|
"ON CONFLICT(key) DO UPDATE SET value=excluded.value",
|
|
(SEED_VERSION,),
|
|
)
|
|
db.invalidate_settings_cache()
|
|
|
|
|
|
def clear_all_data() -> None:
|
|
with db.db() as conn:
|
|
conn.execute("DELETE FROM samples")
|
|
conn.execute("DELETE FROM alerts")
|
|
conn.execute("DELETE FROM assets")
|
|
conn.execute("UPDATE modules SET last_run=NULL, status='pending', message='Awaiting first collection'")
|
|
conn.execute("UPDATE settings SET value='' WHERE key='seed_version'")
|
|
db.invalidate_settings_cache()
|