#!/usr/bin/env python3
from __future__ import annotations

import json
import shutil
import subprocess
from datetime import datetime, timezone
from pathlib import Path

result = subprocess.run(
    [
        "/usr/bin/docker",
        "ps",
        "--filter",
        "label=local.devops.workbench/owned=true",
        "--format",
        "{{json .}}",
    ],
    check=False,
    capture_output=True,
    text=True,
)
payload = {
    "timestamp": datetime.now(timezone.utc).isoformat(),  # noqa: UP017 -- Ubuntu 22.04
    "disk_free": shutil.disk_usage("/").free,
    "containers": [json.loads(line) for line in result.stdout.splitlines() if line],
}
Path("/var/lib/ldw/health.json").write_text(json.dumps(payload), encoding="utf-8")
