Skip to content

Commit

Permalink
Fix discovery of data sources defined by dashboard variables
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
amotl committed Feb 9, 2023
1 parent 6c0e9b0 commit 7c0c835
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions grafana_wtf/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from collections import OrderedDict
from concurrent.futures.thread import ThreadPoolExecutor
from urllib.parse import parse_qs, urljoin, urlparse
from tqdm.contrib.logging import tqdm_logging_redirect

import colored
import requests
Expand All @@ -18,6 +17,7 @@
from grafana_client.client import GrafanaClientError, GrafanaUnauthorizedError
from munch import Munch, munchify
from tqdm import tqdm
from tqdm.contrib.logging import tqdm_logging_redirect
from urllib3.exceptions import InsecureRequestWarning

from grafana_wtf.model import (
Expand Down Expand Up @@ -492,20 +492,35 @@ def __init__(self, engine: GrafanaWtf):
self.index()

def index(self):
self.index_dashboards()
self.index_datasources()
self.index_dashboards()
self.index_crossref()

@staticmethod
def collect_datasource_items(element):
def collect_datasource_items(self, element):
element = element or []
items = []
for node in element:
ds = None

# Directly defined datasources.
if "datasource" in node and node["datasource"]:
ds = node.datasource
if isinstance(ds, Munch):
ds = dict(ds)
if ds not in items:
items.append(ds)

# Datasources defined as variables.
if "type" in node and node["type"] == "datasource":
ds_name = node.get("current", {}).get("value")
datasource = self.datasource_by_name.get(ds_name, {})
ds = dict(
type=datasource.get("type"),
uid=datasource.get("uid"),
name=datasource.get("name"),
url=datasource.get("url"),
)

if ds is not None and ds not in items:
items.append(ds)
return items

def index_dashboards(self):
Expand Down Expand Up @@ -548,6 +563,7 @@ def index_datasources(self):
self.datasource_by_ident[datasource.uid] = datasource
self.datasource_by_uid[datasource.uid] = datasource

def index_crossref(self):
for dashboard_uid, datasource_items in self.dashboard_datasource_index.items():
datasource_item: DatasourceItem
for datasource_item in datasource_items:
Expand Down

0 comments on commit 7c0c835

Please sign in to comment.