-
Notifications
You must be signed in to change notification settings - Fork 17
/
model.py
279 lines (234 loc) · 9.25 KB
/
model.py
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# -*- coding: utf-8 -*-
# (c) 2021 Andreas Motl <andreas@hiveeyes.org>
# License: GNU Affero General Public License, Version 3
import dataclasses
import logging
import warnings
from collections import OrderedDict
from typing import Any, Dict, List, Optional
from urllib.parse import urljoin
from munch import Munch
logger = logging.getLogger(__name__)
@dataclasses.dataclass
class GrafanaDataModel:
admin_stats: Optional[Dict] = dataclasses.field(default_factory=dict)
dashboards: Optional[List[Munch]] = dataclasses.field(default_factory=list)
dashboard_list: Optional[List[Munch]] = dataclasses.field(default_factory=list)
datasources: Optional[List[Munch]] = dataclasses.field(default_factory=list)
folders: Optional[List[Munch]] = dataclasses.field(default_factory=list)
organizations: Optional[List[Munch]] = dataclasses.field(default_factory=list)
users: Optional[List[Munch]] = dataclasses.field(default_factory=list)
teams: Optional[List[Munch]] = dataclasses.field(default_factory=list)
annotations: Optional[List[Munch]] = dataclasses.field(default_factory=list)
snapshots: Optional[List[Munch]] = dataclasses.field(default_factory=list)
notifications: Optional[List[Munch]] = dataclasses.field(default_factory=list)
@dataclasses.dataclass
class DashboardDetails:
dashboard: Dict
@property
def panels(self) -> List:
return self.dashboard.dashboard.get("panels", [])
@property
def annotations(self) -> List:
return self.dashboard.dashboard.get("annotations", {}).get("list", [])
@property
def templating(self) -> List:
return self.dashboard.dashboard.get("templating", {}).get("list", [])
@dataclasses.dataclass
class DashboardDataDetails:
"""
Manage details concerned about "data"-relevant information,
a subset of the complete bunch of dashboard information.
"""
panels: List[Dict]
annotations: List[Dict]
templating: List[Dict]
def to_munch(self):
return Munch(panels=self.panels, annotations=self.annotations, templating=self.templating)
@classmethod
def from_dashboard_details(cls, dbdetails: DashboardDetails):
ds_panels = cls.collect_data_nodes(dbdetails.panels)
ds_annotations = cls.collect_data_nodes(dbdetails.annotations)
ds_templating = cls.collect_data_nodes(dbdetails.templating)
# Inline panel information into data details.
targets = []
for panel in ds_panels:
panel_item = cls._format_panel_compact(panel)
if "targets" in panel:
for target in panel.targets:
target["_panel"] = panel_item
targets.append(target)
return cls(panels=targets, annotations=ds_annotations, templating=ds_templating)
@staticmethod
def collect_data_nodes(element):
"""
Select all element nodes which have a "datasource" attribute.
"""
element = element or []
items = []
def add(item):
if item is not None and item not in items:
items.append(item)
for node in element:
if "datasource" in node and node["datasource"]:
add(node)
return items
@staticmethod
def _format_panel_compact(panel):
"""
Return a compact representation of panel information.
"""
attributes = ["id", "title", "type", "datasource"]
data = OrderedDict()
for attribute in attributes:
data[attribute] = panel.get(attribute)
return data
@staticmethod
def _format_data_node_compact(item: Dict) -> Dict:
"""
Return a compact representation of an element concerned about data.
"""
data = OrderedDict()
data["datasource"] = item.get("datasource")
data["type"] = item.get("type")
for key, value in item.items():
if "query" in key.lower():
data[key] = value
return data
def queries_only(self):
"""
Return a representation of data details information, only where query expressions are present.
"""
# All attributes containing query-likes.
attributes_query_likes = ["expr", "jql", "query", "rawSql", "target"]
attributes = [
# Carry over datasource and panel references for informational purposes.
"datasource",
"_panel",
] + attributes_query_likes
def transform(section):
new_items = []
for item in section:
new_item = OrderedDict()
for key, value in item.items():
if key in attributes:
new_item[key] = value
if any(attr in attributes_query_likes for attr in new_item.keys()):
# Filter annotations without any value.
if "target" in new_item and isinstance(new_item["target"], dict):
if new_item["target"].get("type") == "dashboard":
continue
# Unnest items with nested "query" slot.
for slot in ["query", "target"]:
if slot in new_item and isinstance(new_item[slot], dict) and "query" in new_item[slot]:
new_item["query"] = new_item[slot]["query"]
new_items.append(new_item)
return new_items
return DashboardDataDetails(
panels=transform(self.panels),
annotations=transform(self.annotations),
templating=transform(self.templating),
)
@dataclasses.dataclass
class DatasourceItem:
"""
Represent a datasource reference within a panel, annotation, or templating (variable).
"""
uid: Optional[str] = None
name: Optional[str] = None
type: Optional[str] = None
url: Optional[str] = None
@classmethod
def from_payload(cls, payload: any):
if isinstance(payload, Munch):
payload = dict(payload)
if isinstance(payload, dict):
cls.validate(payload)
return cls(**payload)
if isinstance(payload, str):
return cls(name=payload)
raise TypeError(f"Unknown payload type for DatasourceItem: {type(payload)}")
@classmethod
def validate(cls, data: dict):
if "datasource" in data:
warnings.warn(
f"""
The `datasource` attribute is ignored for the time being.
See also: https://github.com/grafana-toolbox/grafana-wtf/issues/110
Please report back this occurrence to the grafana-wtf issue tracker,
so the maintainers can improve the situation.
The context of this error is in `DatasourceItem`, using this ingress data:
{data}
""".strip(),
UserWarning,
)
del data["datasource"]
@dataclasses.dataclass
class DatasourceExplorationItem:
datasource: Munch
used_in: List[Munch]
grafana_url: str
def format_compact(self):
dsshort = OrderedDict(
uid=self.datasource.get("uid"),
name=self.datasource.name,
type=self.datasource.type,
url=self.datasource.url,
)
item = OrderedDict(datasource=dsshort)
for dashboard in self.used_in:
item.setdefault("dashboards", [])
dbshort = OrderedDict(
title=dashboard.dashboard.title,
uid=dashboard.dashboard.uid,
path=dashboard.meta.url,
url=urljoin(self.grafana_url, dashboard.meta.url),
)
item["dashboards"].append(dbshort)
return item
@dataclasses.dataclass
class DashboardExplorationItem:
dashboard: Munch
datasources: List[Munch]
grafana_url: str
def format(self, with_data_details: bool = False, queries_only: bool = False):
"""
Generate a representation from selected information.
- dashboard
- datasources
- details
- panels/targets
- annotations
- templating
"""
dbshort = OrderedDict(
title=self.dashboard.dashboard.title,
uid=self.dashboard.dashboard.uid,
path=self.dashboard.meta.url,
url=urljoin(self.grafana_url, self.dashboard.meta.url),
)
dsshort = []
for datasource in self.datasources:
item = OrderedDict(
uid=datasource.get("uid"),
name=datasource.name,
type=datasource.type,
url=datasource.url,
)
dsshort.append(item)
data = Munch(dashboard=dbshort, datasources=dsshort)
if with_data_details:
details = self.collect_data_details(queries_only=queries_only)
if not details.panels and not details.annotations and not details.templating:
return None
data.details = details.to_munch()
return data
def collect_data_details(self, queries_only: bool = False):
"""
Collect details concerned about data from dashboard information.
"""
dbdetails = DashboardDetails(dashboard=self.dashboard)
dbdatadetails = DashboardDataDetails.from_dashboard_details(dbdetails)
if queries_only:
dbdatadetails = dbdatadetails.queries_only()
return dbdatadetails