3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
11 Resources to retrieve the task results
20 from flask import jsonify
22 from functest.api.base import ApiResource
23 from functest.api.common import api_utils
24 from functest.api.database.v1.handlers import TasksHandler
25 from functest.utils.constants import CONST
28 LOGGER = logging.getLogger(__name__)
31 class V1Task(ApiResource):
32 """ V1Task Resource class"""
34 def get(self, task_id): # pylint: disable=no-self-use
35 """ GET the result of the task id """
39 return api_utils.result_handler(status=1, data='Invalid task id')
41 task_handler = TasksHandler()
43 task = task_handler.get_task_by_taskid(task_id)
45 return api_utils.result_handler(status=1, data='No such task id')
48 LOGGER.debug('Task status is: %s', status)
50 if status not in ['IN PROGRESS', 'FAIL', 'FINISHED']:
51 return api_utils.result_handler(status=1,
52 data='internal server error')
53 if status == 'IN PROGRESS':
54 result = {'status': status, 'result': ''}
55 elif status == 'FAIL':
56 result = {'status': status, 'error': task.error}
58 result = {'status': status, 'result': json.loads(task.result)}
60 return jsonify(result)
63 class V1TaskLog(ApiResource):
64 """ V1TaskLog Resource class"""
66 def get(self, task_id): # pylint: disable=no-self-use
67 """ GET the log of the task id """
71 return api_utils.result_handler(status=1, data='Invalid task id')
73 task_handler = TasksHandler()
75 task = task_handler.get_task_by_taskid(task_id)
77 return api_utils.result_handler(status=1, data='No such task id')
79 task_log_dir = CONST.__getattribute__('dir_results')
82 with open(os.path.join(task_log_dir,
83 '{}.log'.format(task_id)), 'r') as log_file:
84 data = log_file.readlines()
85 except OSError as err:
86 if err.errno == errno.ENOENT:
87 return api_utils.result_handler(
88 status=1, data='Log file does not exist')
90 return api_utils.result_handler(
91 status=1, data='Error with log file')
93 return_data = {'data': data}
95 return api_utils.result_handler(status=task.status, data=return_data)