forked from SigNoz/signoz-mcp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregenerate_dashboard_templates.py
More file actions
executable file
·183 lines (148 loc) · 5.86 KB
/
Copy pathregenerate_dashboard_templates.py
File metadata and controls
executable file
·183 lines (148 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python3
"""Regenerate the bundled dashboard_templates.json from the upstream repo.
Usage:
python3 .github/scripts/regenerate_dashboard_templates.py
Walks the SigNoz/dashboards repo at the tip of `main`, locates each template
JSON, extracts the title/description (v6 `spec.display.*`, falling back to v1
top-level fields) and tags, and emits the bundled index at
internal/handler/tools/dashboard_templates.json.
The runtime fetcher (signoz_import_dashboard) also reads from `main`, so the
catalog and the fetcher always reference the same ref. There is no pinned
SHA to keep in sync.
Adapted from
https://github.com/SigNoz/agent-skills/blob/main/plugins/signoz/skills/signoz-dashboard-create/scripts/regenerate_index.py
(the upstream version pins a SHA; this one tracks main).
This is a maintenance tool. It is not invoked at runtime. Hits the public
GitHub API; rerun with GITHUB_TOKEN set if rate-limited.
"""
from __future__ import annotations
import argparse
import json
import os
import re
import sys
from pathlib import Path
from typing import Any
from urllib.error import HTTPError
from urllib.parse import quote
from urllib.request import Request, urlopen
REPO_ROOT = Path(__file__).resolve().parent.parent.parent
DEFAULT_OUTPUT = REPO_ROOT / "internal" / "handler" / "tools" / "dashboard_templates.json"
UPSTREAM_REF = "main"
GITHUB_TREE_URL = "https://api.github.com/repos/SigNoz/dashboards/git/trees/{ref}?recursive=1"
GITHUB_RAW_URL = "https://raw.githubusercontent.com/SigNoz/dashboards/{ref}/{path}"
def _http_get(url: str) -> bytes:
req = Request(url)
token = os.environ.get("GITHUB_TOKEN")
if token:
req.add_header("Authorization", f"Bearer {token}")
req.add_header("Accept", "application/vnd.github+json")
with urlopen(req, timeout=60) as response:
body: bytes = response.read()
return body
def _list_template_paths(ref: str) -> list[str]:
body = _http_get(GITHUB_TREE_URL.format(ref=ref))
tree = json.loads(body)
paths: list[str] = []
for node in tree.get("tree", []):
if node.get("type") != "blob":
continue
path = node.get("path", "")
if not path.endswith(".json"):
continue
parts = path.split("/")
if len(parts) != 2:
continue
if parts[1].startswith("."):
continue
paths.append(path)
return sorted(paths)
def _tokenize(text: str) -> list[str]:
return [t for t in re.split(r"[\s/_\-\.]+", text.lower()) if t and len(t) > 1]
def _derive_keywords(path: str, title: str, tags: list[str]) -> list[str]:
tokens: list[str] = []
tokens.extend(_tokenize(path))
tokens.extend(_tokenize(title))
for tag in tags:
tokens.extend(_tokenize(tag))
seen: set[str] = set()
unique: list[str] = []
stopwords = {"dashboard", "json", "template"}
for token in tokens:
if token in stopwords or token in seen:
continue
seen.add(token)
unique.append(token)
return unique
def _derive_category(path: str) -> str:
first = path.split("/", 1)[0]
return first.replace("-", " ").title()
def _build_entry(ref: str, path: str) -> dict[str, Any] | None:
body = _http_get(GITHUB_RAW_URL.format(ref=ref, path=quote(path)))
try:
data = json.loads(body)
except json.JSONDecodeError:
return None
# v6 puts the human title/description under spec.display; fall back to the
# v1 top-level fields so the catalog regenerates cleanly through the
# migration, then to the filename.
spec = data.get("spec") or {}
display = spec.get("display") or {}
title = display.get("name") or data.get("title") or path.rsplit("/", 1)[-1].removesuffix(".json")
description = display.get("description") or data.get("description") or ""
# Tags feed the keyword index only (the entry has no tags field). v6 tags
# are {key, value} objects, v1 tags are plain strings; flatten both to text
# so _derive_keywords can tokenize them, indexing key AND value for v6.
raw_tags = data.get("tags")
tags: list[str] = []
if isinstance(raw_tags, list):
for t in raw_tags:
if isinstance(t, dict):
tags.extend(str(v) for v in (t.get("key"), t.get("value")) if v)
elif isinstance(t, str):
tags.append(t)
return {
"id": path.split("/", 1)[0],
"title": title,
"path": path,
"keywords": _derive_keywords(path, title, tags),
"description": description,
"category": _derive_category(path),
}
def regenerate(ref: str, output: Path) -> int:
paths = _list_template_paths(ref)
if not paths:
sys.stderr.write("No template paths found in upstream tree\n")
return 1
entries: list[dict[str, Any]] = []
for path in paths:
try:
entry = _build_entry(ref, path)
except HTTPError as exc:
sys.stderr.write(f"skip {path}: HTTP {exc.code}\n")
continue
if entry is None:
sys.stderr.write(f"skip {path}: not valid JSON\n")
continue
entries.append(entry)
entries.sort(key=lambda e: (e["category"], e["title"].lower()))
output.write_text(
json.dumps(entries, indent=2, ensure_ascii=False) + "\n",
encoding="utf-8",
)
sys.stderr.write(f"Wrote {len(entries)} entries to {output}\n")
return 0
def _parse_args(argv: list[str]) -> argparse.Namespace:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--output",
type=Path,
default=DEFAULT_OUTPUT,
help="Output dashboard_templates.json path",
)
return parser.parse_args(argv)
def main(argv: list[str] | None = None) -> int:
args = _parse_args(argv if argv is not None else sys.argv[1:])
return regenerate(UPSTREAM_REF, args.output)
if __name__ == "__main__":
raise SystemExit(main())