diff --git a/dashboards/panels.py b/dashboards/panels.py index b6f1c3b92b89a21fda35f2c80d3cccc98e986ea3..766fcee51095df9a2eb497099f2e5ae19e2e12a9 100644 --- a/dashboards/panels.py +++ b/dashboards/panels.py @@ -1,10 +1,22 @@ from dataclasses import dataclass, field + # from collections import namedtuple from dashboards.influx_queries import Query -from grafanalib.core import TimeSeries, Text, Stat, Template, Repeat, Threshold, Table, BarChart, PieChartv2 +from grafanalib.core import ( + TimeSeries, + Text, + Stat, + Template, + Repeat, + Threshold, + Table, + BarChart, + PieChartv2, +) from dashboards.dashboard_base import get_influx_target from dashboards.legends import Units from numbers import Number +from typing import List # PanelInfos = namedtuple("PanelInfos", ("name", "unit")) @@ -20,30 +32,49 @@ def is_regex(name): return name[0] == "/" and name[-1] == "/" -def get_time_series_panel(panel_infos: PanelInfos, - data_source: str, - query_list: list[Query], - *, - overrides=None, - pointSize: int = 9, - **kwargs): +def get_time_series_panel( + panel_infos: PanelInfos, + data_source: str, + query_list: List[Query], + *, + overrides=None, + pointSize: int = 9, + **kwargs, +): targets = [get_influx_target(str(query)) for query in query_list] new_kwargs = {**kwargs} if panel_infos.absthreshold is not None: - if 'thresholdsStyleMode' not in new_kwargs: - new_kwargs.update({ - 'thresholdType': 'absolute', - 'thresholds': [Threshold('green', 0, 0.0), - Threshold('red', index=1, value=float(panel_infos.absthreshold), op='lt')], - 'thresholdsStyleMode': 'line' - }) + if "thresholdsStyleMode" not in new_kwargs: + new_kwargs.update( + { + "thresholdType": "absolute", + "thresholds": [ + Threshold("green", 0, 0.0), + Threshold( + "red", + index=1, + value=float(panel_infos.absthreshold), + op="lt", + ), + ], + "thresholdsStyleMode": "line", + } + ) else: - new_kwargs.update({ - 'thresholdType': 'absolute', - 'thresholds': [Threshold('green', 0, 0.0), - Threshold('red', index=1, value=float(panel_infos.absthreshold), op='lt')] - }) - + new_kwargs.update( + { + "thresholdType": "absolute", + "thresholds": [ + Threshold("green", 0, 0.0), + Threshold( + "red", + index=1, + value=float(panel_infos.absthreshold), + op="lt", + ), + ], + } + ) return TimeSeries( title=panel_infos.name, @@ -53,71 +84,85 @@ def get_time_series_panel(panel_infos: PanelInfos, pointSize=pointSize, overrides=overrides, **new_kwargs, - ) -def get_text_panel(content: str, *, mode='markdown', **kwargs) -> Text: - return Text( - content=content, - mode=mode, - **kwargs - ) +def get_text_panel(content: str, *, mode="markdown", **kwargs) -> Text: + return Text(content=content, mode=mode, **kwargs) -def get_stat_panel(title: str, - dataSource: str, - stat_query: Query, - repeat: Template = None, - alias: str = "", - *, - maxPerRow=0, - **kwargs): +def get_stat_panel( + title: str, + dataSource: str, + stat_query: Query, + repeat: Template = None, + alias: str = "", + *, + maxPerRow=0, + **kwargs, +): new_kwargs = { - 'alignment': 'center', - 'colorMode': 'value', - 'graphMode': 'area', - 'reduceCalc': 'last', - 'orientation': 'auto', - 'transparent': True, + "alignment": "center", + "colorMode": "value", + "graphMode": "area", + "reduceCalc": "last", + "orientation": "auto", + "transparent": True, } new_kwargs.update(kwargs) if repeat: - rep_args = ['h', repeat.name] + rep_args = ["h", repeat.name] if maxPerRow: rep_args.append(maxPerRow) - new_kwargs.setdefault('repeat', Repeat(*rep_args)) + new_kwargs.setdefault("repeat", Repeat(*rep_args)) return Stat( title=title, dataSource=dataSource, targets=[get_influx_target(str(stat_query), alias=alias)], - thresholdType='percentage', - thresholds=[Threshold('green', 0, 0.0), Threshold('yellow', 1, 50.0), Threshold('red', 2, 80.0)], - ** new_kwargs, + thresholdType="percentage", + thresholds=[ + Threshold("green", 0, 0.0), + Threshold("yellow", 1, 50.0), + Threshold("red", 2, 80.0), + ], + **new_kwargs, ) -def get_table_panel(panel_infos: PanelInfos, - data_source: str, - query_list: list[Query], - *, - result_format_list: list[str] = None, - alias_list: list[str] = None, - transformations=[], - overrides=None, - **kwargs): +def get_table_panel( + panel_infos: PanelInfos, + data_source: str, + query_list: List[Query], + *, + result_format_list: List[str] = None, + alias_list: List[str] = None, + transformations=[], + overrides=None, + **kwargs, +): if not alias_list: - alias_list = [''] * len(query_list) + alias_list = [""] * len(query_list) if not result_format_list: - result_format_list = ['table'] * len(query_list) - targets = [get_influx_target(str(query), result_format=result_format, alias=alias) for query, result_format, alias in zip(query_list, result_format_list, alias_list)] + result_format_list = ["table"] * len(query_list) + targets = [ + get_influx_target(str(query), result_format=result_format, alias=alias) + for query, result_format, alias in zip( + query_list, result_format_list, alias_list + ) + ] new_kwargs = {**kwargs} if panel_infos.absthreshold is not None: - new_kwargs.update({'thresholdType': 'absolute', - 'thresholds': [Threshold('green', 0, 0.0), - Threshold('red', index=1, value=float(panel_infos.absthreshold), op='lt'), ], - } - ) + new_kwargs.update( + { + "thresholdType": "absolute", + "thresholds": [ + Threshold("green", 0, 0.0), + Threshold( + "red", index=1, value=float(panel_infos.absthreshold), op="lt" + ), + ], + } + ) return Table( title=panel_infos.name, @@ -127,80 +172,106 @@ def get_table_panel(panel_infos: PanelInfos, unit=panel_infos.unit, overrides=overrides, **new_kwargs, - ) -def get_bar_chart_panel(panel_infos: PanelInfos, - data_source: str, - query_list: list[Query], - *, - result_format_list: list[str] = None, - alias_list: list[str] = None, - transformations=[], - overrides=None, - **kwargs): +def get_bar_chart_panel( + panel_infos: PanelInfos, + data_source: str, + query_list: List[Query], + *, + result_format_list: List[str] = None, + alias_list: List[str] = None, + transformations=[], + overrides=None, + **kwargs, +): if not alias_list: - alias_list = [''] * len(query_list) + alias_list = [""] * len(query_list) if not result_format_list: - result_format_list = ['table'] * len(query_list) - targets = [get_influx_target(str(query), result_format=result_format, alias=alias) for query, result_format, alias in zip(query_list, result_format_list, alias_list)] + result_format_list = ["table"] * len(query_list) + targets = [ + get_influx_target(str(query), result_format=result_format, alias=alias) + for query, result_format, alias in zip( + query_list, result_format_list, alias_list + ) + ] new_kwargs = {**kwargs} if panel_infos.absthreshold is not None: - new_kwargs.update({'thresholdType': 'absolute', - 'thresholds': [Threshold('green', 0, 0.0), - Threshold('red', index=1, value=float(panel_infos.absthreshold), op='lt'), ], - } - ) - extraJson = {"fieldConfig": { - "defaults": { - "fieldMinMax": True, - "max": 1, - "unit": panel_infos.unit + new_kwargs.update( + { + "thresholdType": "absolute", + "thresholds": [ + Threshold("green", 0, 0.0), + Threshold( + "red", index=1, value=float(panel_infos.absthreshold), op="lt" + ), + ], + } + ) + extraJson = { + "fieldConfig": { + "defaults": {"fieldMinMax": True, "max": 1, "unit": panel_infos.unit} } } - } - + return BarChart( title=panel_infos.name, dataSource=data_source, targets=targets, transformations=transformations, - xTickLabelRotation=-45, + xTickLabelRotation=-45, extraJson=extraJson, **new_kwargs, - ) -def get_pie_chart_panel(panel_infos: PanelInfos, - data_source: str, - query_list: list[Query], - *, - result_format_list: list[str] = None, - alias_list: list[str] = None, - transformations=[], - overrides=[], - **kwargs): - +def get_pie_chart_panel( + panel_infos: PanelInfos, + data_source: str, + query_list: List[Query], + *, + result_format_list: List[str] = None, + alias_list: List[str] = None, + transformations=[], + overrides=[], + **kwargs, +): targets = [get_influx_target(str(query)) for query in query_list] new_kwargs = {**kwargs} if panel_infos.absthreshold is not None: - if 'thresholdsStyleMode' not in new_kwargs: - new_kwargs.update({ - 'thresholdType': 'absolute', - 'thresholds': [Threshold('green', 0, 0.0), - Threshold('red', index=1, value=float(panel_infos.absthreshold), op='lt')], - 'thresholdsStyleMode': 'line' - }) + if "thresholdsStyleMode" not in new_kwargs: + new_kwargs.update( + { + "thresholdType": "absolute", + "thresholds": [ + Threshold("green", 0, 0.0), + Threshold( + "red", + index=1, + value=float(panel_infos.absthreshold), + op="lt", + ), + ], + "thresholdsStyleMode": "line", + } + ) else: - new_kwargs.update({ - 'thresholdType': 'absolute', - 'thresholds': [Threshold('green', 0, 0.0), - Threshold('red', index=1, value=float(panel_infos.absthreshold), op='lt')] - }) + new_kwargs.update( + { + "thresholdType": "absolute", + "thresholds": [ + Threshold("green", 0, 0.0), + Threshold( + "red", + index=1, + value=float(panel_infos.absthreshold), + op="lt", + ), + ], + } + ) - return PieChartv2( title=panel_infos.name, dataSource=data_source, @@ -209,5 +280,4 @@ def get_pie_chart_panel(panel_infos: PanelInfos, unit=panel_infos.unit, overrides=overrides, **new_kwargs, - )