Merge "added flag --yes to install_rally to force install dependencies"
[functest.git] / testcases / Dashboard / dashboard_utils.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 get data from test DB
13 # and format them into a json format adapted for a dashboard
14 #
15 # v0.1: basic example
16 #
17 import json
18 import requests
19 from vPing2Dashboard import format_vPing_for_dashboard
20
21
22 class TestCriteria:
23
24     """ describes the test criteria platform """
25     def __init__(self):
26         self.project = ''
27         self.testcase = ''
28         self.pod_id = -1
29         self.duration = 'all'
30         self.version = 'all'
31         self.installer = 'all'
32
33     def setCriteria(self, project, testcase, pod_id,
34                     duration, version, installer):
35         self.project = project
36         self.testcase = testcase
37         self.pod_id = pod_id
38         self.duration = duration
39         self.version = version
40         self.installer = installer
41
42     def format_criteria(self, name):
43         if(name == 'all' or name == 0):
44             return ""
45         else:
46             if(type(name) == int):
47                 return "-" + str(name)
48             else:
49                 return "-" + name
50
51     def format(self):
52         pod_name = self.format_criteria(self.pod_id)
53         version_name = self.format_criteria(self.version)
54         installer_name = self.format_criteria(self.installer)
55         duration_name = self.format_criteria(self.duration)
56         try:
57             fileName = "result-" + self.project + "-" + self.testcase + \
58                        pod_name + version_name + installer_name + \
59                        duration_name + ".json"
60         except:
61             print "Impossible to format json file name"
62         return fileName
63
64
65 def get_pods(db_url):
66     # retrieve the list of pods
67     url = db_url + "/pods"
68     # Build headers
69     headers = {'Content-Type': 'application/json'}
70
71     try:
72         db_data = requests.get(url, headers=headers)
73         # Get result as a json object
74         pods_data = json.loads(db_data.text)
75         # Get results
76         pods = pods_data['pods']
77         pods_table = []
78         for pod in pods:
79             # cast int becase otherwise API retrieve 1.0
80             # TODO check format with API
81             pods_table.append(int(pod['_id']))
82
83         pods_table.append(0)  # 0 means all the pods here
84         return pods_table
85     except:
86         print "Error retrieving the list of PODs"
87         return None
88
89
90 def get_versions(db_url):
91     # retrieve the list of versions
92     # TODO not supported in API yet
93     url = db_url + "/versions"
94     # Build headers
95     headers = {'Content-Type': 'application/json'}
96
97     try:
98         db_data = requests.get(url, headers=headers)
99         # Get result as a json object
100         versions_data = json.loads(db_data.text)
101         # Get results
102         versions = versions_data['versions']
103
104         versions_table = []
105         for version in versions:
106             versions_table.append(version['version'])
107
108         versions_table.append('all')
109
110         return versions_table
111     except:
112         print "Error retrieving the list of OPNFV versions"
113         return None
114
115
116 def get_installers(db_url):
117     # retrieve the list of installers
118     # TODO not supported in API yet
119     url = db_url + "/installers"
120     # Build headers
121     headers = {'Content-Type': 'application/json'}
122
123     try:
124         db_data = requests.get(url, headers=headers)
125         # Get result as a json object
126         installers_data = json.loads(db_data.text)
127         # Get results
128         installers = installers_data['installers']
129
130         installers_table = []
131         for installer in installers:
132             installers_table.append(installer['installer'])
133
134         installers_table.append('all')
135
136         return installers
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_id":pod_id, "testcase":testcase}
184     # filter_date = days # data from now - days
185
186     # test_project = test_criteria.project
187     testcase = test_criteria.testcase
188     # duration_frame = test_criteria.duration
189     # version = test_criteria.version
190     # installer_type = test_criteria.installer
191     pod_id = test_criteria.pod_id
192
193     pod_criteria = ""
194     if (pod_id > 0):
195         pod_criteria = "&pod=" + str(pod_id)
196
197     # TODO complete params (installer type, testcase, version )
198     # need API to be up to date
199     # we assume that criteria could be used at the API level
200     # no need to processing on date for instance
201     params = {"pod_id": pod_id}
202
203     # Build headers
204     headers = {'Content-Type': 'application/json'}
205
206     url = db_url + "/results?case=" + testcase + pod_criteria
207
208     # Send Request to Test DB
209     myData = requests.get(url, data=json.dumps(params), headers=headers)
210     # Get result as a json object
211     myNewData = json.loads(myData.text)
212
213     # Get results
214     myDataResults = myNewData['test_results']
215
216     return myDataResults
217
218
219 def generateJson(test_name, test_case, db_url):
220     # pod_id = 1
221     # test_version = 'Arno master'
222     # test_installer = 'fuel'
223     # test_retention = 30
224
225     pods = get_pods(db_url)
226     versions = ['ArnoR1', 'ArnoSR1', 'all']  # not available in the API yet
227     installers = ['fuel', 'foreman', 'all']  # not available in the API yet
228     test_durations = [90, 365, 'all']  # not available through the API yet
229
230     # For all the PoDs
231     for pod in pods:
232         # all the versions
233         for version in versions:
234             # all the installers
235             for installer in installers:
236                 # all the retention time
237                 for test_duration in test_durations:
238
239                     criteria = TestCriteria()
240                     criteria.setCriteria(test_name, test_case, pod,
241                                          test_duration, version, installer)
242                     format_data_for_dashboard(criteria)
243
244
245 def format_data_for_dashboard(criteria):
246
247     # Depending on the use case, json for dashboarding is customized
248     # depending on the graph you want to show
249
250     if (criteria.testcase == "vPing"):
251         format_vPing_for_dashboard(criteria)