Merge "Reporting docker deployment improvement"
[releng.git] / utils / test / reporting / api / 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
11 from tornado.escape import json_encode
12 from tornado.escape import json_decode
13
14 from api.handlers import BaseHandler
15 from api import conf
16
17
18 class FiltersHandler(BaseHandler):
19     def get(self):
20         self._set_header()
21
22         filters = {
23             'filters': {
24                 'status': ['success', 'warning', 'danger'],
25                 'projects': ['functest', 'yardstick'],
26                 'installers': ['apex', 'compass', 'fuel', 'joid'],
27                 'version': ['colorado', 'master'],
28                 'loops': ['daily', 'weekly', 'monthly'],
29                 'time': ['10 days', '30 days']
30             }
31         }
32         return self.write(json_encode(filters))
33
34
35 class ScenariosHandler(BaseHandler):
36     def post(self):
37         self._set_header()
38
39         body = json_decode(self.request.body)
40         args = self._get_args(body)
41
42         scenarios = self._get_result_data(self._get_scenarios(), args)
43
44         return self.write(json_encode(dict(scenarios=scenarios)))
45
46     def _get_result_data(self, data, args):
47         data = self._filter_status(data, args)
48         return {s: self._get_scenario_result(s, data[s], args) for s in data}
49
50     def _filter_status(self, data, args):
51         return {k: v for k, v in data.items() if v['status'] in args['status']}
52
53     def _get_scenario_result(self, scenario, data, args):
54         result = {
55             'status': data.get('status'),
56             'installers': self._get_installers_result(data['installers'], args)
57         }
58         return result
59
60     def _get_installers_result(self, data, args):
61         func = self._get_installer_result
62         return {k: func(k, data.get(k, {}), args) for k in args['installers']}
63
64     def _get_installer_result(self, installer, data, args):
65         projects = data.get(args['version'], [])
66         return [self._get_project_data(projects, p) for p in args['projects']]
67
68     def _get_project_data(self, projects, project):
69         atom = {
70             'project': project,
71             'score': None,
72             'status': None
73         }
74         for p in projects:
75             if p['project'] == project:
76                 return p
77         return atom
78
79     def _get_scenarios(self):
80         url = '{}/scenarios'.format(conf.base_url)
81         resp = requests.get(url).json()
82         data = self._change_to_utf8(resp).get('scenarios', {})
83         return {a.get('name'): self._get_scenario(a.get('installers', [])
84                                                   ) for a in data}
85
86     def _get_scenario(self, data):
87         installers = {a.get('installer'): self._get_installer(a.get('versions',
88                                                                     [])
89                                                               ) for a in data}
90         scenario = {
91             'status': self._get_status(),
92             'installers': installers
93         }
94         return scenario
95
96     def _get_status(self):
97         return 'success'
98
99     def _get_installer(self, data):
100         return {a.get('version'): self._get_version(a) for a in data}
101
102     def _get_version(self, data):
103         try:
104             scores = data.get('score', {}).get('projects')[0]
105             trusts = data.get('trust_indicator', {}).get('projects')[0]
106         except (TypeError, IndexError):
107             return []
108         else:
109             scores = {key: [dict(date=a.get('date')[:10],
110                                  score=a.get('score')
111                                  ) for a in scores[key]] for key in scores}
112             trusts = {key: [dict(date=a.get('date')[:10],
113                                  status=a.get('status')
114                                  ) for a in trusts[key]] for key in trusts}
115             atom = self._get_atom(scores, trusts)
116             return [dict(project=k,
117                          score=sorted(atom[k], reverse=True)[0].get('score'),
118                          status=sorted(atom[k], reverse=True)[0].get('status')
119                          ) for k in atom if atom[k]]
120
121     def _get_atom(self, scores, trusts):
122         s = {k: {a['date']: a['score'] for a in scores[k]} for k in scores}
123         t = {k: {a['date']: a['status'] for a in trusts[k]} for k in trusts}
124         return {k: [dict(score=s[k][a], status=t[k][a], data=a
125                          ) for a in s[k] if a in t[k]] for k in s}
126
127     def _change_to_utf8(self, obj):
128         if isinstance(obj, dict):
129             return {str(k): self._change_to_utf8(v) for k, v in obj.items()}
130         elif isinstance(obj, list):
131             return [self._change_to_utf8(ele) for ele in obj]
132         else:
133             try:
134                 new = eval(obj)
135                 if isinstance(new, int):
136                     return obj
137                 return self._change_to_utf8(new)
138             except (NameError, TypeError, SyntaxError):
139                 return str(obj)
140
141     def _get_args(self, body):
142         status = self._get_status_args(body)
143         projects = self._get_projects_args(body)
144         installers = self._get_installers_args(body)
145
146         args = {
147             'status': status,
148             'projects': projects,
149             'installers': installers,
150             'version': body.get('version', 'master').lower(),
151             'loops': body.get('loops', 'daily').lower(),
152             'time': body.get('times', '10 days')[:2].lower()
153         }
154         return args
155
156     def _get_status_args(self, body):
157         status_all = ['success', 'warning', 'danger']
158         status = [a.lower() for a in body.get('status', ['all'])]
159         return status_all if 'all' in status else status
160
161     def _get_projects_args(self, body):
162         project_all = ['functest', 'yardstick']
163         projects = [a.lower() for a in body.get('projects', ['all'])]
164         return project_all if 'all' in projects else projects
165
166     def _get_installers_args(self, body):
167         installer_all = ['apex', 'compass', 'fuel', 'joid']
168         installers = [a.lower() for a in body.get('installers', ['all'])]
169         return installer_all if 'all' in installers else installers