Record test case names when run a task using API
[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 import logging
10 import uuid
11
12 from api.utils import influx as influx_utils
13 from api.utils import common as common_utils
14 from api.database.handlers import TasksHandler
15
16 logger = logging.getLogger(__name__)
17
18
19 def default(args):
20     return getResult(args)
21
22
23 def getResult(args):
24     try:
25         task_id = args['task_id']
26
27         uuid.UUID(task_id)
28     except KeyError:
29         message = 'task_id must be provided'
30         return common_utils.error_handler(message)
31
32     task = TasksHandler().get_task_by_taskid(task_id)
33
34     def _unfinished():
35         return common_utils.result_handler(0, [])
36
37     def _finished():
38         testcases = task.details.split(',')
39
40         def get_data(testcase):
41             query_template = "select * from %s where task_id='%s'"
42             query_sql = query_template % (testcase, task_id)
43             data = common_utils.translate_to_str(influx_utils.query(query_sql))
44             return data
45
46         result = {k: get_data(k) for k in testcases}
47
48         return common_utils.result_handler(1, result)
49
50     def _error():
51         return common_utils.result_handler(2, task.error)
52
53     try:
54         status = task.status
55
56         switcher = {
57             0: _unfinished,
58             1: _finished,
59             2: _error
60         }
61         return switcher.get(status, lambda: 'nothing')()
62     except IndexError:
63         return common_utils.error_handler('no such task')