1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
12 from tornado.escape import json_encode
13 from tornado.escape import json_decode
15 from api.handlers import BaseHandler
19 class FiltersHandler(BaseHandler):
25 'status': ['success', 'warning', 'danger'],
26 'projects': ['functest', 'yardstick'],
27 'installers': ['apex', 'compass', 'fuel', 'joid'],
28 'version': ['master', 'colorado', 'danube'],
29 'loops': ['daily', 'weekly', 'monthly'],
30 'time': ['10 days', '30 days']
33 return self.write(json_encode(filters))
36 class ScenariosHandler(BaseHandler):
40 body = json_decode(self.request.body)
41 args = self._get_args(body)
43 scenarios = self._get_result_data(self._get_scenarios(), args)
45 return self.write(json_encode(dict(scenarios=scenarios)))
47 def _get_result_data(self, data, args):
48 data = self._filter_status(data, args)
49 return {s: self._get_scenario_result(s, data[s], args) for s in data}
51 def _filter_status(self, data, args):
52 return {k: v for k, v in data.items() if v['status'] in args['status']}
54 def _get_scenario_result(self, scenario, data, args):
56 'status': data.get('status'),
57 'installers': self._get_installers_result(data, args)
61 def _get_installers_result(self, data, args):
62 func = self._get_installer_result
63 return {k: func(data.get(k, {}), args) for k in args['installers']}
65 def _get_installer_result(self, data, args):
66 return self._get_version_data(data.get(args['version'], {}), args)
68 def _get_version_data(self, data, args):
69 return {k: self._get_project_data(data.get(k, {}))
70 for k in args['projects']}
72 def _get_project_data(self, data):
74 'score': data.get('score', ''),
75 'status': data.get('status', '')
80 def _get_scenarios(self):
81 url = '{}/scenarios'.format(conf.base_url)
82 resp = requests.get(url).json()
83 data = self._change_to_utf8(resp).get('scenarios', {})
84 return {a.get('name'): self._get_scenario(a.get('installers', [])
87 def _get_scenario(self, data):
88 installers = {a.get('installer'): self._get_installer(a.get('versions',
92 'status': self._get_status()
94 scenario.update(installers)
98 def _get_status(self):
101 def _get_installer(self, data):
102 return {a.get('version'): self._get_version(a.get('projects'))
105 def _get_version(self, data):
106 return {a.get('project'): self._get_project(a) for a in data}
108 def _get_project(self, data):
109 scores = data.get('scores', [])
110 trusts = data.get('trust_indicators', [])
113 date = sorted(scores, reverse=True)[0].get('date')
118 score = sorted(scores, reverse=True)[0].get('score')
123 status = sorted(trusts, reverse=True)[0].get('status')
127 return {'date': date, 'score': score, 'status': status}
129 def _change_to_utf8(self, obj):
130 if isinstance(obj, dict):
131 return {str(k): self._change_to_utf8(v) for k, v in obj.items()}
132 elif isinstance(obj, list):
133 return [self._change_to_utf8(ele) for ele in obj]
137 if isinstance(new, int):
139 return self._change_to_utf8(new)
140 except (NameError, TypeError, SyntaxError):
143 def _get_args(self, body):
144 status = self._get_status_args(body)
145 projects = self._get_projects_args(body)
146 installers = self._get_installers_args(body)
150 'projects': projects,
151 'installers': installers,
152 'version': body.get('version', 'master').lower(),
153 'loops': body.get('loops', 'daily').lower(),
154 'time': body.get('times', '10 days')[:2].lower()
158 def _get_status_args(self, body):
159 status_all = ['success', 'warning', 'danger']
160 status = [a.lower() for a in body.get('status', ['all'])]
161 return status_all if 'all' in status else status
163 def _get_projects_args(self, body):
164 project_all = ['functest', 'yardstick']
165 projects = [a.lower() for a in body.get('projects', ['all'])]
166 return project_all if 'all' in projects else projects
168 def _get_installers_args(self, body):
169 installer_all = ['apex', 'compass', 'fuel', 'joid']
170 installers = [a.lower() for a in body.get('installers', ['all'])]
171 return installer_all if 'all' in installers else installers