Utilities

grafanarmadillo.util

Helpers and generic functions.

grafanarmadillo.util.erase_alert_rule_identity(alertlike) Dict[source]

Delete the fields of an alert_rule which are used for determining identity.

grafanarmadillo.util.erase_dashboard_identity(dashboardlike: DashboardSearchResult | DashboardContent) Dict[source]

Delete the fields of a dashboard which are used for determining identity.

grafanarmadillo.util.exactly_one(items: List[A], message: str | None = None) A[source]

Throws if list does not contain exactly 1 item.

>>> exactly_one([1])
1
>>> exactly_one([1,2])
Traceback (most recent call last):
ValueError: expected exactly 1 item, found=2 message=None
>>> exactly_one([])
Traceback (most recent call last):
ValueError: expected exactly 1 item, found=0 message=None
grafanarmadillo.util.flat_map(f, xs)[source]

Flatmap: Map on a list and then merge the results.

>>> and_reversed = lambda s: [s,s[::-1]]
>>> flat_map(and_reversed, [])
[]
>>> flat_map(and_reversed, ['hi'])
['hi', 'ih']
>>> flat_map(and_reversed, ['hi', 'hello'])
['hi', 'ih', 'hello', 'olleh']
grafanarmadillo.util.load_data(data_str: str)[source]

Attempt to load data.

grafanarmadillo.util.map_json_strings(f: Callable[[str], str], obj: JSON) JSON[source]

Transform all strings in an object made of JSON primitives.

>>> f = lambda s: s.upper()
>>> map_json_strings(f, 's')
'S'
>>> map_json_strings(f, 1)
1
>>> map_json_strings(f, ['s'])
['S']
>>> map_json_strings(f, ['s', 1])
['S', 1]
>>> map_json_strings(f, {'a': 's'})
{'a': 'S'}
>>> map_json_strings(f, {'a': ['s', 1]})
{'a': ['S', 1]}
grafanarmadillo.util.project_dashboard_identity(dashboardlike: DashboardSearchResult | DashboardContent) Dict[source]

Project only the fields of a dashboard which are used for determining identity.

grafanarmadillo.util.project_dict(d: Dict, keys: set, inverse: bool = False) Dict[source]

Select the given fields from a dictionary.

>>> project_dict({'a': 1, 'b': 2}, {'a'})
{'a': 1}
>>> project_dict({'a': 1, 'b': 2}, {'a'}, inverse=True)
{'b': 2}
grafanarmadillo.util.read_from_file(file_path: Path) dict[source]

Read JSON from a file.

grafanarmadillo.util.resolve_object_to_filepath(base_path: Path, name: str)[source]

Transform the “/folder/object” format to the path on disk that contains the template.

grafanarmadillo.util.write_to_file(out_path: Path, obj: dict)[source]

Write an object to file as JSON.

grafanarmadillo.types

Type hints for Grafana interaction.

class grafanarmadillo.types.AlertSearchResult(*args, **kwargs)[source]

Relevant keys returned for an alert.

class grafanarmadillo.types.AnySearchResult(*args, **kwargs)[source]

Metadata for both Grafana dashboards and alerts.

class grafanarmadillo.types.Dashboard(*args, **kwargs)[source]

Relevant keys returned by GET of a Grafana dashboard.

class grafanarmadillo.types.DashboardSearchResult(*args, **kwargs)[source]

Relevant keys returned by a Grafana search.