Skip to content

Commit e13e5c1

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

1 file changed

Lines changed: 99 additions & 7 deletions

File tree

grafana/provisioning/dashboards/wrapper.py

Lines changed: 99 additions & 7 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,110 @@ def AddPanel(
160160
panel.unit = unit
161161
elif hasattr(panel, "format"):
162162
panel.format = unit
163+
return panel
164+
165+
def AddPanelToRow(
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 to latest defined row
180+
181+
:param title: panel title
182+
:param queries: Description
183+
:type queries: list[ExpressionAndLegendPair]
184+
:param unit: Description
185+
:param dydt: Description
186+
:param panel_type_enum: Description
187+
:param lineWidth: Description
188+
:param fillOpacity: Description
189+
:param showPoints: Description
190+
:param stacking: Description
191+
:param extraJson: Description
192+
'''
193+
if not self.rows:
194+
raise ValueError("No rows defined for this dashboard")
195+
196+
panel = self.CreatePanel(
197+
title,
198+
queries,
199+
unit,
200+
dydt,
201+
panel_type_enum,
202+
lineWidth,
203+
fillOpacity,
204+
showPoints,
205+
stacking,
206+
extraJson)
207+
163208
row_or_panel = self.rows[-1].panels if self.rows else self.panels
164209
row_or_panel.append(panel)
165210
self.current_x += self.panel_width
166211
if self.current_x >= self.MAX_WIDTH:
167212
self.current_y += self.panel_height
168213
self.current_x = 0
214+
215+
216+
def AddPanel(
217+
self,
218+
title,
219+
queries: list[ExpressionAndLegendPair],
220+
unit="",
221+
dydt=False,
222+
panel_type_enum=PanelType.TIME_SERIES,
223+
lineWidth=None,
224+
fillOpacity=None,
225+
showPoints=None,
226+
stacking=None,
227+
extraJson=None,
228+
):
229+
'''
230+
Add panel to latest defined row
231+
232+
:param title: panel title
233+
:param queries: Description
234+
:type queries: list[ExpressionAndLegendPair]
235+
:param unit: Description
236+
:param dydt: Description
237+
:param panel_type_enum: Description
238+
:param lineWidth: Description
239+
:param fillOpacity: Description
240+
:param showPoints: Description
241+
:param stacking: Description
242+
:param extraJson: Description
243+
'''
244+
245+
panel = self.CreatePanel(
246+
title,
247+
queries,
248+
unit,
249+
dydt,
250+
panel_type_enum,
251+
lineWidth,
252+
fillOpacity,
253+
showPoints,
254+
stacking,
255+
extraJson)
256+
257+
# add the new panel as a new Row
258+
row = Row(title=title, panels=[panel])
259+
self.rows.append(row)
260+
self.current_y += self.panel_height
169261

170262
def Render(self):
263+
valid_rows = [r for r in self.rows if r.panels]
171264
return Dashboard(
172265
title=self.title,
173-
rows=self.rows,
174-
panels=self.panels,
266+
rows=valid_rows,
175267
timezone="browser",
176268
templating=Templating(list=self.templates),
177269
).auto_panel_ids()

0 commit comments

Comments
 (0)