refactor modify_mongo_entry of kibana_dashboard
[releng.git] / utils / test / scripts / mongo2elastic_format.py
1 #! /usr/bin/env python
2
3
4 def _convert_value(value):
5     return value if value != '' else 0
6
7
8 def _convert_duration(duration):
9     if (isinstance(duration, str) or isinstance(duration, unicode)) and ':' in duration:
10         hours, minutes, seconds = duration.split(":")
11         hours = _convert_value(hours)
12         minutes = _convert_value(minutes)
13         seconds = _convert_value(seconds)
14         int_duration = 3600 * int(hours) + 60 * int(minutes) + float(seconds)
15     else:
16         int_duration = duration
17     return int_duration
18
19
20 def format_normal(testcase):
21     """
22     Look for these and leave any of those:
23         details.duration
24         details.tests
25         details.failures
26
27     If none are present, then return False
28     """
29     found = False
30     testcase_details = testcase['details']
31     fields = ['duration', 'tests', 'failures']
32     if isinstance(testcase_details, dict):
33         for key, value in testcase_details.items():
34             if key in fields:
35                 found = True
36                 if key == 'duration':
37                     testcase_details[key] = _convert_duration(value)
38             else:
39                 del testcase_details[key]
40
41     if 'tests' in testcase_details and 'failures' in testcase_details:
42         testcase_tests = float(testcase_details['tests'])
43         testcase_failures = float(testcase_details['failures'])
44         if testcase_tests != 0:
45             testcase_details['success_percentage'] = 100 * (testcase_tests - testcase_failures) / testcase_tests
46         else:
47             testcase_details['success_percentage'] = 0
48
49
50     return found
51
52
53 def format_rally(testcase):
54     """
55     Structure:
56         details.[{summary.duration}]
57         details.[{summary.nb success}]
58         details.[{summary.nb tests}]
59
60     Find data for these fields
61         -> details.duration
62         -> details.tests
63         -> details.success_percentage
64     """
65     summary = testcase['details']['summary']
66
67     testcase['details'] = {
68         'duration': summary['duration'],
69         'tests': summary['nb tests'],
70         'success_percentage': summary['nb success']
71     }
72     return True
73
74
75 def _get_statistics(orig_data, stat_fields, stat_values=None):
76     test_results = {}
77     for stat_data in orig_data:
78         for field in stat_fields:
79             stat_value = stat_data[field]
80             if stat_value in test_results:
81                 test_results[stat_value] += 1
82             else:
83                 test_results[stat_value] = 1
84
85     if stat_values is not None:
86         for stat_value in stat_values:
87             if stat_value not in test_results:
88                 test_results[stat_value] = 0
89
90     return test_results
91
92
93 def format_onos(testcase):
94     """
95     Structure:
96         details.FUNCvirNet.duration
97         details.FUNCvirNet.status.[{Case result}]
98         details.FUNCvirNetL3.duration
99         details.FUNCvirNetL3.status.[{Case result}]
100
101     Find data for these fields
102         -> details.FUNCvirNet.duration
103         -> details.FUNCvirNet.tests
104         -> details.FUNCvirNet.failures
105         -> details.FUNCvirNetL3.duration
106         -> details.FUNCvirNetL3.tests
107         -> details.FUNCvirNetL3.failures
108     """
109     testcase_details = testcase['details']
110
111     if 'FUNCvirNet' not in testcase_details or 'FUNCvirNetL3' not in testcase_details:
112         return False
113
114     funcvirnet_details = testcase_details['FUNCvirNet']['status']
115     funcvirnet_stats = _get_statistics(funcvirnet_details, ('Case result',), ('PASS', 'FAIL'))
116     funcvirnet_passed = funcvirnet_stats['PASS']
117     funcvirnet_failed = funcvirnet_stats['FAIL']
118     funcvirnet_all = funcvirnet_passed + funcvirnet_failed
119
120     funcvirnetl3_details = testcase_details['FUNCvirNetL3']['status']
121     funcvirnetl3_stats = _get_statistics(funcvirnetl3_details, ('Case result',), ('PASS', 'FAIL'))
122     funcvirnetl3_passed = funcvirnetl3_stats['PASS']
123     funcvirnetl3_failed = funcvirnetl3_stats['FAIL']
124     funcvirnetl3_all = funcvirnetl3_passed + funcvirnetl3_failed
125
126     testcase_details['FUNCvirNet'] = {
127         'duration': _convert_duration(testcase_details['FUNCvirNet']['duration']),
128         'tests': funcvirnet_all,
129         'failures': funcvirnet_failed
130     }
131     testcase_details['FUNCvirNetL3'] = {
132         'duration': _convert_duration(testcase_details['FUNCvirNetL3']['duration']),
133         'tests': funcvirnetl3_all,
134         'failures': funcvirnetl3_failed
135     }
136     return True
137
138
139 def format_vims(testcase):
140     """
141     Structure:
142         details.sig_test.result.[{result}]
143         details.sig_test.duration
144         details.vIMS.duration
145         details.orchestrator.duration
146
147     Find data for these fields
148         -> details.sig_test.duration
149         -> details.sig_test.tests
150         -> details.sig_test.failures
151         -> details.sig_test.passed
152         -> details.sig_test.skipped
153         -> details.vIMS.duration
154         -> details.orchestrator.duration
155     """
156     testcase_details = testcase['details']
157     test_results = _get_statistics(testcase_details['sig_test']['result'],
158                                    ('result',),
159                                    ('Passed', 'Skipped', 'Failed'))
160     passed = test_results['Passed']
161     skipped = test_results['Skipped']
162     failures = test_results['Failed']
163     all_tests = passed + skipped + failures
164     testcase['details'] = {
165         'sig_test': {
166             'duration': testcase_details['sig_test']['duration'],
167             'tests': all_tests,
168             'failures': failures,
169             'passed': passed,
170             'skipped': skipped
171         },
172         'vIMS': {
173             'duration': testcase_details['vIMS']['duration']
174         },
175         'orchestrator': {
176             'duration': testcase_details['orchestrator']['duration']
177         }
178     }
179     return True