Merge "ODL test suite requirements.pip moved to general requirements"
[functest.git] / testcases / Dashboard / vPing2Dashboard.py
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2015 Orange
4 # morgan.richomme@orange.com
5 #
6 # This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # This script is used to build json files for the dashboard
13 # for the vPing test case
14 #
15 # v0.1: basic example
16 #
17 import logging
18 import argparse
19 import pprint
20 import json
21 import dashboard_utils
22 import os
23 import yaml
24
25 pp = pprint.PrettyPrinter(indent=4)
26
27 parser = argparse.ArgumentParser()
28 parser.add_argument("repo_path", help="Path to the repository")
29 parser.add_argument("-d", "--debug", help="Debug mode",  action="store_true")
30 args = parser.parse_args()
31
32 """ logging configuration """
33 logger = logging.getLogger('config_functest')
34 logger.setLevel(logging.DEBUG)
35
36 if not os.path.exists(args.repo_path):
37     logger.error("Repo directory not found '%s'" % args.repo_path)
38     exit(-1)
39
40 with open(args.repo_path+"testcases/config_functest.yaml") as f:
41     functest_yaml = yaml.safe_load(f)
42 f.close()
43
44 """ global variables """
45 # Directories
46 HOME = os.environ['HOME']+"/"
47 REPO_PATH = args.repo_path
48 TEST_DB = functest_yaml.get("results").get("test_db_url")
49
50
51 def format_vPing_for_dashboard(criteria):
52     # Get results
53     myDataResults = dashboard_utils.get_results(TEST_DB, criteria)
54
55     # Depending on the use case, json for dashboarding is customized
56     # depending on the graph you want to show
57
58     test_data = [{'description': 'vPing results for Dashboard'}]
59
60     # Graph 1: Duration = f(time)
61     # ***************************
62     new_element = []
63     for data in myDataResults:
64         new_element.append({'x': data['creation_date'],
65                             'y': data['details']['duration']})
66
67     test_data.append({'name': "vPing duration",
68                       'info': {'type': "graph",
69                                'xlabel': 'time',
70                                'ylabel': 'duration (s)'},
71                       'data_set': new_element})
72
73     # Graph 2: bar
74     # ************
75     nbTest = 0
76     nbTestOk = 0
77
78     for data in myDataResults:
79         nbTest += 1
80         if data['details']['status'] == "OK":
81             nbTestOk += 1
82
83     test_data.append({'name': "vPing status",
84                       'info': {"type": "bar"},
85                       'data_set': [{'Nb tests': nbTest,
86                                     'Nb Success': nbTestOk}]})
87
88     # Generate json file
89     fileName = criteria.format()
90     logger.debug("Generate json file:" + fileName)
91
92     with open(fileName, "w") as outfile:
93         json.dump(test_data, outfile, indent=4)