Add support for Python 3
[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.error_handler(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 = {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.error_handler('no such task')