Integrate functest with ARM Architecture
[releng.git] / utils / test / reporting / api / handlers / landing.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
3 #
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 ##############################################################################
9 import requests
10 import time
11
12 from tornado.escape import json_encode
13 from tornado.escape import json_decode
14
15 from api.handlers import BaseHandler
16 from api import conf
17
18
19 class FiltersHandler(BaseHandler):
20     def get(self):
21         self._set_header()
22
23         filters = {
24             'filters': {
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']
31             }
32         }
33         return self.write(json_encode(filters))
34
35
36 class ScenariosHandler(BaseHandler):
37     def post(self):
38         self._set_header()
39
40         body = json_decode(self.request.body)
41         args = self._get_args(body)
42
43         scenarios = self._get_result_data(self._get_scenarios(), args)
44
45         return self.write(json_encode(dict(scenarios=scenarios)))
46
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}
50
51     def _filter_status(self, data, args):
52         return {k: v for k, v in data.items() if v['status'] in args['status']}
53
54     def _get_scenario_result(self, scenario, data, args):
55         result = {
56             'status': data.get('status'),
57             'installers': self._get_installers_result(data, args)
58         }
59         return result
60
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']}
64
65     def _get_installer_result(self, data, args):
66         return self._get_version_data(data.get(args['version'], {}), args)
67
68     def _get_version_data(self, data, args):
69         return {k: self._get_project_data(data.get(k, {}))
70                 for k in args['projects']}
71
72     def _get_project_data(self, data):
73         atom = {
74             'score': data.get('score', ''),
75             'status': data.get('status', '')
76         }
77
78         return atom
79
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', [])
85                                                   ) for a in data}
86
87     def _get_scenario(self, data):
88         installers = {a.get('installer'): self._get_installer(a.get('versions',
89                                                                     [])
90                                                               ) for a in data}
91         scenario = {
92             'status': self._get_status()
93         }
94         scenario.update(installers)
95
96         return scenario
97
98     def _get_status(self):
99         return 'success'
100
101     def _get_installer(self, data):
102         return {a.get('version'): self._get_version(a.get('projects'))
103                 for a in data}
104
105     def _get_version(self, data):
106         return {a.get('project'): self._get_project(a) for a in data}
107
108     def _get_project(self, data):
109         scores = data.get('scores', [])
110         trusts = data.get('trust_indicators', [])
111
112         try:
113             date = sorted(scores, reverse=True)[0].get('date')
114         except IndexError:
115             data = time.time()
116
117         try:
118             score = sorted(scores, reverse=True)[0].get('score')
119         except IndexError:
120             score = None
121
122         try:
123             status = sorted(trusts, reverse=True)[0].get('status')
124         except IndexError:
125             status = None
126
127         return {'date': date, 'score': score, 'status': status}
128
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]
134         else:
135             try:
136                 new = eval(obj)
137                 if isinstance(new, int):
138                     return obj
139                 return self._change_to_utf8(new)
140             except (NameError, TypeError, SyntaxError):
141                 return str(obj)
142
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)
147
148         args = {
149             'status': status,
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()
155         }
156         return args
157
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
162
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
167
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