1
1
# -*- coding: utf-8 -*-
2
- # (c) 2019-2021 Andreas Motl <[email protected] >
2
+ # (c) 2019-2023 Andreas Motl <[email protected] >
3
3
# License: GNU Affero General Public License, Version 3
4
4
import logging
5
5
import os
15
15
from grafana_wtf .report .tabular import TabularSearchReport , get_table_format , TabularEditHistoryReport
16
16
from grafana_wtf .util import (
17
17
configure_http_logging ,
18
+ filter_with_sql ,
18
19
normalize_options ,
19
20
read_list ,
20
21
setup_logging ,
@@ -31,7 +32,7 @@ def run():
31
32
grafana-wtf [options] explore dashboards
32
33
grafana-wtf [options] find [<search-expression>]
33
34
grafana-wtf [options] replace <search-expression> <replacement> [--dry-run]
34
- grafana-wtf [options] log [<dashboard_uid>] [--number=<count>] [--head=<count>] [--tail=<count>] [--reverse]
35
+ grafana-wtf [options] log [<dashboard_uid>] [--number=<count>] [--head=<count>] [--tail=<count>] [--reverse] [--sql=<sql>]
35
36
grafana-wtf --version
36
37
grafana-wtf (-h | --help)
37
38
@@ -129,6 +130,22 @@ def run():
129
130
# Output full history table in Markdown format
130
131
grafana-wtf log --format=tabular:pipe
131
132
133
+ # Display dashboards with only a single edit, in JSON format.
134
+ grafana-wtf log --sql="
135
+ SELECT uid, url, COUNT(version) as number_of_edits
136
+ FROM dashboard_versions
137
+ GROUP BY uid, url
138
+ HAVING number_of_edits=1
139
+ "
140
+
141
+ # Display dashboards with only a single edit, in YAML format, `url` attribute only.
142
+ grafana-wtf log --format=yaml --sql="
143
+ SELECT url
144
+ FROM dashboard_versions
145
+ GROUP BY uid, url
146
+ HAVING COUNT(version)=1
147
+ "
148
+
132
149
Cache control:
133
150
134
151
# Use infinite cache expiration time, essentially caching forever.
@@ -231,8 +248,23 @@ def run():
231
248
232
249
if options .log :
233
250
251
+ # Sanity checks.
252
+ if output_format .startswith ("tab" ) and options .sql :
253
+ raise DocoptExit (
254
+ f"Options --format={ output_format } and --sql can not be used together, only data output is supported."
255
+ )
256
+
234
257
entries = engine .log (dashboard_uid = options .dashboard_uid )
235
- entries = sorted (entries , key = itemgetter ("datetime" ))
258
+
259
+ if options .sql is not None :
260
+ log .info (f"Filtering result with SQL expression: { options .sql } " )
261
+ entries = filter_with_sql (
262
+ data = entries ,
263
+ view_name = "dashboard_versions" ,
264
+ expression = options .sql ,
265
+ )
266
+ else :
267
+ entries = sorted (entries , key = itemgetter ("datetime" ))
236
268
237
269
if options .number is not None :
238
270
limit = int (options .number )
0 commit comments