@@ -189,6 +189,8 @@ def __init__(self, *args):
189189from typing import Any , Callable , Dict , List , Optional , Tuple
190190
191191import yaml
192+ from cosl import DashboardPath40UID , LZMABase64
193+ from cosl .types import type_convert_stored
192194from ops .charm import (
193195 CharmBase ,
194196 HookEvent ,
@@ -203,12 +205,9 @@ def __init__(self, *args):
203205 EventSource ,
204206 Object ,
205207 ObjectEvents ,
206- StoredDict ,
207- StoredList ,
208208 StoredState ,
209209)
210210from ops .model import Relation
211- from cosl import LZMABase64 , DashboardPath40UID
212211
213212# The unique Charmhub library identifier, never change it
214213LIBID = "c49eb9c7dfef40c7b6235ebd67010a3f"
@@ -219,7 +218,7 @@ def __init__(self, *args):
219218# Increment this PATCH version before using `charmcraft publish-lib` or reset
220219# to 0 if you are raising the major API version
221220
222- LIBPATCH = 44
221+ LIBPATCH = 46
223222
224223PYDEPS = ["cosl >= 0.0.50" ]
225224
@@ -946,23 +945,33 @@ def _replace_uid(
946945 # If we're running this from within an aggregator (such as grafana agent), then the uid was
947946 # already rendered there, so we do not want to overwrite it with a uid generated from aggregator's info.
948947 # We overwrite the uid only if it's not a valid "Path40" uid.
949- if not DashboardPath40UID .is_valid (original_uid := dashboard_dict .get ("uid" , "" )):
948+ original_uid = dashboard_dict .get ("uid" , "" )
949+
950+ if DashboardPath40UID .is_valid (original_uid ):
951+ logger .debug (
952+ "Processed dashboard '%s': kept original uid '%s'" , dashboard_path , original_uid
953+ )
954+ return
955+
956+ try :
950957 rel_path = str (
951958 dashboard_path .relative_to (charm_dir )
952959 if dashboard_path .is_absolute ()
953960 else dashboard_path
954961 )
955- dashboard_dict ["uid" ] = DashboardPath40UID .generate (charm_name , rel_path )
956- logger .debug (
957- "Processed dashboard '%s': replaced original uid '%s' with '%s'" ,
958- dashboard_path ,
959- original_uid ,
960- dashboard_dict ["uid" ],
961- )
962+ except ValueError :
963+ uid = DashboardPath40UID .generate (charm_name , str (dashboard_path ))
962964 else :
963- logger .debug (
964- "Processed dashboard '%s': kept original uid '%s'" , dashboard_path , original_uid
965- )
965+ uid = DashboardPath40UID .generate (charm_name , rel_path )
966+
967+
968+ logger .debug (
969+ "Processed dashboard '%s': replaced original uid '%s' with '%s'" ,
970+ dashboard_path ,
971+ original_uid ,
972+ uid ,
973+ )
974+ dashboard_dict ["uid" ] = uid
966975
967976 @classmethod
968977 def _add_tags (cls , dashboard_dict : dict , charm_name : str ):
@@ -1027,18 +1036,6 @@ def _is_dashboard(p: Path) -> bool:
10271036 return dashboard_templates
10281037
10291038
1030- def _type_convert_stored (obj ):
1031- """Convert Stored* to their appropriate types, recursively."""
1032- if isinstance (obj , StoredList ):
1033- return list (map (_type_convert_stored , obj ))
1034- if isinstance (obj , StoredDict ):
1035- rdict = {} # type: Dict[Any, Any]
1036- for k in obj .keys ():
1037- rdict [k ] = _type_convert_stored (obj [k ])
1038- return rdict
1039- return obj
1040-
1041-
10421039class GrafanaDashboardsChanged (EventBase ):
10431040 """Event emitted when Grafana dashboards change."""
10441041
@@ -1346,7 +1343,7 @@ def _upset_dashboards_on_relation(self, relation: Relation) -> None:
13461343 # It's completely ridiculous to add a UUID, but if we don't have some
13471344 # pseudo-random value, this never makes it across 'juju set-state'
13481345 stored_data = {
1349- "templates" : _type_convert_stored (self ._stored .dashboard_templates ), # pyright: ignore
1346+ "templates" : type_convert_stored (self ._stored .dashboard_templates ), # pyright: ignore
13501347 "uuid" : str (uuid .uuid4 ()),
13511348 }
13521349
@@ -1612,7 +1609,7 @@ def _render_dashboards_and_signal_changed(self, relation: Relation) -> bool: #
16121609 stored_data = rendered_dashboards
16131610 currently_stored_data = self ._get_stored_dashboards (relation .id )
16141611
1615- coerced_data = _type_convert_stored (currently_stored_data ) if currently_stored_data else {}
1612+ coerced_data = type_convert_stored (currently_stored_data ) if currently_stored_data else {}
16161613
16171614 if not coerced_data == stored_data :
16181615 stored_dashboards = self .get_peer_data ("dashboards" )
@@ -1796,7 +1793,7 @@ def _update_remote_grafana(self, _: Optional[RelationEvent] = None) -> None:
17961793 """Push dashboards to the downstream Grafana relation."""
17971794 # It's still ridiculous to add a UUID here, but needed
17981795 stored_data = {
1799- "templates" : _type_convert_stored (self ._stored .dashboard_templates ), # pyright: ignore
1796+ "templates" : type_convert_stored (self ._stored .dashboard_templates ), # pyright: ignore
18001797 "uuid" : str (uuid .uuid4 ()),
18011798 }
18021799
@@ -1806,7 +1803,7 @@ def _update_remote_grafana(self, _: Optional[RelationEvent] = None) -> None:
18061803
18071804 def remove_dashboards (self , event : RelationBrokenEvent ) -> None :
18081805 """Remove a dashboard if the relation is broken."""
1809- app_ids = _type_convert_stored (self ._stored .id_mappings .get (event .app .name , "" )) # type: ignore
1806+ app_ids = type_convert_stored (self ._stored .id_mappings .get (event .app .name , "" )) # type: ignore
18101807
18111808 if not app_ids :
18121809 logger .info ("Could not look up stored dashboards for %s" , event .app .name ) # type: ignore
@@ -1817,7 +1814,7 @@ def remove_dashboards(self, event: RelationBrokenEvent) -> None:
18171814 del self ._stored .dashboard_templates [id ] # type: ignore
18181815
18191816 stored_data = {
1820- "templates" : _type_convert_stored (self ._stored .dashboard_templates ), # pyright: ignore
1817+ "templates" : type_convert_stored (self ._stored .dashboard_templates ), # pyright: ignore
18211818 "uuid" : str (uuid .uuid4 ()),
18221819 }
18231820
0 commit comments