Merge "Yardstick Preliminary Documentation"
[yardstick.git] / api / resources / asynctask.py
1 # ############################################################################
2 # Copyright (c) 2017 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 uuid
10
11 from api.utils import common as common_utils
12 from api.database.models import AsyncTasks
13
14
15 def default(args):
16     return _get_status(args)
17
18
19 def _get_status(args):
20     try:
21         task_id = args['task_id']
22         uuid.UUID(task_id)
23     except KeyError:
24         message = 'measurement and task_id must be provided'
25         return common_utils.error_handler(message)
26
27     asynctask = AsyncTasks.query.filter_by(task_id=task_id).first()
28
29     try:
30         status = asynctask.status
31         error = asynctask.error if asynctask.error else []
32
33         return common_utils.result_handler(status, error)
34     except AttributeError:
35         return common_utils.error_handler('no such task')