Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions tests/test_dashboard_inventory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import tempfile
import unittest
from pathlib import Path

from tools.dashboard_inventory import inventory


class DashboardInventoryTest(unittest.TestCase):
def test_reports_invalid_json_with_path(self):
with tempfile.TemporaryDirectory() as tmpdir:
root = Path(tmpdir)
dashboard_dir = root / "deployment" / "grafana-dashboard"
dashboard_dir.mkdir(parents=True)
bad_path = dashboard_dir / "broken.json"
bad_path.write_text("{not-json", encoding="utf-8")

with self.assertRaises(RuntimeError) as ctx:
inventory(root)

self.assertIn(str(bad_path), str(ctx.exception))


if __name__ == "__main__":
unittest.main()
45 changes: 45 additions & 0 deletions tools/dashboard_inventory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python3
"""Inventory Grafana dashboards shipped with mx-exporter."""

from __future__ import annotations

import argparse
import json
from pathlib import Path


def inventory(root: Path) -> dict[str, object]:
dashboards = []
for path in sorted((root / "deployment" / "grafana-dashboard").glob("*.json")):
try:
data = json.loads(path.read_text(encoding="utf-8-sig"))
except json.JSONDecodeError as exc:
raise RuntimeError(f"Failed to parse JSON file {path}: {exc}") from exc
panels = data.get("panels", [])
dashboards.append(
{
"path": path.relative_to(root).as_posix(),
"title": data.get("title", ""),
"uid": data.get("uid", ""),
"panel_count": len(panels),
}
)
return {"dashboard_count": len(dashboards), "dashboards": dashboards}


def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--root", type=Path, default=Path.cwd())
parser.add_argument("--output", type=Path)
args = parser.parse_args()

text = json.dumps(inventory(args.root), indent=2, ensure_ascii=False)
if args.output:
args.output.write_text(text + "\n", encoding="utf-8")
else:
print(text)
return 0


if __name__ == "__main__":
raise SystemExit(main())