6ceccd3740a56b64cbfb815540b77e5a47fb0161
[releng.git] / utils / test / result_collection_api / dashboard / qtip2Dashboard.py
1 #!/usr/bin/python
2
3 ##############################################################################
4 # Copyright (c) 2015 Dell Inc  and others.
5 #
6 # All rights reserved. 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 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12
13 def get_qtip_cases():
14     """
15     get the list of the supported test cases
16     TODO: update the list when adding a new test case for the dashboard
17     """
18     return ["compute_test_suite","storage_test_suite","network_test_suite"]
19
20 def check_qtip_case_exist(case):
21     """
22     check if the testcase exists
23     if the test case is not defined or not declared in the list
24     return False
25     """
26     qtip_cases = get_qtip_cases()
27     if (case is None or case not in qtip_cases):
28         return False
29     else:
30         return True
31
32 def format_qtip_for_dashboard(case, results):
33     """
34     generic method calling the method corresponding to the test case
35     check that the testcase is properly declared first
36     then build the call to the specific method
37     """
38     if check_qtip_case_exist(case):
39         res = format_common_for_dashboard(case, results)
40     else:
41         res = []
42         print "Test cases not declared"
43     return res
44
45 def format_common_for_dashboard(case, results):
46     """
47     Common post processing
48     """
49     test_data_description = case + " results for Dashboard"
50     test_data = [{'description': test_data_description}]
51
52     graph_name = ''
53     if "network_test_suite" in case:
54         graph_name = "Throughput index"
55     else:
56         graph_name = "Index"
57
58     # Graph 1:
59     # ********************************
60     new_element = []
61     for date, index in results:
62         new_element.append({'x': date,
63                             'y1': index,
64                             })
65
66     test_data.append({'name': graph_name,
67                       'info': {'type': "graph",
68                                'xlabel': 'time',
69                                'y1label': 'Index Number'},
70                       'data_set': new_element})
71
72     return test_data
73
74
75 ############################  For local test  ################################
76 import os
77 import requests
78 import json
79 from collections import defaultdict
80
81 def _get_results(db_url, testcase):
82
83     testproject = testcase["project"]
84     testcase = testcase["testcase"]
85     resultarray = defaultdict()
86     #header
87     header = {'Content-Type': 'application/json'}
88     #url
89     url = db_url + "/results?project="+testproject+"&case="+testcase
90     data = requests.get(url,header)
91     datajson = data.json()
92     for x in range(0, len(datajson['test_results'])):
93
94         rawresults = datajson['test_results'][x]['details']
95         index = rawresults['index']
96         resultarray[str(datajson['test_results'][x]['start_date'])]=index
97
98     return resultarray
99
100 def _test():
101
102     db_url = "http://testresults.opnfv.org/testapi"
103     raw_result = defaultdict()
104
105     raw_result = _get_results(db_url, {"project": "qtip", "testcase": "compute_test_suite"})
106     resultitems= raw_result.items()
107     result = format_qtip_for_dashboard("compute_test_suite", resultitems)
108     print result
109
110     raw_result = _get_results(db_url, {"project": "qtip", "testcase": "storage_test_suite"})
111     resultitems= raw_result.items()
112     result = format_qtip_for_dashboard("storage_test_suite", resultitems)
113     print result
114
115     raw_result = _get_results(db_url, {"project": "qtip", "testcase": "network_test_suite"})
116     resultitems= raw_result.items()
117     result = format_qtip_for_dashboard("network_test_suite", resultitems)
118     print result
119
120 if __name__ == '__main__':
121     _test()