]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/ci/check_grafana_uids.py
f82ddbbc8bab7355bce536bc02713969645066a8
1 # -*- coding: utf-8 -*-
2 # pylint: disable=F0401
5 * Scan through Angular html templates and extract <cd-grafana> tags
6 * Check if every tag has a corresponding Grafana dashboard by `uid`
9 python <script> <angular_app_dir> <grafana_dashboard_dir>
12 cd /ceph/src/pybind/mgr/dashboard
13 python ci/<script> frontend/src/app /ceph/monitoring/grafana/dashboards
20 from html
.parser
import HTMLParser
23 class TemplateParser(HTMLParser
):
25 def __init__(self
, _file
, search_tag
):
27 self
.search_tag
= search_tag
32 with codecs
.open(self
.file, encoding
='UTF-8') as f
:
35 def handle_starttag(self
, tag
, attrs
):
36 if tag
!= self
.search_tag
:
41 'line': self
.getpos()[0]
43 self
.parsed_data
.append(tag_data
)
45 def error(self
, message
):
46 error_msg
= 'fail to parse file {} (@{}): {}'.\
47 format(self
.file, self
.getpos(), message
)
51 def get_files(base_dir
, file_ext
):
53 for root
, _
, files
in os
.walk(base_dir
):
55 if _file
.endswith('.{}'.format(file_ext
)):
56 result
.append(os
.path
.join(root
, _file
))
60 def get_tags(base_dir
, tag
='cd-grafana'):
61 templates
= get_files(base_dir
, 'html')
63 for templ
in templates
:
64 parser
= TemplateParser(templ
, tag
)
66 if parser
.parsed_data
:
67 tags
.extend(parser
.parsed_data
)
71 def get_grafana_dashboards(base_dir
):
72 json_files
= get_files(base_dir
, 'json')
74 for json_file
in json_files
:
75 with
open(json_file
) as f
:
76 dashboard_config
= json
.load(f
)
77 uid
= dashboard_config
.get('uid')
79 # Grafana dashboard checks
80 title
= dashboard_config
['title']
81 assert len(title
) > 0, \
82 "Title not found in '{}'".format(json_file
)
83 assert len(dashboard_config
.get('links', [])) == 0, \
84 "Links found in '{}'".format(json_file
)
89 error_msg
= 'Duplicated UID {} found, already defined in {}'.\
90 format(uid
, dashboards
[uid
]['file'])
101 long_desc
= ('Check every <cd-grafana> component in Angular template has a'
102 ' mapped Grafana dashboard.')
103 parser
= argparse
.ArgumentParser(description
=long_desc
)
104 parser
.add_argument('angular_app_dir', type=str,
105 help='Angular app base directory')
106 parser
.add_argument('grafana_dash_dir', type=str,
107 help='Directory contains Grafana dashboard JSON files')
108 parser
.add_argument('--verbose', action
='store_true',
109 help='Display verbose mapping information.')
110 return parser
.parse_args()
115 tags
= get_tags(args
.angular_app_dir
)
116 grafana_dashboards
= get_grafana_dashboards(args
.grafana_dash_dir
)
117 verbose
= args
.verbose
120 error_msg
= 'Can not find any cd-grafana component under {}'.\
121 format(args
.angular_app_dir
)
125 print('Found mappings:')
126 no_dashboard_tags
= []
128 uid
= tag
['attrs']['uid']
129 if uid
not in grafana_dashboards
:
130 no_dashboard_tags
.append(copy
.copy(tag
))
133 msg
= '{} ({}:{}) \n\t-> {} ({})'.\
134 format(uid
, tag
['file'], tag
['line'],
135 grafana_dashboards
[uid
]['title'],
136 grafana_dashboards
[uid
]['file'])
139 if no_dashboard_tags
:
140 title
= ('Checking Grafana dashboards UIDs: ERROR\n'
141 'Components that have no mapped Grafana dashboards:\n')
142 lines
= ('{} ({}:{})'.format(tag
['attrs']['uid'],
145 for tag
in no_dashboard_tags
)
146 error_msg
= title
+ '\n'.join(lines
)
149 print('Checking Grafana dashboards UIDs: OK')
152 if __name__
== '__main__':