Skip to content

Commit 33f6da7

Browse files
committed
History: Stop grafana-wtf log <UID> acquiring *all* dashboards
1 parent c6dc165 commit 33f6da7

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ in progress
88
- Explore dashboards: Ignore ``-- Mixed --`` data sources
99
- Caching: Increase default cache TTL to five minutes again
1010
- Caching: Optionally configure TTL using environment variable ``CACHE_TTL``
11+
- History: Stop ``grafana-wtf log <UID>`` acquiring *all* dashboards
1112

1213
2023-03-05 0.14.1
1314
=================

grafana_wtf/commands.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ def run():
186186
'No Grafana URL given. Please use "--grafana-url" option or environment variable "GRAFANA_URL".'
187187
)
188188

189+
log.info(f"Using Grafana at {grafana_url}")
190+
189191
engine = GrafanaWtf(grafana_url, grafana_token)
190192
engine.enable_cache(expire_after=cache_ttl, drop_cache=options["drop-cache"])
191193
engine.enable_concurrency(int(options["concurrency"]))
@@ -222,7 +224,6 @@ def run():
222224
engine.clear_cache()
223225

224226
if options.log:
225-
engine.scan_dashboards()
226227
entries = engine.log(dashboard_uid=options.dashboard_uid)
227228
entries = sorted(entries, key=itemgetter("datetime"), reverse=True)
228229

grafana_wtf/core.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,21 +357,30 @@ def log(self, dashboard_uid=None):
357357
what = 'Grafana dashboard "{}"'.format(dashboard_uid)
358358
else:
359359
what = "multiple Grafana dashboards"
360-
log.info("Aggregating edit history for {what} at {url}".format(what=what, url=self.grafana_url))
360+
log.info(f"Acquiring data for {what}")
361361

362+
uid_filter = None
363+
if dashboard_uid:
364+
uid_filter = [dashboard_uid]
365+
dashboards = self.scan_dashboards(dashboard_uids=uid_filter)
366+
367+
log.info(f"Aggregating edit history for {what}")
362368
entries = []
363-
for dashboard_meta in self.data.dashboard_list:
364-
if dashboard_uid is not None and dashboard_meta["uid"] != dashboard_uid:
369+
for dashboard in dashboards:
370+
dashboard_data = dashboard["dashboard"]
371+
dashboard_meta = dashboard["meta"]
372+
373+
if dashboard_uid is not None and dashboard_data["uid"] != dashboard_uid:
365374
continue
366375

367-
dashboard_versions = self.get_dashboard_versions(dashboard_meta["id"])
376+
dashboard_versions = self.get_dashboard_versions(dashboard_data["id"])
368377
for dashboard_revision in dashboard_versions:
369378
entry = OrderedDict(
370379
datetime=dashboard_revision["created"],
371380
user=dashboard_revision["createdBy"],
372381
message=dashboard_revision["message"],
373382
folder=dashboard_meta.get("folderTitle"),
374-
title=dashboard_meta["title"],
383+
title=dashboard_data["title"],
375384
version=dashboard_revision["version"],
376385
url=urljoin(self.grafana_url, dashboard_meta["url"]),
377386
)

tests/test_commands.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,18 +191,35 @@ def test_replace_dashboard_dry_run_success(ldi_resources, capsys):
191191
assert "Dashboards: 0 hits" in captured.out
192192

193193

194-
def test_log_empty(capsys, caplog):
194+
def test_log_empty(docker_grafana, capsys, caplog):
195195
# Run command and capture output.
196196
set_command("log foobar")
197197
with caplog.at_level(logging.DEBUG):
198198
grafana_wtf.commands.run()
199199
captured = capsys.readouterr()
200200

201201
# Verify output.
202-
assert 'Aggregating edit history for Grafana dashboard "foobar" at http://localhost:33333' in caplog.text
202+
assert 'Aggregating edit history for Grafana dashboard "foobar"' in caplog.text
203203
assert "[]" in captured.out
204204

205205

206+
def test_log_all(ldi_resources, capsys, caplog):
207+
208+
# Only provision specific dashboard(s).
209+
ldi_resources(dashboards=["tests/grafana/dashboards/ldi-v27.json", "tests/grafana/dashboards/ldi-v33.json"])
210+
211+
# Run command and capture output.
212+
set_command("log")
213+
with caplog.at_level(logging.DEBUG):
214+
grafana_wtf.commands.run()
215+
captured = capsys.readouterr()
216+
217+
# Verify output.
218+
assert 'Aggregating edit history for multiple Grafana dashboards' in caplog.text
219+
history = json.loads(captured.out)
220+
assert len(history) == 3
221+
222+
206223
def test_log_json_success(ldi_resources, capsys, caplog):
207224
# Only provision specific dashboard(s).
208225
ldi_resources(dashboards=["tests/grafana/dashboards/ldi-v27.json", "tests/grafana/dashboards/ldi-v33.json"])
@@ -214,7 +231,7 @@ def test_log_json_success(ldi_resources, capsys, caplog):
214231
captured = capsys.readouterr()
215232

216233
# Verify output.
217-
assert 'Aggregating edit history for Grafana dashboard "ioUrPwQiz" at http://localhost:33333' in caplog.text
234+
assert 'Aggregating edit history for Grafana dashboard "ioUrPwQiz"' in caplog.text
218235

219236
reference = {
220237
# "datetime": "2021-09-29T17:32:23Z",
@@ -244,7 +261,7 @@ def test_log_tabular_success(ldi_resources, capsys, caplog):
244261
captured = capsys.readouterr()
245262

246263
# Verify output.
247-
assert 'Aggregating edit history for Grafana dashboard "ioUrPwQiz" at http://localhost:33333' in caplog.text
264+
assert 'Aggregating edit history for Grafana dashboard "ioUrPwQiz"' in caplog.text
248265

249266
reference = """
250267
| Notes: n/a<br/>[Testdrive » luftdaten.info generic trend v27](http://localhost:33333/d/ioUrPwQiz/luftdaten-info-generic-trend-v27) | User: admin<br/>Date: xxxx-xx-xxTxx:xx:xxZ |

0 commit comments

Comments
 (0)