Dashboard

One base data class to create a dashboard is provided.

grafanacode: base class for generating dashboards

class grafanacode.plugins.dashboard_base.Dashboard(debuglevel=1, overwrite=False, filename=None, folderid=None, folderuid=None, annotations=_Nothing.NOTHING, description='', editable=True, hidecontrols=False, id=None, inputs=_Nothing.NOTHING, links=_Nothing.NOTHING, panels=_Nothing.NOTHING, refresh='', schemaversion=36, style='dark', tags=_Nothing.NOTHING, templating=_Nothing.NOTHING, time=_Nothing.NOTHING, timepicker=_Nothing.NOTHING, timezone='Europe/Brussels', title=None, version=0, uid=None, extraprops=None)[source]

Bases: object

Dashboard dataclass. This class encapsulates all properties and panels of a single Grafana dashboard. It creates the final Grafana dashboard JSON by means of its getJson method.

  • param debuglevel:
    • default: 1 - type: int

  • param d_json:
    • read only

  • param overwrite:
    • type: bool

  • param filename:
    • default: None - type: str

  • param folderid:
    • default: None - optional - type: str

  • param folderuid:
    • default: None - optional - type: str

  • param annotations:
    • default: Factory(factory=<class ‘grafanacode.plugins.dashboard_base.PropAnnotations’>, takes_self=False) - type: grafanacode.plugins.dashboard_base.PropAnnotations

  • param description:
    • type: str

  • param editable:
    • default: True - type: bool

  • param hidecontrols:
    • type: bool

  • param id:
    • default: None - optional - type: int

  • param inputs:
    • default: Factory(factory=<class ‘list’>, takes_self=False) - type: list

  • param links:
    • default: Factory(factory=<class ‘list’>, takes_self=False) - type: list - of: grafanacode.plugins.dashboard_base.PropDashboardLinkItem

  • param panels:
    • default: Factory(factory=<class ‘list’>, takes_self=False) - type: list

  • param refresh:
    • type: str

  • param schemaversion:
    • default: 36 - optional - type: int

  • param style:
    • default: dark - optional - in: [‘dark’, ‘light’]

  • param tags:
    • default: Factory(factory=<class ‘list’>, takes_self=False) - type: list - of: str

  • param templating:
    • default: Factory(factory=<class ‘grafanacode.plugins.dashboard_base.PropTemplating’>, takes_self=False) - type: grafanacode.plugins.dashboard_base.PropTemplating

  • param time:
    • default: Factory(factory=<class ‘grafanacode.plugins.dashboard_base.PropTime’>, takes_self=False) - optional - type: grafanacode.plugins.dashboard_base.PropTime

  • param timepicker:
    • default: Factory(factory=<class ‘grafanacode.plugins.dashboard_base.PropTimePicker’>, takes_self=False) - optional - type: grafanacode.plugins.dashboard_base.PropTimePicker

  • param timezone:
    • default: Europe/Brussels - optional - type: str

  • param title:
    • default: None - validator: _AndValidator(_validators=(<instance_of validator for type <class ‘str’>>, <function Dashboard._check_title at 0x7fa54d19adc0>))

  • param version:
    • optional - type: int

  • param uid:
    • default: None - optional - type: str

  • param extraprops:
    • default: None - optional - type: dict

addPanel(panel)[source]

Add a panel class to this dashboard

Parameters:

panel (PanelBase or derived) – the panel dataclass object to be added to the dashboard

getDashboardJson(overwrite=False, debuglevel=1)[source]

Create the json to pack the complete dashboard All included dataclasses will be resolved with their own _getJson method.

Parameters:

customjson (dict) – custom or child class json code to add

Returns:

dict – panel json code

getJson(dashboard=None, panel=None)[source]

Create the json to pack the ‘dashboard’ root element.

Parameters:
  • dashboard (Dashboard) – Dashboard object we are in

  • panel (Panel_base) – Panel object we are in

Returns:

dict – panel json code

loglevel: int = 20
logstr: str = ''
class grafanacode.plugins.dashboard_base.DashboardEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: JSONEncoder

Encode dashboard objects for json.dumps

Here we also store the dashboard and panel object. This is because in the case of something like:

hdashboard.addPanel(PanelText(gridpos=PropGridPos(3, 16, 0, 0)))

The PropGridpos object does not know it belongs to that PanelText object and not to that Dashboard object. Even so the PanelText object does not know it belongs to that Dashboard object. Worse: in fact the can belong to many parents.

But if f.i. we want to use the dashboard.style to color some property, we need to know in which ‘parent’ we are. The solution lies in the fact the rendering of the dashboards goes form dashboard -> panels -> component -> … So here in this JSONEncoder, we store the dashboard and panel if we receive an object of that type and we use thes store values to execute the objects getJson(self, dashboard=None, panel=None) property.

See: getjson = getattr(obj, 'getJson', self.dashboard, self.panel)

dashboard = _CountingAttr(counter=436, _default=None, repr=True, eq=True, order=True, hash=None, init=False, on_setattr=None, alias=None, metadata={})
default(o)[source]

see JSONEncoder documentation

