Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 99 additions & 7 deletions grafana/provisioning/dashboards/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ class ExpressionAndLegendPair:
class SceGrafanalibWrapper:
MAX_WIDTH: Final[int] = 24

def __init__(self, title, panel_width=12, panel_height=8):
def __init__(self, title, description="", panel_width=12, panel_height=8):
self.rows = []
self.panels = []
self.title = title
self.description = description
self.current_x = 0
self.current_y = 0
self.panel_width = min(panel_width, self.MAX_WIDTH)
Expand Down Expand Up @@ -79,8 +80,7 @@ def DefineTemplating(
)
)

def AddPanel(
self,
def CreatePanel(self,
title,
queries: list[ExpressionAndLegendPair],
unit="",
Expand All @@ -90,8 +90,8 @@ def AddPanel(
fillOpacity=None,
showPoints=None,
stacking=None,
extraJson=None,
):
extraJson=None):

targets = []
iterator = RefIdGenerator()
for query in queries:
Expand Down Expand Up @@ -160,18 +160,110 @@ def AddPanel(
panel.unit = unit
elif hasattr(panel, "format"):
panel.format = unit
return panel

def AddPanelToRow(
self,
title,
queries: list[ExpressionAndLegendPair],
unit="",
dydt=False,
panel_type_enum=PanelType.TIME_SERIES,
lineWidth=None,
fillOpacity=None,
showPoints=None,
stacking=None,
extraJson=None,
):
'''
Add panel to latest defined row

:param title: panel title
:param queries: Description
:type queries: list[ExpressionAndLegendPair]
:param unit: Description
:param dydt: Description
:param panel_type_enum: Description
:param lineWidth: Description
:param fillOpacity: Description
:param showPoints: Description
:param stacking: Description
:param extraJson: Description
'''
if not self.rows:
raise ValueError("No rows defined for this dashboard")

panel = self.CreatePanel(
title,
queries,
unit,
dydt,
panel_type_enum,
lineWidth,
fillOpacity,
showPoints,
stacking,
extraJson)

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 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,
):
'''
Add panel to latest defined row

:param title: panel title
:param queries: Description
:type queries: list[ExpressionAndLegendPair]
:param unit: Description
:param dydt: Description
:param panel_type_enum: Description
:param lineWidth: Description
:param fillOpacity: Description
:param showPoints: Description
:param stacking: Description
:param extraJson: Description
'''

panel = self.CreatePanel(
title,
queries,
unit,
dydt,
panel_type_enum,
lineWidth,
fillOpacity,
showPoints,
stacking,
extraJson)

# add the new panel as a new Row
row = Row(title=title, panels=[panel])
self.rows.append(row)
self.current_y += self.panel_height

def Render(self):
valid_rows = [r for r in self.rows if r.panels]
return Dashboard(
title=self.title,
rows=self.rows,
panels=self.panels,
rows=valid_rows,
timezone="browser",
templating=Templating(list=self.templates),
).auto_panel_ids()