Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ x.x.x ?
* Fix mappings for Table
* Added support for AWS Cross-Account in CloudwatchMetricsTarget
* Added `LokiTarget`
* Added Status_History_ panel support

.. _`Status_History`: https://grafana.com/docs/grafana/latest/panels-visualizations/visualizations/status-history

0.7.1 2024-01-12
================
Expand Down
91 changes: 91 additions & 0 deletions grafanalib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def to_json_data(self):
LOGS_TYPE = 'logs'
HEATMAP_TYPE = 'heatmap'
STATUSMAP_TYPE = 'flant-statusmap-panel'
STATUS_HISTORY_TYPE = 'status-history'
SVG_TYPE = 'marcuscalidus-svg-panel'
PIE_CHART_TYPE = 'grafana-piechart-panel'
PIE_CHART_V2_TYPE = 'piechart'
Expand Down Expand Up @@ -3741,6 +3742,96 @@ def to_json_data(self):
return self.panel_json(graphObject)


@attr.s
class StatusHistory(Panel):
"""Generates Status History panel json structure (https://grafana.com/docs/grafana/latest/panels-visualizations/visualizations/status-history/)

:param showHistoryValues: define how the history values appear in your visualization
auto (Default), always, never
:param rowHeight: how tall row in visialization should be
:param colWidth: how wide cells in visialization should be
:param lineWidth: how thick lines in visialization should be
:param fillOpacity: fillOpacity
:param legendShow: show legend (enabled by default)
:param legendDisplayMode: define how the legend appear in your visualization
list (Default), table, hidden
:param tooltipMode: When you hover your cursor over the visualization, Grafana can display tooltips
single (Default), multi, none
:param tooltipSort: To sort the tooltips
none (Default), asc, desc
:param format: units
:param valueMin: Minimum value for Panel
:param valueMax: Maximum value for Panel
:param valueDecimals: Number of display decimals
:param displayName: Change the field or series name
:param colorMode: Color mode
palette-classic (Default),
:param noValue: What to display when there are no values
:param dataLinks: Additional web links to be presented for data
:param mappings: To assign colors to boolean or string values, use Value mappings
"""

showHistoryValues = attr.ib(default='auto', validator=instance_of(str))
rowHeight = attr.ib(default=1.0, validator=instance_of(float))
colWidth = attr.ib(default=1.0, validator=instance_of(float))
lineWidth = attr.ib(default=5, validator=instance_of(int))
fillOpacity = attr.ib(default=70, validator=instance_of(int))
legendShow = attr.ib(default=True, validator=instance_of(bool))
legendDisplayMode = attr.ib(default='list', validator=instance_of(str))
legendPlacement = attr.ib(default='bottom', validator=instance_of(str))
tooltipMode = attr.ib(default='single', validator=instance_of(str))
tooltipSort = attr.ib(default='none', validator=instance_of(str))
format = attr.ib(default='', validator=instance_of(str))
valueMin = attr.ib(default=None, validator=attr.validators.optional(instance_of(int)))
valueMax = attr.ib(default=None, validator=attr.validators.optional(instance_of(int)))
valueDecimals = attr.ib(default=None, validator=attr.validators.optional(instance_of(int)))
displayName = attr.ib(default='', validator=instance_of(str))
colorMode = attr.ib(default='thresholds', validator=instance_of(str))
noValue = attr.ib(default='', validator=instance_of(str))
dataLinks = attr.ib(default=attr.Factory(list))
mappings = attr.ib(default=attr.Factory(list))

def to_json_data(self):
return self.panel_json(
{
'fieldConfig': {
'defaults': {
'color': {
'mode': self.colorMode
},
'unit': self.format,
'custom': {
'fillOpacity': self.fillOpacity,
'lineWidth': self.lineWidth,
},
'displayName': self.displayName,
"min": self.valueMin,
"max": self.valueMax,
'decimals': self.valueDecimals,
'noValue': self.noValue,
'links': self.dataLinks,
'mappings': self.mappings
}
},
'options': {
'showValue': self.showHistoryValues,
'rowHeight': self.rowHeight,
'colWidth': self.colWidth,
'legend': {
'displayMode': self.legendDisplayMode,
'showLegend': self.legendShow,
'placement': self.legendPlacement
},
'tooltip': {
'mode': self.tooltipMode,
'sort': self.tooltipSort
}
},
'type': STATUS_HISTORY_TYPE
}
)


@attr.s
class Svg(Panel):
"""Generates SVG panel json structure
Expand Down
25 changes: 25 additions & 0 deletions grafanalib/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,31 @@ def test_default_heatmap():
assert h.to_json_data()["options"] == []


def test_status_history_default():
data_source = "dummy data source"
targets = ["dummy_prom_query"]
title = "dummy title"
panel = G.StatusHistory(data_source, targets, title)

data = panel.to_json_data()

assert data['type'] == G.STATUS_HISTORY_TYPE
assert data['options']['colWidth'] == 1.0
assert data['options']['rowHeight'] == 1.0
assert data['options']['showValue'] == 'auto'


def test_status_history():
data_source = "dummy data source"
targets = ["dummy_prom_query"]
title = "dummy title"
showHistoryValues = 'never'
panel = G.StatusHistory(data_source, targets, title, showHistoryValues = showHistoryValues)
data = panel.to_json_data()

assert data['options']['showValue'] == showHistoryValues


class TestDashboardLink():

def test_validators(self):
Expand Down