Merge "Add SLA to Rally test cases"
[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
53     # Get results
54     myDataResults = dashboard_utils.get_results(TEST_DB, criteria)
55
56     # Depending on the use case, json for dashboarding is customized
57     # depending on the graph you want to show
58
59     test_data = [{'description': 'vPing results for Dashboard'}]
60
61     # Graph 1: Duration = f(time)
62     # ***************************
63     new_element = []
64     for data in myDataResults:
65         new_element.append({'x': data['creation_date'],
66                             'y': data['details']['duration']})
67
68     test_data.append({'name': "vPing duration",
69                       'info': {'type': "graph",
70                                'xlabel': 'time',
71                                'ylabel': 'duration (s)'},
72                       'data_set': new_element})
73
74     # Graph 2: bar
75     # ************
76     nbTest = 0
77     nbTestOk = 0
78
79     for data in myDataResults:
80         nbTest += 1
81         if data['details']['status'] == "OK":
82             nbTestOk += 1
83
84     test_data.append({'name': "vPing status",
85                       'info': {"type": "bar"},
86                       'data_set': [{'Nb tests': nbTest,
87                                     'Nb Success': nbTestOk}]})
88
89     # Generate json file
90     fileName = criteria.format()
91     logger.debug("Generate json file:" + fileName)
92
93     with open(fileName, "w") as outfile:
94         json.dump(test_data, outfile, indent=4)