def get_completed(): #completed all_tasks = len(Task.objects(version=settings.VERSION).all()) task = Task.objects(version=settings.VERSION, state='COMPLETED').all() error = Task.objects(version=settings.VERSION, state='ERROR').all() len_error = len(error) if not error: len_error = 0 error = {} else: error = {i.id_: i.task_id for i in error} if not task: return jsonify([{ 'completed': 0, 'errors': 0, 'len': 0, 'falta': all_tasks }, { 'task_ok': {}, 'task_error': {} }]) else: completed = {i.id_: i.task_id for i in task} tamanho = len(completed) return jsonify([{ 'completed': tamanho, 'errors': len_error, 'falta': (all_tasks - (tamanho + len_error)) }, { 'task_ok': completed, 'task_error': error }]), 200, { 'ContentType': 'application/json' }
def generate_task_list(): try: queue = [] for n, i in enumerate(settings.LIST_OF_TASKS): rest = { 'version': settings.VERSION, 'name': str(i), 'state': type_process('None'), 'task_id': 'None', 'num': n, } my_id = id_(rest['version'], rest['name']) if Task.objects(id_=my_id).first(): current_app.logger.warning( f'Task já existe {id_(rest["version"],rest["name"])}') else: task = Task(id_=my_id, **rest) task.save() rest['id_'] = my_id queue.append(rest) current_app.logger.warning(f'Add {rest}') return jsonify([{ 'state': 'sucesso', 'len_add_in_queue': len(queue) }, { 'add_in_queue': queue }]), 201 except Exception as e: return jsonify([{ 'state': 'error', 'error': str(e), 'messagem': 'Erro a gerar a lista' }])
def get_runnig(): task = Task.objects(version=settings.VERSION, state='RUNNING').all() if not task: return jsonify({}) else: runnig = {i.id_: i.task_id for i in task} return runnig, 200, {'ContentType': 'application/json'}
def check_tasks(): tasks = Task.objects(version=settings.VERSION, task_id__ne='None').filter( state__ne='COMPLETED').filter(state__ne='ERROR').all() queue = [] tasks_in_queue = {i.task_id: i for i in tasks} try: all_task = ee.batch.Task.list() except urllib3.exceptions.ProtocolError: current_app.logger.warning( 'Error login no GEE na hora de checkar as task') return jsonify([]) gee_task = { i.id: i.state for i in all_task if i.id in list(tasks_in_queue) } for id_ in tasks_in_queue: i = tasks_in_queue[id_] try: state = gee_task[id_] if state == 'FAILED': state = error_in_task(ee.batch.Task(id_, '', '').status()) except: __task = ee.batch.Task(id_, '', '').status() state = __task['state'] if state == 'FAILED': state = error_in_task(__task) current_app.logger.warning( f'state obitdo de forma bruta {id_} {state}') i.update(state=type_process(state)) queue.append(i.to_json()) return jsonify(queue)
def update_record(): record = request.json # print(record) task = Task.objects(id_=record['id_']).first() if not task: return jsonify({'error': 'data not found'}) else: state = record['state'] task_id = record['task_id'] task.update(state=state, task_id=task_id) current_app.logger.warning(f'Update na Task: {task_id}') return task.to_json()
def get_errors(): #errors task = Task.objects(version=settings.VERSION, state='ERROR').all() if not task: return jsonify([{'len': 0}, {'task': {}}]) else: errors = {i.id_: i.task_id for i in task} tamanho = len(errors) return jsonify([{ 'len': tamanho, 'falta': (len(settings.LIST_OF_TASKS) - tamanho) }, { 'task': errors }]), 200, { 'ContentType': 'application/json' }
def get_state(id): task = Task.objects(id_=id).first() if not task: return jsonify([]) else: return task.to_json(), 200, {'ContentType': 'application/json'}
def get_tasks(): task = Task.objects(state='IN_QUEUE').all() if not task: return jsonify([]) else: return task.to_json(), 200, {'ContentType': 'application/json'}
def add(): record = request.json # {'version': 'V001', 'name': '2019', 'state': 'None', 'task_id': 'None'} task = Task(id_=id_(record['version'], record['name']), **record) task.save() return jsonify(task.to_json())