-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathwrapper.py
More file actions
181 lines (161 loc) · 5.14 KB
/
wrapper.py
File metadata and controls
181 lines (161 loc) · 5.14 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
from dataclasses import dataclass
from typing import Optional, Final
from enum import Enum
from grafanalib.core import (
Row,
Dashboard,
Target,
TimeSeries,
GridPos,
GaugePanel,
BarGauge,
Stat,
Template,
Templating,
REFRESH_ON_TIME_RANGE_CHANGE,
)
from common import PROMETHEUS_DATASOURCE_NAME
class PanelType(Enum):
TIME_SERIES = TimeSeries
GAUGE = GaugePanel
BARGAUGE = BarGauge
STAT = Stat
class RefIdGenerator:
STARTING_CHAR_INTEGER = ord("A")
def __init__(self):
self.offset = 0
def next(self) -> str:
result = chr(self.STARTING_CHAR_INTEGER + self.offset)
self.offset += 1
return result
@dataclass
class ExpressionAndLegendPair:
expression: str
legend: Optional[str] = None
class SceGrafanalibWrapper:
MAX_WIDTH: Final[int] = 24
def __init__(self, title, panel_width=12, panel_height=8):
self.rows = []
self.panels = []
self.title = title
self.current_x = 0
self.current_y = 0
self.panel_width = min(panel_width, self.MAX_WIDTH)
self.panel_height = panel_height
self.templates = []
def DefineRow(self, title):
self.rows.append(Row(title=title, panels=[]))
def DefineTemplating(
self,
label,
query,
):
self.templates.append(
Template(
name= label.lower().replace(" ", "_"),
label=label,
query=query,
dataSource=PROMETHEUS_DATASOURCE_NAME,
includeAll=True,
multi=True,
refresh=REFRESH_ON_TIME_RANGE_CHANGE,
)
)
def AddPanel(
self,
title,
queries: list[ExpressionAndLegendPair],
unit="",
dydt=False,
panel_type_enum=PanelType.TIME_SERIES,
lineWidth=None,
fillOpacity=None,
showPoints=None,
stacking=None,
extraJson=None,
tooltipMode='single',
tooltipSort=None,
):
targets = []
iterator = RefIdGenerator()
for query in queries:
query_text = query.expression
query_label = query.legend
if not dydt:
targets.append(
Target(
expr=query_text,
legendFormat=query_label,
refId=iterator.next(),
datasource=PROMETHEUS_DATASOURCE_NAME,
)
)
continue
total_query = f"sum({query_text})"
rate_query = f"sum(rate({query_text}[1h]))"
total_label = "total"
rate_label = "dY/dt [hourly]"
if query_label:
total_query += f' by ({query_label.strip("{}")})'
rate_query += f' by ({query_label.strip("{}")})'
total_label = f"{query_label} " + total_label
rate_label = f"{query_label} " + rate_label
targets.append(
Target(
expr=total_query,
legendFormat=total_label,
refId=iterator.next(),
datasource=PROMETHEUS_DATASOURCE_NAME,
)
)
targets.append(
Target(
expr=rate_query + " * 3600",
legendFormat=rate_label,
refId=iterator.next(),
datasource=PROMETHEUS_DATASOURCE_NAME,
)
)
panel = panel_type_enum.value(
title=title,
targets=targets,
gridPos=GridPos(
h=self.panel_height,
w=self.panel_width,
x=self.current_x,
y=self.current_y,
),
extraJson=extraJson,
tooltipMode=tooltipMode,
tooltipSort=tooltipSort
)
if isinstance(panel, TimeSeries):
if fillOpacity is not None:
panel.fillOpacity = fillOpacity
if showPoints is not None:
panel.showPoints = showPoints
if stacking is not None:
panel.stacking = stacking
if lineWidth is not None:
panel.lineWidth = lineWidth
# maybe only a few of the panel types are missing the unit field
# unit_var = "unit" if hasattr(panel_type_enum.value, "unit") else "format"
# setattr(panel, unit_var, unit)
if hasattr(panel, "unit"):
panel.unit = unit
elif hasattr(panel, "format"):
panel.format = unit
row_or_panel = self.rows[-1].panels if self.rows else self.panels
row_or_panel.append(panel)
self.current_x += self.panel_width
if self.current_x >= self.MAX_WIDTH:
self.current_y += self.panel_height
self.current_x = 0
def Render(self):
return Dashboard(
title=self.title,
rows=self.rows,
panels=self.panels,
timezone="browser",
templating=Templating(list=self.templates),
).auto_panel_ids()