Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions grafana_wtf/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
grafana-wtf [options] log [<dashboard_uid>] [--number=<count>] [--head=<count>] [--tail=<count>] [--reverse] [--sql=<sql>]
grafana-wtf [options] plugins list [--id=]
grafana-wtf [options] plugins status [--id=]
grafana-wtf [options] channels [--id=]
grafana-wtf [options] channels [--uid=]
grafana-wtf --version
grafana-wtf (-h | --help)

Expand Down Expand Up @@ -346,8 +346,8 @@
output_results(output_format, response)

if options.channels:
if options.id:
response = engine.channels_list_by_id(options.id)
if options.uid:
response = engine.channels_list_by_uid(options.uid)

Check warning on line 350 in grafana_wtf/commands.py

View check run for this annotation

Codecov / codecov/patch

grafana_wtf/commands.py#L349-L350

Added lines #L349 - L350 were not covered by tests
else:
response = engine.channels_list()
output_results(output_format, response)
28 changes: 26 additions & 2 deletions grafana_wtf/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,10 +588,34 @@
def channels_list(self):
return self.grafana.notifications.lookup_channels()

def channels_list_by_id(self, channel_id):
channel = self.grafana.notifications.get_channel_by_id(channel_id)
def channels_list_by_uid(self, channel_uid):
channel = self.grafana.notifications.get_channel_by_uid(channel_uid)

Check warning on line 592 in grafana_wtf/core.py

View check run for this annotation

Codecov / codecov/patch

grafana_wtf/core.py#L592

Added line #L592 was not covered by tests

# Scan dashboards and panels to find where the channel is used
dashboards = self.scan_dashboards()
related_panels = []
for dashboard in dashboards:
for panel in dashboard["dashboard"].get("panels", []):
if "alert" in panel and panel["alert"]["notifications"]:
related_panels += self.extract_channel_related_information(channel_uid, dashboard, panel)

Check warning on line 600 in grafana_wtf/core.py

View check run for this annotation

Codecov / codecov/patch

grafana_wtf/core.py#L595-L600

Added lines #L595 - L600 were not covered by tests

# Some dashboards have a deeper nested structure
elif "panels" in panel:
for subpanel in panel["panels"]:
if "alert" in subpanel and subpanel["alert"]["notifications"]:
related_panels += self.extract_channel_related_information(channel_uid, dashboard, subpanel)
if related_panels:
channel["related_panels"] = related_panels

Check warning on line 608 in grafana_wtf/core.py

View check run for this annotation

Codecov / codecov/patch

grafana_wtf/core.py#L603-L608

Added lines #L603 - L608 were not covered by tests
return channel

@staticmethod
def extract_channel_related_information(channel_uid, dashboard, panel):
related_information = []
for notification in panel["alert"]["notifications"]:
if "uid" in notification and notification["uid"] == channel_uid:
related_information.append({"dashboard": dashboard["dashboard"]["title"], "panel": panel["title"]})
return related_information

Check warning on line 617 in grafana_wtf/core.py

View check run for this annotation

Codecov / codecov/patch

grafana_wtf/core.py#L613-L617

Added lines #L613 - L617 were not covered by tests


class Indexer:
def __init__(self, engine: GrafanaWtf):
Expand Down
Loading