Merge "Include cirros image in the docker container"
[functest.git] / testcases / Dashboard / dashboard_utils.py
1 # copyrighi (c) 2015 Orange
2
3 # morgan.richomme@orange.com
4 #
5 # This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # This script is used to get data from test DB
12 # and format them into a json format adapted for a dashboard
13 #
14 # v0.1: basic example
15 #
16 import json
17 import requests
18 from vPing2Dashboard import format_vPing_for_dashboard
19
20
21 class TestCriteria:
22
23     """ describes the test criteria platform """
24     def __init__(self):
25         self.project = ''
26         self.testcase = ''
27         self.pod_name = 'all'
28         self.duration = 'all'
29         self.version = 'all'
30         self.installer = 'all'
31
32     def setCriteria(self, project, testcase, pod_name,
33                     duration, version, installer):
34         self.project = project
35         self.testcase = testcase
36         self.pod_name = pod_name
37         self.duration = duration
38         self.version = version
39         self.installer = installer
40
41     def format_criteria(self, name):
42         if(name == 'all' or name == 0):
43             return ""
44         else:
45             if(type(name) == int):
46                 return "_" + str(name)
47             else:
48                 return "_" + name
49
50     def format(self):
51         pod_name = self.format_criteria(self.pod_name)
52         version_name = self.format_criteria(self.version)
53         installer_name = self.format_criteria(self.installer)
54         duration_name = self.format_criteria(self.duration)
55         try:
56             fileName = "result_" + self.project + "_" + self.testcase + \
57                        pod_name + version_name + installer_name + \
58                        duration_name + ".json"
59         except:
60             print "Impossible to format json file name"
61         return fileName
62
63
64 def get_pods(db_url):
65     # retrieve the list of pods
66     url = db_url + "/pods"
67     # Build headers
68     headers = {'Content-Type': 'application/json'}
69
70     try:
71         db_data = requests.get(url, headers=headers)
72         # Get result as a json object
73         pods_data = json.loads(db_data.text)
74         # Get results
75         pods = pods_data['pods']
76         pods_table = []
77         for pod in pods:
78             # cast int becase otherwise API retrieve 1.0
79             # TODO check format with API
80             pods_table.append(pod['name'])
81
82         pods_table.append('all')
83         return pods_table
84     except:
85         print "Error retrieving the list of PODs"
86         return None
87
88
89 def get_versions(db_url):
90     # retrieve the list of versions
91     # TODO not supported in API yet
92     url = db_url + "/results"
93     # Build headers
94     headers = {'Content-Type': 'application/json'}
95
96     try:
97         db_data = requests.get(url, headers=headers)
98         # Get result as a json object
99         versions_data = json.loads(db_data.text)
100         # Get results
101         versions = versions_data['test_results']
102         versions_table = []
103         for version in versions:
104             if (version['version'] is not None):
105                 versions_table.append(version['version'])
106
107         versions_table.append('all')
108         versions_table = sorted(set(versions_table))
109         return versions_table
110     except:
111         print "Error retrieving the list of OPNFV versions"
112         return None
113
114
115 def get_installers(db_url):
116     # retrieve the list of installers
117     # TODO not supported in API yet
118     url = db_url + "/results"
119     # Build headers
120     headers = {'Content-Type': 'application/json'}
121
122     try:
123         db_data = requests.get(url, headers=headers)
124         # Get result as a json object
125         installers_data = json.loads(db_data.text)
126         # Get results
127         installers = installers_data['test_results']
128
129         installers_table = []
130         for installer in installers:
131             if (installer['installer'] is not None):
132                 installers_table.append(installer['installer'])
133
134         installers_table.append('all')
135         installers_table = sorted(set(installers_table))
136         return installers_table
137     except:
138         print "Error retrieving the list of OPNFV installers"
139         return None
140
141
142 def get_testcases(db_url, project):
143     # retrieve the list of pods
144     url = db_url + "/test_projects/" + project + "/cases"
145     # Build headers
146     headers = {'Content-Type': 'application/json'}
147
148     try:
149         db_data = requests.get(url, headers=headers)
150         # Get result as a json object
151         testcases_data = json.loads(db_data.text)
152         # Get results
153         testcases = testcases_data['test_cases']
154         testcases_table = []
155         for testcase in testcases:
156             testcases_table.append(testcase['name'])
157
158         testcases_table.append('all')
159
160         return testcases_table
161     except:
162         print "Error retrieving the list of testcases"
163         return None
164
165
166 def get_results(db_url, test_criteria):
167
168     # use param to filter request to result DB
169     # if not precised => no filter
170     # filter criteria:
171     # - POD
172     # - versions
173     # - installers
174     # - testcase
175     # - test projects
176     # - timeframe (last 30 days, 365 days, since beginning of the project)
177     # e.g.
178     # - vPing tests since 2 months
179     # - Tempest tests on LF POD2 fuel based / Arno stable since the beginning
180     # - yardstick tests on any POD since 30 days
181     # - Qtip tests on dell-test1 POD
182     #
183     # params = {"pod_name":pod, "testcase":testcase}
184     # filter_date = days # data from now - days
185
186     test_project = test_criteria.project
187     testcase = test_criteria.testcase
188     period = test_criteria.duration
189     version = test_criteria.version
190     installer = test_criteria.installer
191     pod = test_criteria.pod_name
192
193     # TODO complete params (installer type, testcase, version )
194     # need API to be up to date
195     # we assume that criteria could be used at the API level
196     # no need to processing on date for instance
197     #  params = {"pod_name": pod_name}
198
199     # Build headers
200     headers = {'Content-Type': 'application/json'}
201
202     # build the request
203     # if criteria is all => remove criteria
204     url = db_url + "/results?project=" + test_project + "&case=" + testcase
205
206     if (pod != "all"):
207         url += "&pod=" + pod
208     if (installer != "all"):
209         url += "&installer=" + installer
210     if (version != "all"):
211         url += "&version=" + version
212     url += "&period=" + str(period)
213
214     # Send Request to Test DB
215     myData = requests.get(url, headers=headers)
216
217     # Get result as a json object
218     myNewData = json.loads(myData.text)
219
220     # Get results
221     myDataResults = myNewData['test_results']
222
223     return myDataResults
224
225
226 def generateJson(test_name, test_case, db_url):
227     # pod_id = "opnfv-jump-1'
228     # test_version = 'Arno master'
229     # test_installer = 'fuel'
230     # test_retention = 30
231
232     pods = get_pods(db_url)
233     versions = get_versions(db_url)
234     installers = get_installers(db_url)
235
236     test_durations = [90, 365, 0]  # 0 means since the beginning
237
238     # For all the PoDs
239     for pod in pods:
240         # all the versions
241         for version in versions:
242             # all the installers
243             for installer in installers:
244                 # all the retention time
245                 for test_duration in test_durations:
246
247                     criteria = TestCriteria()
248                     criteria.setCriteria(test_name, test_case, pod,
249                                          test_duration, version, installer)
250                     format_data_for_dashboard(criteria)
251
252
253 def format_data_for_dashboard(criteria):
254
255     # Depending on the use case, json for dashboarding is customized
256     # depending on the graph you want to show
257
258     if (criteria.testcase == "vPing"):
259         format_vPing_for_dashboard(criteria)