panel = _CountingAttr(counter=437, _default=None, repr=True, eq=True, order=True, hash=None, init=False, on_setattr=None, alias=None, metadata={})
class grafanacode.plugins.dashboard_base.PropAnnotations(datasource={'type': 'datasource', 'uid': 'grafana'}, enable=True, hide=True, iconcolor=RGBA(r=0, g=211, b=255, a=1), name='Annotations & Alerts', limit=100, matchany=False, tags=[], targettype='dashboard', atype='dashboard')[source]

Bases: object

Dataclass encapsulating the dashboard annotations

  • param datasource:
    • default: {‘type’: ‘datasource’, ‘uid’: ‘grafana’} - optional - type: dict

  • param enable:
    • default: True - type: bool

  • param hide:
    • default: True - type: bool

  • param iconcolor:
    • default: RGBA(r=0, g=211, b=255, a=1) - optional - type: str

  • param name:
    • default: Annotations & Alerts - type: str

  • param limit:
    • default: 100 - type: int

  • param matchany:
    • type: bool

  • param tags:
    • type: list

  • param targettype:
    • default: dashboard - type: str

  • param atype:
    • default: dashboard - type: str

getJson(dashboard=None, panel=None)[source]

Create the json to pack this item.

Parameters:
  • dashboard (Dashboard) – Dashboard object we are in

  • panel (Panel_base) – Panel object we are in

Returns:

dict – panel json code

loglevel: int = 20
class grafanacode.plugins.dashboard_base.PropDashboardLinkItem(asdropdown=False, icon='external link', includevars=False, keeptime=False, newtab=False, tags=[], ltype='dashboards', title='', tooltip='', url='')[source]

Bases: object

Dataclass encapsulating a dashboardlink item

  • param asdropdown:
    • type: bool

  • param icon:
    • default: external link - type: str

  • param includevars:
    • type: bool

  • param keeptime:
    • type: bool

  • param newtab:
    • type: bool

  • param tags:
    • type: list

  • param ltype:
    • default: dashboards - type: str

  • param title:
    • type: str

  • param tooltip:
    • type: str

  • param url:
    • type: str

getJson(dashboard=None, panel=None)[source]

Create the json to pack this item.

Parameters:
  • dashboard (Dashboard) – Dashboard object we are in

  • panel (Panel_base) – Panel object we are in

Returns:

dict – panel json code

loglevel: int = 21
class grafanacode.plugins.dashboard_base.PropTemplating(lst=_Nothing.NOTHING)[source]

Bases: object

Dataclass encapsulating the dashboard templating

  • param lst:
    • default: Factory(factory=<class ‘list’>, takes_self=False)

getJson(dashboard=None, panel=None)[source]

Create the json to pack this item.

Parameters:
  • dashboard (Dashboard) – Dashboard object we are in

  • panel (Panel_base) – Panel object we are in

Returns:

dict – panel json code

loglevel: int = 21
class grafanacode.plugins.dashboard_base.PropTime(s='now-30d', e='now')[source]

Bases: object

Dataclass encapsulating the dashboard time

  • param s:
    • default: now-30d - type: str

  • param e:
    • default: now - type: str

getJson(dashboard=None, panel=None)[source]

Create the json to pack this item.

Parameters:
  • dashboard (Dashboard) – Dashboard object we are in

  • panel (Panel_base) – Panel object we are in

Returns:

dict – panel json code

loglevel: int = 20
class grafanacode.plugins.dashboard_base.PropTimePicker(refreshintervals=['5s', '10s', '30s', '1m', '5m', '15m', '30m', '1h', '2h', '1d'], timeoptions=['5m', '15m', '1h', '6h', '12h', '24h', '2d', '7d', '30d'], hidden=False)[source]

Bases: object

Dataclass encapsulating the dashboard timepicker

  • param refreshintervals:
    • default: [‘5s’, ’10s’, ’30s’, ‘1m’, ‘5m’, ‘15m’, ‘30m’, ‘1h’, ‘2h’, ‘1d’] - type: list - of: str

  • param timeoptions:
    • default: [‘5m’, ‘15m’, ‘1h’, ‘6h’, ‘12h’, ‘24h’, ‘2d’, ‘7d’, ‘30d’] - type: list - of: str

  • param hidden:
    • type: bool

getJson(dashboard=None, panel=None)[source]

Create the json to pack this item.

Parameters:
  • dashboard (Dashboard) – Dashboard object we are in

  • panel (Panel_base) – Panel object we are in

Returns:

dict – panel json code

loglevel: int = 20
grafanacode.plugins.dashboard_base.extractDashboard(dashboardjson, settings={}, extractdashboard=True, extractqueries=True, debuglevel=1)[source]

Extract one dashboard exported from Grafana to a piece of Python code to be used by this module

Parameters:
  • dashboardjson (dict) – dict with settinga and json to extract (is input and output)

  • settings (dict) – dict with settinga and json to extract (is input and output)

  • extractdashboard (dict) – dict with settinga and json to extract (is input and output)

  • extractqueries (dict) – dict with settinga and json to extract (is input and output)

  • debuglevel (dict) – dict with settinga and json to extract (is input and output)

Returns:

string – Title string: python code representing the dashboard

grafanacode.plugins.dashboard_base.extractDashboard_base(dset)[source]

Extract one dashboard exported from Grafana to a piece of Python code to be used by this module

Parameters:

dset (dict) – dict with settinga and json to extract (is input and output)

Returns:

boolean – True