1 # SPDX-License-Identifier: EPL-1.0
2 ##############################################################################
3 # Copyright (c) 2018 The Linux Foundation and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Eclipse Public License v1.0
7 # which accompanies this distribution, and is available at
8 # http://www.eclipse.org/legal/epl-v10.html
9 ##############################################################################
20 print(json.dumps(x, indent=4, sort_keys=True))
31 'categoryLines': False,
41 'legendPosition': 'right',
43 'addTimeMarker': False
48 def create(self, config):
50 temp['title'] = config['title']
51 temp['type'] = temp['params']['type'] = config['type']
54 temp['params']['categoryAxes'] = [dc(
55 cat.create()) for i in range(config['num_cat_axes'])]
58 temp['params']['valueAxes'] = [dc(val.create(position=i['position'],
61 config['value_axes'].items()]
65 temp['aggs'] = [dc(agg.create(field=i['field'],
66 custom_label=i['custom_label'],
69 config['aggs'].items()]
71 temp['params']['seriesParams'] = [seriesParams(i['data_type'],
78 config['seriesParams'].items()]
104 temp = dc(self.content)
105 temp['id'] = 'CategoryAxis-{}'.format(self.counter)
134 def create(self, position='left', title='Value'):
136 temp = dc(self.content)
137 temp['id'] = 'ValueAxis-{}'.format(self.counter)
138 if position == 'left':
139 temp['name'] = 'LeftAxis-{}'.format(self.counter)
140 elif position == 'right':
141 temp['name'] = 'RightAxis-{}'.format(self.counter)
143 # raise ValueError('Not one of left or right')
145 temp['name'] = 'LeftAxis-{}'.format(self.counter)
147 temp['title']['text'] = title
153 def __init__(self, data_type, mode, label, agg_id, value_axis):
162 'valueAxis': 'ValueAxis-{}'.format(value_axis),
163 'drawLinesBetweenPoints': True,
185 def create(self, field, custom_label, schema):
187 temp = dc(self.content)
188 temp['id'] = str(self.counter)
189 temp['params']['field'] = field
190 temp['params']['customLabel'] = custom_label
191 temp['schema'] = schema
192 if schema == 'metric':
195 elif schema == 'segment':
196 temp['type'] = 'terms'
197 temp['params']['size'] = 20 # default
198 temp['params']['order'] = 'asc'
199 temp['params']['orderBy'] = '_term'
203 # generate takes both the template config and project specific config and
204 # parses and organizes as much info available from that and
205 # generates an intermediate format first which
206 # contains all necessary info to deterministically create the visState to
207 # be sent to Kibana. Hence, any error occuring in the visualizaton side
208 # must first be checked by looking at the intermediate format.
210 def generate(dash_config, viz_config):
216 "index_pattern": None,
224 value_axes_format = {
231 seriesParams_format = {
243 "custom_label": None,
249 # all general description must be present in either of the config files
250 for config in [viz_config, dash_config]:
251 general_fields = ['type', 'index_pattern',
252 'num_cat_axes', 'title', 'desc', 'id']
253 for i in general_fields:
255 format[i] = config[i]
256 except KeyError as e:
259 # setting any default values if available
260 mappings = {'value_axes': value_axes_format,
261 'seriesParams': seriesParams_format, 'aggs': aggs_format}
262 for index, container in mappings.items():
264 default_values = viz_config[index]['default']
265 for i in default_values:
266 container['index'][i] = default_values[i]
270 value_axes_counter = 1
271 for m in viz_config['value_axes']:
273 temp = dc(value_axes_format)
274 temp[str(value_axes_counter)] = temp['index']
275 for i in ['position', 'title']:
277 temp[str(value_axes_counter)
278 ][i] = viz_config['value_axes'][m][i]
281 format['value_axes'].update(temp)
282 value_axes_counter += 1
284 seriesParams_counter = 1
285 seriesParams_fields = ['value_axis',
286 'data_type', 'mode', 'label', 'agg_id']
288 for m in viz_config['seriesParams']:
290 temp = dc(seriesParams_format)
291 temp[str(seriesParams_counter)] = temp['index']
292 for i in seriesParams_fields:
294 temp[str(seriesParams_counter)
295 ][i] = viz_config['seriesParams'][m][i]
298 format['seriesParams'].update(temp)
299 seriesParams_counter += 1
305 for m in viz_config['aggs']:
307 temp = dc(aggs_format)
308 temp[str(agg_counter)] = temp['index']
309 for i in ['field', 'custom_label', 'schema']:
311 temp[str(agg_counter)][i] = viz_config['aggs'][m][i]
314 format['aggs'].update(temp)
322 configs.append(viz_config)
327 dash_config['y-axis']['series']
328 configs.append(dash_config['y-axis'])
332 for config in configs:
334 value_axes_counter = 1
335 for key in config['value_axes']:
337 value_axes_temp = dc(value_axes_format)
338 value_axes_temp[str(value_axes_counter)
339 ] = value_axes_temp['index']
341 for index in ['position', 'title']:
344 value_axes_counter)][index] = \
345 config['value_axes'][key][index]
346 except KeyError as e:
348 format['value_axes'].update(value_axes_temp)
349 value_axes_counter += 1
351 except KeyError as e:
355 for key in config['series']:
357 # check if this key is present or not
358 config['series'][key]['not_in_seriesParams']
360 seriesParams_temp = dc(seriesParams_format)
361 seriesParams_temp[str(
362 seriesParams_counter)] = seriesParams_temp['index']
363 for index in ['value_axis', 'data_type', 'mode', 'label']:
365 seriesParams_temp[str(
366 seriesParams_counter)][index] = \
367 config['series'][key][index]
368 except KeyError as e:
370 seriesParams_temp[str(
371 seriesParams_counter)]['agg_id'] = agg_counter
372 format['seriesParams'].update(seriesParams_temp)
373 seriesParams_counter += 1
375 agg_temp = dc(aggs_format)
376 agg_temp[str(agg_counter)] = agg_temp['index']
377 for index in ['field', 'schema']:
379 agg_temp[str(agg_counter)
380 ][index] = config['series'][key][index]
381 except KeyError as e:
384 agg_counter)]['custom_label'] = \
385 config['series'][key]['label']
386 format['aggs'].update(agg_temp)
388 except KeyError as e:
389 print("required fields are empty!")
391 # to remove the default template index
392 for i in ['value_axes', 'seriesParams', 'aggs']:
394 format[i].pop('index')
396 # print("No default index found")
399 if not config_validator(format):
400 raise ValueError('Missing required field values')
405 generated_visState = vis.create(format)
407 # checking incase there are None values \
408 # in the format indicating missing fields
410 if not config_validator(generated_visState):
411 raise KeyError('required fields are missing values!')
412 return format, generated_visState
415 # Check the generated format if it contains any key with None
416 # as it's value which indicates incomplete information
417 def config_validator(val):
419 for _, i in val.items():
420 if isinstance(i, dict):
421 flag = config_validator(i)
427 if __name__ == '__main__':
428 with open('viz.yaml', 'r') as f:
429 viz_config = yaml.safe_load(f)
431 with open('dashboard.yaml', 'r') as f:
432 dash_config = yaml.safe_load(f)
434 generate(dash_config['dashboard']['viz'][2],
435 viz_config['opendaylight-test-performance'])
436 # generate(dash_config['dashboard']['viz'][3],viz_config['opendaylight-test-performance'])
437 # generate(dash_config['dashboard']['viz'][1],viz_config['opendaylight-test-feature'])