generated from canonical/template-operator
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add revision to the dashboard title of the Grafana (#510)
With this change, two different OpenSearch applications in different revisions, will have separate dashboards when related to COS. - data-platform-helpers was added to use the get_charm_revision - titles of dashboards are changed during the charm upgrade hook - config_changed was added on refresh_events of COSAgentProvider to re-render the dashboards after a charm upgrade ## How to test it 1. Deploy more than one opensearch application 2. Relate both apps with grafana-agent 3. In one of the applications use juju refresh and apply a local build of this patch 4. Go to the Grafana dashboards an you will see that two dashboards are created: - one with no revision (from charmhub) - one with Rev 0 (meaning that is a local build) ![image](https://github.com/user-attachments/assets/d9d82ab6-10b3-4d00-bfa2-f864bbad4d9a)
- Loading branch information
1 parent
9f5e770
commit 4776c62
Showing
7 changed files
with
230 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Copyright 2025 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
"""Utility functions for charms COS operations.""" | ||
|
||
|
||
import json | ||
import logging | ||
from typing import TYPE_CHECKING | ||
|
||
from data_platform_helpers.version_check import get_charm_revision | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
if TYPE_CHECKING: | ||
from charms.opensearch.v0.opensearch_base_charm import OpenSearchBaseCharm | ||
|
||
|
||
def update_grafana_dashboards_title(charm: "OpenSearchBaseCharm") -> None: | ||
"""Update the title of the Grafana dashboard file to include the charm revision.""" | ||
revision = get_charm_revision(charm.model.unit) | ||
dashboard_path = charm.charm_dir / "src/grafana_dashboards/opensearch.json" | ||
|
||
with open(dashboard_path, "r") as file: | ||
dashboard = json.load(file) | ||
|
||
old_title = dashboard.get("title", "Charmed OpenSearch") | ||
title_prefix = old_title.split(" - Rev")[0] | ||
new_title = f"{old_title} - Rev {revision}" | ||
dashboard["title"] = f"{title_prefix} - Rev {revision}" | ||
|
||
logger.info( | ||
"Changing the title of dashboard %s from %s to %s", | ||
dashboard_path.name, | ||
old_title, | ||
new_title, | ||
) | ||
|
||
with open(dashboard_path, "w") as file: | ||
json.dump(dashboard, file, indent=4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# Copyright 2024 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
|
||
"""Unit test for the helper_cos library.""" | ||
|
||
import json | ||
import unittest | ||
from pathlib import Path | ||
from unittest.mock import MagicMock, PropertyMock, mock_open, patch | ||
|
||
from charms.opensearch.v0.helper_cos import update_grafana_dashboards_title | ||
|
||
|
||
class TestCOSGrafanaDashboard(unittest.TestCase): | ||
|
||
@patch("charms.opensearch.v0.helper_cos.get_charm_revision", return_value=167) | ||
@patch( | ||
"builtins.open", | ||
new_callable=mock_open, | ||
read_data=json.dumps({"title": "Charmed OpenSearch"}), | ||
) | ||
@patch("json.dump") | ||
def test_update_grafana_dashboards_title_no_prior_revision( | ||
self, mock_json_dump, mock_open_func, _ | ||
): | ||
mock_charm = MagicMock() | ||
mock_charm.model.unit = MagicMock() | ||
type(mock_charm).charm_dir = PropertyMock(return_value=Path("/fake/charm/dir")) | ||
|
||
update_grafana_dashboards_title(mock_charm) | ||
|
||
expected_updated_dashboard = {"title": "Charmed OpenSearch - Rev 167"} | ||
mock_json_dump.assert_called_once_with( | ||
expected_updated_dashboard, mock_open_func(), indent=4 | ||
) | ||
|
||
@patch("charms.opensearch.v0.helper_cos.get_charm_revision", return_value=167) | ||
@patch( | ||
"builtins.open", | ||
new_callable=mock_open, | ||
read_data=json.dumps({"title": "Charmed OpenSearch - Rev 166"}), | ||
) | ||
@patch("json.dump") | ||
def test_update_grafana_dashboards_title_prior_revision( | ||
self, | ||
mock_json_dump, | ||
mock_open_func, | ||
_, | ||
): | ||
mock_charm = MagicMock() | ||
mock_charm.model.unit = MagicMock() | ||
type(mock_charm).charm_dir = PropertyMock(return_value=Path("/fake/charm/dir")) | ||
|
||
update_grafana_dashboards_title(mock_charm) | ||
|
||
expected_updated_dashboard = {"title": "Charmed OpenSearch - Rev 167"} | ||
mock_json_dump.assert_called_once_with( | ||
expected_updated_dashboard, mock_open_func(), indent=4 | ||
) | ||
|
||
@patch("charms.opensearch.v0.helper_cos.get_charm_revision", return_value=167) | ||
@patch( | ||
"builtins.open", | ||
new_callable=mock_open, | ||
read_data=json.dumps({"my-content": "content"}), | ||
) | ||
@patch("json.dump") | ||
def test_update_grafana_dashboards_title_json_no_title( | ||
self, | ||
mock_json_dump, | ||
mock_open_func, | ||
_, | ||
): | ||
mock_charm = MagicMock() | ||
mock_charm.model.unit = MagicMock() | ||
type(mock_charm).charm_dir = PropertyMock(return_value=Path("/fake/charm/dir")) | ||
update_grafana_dashboards_title(mock_charm) | ||
|
||
expected_updated_dashboard = { | ||
"title": "Charmed OpenSearch - Rev 167", | ||
"my-content": "content", | ||
} | ||
mock_json_dump.assert_called_once_with( | ||
expected_updated_dashboard, mock_open_func(), indent=4 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters