def process_operation(uuid, operation): logger.info( f"Processing operation '{operation}' on data associated with UUID '{uuid}'..." ) try: stored_data = data_store.get(uuid) operation_func = get_operation(operation) result = operation_func(stored_data) except KeyError: logger.warning(f"Cannot retrieving data associated with UUID '{uuid}'") return jsonify({ "status": "failed", "message": "Cannot retrieve data", "result": None }) except NoSuchOperationError: logger.warning(f"Cannot find operation '{operation}'") return jsonify({ "status": "failed", "message": f"No such '{operation}'", "result": None }) logger.warning( f"Operation '{operation}' on data associated with UUID '{uuid}' finished successfully!" ) return jsonify({ "status": "success", "message": "Result completed", "result": result })
def retrieve_data(uuid): logger.info(f"Retrieving data associated with UUID '{uuid}'") try: data = data_store.get(uuid) except KeyError: logger.warning(f"cannot retrieve data associated with UUID '{uuid}'") return jsonify({"status": "failed", "message": "data cannot be retrieved", "data": []}) logger.info(f"Data associated with UUID '{uuid}' retrieved sucessfully") return jsonify({"status": "success", "message": "data retrieved sucessfully", "data": data})
def retrieve_data(): logger.info(f"Retrieving data associated with uuid '{uuid}'...") try: stored_data = data_store.get(uuid) except KeyError: logger.warning(f"Cannot retrieve data associated with uuid '{uuid}'...") return jsonify({"status": "failed", "message": "data cannot be retrieved", "data": []}) # ao invés de retornar "none", peço para retornar uma lista vazia => "[]" # tanto o "none" quanto a lista vazia vai significar erro # não é possível iterar com "none", mas é possível fazer com lista vazia logger.info(f"Data associated with uuid '{uuid}' retrieved successfully") return jsonify({"status": "failed", "message": "data retrieved successfully", "data": stored_data})
def atempt_get_data(uuid): raw_stored_data = [] try: raw_stored_data = data_store.get(uuid) except KeyError: logger.warning(f"Cannot retrieve data associated with UUID '{uuid}'.") return jsonify({ "status": "failed", "message": "data cannot be retrieved.", "data": [] }) return raw_stored_data
def operation_data(uuid, operation): list_operations = ['max', 'min', 'mean'] if operation not in list_operations: return jsonify({"status": "failed", "message": "invalid operation", "data": []}) logger.info(f"Retrieving data associated with UUID '{uuid}'") try: data = data_store.get(uuid) except KeyError: logger.warning(f"cannot retrieve data associated with UUID '{uuid}'") return jsonify({"status": "failed", "message": "data cannot be retrieved", "data": []}) if operation == 'max': return jsonify({"status": "success", "message": "operation successfuly", "max": max(data)}) elif operation == 'min': return jsonify({"status": "success", "message": "operation successfuly", "max": min(data)}) elif operation == 'mean': return jsonify({"status": "success", "message": "operation successfuly", "mean": mean(data)})
def retrieve_data(uuid): logger.info(f'Retrieving data associated with UUID `{uuid}` ...') try: retrivied_data = data_store.get(uuid) except KeyError: logger.warning(f'Cannot retieve data associated with `{uuid}` ') return jsonify({ 'status': 'failed', 'message': 'data cannot be retieved', 'data': [] }) logger.info(f'Data associated with UUID `{uuid}` retrieved successfuly') return jsonify({ 'status': 'success', 'message': 'data retieved successfuly', 'data': retrivied_data })
def process_operation(uuid, operation): logger.info( f'Processing operation `{operation}` on data associated with UUID `{uuid}`...' ) try: stored_data = data_store.get(uuid) operation_func = get_operation(operation) result = operation_func(stored_data) except KeyError: logger.warning(f'Cannot retrieve data associated with UUID `{uuid}`') return jsonify({ 'status': 'failed', 'message': 'data cannot be retieved', 'result': None }) except NoSuchOperationError: logger.warning(f'Cannot find operation `{operation}`') return jsonify({ 'status': 'failed', 'message': f'no such `{operation}`', 'result': None }) logger.info( f'Operation `{operation}` on data associatd wwith UUID `{uuid}` fineshed successfuly' ) return jsonify({ 'status': 'success', 'message': 'result completed', 'result': result })
except KeyError: logger.warning(f"Cannot retrieve data associated with uuid '{uuid}'...") return jsonify({"status": "failed", "message": "data cannot be retrieved", "data": []}) # ao invés de retornar "none", peço para retornar uma lista vazia => "[]" # tanto o "none" quanto a lista vazia vai significar erro # não é possível iterar com "none", mas é possível fazer com lista vazia logger.info(f"Data associated with uuid '{uuid}' retrieved successfully") return jsonify({"status": "failed", "message": "data retrieved successfully", "data": stored_data}) # criação de endpoint para realizar operações em cima dos dados que estão salvos @app.route("/data/<uuid>/<operation>", method = ["GET"]) def process.operation(uuid, operation): logger.info(f"Processing operation '{operation}' on data associated with UUID '{uuid}'...") try: stored_data = data_store.get(uuid) operation_func = get_operation(operation) result = operation_func(stored_data) except KeyError: logger.warning(f"Cannot retrieve data associated with uuid '{uuid}'...") return jsonify({"status": "failed", "message": "data cannot be retrieved", "result": None}) except NoSuchOperationError: logger.warning(f"Cannot find operation '{operation}'") return jsonify({"status": "failed", "message": f"no such '{operation}'", "result": None}) logger.info(f"Operation '{operation}' on data associated with UUID '{uuid}' finished successfully") return jsonify({"status": "success", "message": "result completed", "result": result}) class NoSuchOperationError(Exception): pass def get_operation(operation_name):