def get_data_details(project_id: str): """ Route to get the details for all data for a given project matching the project_id. Raises an HTTPNotFoundError if the project is not found in the database. :param project_id: the project_id to get the data details for :return: a tuple containing (json response, http status code) """ args = {key: val for key, val in request.args.items()} _LOGGER.info( "getting all the data for project_id {} and request args {}".format( project_id, args)) args = SearchProjectDataSchema().load(args) # Validate project and model get_project_by_id(project_id) get_project_model_by_project_id(project_id) project_data = (ProjectData.select().where( ProjectData.project_id == project_id).group_by(ProjectData).order_by( ProjectData.created).paginate(args["page"], args["page_length"])) resp_data = data_dump_and_validation(ResponseProjectDataSchema(), {"data": project_data}) _LOGGER.info("sending project data {}".format(resp_data)) return jsonify(resp_data), HTTPStatus.OK.value
def _save_project_data(data: ProjectData, path: str, model_path: str): validate_model_data(path, model_path) try: data.setup_filesystem() data.file = "{}.npz".format(data.data_id) shutil.copy(path, data.file_path) # revalidate to make sure the copy worked validate_model_data(data.file_path, model_path) data.save() except Exception as err: if data: try: os.remove(data.file_path) except OSError: pass try: data.delete_instance() except Exception as rollback_err: _LOGGER.error( "error while rolling back new data: {}".format(rollback_err) ) _LOGGER.error( "error while creating new project data, rolling back: {}".format(err) ) raise err
def _get_project_data(self) -> ProjectData: data = ProjectData.get_or_none(ProjectData.data_id == self._data_id) if data is None: raise ValueError( "ProjectData with data_id {} was not found".format(self._data_id) ) return data
def get_project_data_by_ids(project_id: str, data_id: str) -> ProjectData: query = ProjectData.get_or_none(ProjectData.project_id == project_id, ProjectData.data_id == data_id) if query is None: _LOGGER.error( "could not find data with data_id {} for project_id {}".format( data_id, project_id)) raise HTTPNotFoundError( "could not find data with data_id {} for project_id {}".format( data_id, project_id)) return query
def _create_project_data(self) -> ProjectData: original = self._get_project_data() data = ProjectData.create( project=original.project, source=original.source, job=original.job ) return data
def load_data_from_repo(project_id: str): """ Route for loading data file(s) for a project from the Neural Magic model repo. Starts a background job in the JobWorker setup to run. The state of the job can be checked after. Raises an HTTPNotFoundError if the project is not found in the database. :param project_id: the id of the project to load the data for :return: a tuple containing (json response, http status code) """ _LOGGER.info( "loading data from repo for project {} for request json {}".format( project_id, request.json)) project = get_project_by_id(project_id) get_project_model_by_project_id(project_id) data = SetProjectDataFromSchema().load(request.get_json(force=True)) project_data = None job = None try: project_data = ProjectData.create(project=project, source="downloaded_path", job=None) job = Job.create( project_id=project.project_id, type_=DataFromRepoJobWorker.get_type(), worker_args=DataFromRepoJobWorker.format_args( data_id=project_data.data_id, uri=data["uri"]), ) project_data.job = job project_data.save() project_data.setup_filesystem() project_data.validate_filesystem() except Exception as err: if project_data: try: os.remove(project_data.file_path) except OSError: pass try: project_data.delete_instance() except Exception as rollback_err: _LOGGER.error("error while rolling back new data: {}".format( rollback_err)) if job: try: job.delete_instance() except Exception as rollback_err: _LOGGER.error("error while rolling back new data: {}".format( rollback_err)) _LOGGER.error( "error while creating new project data, rolling back: {}".format( err)) raise err # call into JobWorkerManager to kick off job if it's not already running JobWorkerManager().refresh() resp_data = data_dump_and_validation(ResponseProjectDataSingleSchema(), {"data": project_data}) _LOGGER.info("created project data from path {}".format(resp_data)) return jsonify(resp_data), HTTPStatus.OK.value
def upload_data(project_id: str): """ Route for uploading a data file to a project. Raises an HTTPNotFoundError if the project is not found in the database. :param project_id: the id of the project to upload the data for :return: a tuple containing (json response, http status code) """ _LOGGER.info("uploading data file for project {}".format(project_id)) project = get_project_by_id(project_id) project_model = get_project_model_by_project_id(project_id) if "data_file" not in request.files: _LOGGER.error("missing uploaded file 'data_file'") raise ValidationError("missing uploaded file 'data_file'") data_file = request.files["data_file"] with NamedTemporaryFile() as temp: data_path = gettempdir() tempname = os.path.join(data_path, temp.name) data_file.save(tempname) try: _LOGGER.info(project_model.file_path) validate_model_data(os.path.join(data_path, "*"), project_model.file_path) data = CreateUpdateProjectDataSchema().dump({ "source": "uploaded", "job": None }) project_data = ProjectData.create(project=project, **data) project_data.file = "{}.npz".format(project_data) project_data.setup_filesystem() shutil.copy(tempname, project_data.file_path) project_data.validate_filesystem() validate_model_data(project_data.file_path, project_model.file_path) project_data.save() except Exception as err: if project_data: try: os.remove(project_data.file_path) except OSError: pass try: project_data.delete_instance() except Exception as rollback_err: _LOGGER.error( "error while rolling back new data: {}".format( rollback_err)) _LOGGER.error( "error while creating new project data, rolling back: {}". format(err)) raise err resp_data = data_dump_and_validation(ResponseProjectDataSingleSchema(), {"data": project_data}) _LOGGER.info("created project data {}".format(resp_data)) return jsonify(resp_data), HTTPStatus.OK.value