def analyse_patch_request_errors(request_validator: UserRequest, data: Data, filename: str, method_parameters: dict) \ -> Union[tuple, None]: try: request_validator.existent_filename_validator(filename) except Exception as nonexistent_train_filename: return ( jsonify( {Constants.MESSAGE_RESULT: str(nonexistent_train_filename)}), Constants.HTTP_STATUS_CODE_NOT_FOUND, ) module_path, class_name = data.get_module_and_class_from_a_executor_name( filename) class_method = data.get_class_method_from_a_executor_name(filename) try: request_validator.valid_method_parameters_validator( module_path, class_name, class_method, method_parameters) except Exception as invalid_function_parameters: return ( jsonify( {Constants.MESSAGE_RESULT: str(invalid_function_parameters)}), Constants.HTTP_STATUS_CODE_NOT_ACCEPTABLE, ) return None
def analyse_post_request_errors(request_validator: UserRequest, filename: str) \ -> Union[tuple, None]: try: request_validator.not_duplicated_filename_validator(filename) except Exception as duplicated_filename: return ( jsonify({Constants.MESSAGE_RESULT: str(duplicated_filename)}), Constants.HTTP_STATUS_CODE_CONFLICT, ) return None
def analyse_patch_request_errors(request_validator: UserRequest, filename: str) \ -> Union[tuple, None]: try: request_validator.existent_filename_validator(filename) except Exception as nonexistent_filename: return ( jsonify({Constants.MESSAGE_RESULT: str(nonexistent_filename)}), Constants.HTTP_STATUS_CODE_NOT_FOUND, ) return None
def get_image(filename): try: UserRequest.tsne_filename_nonexistence_validator(filename) except Exception as invalid_tsne_filename: return ( jsonify( {MESSAGE_RESULT: invalid_tsne_filename.args[FIRST_ARGUMENT]}), HTTP_STATUS_CODE_NOT_FOUND, ) image_path = os.environ[IMAGES_PATH] + "/" + filename + IMAGE_FORMAT return send_file(image_path, mimetype="image/png")
def delete_image(filename): try: UserRequest.tsne_filename_nonexistence_validator(filename) except Exception as invalid_tsne_filename: return ( jsonify( {MESSAGE_RESULT: invalid_tsne_filename.args[FIRST_ARGUMENT]}), HTTP_STATUS_CODE_NOT_FOUND, ) image_path = os.environ[IMAGES_PATH] + "/" + filename + IMAGE_FORMAT thread_pool.submit(os.remove, image_path) return jsonify({MESSAGE_RESULT: MESSAGE_DELETED_FILE}), HTTP_STATUS_CODE_SUCCESS
def create_tsne(): database = Database(os.environ[DATABASE_URL], os.environ[DATABASE_REPLICA_SET], os.environ[DATABASE_PORT], os.environ[DATABASE_NAME]) request_validator = UserRequest(database) request_errors = analyse_request_errors(request_validator, request.json[PARENT_FILENAME_NAME], request.json[TSNE_FILENAME_NAME], request.json[LABEL_NAME]) if request_errors is not None: return request_errors database_url_input = Database.collection_database_url( os.environ[DATABASE_URL], os.environ[DATABASE_NAME], request.json[PARENT_FILENAME_NAME], os.environ[DATABASE_REPLICA_SET], ) thread_pool.submit(tsne_async_processing, database_url_input, request.json[LABEL_NAME], request.json[TSNE_FILENAME_NAME]) return ( jsonify({ MESSAGE_RESULT: MICROSERVICE_URI_GET + request.json[TSNE_FILENAME_NAME] }), HTTP_STATUS_CODE_SUCCESS_CREATED, )
def create_model(): database_url = os.environ[DATABASE_URL] database_replica_set = os.environ[DATABASE_REPLICA_SET] database_name = os.environ[DATABASE_NAME] train_filename = request.json[TRAINING_FILENAME] test_filename = request.json[TEST_FILENAME] classifiers_name = request.json[CLASSIFIERS_NAME] database = Database( database_url, database_replica_set, os.environ[DATABASE_PORT], database_name, ) request_validator = UserRequest(database) request_errors = analyse_request_errors( request_validator, train_filename, test_filename, classifiers_name) if request_errors is not None: return request_errors database_url_training = Database.collection_database_url( database_url, database_name, train_filename, database_replica_set, ) database_url_test = Database.collection_database_url( database_url, database_name, test_filename, database_replica_set, ) metadata_creator = Metadata(database, train_filename, test_filename) model_builder = Model(database, metadata_creator, database_url_training, database_url_test) model_builder.build( request.json[MODELING_CODE_NAME], classifiers_name ) return ( jsonify({ MESSAGE_RESULT: create_prediction_files_uri( classifiers_name, test_filename)}), HTTP_STATUS_CODE_SUCCESS_CREATED, )
def analyse_request_errors(request_validator: UserRequest, filename: str, url: str): try: request_validator.filename_validator(filename) except Exception as invalid_filename: return ( jsonify({Constants.MESSAGE_RESULT: str(invalid_filename)}), Constants.HTTP_STATUS_CODE_CONFLICT, ) try: request_validator.url_validator(url) except Exception as invalid_url: return ( jsonify({Constants.MESSAGE_RESULT: str(invalid_url)}), Constants.HTTP_STATUS_CODE_NOT_ACCEPTABLE, ) return None
def analyse_post_request_errors(request_validator: UserRequest, model_name: str, module_path: str, class_name: str, class_parameters: dict) \ -> Union[tuple, None]: try: request_validator.not_duplicated_filename_validator( model_name ) except Exception as duplicated_model_filename: return ( jsonify({Constants.MESSAGE_RESULT: str(duplicated_model_filename)}), Constants.HTTP_STATUS_CODE_CONFLICT, ) try: request_validator.available_module_path_validator( module_path ) except Exception as invalid_tool_name: return ( jsonify({Constants.MESSAGE_RESULT: str(invalid_tool_name)}), Constants.HTTP_STATUS_CODE_NOT_ACCEPTABLE, ) try: request_validator.valid_class_validator( module_path, class_name ) except Exception as invalid_function_name: return ( jsonify({Constants.MESSAGE_RESULT: str(invalid_function_name)}), Constants.HTTP_STATUS_CODE_NOT_ACCEPTABLE, ) try: request_validator.valid_class_parameters_validator( module_path, class_name, class_parameters ) except Exception as invalid_function_parameters: return ( jsonify( {Constants.MESSAGE_RESULT: str(invalid_function_parameters)}), Constants.HTTP_STATUS_CODE_NOT_ACCEPTABLE, ) return None
def create_projection(): database_url = os.environ[DATABASE_URL] database_replica_set = os.environ[DATABASE_REPLICA_SET] database_name = os.environ[DATABASE_NAME] parent_filename = request.json[PARENT_FILENAME_NAME] projection_filename = request.json[PROJECTION_FILENAME_NAME] projection_fields = request.json[FIELDS_NAME] database = Database( database_url, database_replica_set, os.environ[DATABASE_PORT], database_name, ) request_validator = UserRequest(database) request_errors = analyse_request_errors(request_validator, parent_filename, projection_filename, projection_fields) if request_errors is not None: return request_errors database_url_input = Database.collection_database_url( database_url, database_name, parent_filename, database_replica_set, ) database_url_output = Database.collection_database_url( database_url, database_name, projection_filename, database_replica_set, ) metadata_creator = Metadata(database) projection = Projection(metadata_creator, database_url_input, database_url_output) projection.create(parent_filename, projection_filename, projection_fields) return ( jsonify({ MESSAGE_RESULT: MICROSERVICE_URI_GET + projection_filename + MICROSERVICE_URI_GET_PARAMS }), HTTP_STATUS_CODE_SUCCESS_CREATED, )
def analyse_post_request_errors(request_validator: UserRequest, data: Data, filename: str, parent_name: str, class_method: str, method_parameters: dict) \ -> Union[tuple, None]: try: request_validator.not_duplicated_filename_validator(filename) except Exception as duplicated_train_filename: return ( jsonify({Constants.MESSAGE_RESULT: str(duplicated_train_filename)}), Constants.HTTP_STATUS_CODE_CONFLICT, ) try: request_validator.existent_filename_validator(parent_name) except Exception as invalid_model_name: return ( jsonify({Constants.MESSAGE_RESULT: str(invalid_model_name)}), Constants.HTTP_STATUS_CODE_NOT_ACCEPTABLE, ) module_path, class_name = data.get_module_and_class_from_a_model( parent_name) try: request_validator.valid_method_class_validator(module_path, class_name, class_method) except Exception as invalid_method_name: return ( jsonify({Constants.MESSAGE_RESULT: str(invalid_method_name)}), Constants.HTTP_STATUS_CODE_NOT_ACCEPTABLE, ) try: request_validator.valid_method_parameters_validator( module_path, class_name, class_method, method_parameters) except Exception as invalid_method_parameters: return ( jsonify({Constants.MESSAGE_RESULT: str(invalid_method_parameters)}), Constants.HTTP_STATUS_CODE_NOT_ACCEPTABLE, ) return None
def create_histogram(): parent_filename = request.json[PARENT_FILENAME_NAME] histogram_filename = request.json[HISTOGRAM_FILENAME_NAME] fields_name = request.json[FIELDS_NAME] database = Database( os.environ[DATABASE_URL], os.environ[DATABASE_REPLICA_SET], os.environ[DATABASE_PORT], os.environ[DATABASE_NAME], ) request_validator = UserRequest(database) request_errors = analyse_request_errors( request_validator, parent_filename, histogram_filename, fields_name) if request_errors is not None: return request_errors metadata = Metadata(database) histogram = Histogram(database, metadata) histogram.create_file( parent_filename, histogram_filename, fields_name, ) return ( jsonify({ MESSAGE_RESULT: MICROSERVICE_URI_GET + histogram_filename + MICROSERVICE_URI_GET_PARAMS}), HTTP_STATUS_CODE_SUCCESS_CREATED, )
from flask import jsonify, request, Flask import os from binary_execution import Execution, Parameters from utils import UserRequest, Database, ObjectStorage, Data, Metadata from typing import Union from constants import Constants app = Flask(__name__) database = Database( os.environ[Constants.DATABASE_URL], os.environ[Constants.DATABASE_REPLICA_SET], int(os.environ[Constants.DATABASE_PORT]), os.environ[Constants.DATABASE_NAME], ) request_validator = UserRequest(database) storage = ObjectStorage(database) data = Data(database, storage) metadata_creator = Metadata(database) parameters_handler = Parameters(database, data) @app.route(Constants.MICROSERVICE_URI_PATH, methods=["POST"]) def create_execution() -> jsonify: service_type = request.args.get(Constants.TYPE_FIELD_NAME) model_name = request.json[Constants.MODEL_NAME_FIELD_NAME] parent_name = request.json[Constants.PARENT_NAME_FIELD_NAME] filename = request.json[Constants.NAME_FIELD_NAME] description = request.json[Constants.DESCRIPTION_FIELD_NAME] class_method = request.json[Constants.METHOD_FIELD_NAME]
from flask import jsonify, request, Flask import os from database import Dataset, Csv, Generic from utils import Database, UserRequest, Metadata from constants import Constants import json app = Flask(__name__) database_connector = Database(os.environ[Constants.DATABASE_URL], os.environ[Constants.DATABASE_REPLICA_SET], int(os.environ[Constants.DATABASE_PORT]), os.environ[Constants.DATABASE_NAME]) request_validator = UserRequest(database_connector) metadata_creator = Metadata(database_connector) @app.route(Constants.MICROSERVICE_URI_PATH, methods=["POST"]) def create_file(): service_type = request.args.get(Constants.TYPE_FIELD_NAME) url = request.json[Constants.URL_FIELD_NAME] filename = request.json[Constants.FILENAME_FIELD_NAME] request_errors = analyse_request_errors(request_validator, filename, url) if request_errors is not None: return request_errors if service_type == Constants.DATASET_CSV_TYPE: file_downloader = Csv(database_connector, metadata_creator) else: