Yardstick: Aarch64 jenkins slave support
[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 import json
13
14 from api.utils.common import result_handler
15 from api.database.v1.handlers import TasksHandler
16 from yardstick.common import constants as consts
17
18 logger = logging.getLogger(__name__)
19 logger.setLevel(logging.DEBUG)
20
21
22 def default(args):
23     return getResult(args)
24
25
26 def getResult(args):
27     try:
28         task_id = args['task_id']
29     except KeyError:
30         return result_handler(consts.API_ERROR, 'task_id must be provided')
31
32     try:
33         uuid.UUID(task_id)
34     except ValueError:
35         return result_handler(consts.API_ERROR, 'invalid task_id')
36
37     task_handler = TasksHandler()
38     try:
39         task = task_handler.get_task_by_taskid(task_id)
40     except ValueError:
41         return result_handler(consts.API_ERROR, 'invalid task_id')
42
43     def _unfinished():
44         return result_handler(consts.TASK_NOT_DONE, {})
45
46     def _finished():
47         if task.result:
48             return result_handler(consts.TASK_DONE, json.loads(task.result))
49         else:
50             return result_handler(consts.TASK_DONE, {})
51
52     def _error():
53         return result_handler(consts.TASK_FAILED, task.error)
54
55     status = task.status
56     logger.debug('Task status is: %s', status)
57
58     if status not in [consts.TASK_NOT_DONE,
59                       consts.TASK_DONE,
60                       consts.TASK_FAILED]:
61         return result_handler(consts.API_ERROR, 'internal server error')
62
63     switcher = {
64         consts.TASK_NOT_DONE: _unfinished,
65         consts.TASK_DONE: _finished,
66         consts.TASK_FAILED: _error
67     }
68
69     return switcher.get(status)()