Utilities

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.

class grafanarmadillo.types.GrafanaPath(name: str, folder: str, org: str | None = None)[source]

Path of an object in Grafana.

grafanarmadillo.paths

Tools for manipulating path-like objects into references to Grafana objects or file-safe paths.

class grafanarmadillo.paths.PathCodec[source]

Safely encode paths which may contain invalid characters, such as forward slashes.

static decode(path: Path) List[str][source]

Decode a path to segments.

static decode_segment(segment: str) str[source]

Decode a single segment.

static encode(segments: List[str]) Path[source]

Encode segments to a path.

static encode_grafana(path: GrafanaPath) Path[source]

Encode a GrafanaPath.

static encode_segment(segment: str) str[source]

Encode a single segment.

static parse_grafana(parts: Sequence[str]) GrafanaPath[source]

Assemble segments into an orderly GrafanaPath.

static try_parse(o: GrafanaPath | str | Path | List[str]) GrafanaPath[source]

Try to decode a pathlike object.

grafanarmadillo.util

Helpers and generic functions.

class grafanarmadillo.util.Cache[source]

Cache values.

get(k)[source]

Get a cached value, if it exists.

getor(k, f: Callable[[], T]) T[source]

Get a cached item or generate it.

set(k, v)[source]

Set a cached value.

unset(k)[source]

Unset a cached value.

unset_method(k_start)[source]

Unset all keys whose first subkey (the method name) matches.

class grafanarmadillo.util.CacheMode(value)[source]

Caching mode for interacting with Grafana.

None: no caching Session: lifetime of the Finder object Global: all Finders share the same cache

You can disable caching globally by setting grafanarmadillo.util.global_cache = grafanarmadillo.util.NoneCache()

static select(cache_mode: CacheMode | Cache) Cache[source]

Create or use a cache. Pass a cache to reuse it.

class grafanarmadillo.util.NoneCache[source]

A Cache-interface-compatible which never caches.

get(k)[source]

Never caches a value.

getor(k, f: Callable[[], T]) T[source]

Always generate the cached item.

set(k, v)[source]

Never caches a value.

unset(k)[source]

No keys are ever set.

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_filepath_to_object(base_path: Path, path: Path) GrafanaPath[source]

Extract the “/folder/object” format from the file on disk that contains the template.

grafanarmadillo.util.resolve_object_to_filepath(base_path: Path, name: GrafanaPath | str | Path | List[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.