|
1 | 1 | from dataclasses import dataclass |
2 | 2 | from typing import Optional, Final |
3 | 3 |
|
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 |
5 | 7 |
|
6 | 8 | from common import PROMETHEUS_DATASOURCE_NAME |
7 | 9 |
|
8 | 10 |
|
| 11 | +class PanelType(Enum): |
| 12 | + TIME_SERIES = TimeSeries |
| 13 | + GAUGE = GaugePanel |
| 14 | + |
| 15 | + |
9 | 16 | class RefIdGenerator: |
10 | 17 | STARTING_CHAR_INTEGER = ord("A") |
11 | 18 |
|
@@ -39,7 +46,14 @@ def __init__(self, title, panel_width=12, panel_height=8): |
39 | 46 | def DefineRow(self, title): |
40 | 47 | self.rows.append(Row(title=title, panels=[])) |
41 | 48 |
|
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 | + ): |
43 | 57 | targets = [] |
44 | 58 | iterator = RefIdGenerator() |
45 | 59 | for query in queries: |
@@ -80,27 +94,27 @@ def AddPanel(self, title, queries: list[ExpressionAndLegendPair], unit='', dydt= |
80 | 94 | datasource=PROMETHEUS_DATASOURCE_NAME, |
81 | 95 | ) |
82 | 96 | ) |
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 | + ), |
97 | 106 | ) |
| 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) |
98 | 112 | self.current_x += self.panel_width |
99 | 113 | if self.current_x > self.MAX_WIDTH / 2: |
100 | 114 | self.current_y += self.panel_height |
101 | 115 | self.current_x = 0 |
102 | 116 |
|
103 | 117 | def Render(self): |
104 | 118 | 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" |
106 | 120 | ).auto_panel_ids() |
0 commit comments