Skip to content

Commit bea35f2

Browse files
authored
Merge branch 'master' into system_status
2 parents 68b44dd + 94cc003 commit bea35f2

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

grafana/provisioning/dashboards/wrapper.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
from dataclasses import dataclass
22
from typing import Optional, Final
33

4-
from grafanalib.core import Row, Dashboard, Target, TimeSeries, GridPos
4+
from enum import Enum
5+
6+
from grafanalib.core import Row, Dashboard, Target, TimeSeries, GridPos, GaugePanel
57

68
from common import PROMETHEUS_DATASOURCE_NAME
79

810

11+
class PanelType(Enum):
12+
TIME_SERIES = TimeSeries
13+
GAUGE = GaugePanel
14+
15+
916
class RefIdGenerator:
1017
STARTING_CHAR_INTEGER = ord("A")
1118

@@ -39,7 +46,14 @@ def __init__(self, title, panel_width=12, panel_height=8):
3946
def DefineRow(self, title):
4047
self.rows.append(Row(title=title, panels=[]))
4148

42-
def AddPanel(self, title, queries: list[ExpressionAndLegendPair], unit='', dydt=False):
49+
def AddPanel(
50+
self,
51+
title,
52+
queries: list[ExpressionAndLegendPair],
53+
unit="",
54+
dydt=False,
55+
panel_type_enum=PanelType.TIME_SERIES,
56+
):
4357
targets = []
4458
iterator = RefIdGenerator()
4559
for query in queries:
@@ -80,27 +94,27 @@ def AddPanel(self, title, queries: list[ExpressionAndLegendPair], unit='', dydt=
8094
datasource=PROMETHEUS_DATASOURCE_NAME,
8195
)
8296
)
83-
row_or_panel = self.rows[-1].panels if self.rows else self.panels
84-
row_or_panel.append(
85-
TimeSeries(
86-
title=title,
87-
targets=targets,
88-
gridPos=GridPos(
89-
h=self.panel_height,
90-
w=self.panel_width,
91-
x=self.current_x,
92-
y=self.current_y,
93-
),
94-
lineWidth=2,
95-
unit=unit
96-
)
97+
panel = panel_type_enum.value(
98+
title=title,
99+
targets=targets,
100+
gridPos=GridPos(
101+
h=self.panel_height,
102+
w=self.panel_width,
103+
x=self.current_x,
104+
y=self.current_y,
105+
),
97106
)
107+
# maybe only a few of the panel types are missing the unit field
108+
unit_var = "unit" if hasattr(panel_type_enum.value, "unit") else "format"
109+
setattr(panel, unit_var, unit)
110+
row_or_panel = self.rows[-1].panels if self.rows else self.panels
111+
row_or_panel.append(panel)
98112
self.current_x += self.panel_width
99113
if self.current_x > self.MAX_WIDTH / 2:
100114
self.current_y += self.panel_height
101115
self.current_x = 0
102116

103117
def Render(self):
104118
return Dashboard(
105-
title=self.title, rows=self.rows, panels=self.panels, timezone='browser'
119+
title=self.title, rows=self.rows, panels=self.panels, timezone="browser"
106120
).auto_panel_ids()

0 commit comments

Comments
 (0)