Skip to content

Commit 7c0c835

Browse files
committed
Fix discovery of data sources defined by dashboard variables
Because you can choose any data source with the corresponding chooser, this will never be accurate. However, it will check if the _default_ data source defined within the variable definition actually exists.
1 parent 6c0e9b0 commit 7c0c835

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

grafana_wtf/core.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from collections import OrderedDict
1010
from concurrent.futures.thread import ThreadPoolExecutor
1111
from urllib.parse import parse_qs, urljoin, urlparse
12-
from tqdm.contrib.logging import tqdm_logging_redirect
1312

1413
import colored
1514
import requests
@@ -18,6 +17,7 @@
1817
from grafana_client.client import GrafanaClientError, GrafanaUnauthorizedError
1918
from munch import Munch, munchify
2019
from tqdm import tqdm
20+
from tqdm.contrib.logging import tqdm_logging_redirect
2121
from urllib3.exceptions import InsecureRequestWarning
2222

2323
from grafana_wtf.model import (
@@ -492,20 +492,35 @@ def __init__(self, engine: GrafanaWtf):
492492
self.index()
493493

494494
def index(self):
495-
self.index_dashboards()
496495
self.index_datasources()
496+
self.index_dashboards()
497+
self.index_crossref()
497498

498-
@staticmethod
499-
def collect_datasource_items(element):
499+
def collect_datasource_items(self, element):
500500
element = element or []
501501
items = []
502502
for node in element:
503+
ds = None
504+
505+
# Directly defined datasources.
503506
if "datasource" in node and node["datasource"]:
504507
ds = node.datasource
505508
if isinstance(ds, Munch):
506509
ds = dict(ds)
507-
if ds not in items:
508-
items.append(ds)
510+
511+
# Datasources defined as variables.
512+
if "type" in node and node["type"] == "datasource":
513+
ds_name = node.get("current", {}).get("value")
514+
datasource = self.datasource_by_name.get(ds_name, {})
515+
ds = dict(
516+
type=datasource.get("type"),
517+
uid=datasource.get("uid"),
518+
name=datasource.get("name"),
519+
url=datasource.get("url"),
520+
)
521+
522+
if ds is not None and ds not in items:
523+
items.append(ds)
509524
return items
510525

511526
def index_dashboards(self):
@@ -548,6 +563,7 @@ def index_datasources(self):
548563
self.datasource_by_ident[datasource.uid] = datasource
549564
self.datasource_by_uid[datasource.uid] = datasource
550565

566+
def index_crossref(self):
551567
for dashboard_uid, datasource_items in self.dashboard_datasource_index.items():
552568
datasource_item: DatasourceItem
553569
for datasource_item in datasource_items:

0 commit comments

Comments
 (0)