11 logger = logger_utils.KibanaDashboardLogger('elastic2kibana').get
13 _installers = {'fuel', 'apex', 'compass', 'joid'}
16 class KibanaDashboard(dict):
17 def __init__(self, project_name, case_name, family, installer, pod, scenarios, visualization):
18 super(KibanaDashboard, self).__init__()
19 self.project_name = project_name
20 self.case_name = case_name
22 self.installer = installer
24 self.scenarios = scenarios
25 self.visualization = visualization
26 self._visualization_title = None
27 self._kibana_visualizations = []
28 self._kibana_dashboard = None
29 self._create_visualizations()
32 def _create_visualizations(self):
33 for scenario in self.scenarios:
34 self._kibana_visualizations.append(KibanaVisualization(self.project_name,
41 self._visualization_title = self._kibana_visualizations[0].vis_state_title
43 def _publish_visualizations(self):
44 for visualization in self._kibana_visualizations:
45 url = urlparse.urljoin(base_elastic_url, '/.kibana/visualization/{}'.format(visualization.id))
46 logger.debug("publishing visualization '{}'".format(url))
47 shared_utils.publish_json(visualization, es_creds, url)
49 def _construct_panels(self):
57 for visualization in self._kibana_visualizations:
59 "id": visualization.id,
60 "type": 'visualization',
61 "panelIndex": panel_index,
69 if column > max_columns:
72 return json.dumps(panels_json, separators=(',', ':'))
75 self['title'] = '{} {} {} {} {}'.format(self.project_name,
78 self._visualization_title,
80 self.id = self['title'].replace(' ', '-').replace('/', '-')
83 self['description'] = "Kibana dashboard for project_name '{}', case_name '{}', installer '{}', data '{}' and" \
84 " pod '{}'".format(self.project_name,
87 self._visualization_title,
89 self['panelsJSON'] = self._construct_panels()
90 self['optionsJSON'] = json.dumps({
93 separators=(',', ':'))
94 self['uiStateJSON'] = "{}"
96 self['timeRestore'] = False
97 self['kibanaSavedObjectMeta'] = {
98 'searchSourceJSON': json.dumps({
104 "analyze_wildcard": True
110 separators=(',', ':'))
113 label = self.case_name
114 if 'label' in self.visualization:
115 label += " %s" % self.visualization.get('label')
116 label += " %s" % self.visualization.get('name')
119 "test_family": self.family
123 url = urlparse.urljoin(base_elastic_url, '/.kibana/dashboard/{}'.format(self.id))
124 logger.debug("publishing dashboard '{}'".format(url))
125 shared_utils.publish_json(self, es_creds, url)
128 self._publish_visualizations()
132 class KibanaSearchSourceJSON(dict):
135 {"match": {"installer": {"query": installer, "type": "phrase"}}},
136 {"match": {"project_name": {"query": project_name, "type": "phrase"}}},
137 {"match": {"case_name": {"query": case_name, "type": "phrase"}}}
141 def __init__(self, project_name, case_name, installer, pod, scenario):
142 super(KibanaSearchSourceJSON, self).__init__()
144 {"match": {"project_name": {"query": project_name, "type": "phrase"}}},
145 {"match": {"case_name": {"query": case_name, "type": "phrase"}}},
146 {"match": {"installer": {"query": installer, "type": "phrase"}}},
147 {"match": {"scenario": {"query": scenario, "type": "phrase"}}}
150 self["filter"].append({"match": {"pod_name": {"query": pod, "type": "phrase"}}})
153 class VisualizationState(dict):
154 def __init__(self, visualization):
155 super(VisualizationState, self).__init__()
156 name = visualization.get('name')
157 fields = visualization.get('fields')
159 if name == 'tests_failures':
162 self['type'] = 'histogram'
164 # duration or success_percentage
167 self['type'] = 'line'
173 "smoothLines": False,
175 "interpolate": "linear",
178 "addTimeMarker": False,
179 "defaultYExtents": False,
180 "setYExtents": False,
188 self['aggs'].append({
193 "field": field.get('field')
198 self['aggs'].append({
200 "type": 'date_histogram',
203 "field": "start_date",
205 "customInterval": "2h",
207 "extended_bounds": {}
211 self['listeners'] = {}
212 self['title'] = ' '.join(['{} {}'.format(x['type'], x['params']['field']) for x in self['aggs']
213 if x['schema'] == 'metric'])
216 class KibanaVisualization(dict):
217 def __init__(self, project_name, case_name, installer, pod, scenario, visualization):
220 1. filter created from
226 2. visualization state
227 field for y axis (metric) with type (avg, sum, etc.)
228 field for x axis (segment) with type (date_histogram)
232 super(KibanaVisualization, self).__init__()
233 vis_state = VisualizationState(visualization)
234 self.vis_state_title = vis_state['title']
235 self['title'] = '{} {} {} {} {} {}'.format(project_name,
237 self.vis_state_title,
241 self.id = self['title'].replace(' ', '-').replace('/', '-')
242 self['visState'] = json.dumps(vis_state, separators=(',', ':'))
243 self['uiStateJSON'] = "{}"
244 self['description'] = "Kibana visualization for project_name '{}', case_name '{}', data '{}', installer '{}'," \
245 " pod '{}' and scenario '{}'".format(project_name,
247 self.vis_state_title,
252 self['kibanaSavedObjectMeta'] = {"searchSourceJSON": json.dumps(KibanaSearchSourceJSON(project_name,
257 separators=(',', ':'))}
260 def _get_pods_and_scenarios(project_name, case_name, installer):
261 query_json = json.JSONEncoder().encode({
268 {"match": {"installer": {"query": installer, "type": "phrase"}}},
269 {"match": {"project_name": {"query": project_name, "type": "phrase"}}},
270 {"match": {"case_name": {"query": case_name, "type": "phrase"}}}
276 elastic_data = shared_utils.get_elastic_data(urlparse.urljoin(base_elastic_url, '/test_results/mongo2elastic'),
277 es_creds, query_json)
279 pods_and_scenarios = {}
281 for data in elastic_data:
282 pod = data['pod_name']
283 if pod in pods_and_scenarios:
284 pods_and_scenarios[pod].add(data['scenario'])
286 pods_and_scenarios[pod] = {data['scenario']}
288 if 'all' in pods_and_scenarios:
289 pods_and_scenarios['all'].add(data['scenario'])
291 pods_and_scenarios['all'] = {data['scenario']}
293 return pods_and_scenarios
296 def construct_dashboards():
298 iterate over testcase and installer
299 1. get available pods for each testcase/installer pair
300 2. get available scenario for each testcase/installer/pod tuple
301 3. construct KibanaInput and append
303 :return: list of KibanaDashboards
305 kibana_dashboards = []
306 for project, case_dicts in conf_utils.testcases_yaml.items():
307 for case in case_dicts:
308 case_name = case.get('name')
309 visualizations = case.get('visualizations')
310 family = case.get('test_family')
311 for installer in _installers:
312 pods_and_scenarios = _get_pods_and_scenarios(project, case_name, installer)
313 for visualization in visualizations:
314 for pod, scenarios in pods_and_scenarios.iteritems():
315 kibana_dashboards.append(KibanaDashboard(project,
322 return kibana_dashboards
325 def generate_js_inputs(js_file_path, kibana_url, dashboards):
327 for dashboard in dashboards:
328 dashboard_meta = dashboard['metadata']
329 test_family = dashboard_meta['test_family']
330 test_label = dashboard_meta['label']
332 if test_family not in js_dict:
333 js_dict[test_family] = {}
335 js_test_family = js_dict[test_family]
337 if test_label not in js_test_family:
338 js_test_family[test_label] = {}
340 js_test_label = js_test_family[test_label]
342 if dashboard.installer not in js_test_label:
343 js_test_label[dashboard.installer] = {}
345 js_installer = js_test_label[dashboard.installer]
346 js_installer[dashboard.pod] = kibana_url + '#/dashboard/' + dashboard.id
348 with open(js_file_path, 'w+') as js_file_fdesc:
349 js_file_fdesc.write('var kibana_dashboard_links = ')
350 js_file_fdesc.write(str(js_dict).replace("u'", "'"))
353 if __name__ == '__main__':
354 parser = argparse.ArgumentParser(description='Create Kibana dashboards from data in elasticsearch')
355 parser.add_argument('-e', '--elasticsearch-url', default='http://localhost:9200',
356 help='the url of elasticsearch, defaults to http://localhost:9200')
358 parser.add_argument('-js', '--generate_js_inputs', action='store_true',
359 help='Use this argument to generate javascript inputs for kibana landing page')
361 parser.add_argument('--js_path', default='/usr/share/nginx/html/kibana_dashboards/conf.js',
362 help='Path of javascript file with inputs for kibana landing page')
364 parser.add_argument('-k', '--kibana_url', default='https://testresults.opnfv.org/kibana/app/kibana',
365 help='The url of kibana for javascript inputs')
367 parser.add_argument('-u', '--elasticsearch-username', default=None,
368 help='The username with password for elasticsearch in format username:password')
370 args = parser.parse_args()
371 base_elastic_url = args.elasticsearch_url
372 generate_inputs = args.generate_js_inputs
373 input_file_path = args.js_path
374 kibana_url = args.kibana_url
375 es_creds = args.elasticsearch_username
377 dashboards = construct_dashboards()
379 for kibana_dashboard in dashboards:
380 kibana_dashboard.publish()
383 generate_js_inputs(input_file_path, kibana_url, dashboards)