add dashboard for bottlenecks project
[releng.git] / utils / test / result_collection_api / dashboard / bottlenecks2Dashboard.py
1 #!/usr/bin/python
2 #
3 ##############################################################################
4 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and other.
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 # This script is used to build dashboard ready json results
13 # It may be used for all the test case of the Bottlenecks project
14 # a new method format_<Test_case>_for_dashboard(results)
15 # v0.1: basic example with methods for Rubbos.
16 #
17
18
19 def get_bottlenecks_cases():
20     """
21     get the list of the supported test cases
22     TODO: update the list when adding a new test case for the dashboard
23     """
24     return ["rubbos"]
25
26
27 def check_bottlenecks_case_exist(case):
28     """
29     check if the testcase exists
30     if the test case is not defined or not declared in the list
31     return False
32     """
33     bottlenecks_cases = get_bottlenecks_cases()
34
35     if case is None or case not in bottlenecks_cases:
36         return False
37     else:
38         return True
39
40
41 def format_bottlenecks_for_dashboard(case, results):
42     """
43     generic method calling the method corresponding to the test case
44     check that the testcase is properly declared first
45     then build the call to the specific method
46     """
47     if check_bottlenecks_case_exist(case):
48         cmd = "format_" + case + "_for_dashboard(results)"
49         res = eval(cmd)
50     else:
51         res = []
52         print "Test cases not declared"
53     return res
54
55
56 def format_rubbos_for_dashboard(results):
57
58     """
59     Post processing for the Rubbos test case
60     """
61     test_data = [{'description': 'Rubbos results'}]
62
63     # Graph 1:
64     # ********************************
65     new_element = []
66     for each_result in results:
67         throughput_data = [record['throughput'] for record in each_result['details']]
68         new_element.append({'x': each_result['creation_date'],
69                             'y': max(throughput_data)})
70
71     test_data.append({'name': "Rubbos max throughput",
72                       'info': {'type': "graph",
73                                'xlabel': 'time',
74                                'ylabel': 'maximal throughput'},
75                       'data_set': new_element})
76     return test_data
77
78
79 # for local test
80 import json
81
82
83 def _test():
84     print('Post processing for the Rubbos test case begin<--')
85     results = '[{"details":[{"client":200,"throughput":20},{"client":300,"throughput":50}],"project_name":' \
86               '"bottlenecks","pod_name":"unknown-pod","version":"unknown","installer":"fuel","description":' \
87               '"bottlenecks test cases result","_id":"56793f11514bc5068a345da4","creation_date":' \
88               '"2015-12-22 12:16:17.131438","case_name":"rubbos"},{"details":[{"client":200,"throughput":25},' \
89               '{"client":300,"throughput":52}],"project_name":"bottlenecks","pod_name":"unknown-pod","version":' \
90               '"unknown","installer":"fuel","description":"bottlenecks test cases result","_id":' \
91               '"56793f11514bc5068a345da4","creation_date":"2015-12-23 12:16:17.131438","case_name":"rubbos"}]'
92
93     print("the output is:")
94     print(format_rubbos_for_dashboard(json.loads(results)))
95     print('Post processing for the Rubbos test case end<--')
96
97
98 if __name__ == '__main__':
99     _test()