Bugfix: remove test.dbf file in recovery script
[yardstick.git] / api / resources / results.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 from __future__ import absolute_import
10 import logging
11 import uuid
12
13 from api.utils import influx as influx_utils
14 from api.utils import common as common_utils
15 from api.database.handlers import TasksHandler
16
17 logger = logging.getLogger(__name__)
18
19
20 def default(args):
21     return getResult(args)
22
23
24 def getResult(args):
25     try:
26         task_id = args['task_id']
27
28         uuid.UUID(task_id)
29     except KeyError:
30         message = 'task_id must be provided'
31         return common_utils.result_handler(2, message)
32
33     task = TasksHandler().get_task_by_taskid(task_id)
34
35     def _unfinished():
36         return common_utils.result_handler(0, {})
37
38     def _finished():
39         testcases = task.details.split(',')
40
41         def get_data(testcase):
42             query_template = "select * from %s where task_id='%s'"
43             query_sql = query_template % (testcase, task_id)
44             data = common_utils.translate_to_str(influx_utils.query(query_sql))
45             return data
46
47         result = _format_data({k: get_data(k) for k in testcases})
48
49         return common_utils.result_handler(1, result)
50
51     def _error():
52         return common_utils.result_handler(2, task.error)
53
54     try:
55         status = task.status
56
57         switcher = {
58             0: _unfinished,
59             1: _finished,
60             2: _error
61         }
62         return switcher.get(status, lambda: 'nothing')()
63     except IndexError:
64         return common_utils.result_handler(2, 'no such task')
65
66
67 def _format_data(data):
68     try:
69         first_value = data.values()[0][0]
70     except IndexError:
71         return {'criteria': 'FAIL', 'testcases': {}}
72     else:
73         info = {
74             'deploy_scenario': first_value.get('deploy_scenario'),
75             'installer': first_value.get('installer'),
76             'pod_name': first_value.get('pod_name'),
77             'version': first_value.get('version')
78         }
79         task_id = first_value.get('task_id')
80         criteria = first_value.get('criteria')
81         testcases = {k: _get_case_data(v) for k, v in data.items()}
82
83         result = {
84             'criteria': criteria,
85             'info': info,
86             'task_id': task_id,
87             'testcases': testcases
88         }
89         return result
90
91
92 def _get_case_data(data):
93     try:
94         scenario = data[0]
95     except IndexError:
96         return {'tc_data': [], 'criteria': 'FAIL'}
97     else:
98         tc_data = [_get_scenario_data(s) for s in data]
99         criteria = scenario.get('criteria')
100         return {'tc_data': tc_data, 'criteria': criteria}
101
102
103 def _get_scenario_data(data):
104     result = {
105         'data': {},
106         'timestamp': ''
107     }
108
109     blacklist = {'criteria', 'deploy_scenario', 'host', 'installer',
110                  'pod_name', 'runner_id', 'scenarios', 'target',
111                  'task_id', 'time', 'version'}
112
113     keys = set(data.keys()) - set(blacklist)
114     for k in keys:
115         result['data'][k] = data[k]
116
117     result['timestamp'] = data.get('time')
118
119     return result