Skip to content

Commit 07d2f58

Browse files
committed
addpanel method in grafana wrapper
1 parent 4e6895f commit 07d2f58

File tree

1 file changed

+100
-10
lines changed

1 file changed

+100
-10
lines changed

grafana/provisioning/dashboards/wrapper.py

Lines changed: 100 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ class ExpressionAndLegendPair:
4848
class SceGrafanalibWrapper:
4949
MAX_WIDTH: Final[int] = 24
5050

51-
def __init__(self, title, panel_width=12, panel_height=8):
51+
def __init__(self, title, description="", panel_width=12, panel_height=8):
5252
self.rows = []
5353
self.panels = []
5454
self.title = title
55+
self.description = description
5556
self.current_x = 0
5657
self.current_y = 0
5758
self.panel_width = min(panel_width, self.MAX_WIDTH)
@@ -79,8 +80,7 @@ def DefineTemplating(
7980
)
8081
)
8182

82-
def AddPanel(
83-
self,
83+
def CreatePanel(self,
8484
title,
8585
queries: list[ExpressionAndLegendPair],
8686
unit="",
@@ -90,8 +90,8 @@ def AddPanel(
9090
fillOpacity=None,
9191
showPoints=None,
9292
stacking=None,
93-
extraJson=None,
94-
):
93+
extraJson=None):
94+
9595
targets = []
9696
iterator = RefIdGenerator()
9797
for query in queries:
@@ -160,18 +160,108 @@ def AddPanel(
160160
panel.unit = unit
161161
elif hasattr(panel, "format"):
162162
panel.format = unit
163-
row_or_panel = self.rows[-1].panels if self.rows else self.panels
164-
row_or_panel.append(panel)
163+
return panel
164+
165+
def AddPanel(
166+
self,
167+
title,
168+
queries: list[ExpressionAndLegendPair],
169+
unit="",
170+
dydt=False,
171+
panel_type_enum=PanelType.TIME_SERIES,
172+
lineWidth=None,
173+
fillOpacity=None,
174+
showPoints=None,
175+
stacking=None,
176+
extraJson=None,
177+
):
178+
'''
179+
Add panel under a new defined row
180+
181+
:param title: panel title
182+
:param queries: Description
183+
:type queries: list[ExpressionAndLegendPair]
184+
:param unit: Y-axis unit/format 'percent' , 'bytes/sec', 'short'
185+
:param dydt: if true aluto-generates two targets per query
186+
:param panel_type_enum: panel visualization PanelType.TIME_SERIES , PanelType.GAUGE , PanelType.BARGAUGE , PanelType.STAT
187+
:param lineWidth: line thickness 1-10
188+
:param fillOpacity: area fill transparency 0.0-1.0
189+
:param showPoints: point visibility "never" , "auto" , "always"
190+
:param stacking: stacking mode "off" , "normal" , "100%"
191+
:param extraJson: dict of raw JSON for panel constructor
192+
'''
193+
194+
panel = self.CreatePanel(
195+
title,
196+
queries,
197+
unit,
198+
dydt,
199+
panel_type_enum,
200+
lineWidth,
201+
fillOpacity,
202+
showPoints,
203+
stacking,
204+
extraJson)
205+
206+
# add the new panel as a new Row
207+
row = Row(title=title, panels=[panel])
208+
self.rows.append(row)
209+
self.current_y += self.panel_height
210+
211+
def AddPanelToRow(
212+
self,
213+
title,
214+
queries: list[ExpressionAndLegendPair],
215+
unit="",
216+
dydt=False,
217+
panel_type_enum=PanelType.TIME_SERIES,
218+
lineWidth=None,
219+
fillOpacity=None,
220+
showPoints=None,
221+
stacking=None,
222+
extraJson=None,
223+
):
224+
'''
225+
Add panel to latest defined row
226+
227+
:param title: panel title
228+
:param queries: Description
229+
:type queries: list[ExpressionAndLegendPair]
230+
:param unit: Y-axis unit/format 'percent' , 'bytes/sec', 'short'
231+
:param dydt: if true aluto-generates two targets per query
232+
:param panel_type_enum: panel visualization PanelType.TIME_SERIES , PanelType.GAUGE , PanelType.BARGAUGE , PanelType.STAT
233+
:param lineWidth: line thickness 1-10
234+
:param fillOpacity: area fill transparency 0.0-1.0
235+
:param showPoints: point visibility "never" , "auto" , "always"
236+
:param stacking: stacking mode "off" , "normal" , "100%"
237+
:param extraJson: dict of raw JSON for panel constructor
238+
'''
239+
if not self.rows:
240+
raise ValueError("No rows defined for this dashboard")
241+
242+
panel = self.CreatePanel(
243+
title,
244+
queries,
245+
unit,
246+
dydt,
247+
panel_type_enum,
248+
lineWidth,
249+
fillOpacity,
250+
showPoints,
251+
stacking,
252+
extraJson)
253+
254+
self.rows[-1].panels.append(panel)
165255
self.current_x += self.panel_width
166256
if self.current_x >= self.MAX_WIDTH:
167257
self.current_y += self.panel_height
168258
self.current_x = 0
169-
259+
170260
def Render(self):
261+
valid_rows = [r for r in self.rows if r.panels]
171262
return Dashboard(
172263
title=self.title,
173-
rows=self.rows,
174-
panels=self.panels,
264+
rows=valid_rows,
175265
timezone="browser",
176266
templating=Templating(list=self.templates),
177267
).auto_panel_ids()

0 commit comments

Comments
 (0)