def update(self, request, pk=None): """ PUT request for updating a project. :param request: PUT request :return: The updated/200 """ dataset_inputs = load_request(request) serializer = self.serializer_class(data=dataset_inputs, context={'request': request}) if serializer.is_valid() and pk is not None: try: project = Project.objects.get(id=int(pk)) except Project.DoesNotExist: return Response("No project found for id: {}".format(pk), status=status.HTTP_400_BAD_REQUEST) if IsOwner().has_object_permission(request, self, project): project = serializer.update(project, serializer.validated_data) if "metadata" not in dataset_inputs.keys(): dataset_inputs["metadata"] = None m = Metadata(project, dataset_inputs["metadata"]) meta = m.set_metadata("ProjectMetadata") response_data = serializer.data if meta: response_data["metadata"] = meta request_status = status.HTTP_200_OK return Response(response_data, status=request_status) else: return Response(status=status.HTTP_401_UNAUTHORIZED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create(self, request): """ POST request that creates a new Dataset. :param request: POST request :return: New dataset """ dataset_inputs = request.data.dict() serializer = self.serializer_class(data=dataset_inputs, context={'request': request}) if serializer.is_valid(): serializer.save() dataset = serializer.data if dataset: d = Dataset.objects.get(id=dataset["id"]) if "metadata" not in dataset_inputs.keys(): dataset_inputs["metadata"] = None m = Metadata(d, dataset_inputs["metadata"]) meta = m.set_metadata("DatasetMetadata") response = "Response" if meta: dataset["metadata"] = meta response = meta["response"] data = pd.read_csv(StringIO(bytes(d.data).decode())) if response not in data: response = data.columns.tolist()[0] dataset["statistics"] = DatasetStatistics( data).calculate_statistics(response) del dataset["data"] return Response(dataset, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def update(self, request, pk=None): dataset_inputs = request.data.dict() serializer = self.serializer_class(data=dataset_inputs, context={'request': request}) if serializer.is_valid() and pk is not None: try: original_dataset = Dataset.objects.get(id=int(pk)) except Dataset.DoesNotExist: return Response("No dataset model found for id: {}".format(pk), status=status.HTTP_400_BAD_REQUEST) if IsOwnerOfWorkflowChild().has_object_permission( request, self, original_dataset): amodel = serializer.update(original_dataset, serializer.validated_data) m = Metadata(amodel, dataset_inputs["metadata"]) meta = m.set_metadata("DatasetMetadata") if amodel: response_status = status.HTTP_201_CREATED response_data = serializer.data response_data["id"] = amodel.id del response_data["data"] if meta: response_data["metadata"] = meta if int(pk) == amodel.id: response_status = status.HTTP_200_OK return Response(response_data, status=response_status) else: return Response(status=status.HTTP_401_UNAUTHORIZED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def update(self, request, pk=None): """ PUT request to update a dataset :param request: PUT request :param pk: dataset ID to be updated :return: 200/details of updated dataset, 400/bad request, or 401/unauthorized """ dataset_inputs = load_request(request) serializer = self.serializer_class(data=dataset_inputs, context={'request': request}) if serializer.is_valid() and pk is not None: try: original_dataset = Dataset.objects.get(id=int(pk)) except Dataset.DoesNotExist: return Response( "No dataset found for id: {}".format(pk), status=status.HTTP_400_BAD_REQUEST ) if IsOwner().has_object_permission(request, self, original_dataset): amodel = serializer.update(original_dataset, serializer.validated_data) m = Metadata(amodel, dataset_inputs["metadata"]) meta = m.set_metadata("DatasetMetadata") if amodel: response_status = status.HTTP_201_CREATED response_data = serializer.data response_data["id"] = amodel.id del response_data["data"] if meta: response_data["metadata"] = meta if int(pk) == amodel.id: response_status = status.HTTP_200_OK return Response(response_data, status=response_status) else: return Response(status=status.HTTP_401_UNAUTHORIZED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create(self, request): """ POST request that creates a new Pipeline. :param request: POST request :return: New pipeline object """ pipeline_inputs = load_request(request) serializer = self.serializer_class(data=pipeline_inputs, context={'request': request}) try: project = Project.objects.get(id=int(pipeline_inputs["project"])) except Project.DoesNotExist: return Response("No project found for id: {}".format( int(pipeline_inputs["project"])), status=status.HTTP_400_BAD_REQUEST) if project.owner != request.user: return Response(status=status.HTTP_401_UNAUTHORIZED) if serializer.is_valid(): serializer.save() pipeline = serializer.data if "metadata" not in pipeline_inputs.keys(): pipeline_inputs["metadata"] = None a = Pipeline.objects.get(pk=int(pipeline["id"])) m = Metadata(a, pipeline_inputs["metadata"]) meta = m.set_metadata("PipelineMetadata") pipeline["metadata"] = meta if pipeline: return Response(pipeline, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def update(self, request, pk=None): """ PUT request to update an existing location. :param request: PUT request :param pk: Location ID :return: """ dataset_inputs = load_request(request) serializer = self.serializer_class(data=dataset_inputs, context={'request': request}) if serializer.is_valid() and pk is not None: try: original_location = Location.objects.get(id=int(pk)) except Location.DoesNotExist: return Response("No location found for id: {}".format(pk), status=status.HTTP_400_BAD_REQUEST) if original_location.owner == request.user: location = serializer.update(original_location, serializer.validated_data) if location: l = serializer.data m = Metadata(location, dataset_inputs["metadata"]) meta = m.set_metadata("LocationMetadata") if meta: l["metadata"] = meta request_status = status.HTTP_201_CREATED if int(pk) == location.id: request_status = status.HTTP_200_OK return Response(l, status=request_status) else: return Response(status=status.HTTP_401_UNAUTHORIZED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def update(self, request, pk=None): amodel_inputs = request.data.dict() serializer = self.serializer_class(data=request.data.dict(), context={'request': request}) if serializer.is_valid() and pk is not None: try: original_amodel = AnalyticalModel.objects.get(id=int(pk)) except AnalyticalModel.DoesNotExist: return Response( "No analytical model found for id: {}".format(pk), status=status.HTTP_400_BAD_REQUEST) if IsOwnerOfWorkflowChild().has_object_permission( request, self, original_amodel): amodel = serializer.update(original_amodel, serializer.validated_data) if amodel: response_status = status.HTTP_201_CREATED response_data = serializer.data response_data["id"] = amodel.id if int(pk) == amodel.id: response_status = status.HTTP_200_OK if "metadata" in amodel_inputs.keys(): amodel_inputs["metadata"] = None m = Metadata(amodel_inputs, amodel_inputs["metadata"]) meta = m.set_metadata("ModelMetadata") response_data["metadata"] = m.get_metadata( "ModelMetadata") return Response(response_data, status=response_status) else: return Response(status=status.HTTP_401_UNAUTHORIZED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def update_status(_id, status, stage, message=None, retry=5, log=None): if _id == -1: return if retry == 0: return meta = 'PipelineMetadata' try: amodel = Pipeline.objects.get(id=int(_id)) m = Metadata(parent=amodel, metadata=json.dumps({"status": status, "stage": stage, "message": message})) m.set_metadata(meta) if log: logger.info(log) PipelineLog(parent=amodel, logtype=status, log=f"Stage: {stage}, Message: {message}", timestamp=str(datetime.datetime.now().timestamp())) except Exception as ex: logger.warning("Error attempting to save status update: {}".format(ex)) update_status(_id, status, stage, None, retry - 1)
def update_status(_id, status, stage, message=None, retry=5): if retry == 0: pass meta = 'ModelMetadata' try: amodel = AnalyticalModel.objects.get(id=int(_id)) m = Metadata(parent=amodel, metadata=json.dumps({ "status": status, "stage": stage, "message": message })) m.set_metadata(meta) except Exception as ex: logger.warning( "Error attempting to save metadata update: {}".format(ex)) DaskTasks.update_status(_id, status, stage, None, retry - 1)
def refit_model(self, request): input_data = load_request(request) required_parameters = ["project_id", "model_id", "predictive_models"] if set(required_parameters).issubset(input_data.keys()): permissions = [] try: project = Project.objects.get(id=int(input_data["project_id"])) if not IsOwnerOfProject().has_object_permission( request, self, project): permissions.append("Unauthorized to access project.") except Project.DoesNotExist: project = None try: model = Model.objects.get(id=int(input_data["model_id"])) if not IsOwnerOfModel().has_object_permission( request, self, model): permissions.append("Unauthorized to access pipeline") except Model.DoesNotExist: model = None if len(permissions) > 0: return Response(permissions, status=status.HTTP_401_UNAUTHORIZED) if model is None or project is None: message = [] if project is None: message.append("No project found for id: {}".format( input_data["project_id"])) if model is None: message.append("No model found for id: {}".format( input_data["model_id"])) return Response(", ".join(message), status=status.HTTP_400_BAD_REQUEST) p_models = {} for p in json.loads(input_data["predictive_models"]): p_models[p[0]] = int(p[1]) m = Metadata(model, json.dumps({"predictive_models": p_models})) meta = m.set_metadata("ModelMetadata") response = {} DaskTasks.refit_task(project.id, model.id, p_models) response["project_id"] = project.id response["pipeline_id"] = model.pipeline.id response["model_id"] = model.id response["dataset_id"] = project.dataset response["model_metadata"] = meta response[ "status"] = "Initiated refit for specified models for prediction" return Response(response, status=status.HTTP_200_OK) data = "Missing required parameters: {}".format( ", ".join(required_parameters)) response_status = status.HTTP_200_OK return Response(data, status=response_status)
def update(self, request, pk=None): """ :param request: :param pk: :return: """ pipeline_inputs = load_request(request) serializer = self.serializer_class(data=pipeline_inputs, context={'request': request}) try: project = Project.objects.get(id=int(pipeline_inputs["project"])) except Project.DoesNotExist: return Response("No project found for id: {}".format( int(pipeline_inputs["project"])), status=status.HTTP_400_BAD_REQUEST) if project.owner != request.user: return Response(status=status.HTTP_401_UNAUTHORIZED) if serializer.is_valid() and pk is not None: try: original_pipeline = Pipeline.objects.get(id=int(pk)) except Pipeline.DoesNotExist: return Response("No pipeline found for id: {}".format(pk), status=status.HTTP_400_BAD_REQUEST) if IsOwnerOfPipeline().has_object_permission( request, self, original_pipeline): experiment = serializer.update(original_pipeline, serializer.validated_data) if experiment: response_status = status.HTTP_201_CREATED response_data = serializer.data response_data["id"] = experiment.id if int(pk) == experiment.id: response_status = status.HTTP_200_OK if "metadata" not in pipeline_inputs.keys(): pipeline_inputs["metadata"] = None a = Pipeline.objects.get(pk=experiment.id) m = Metadata(a, pipeline_inputs["metadata"]) response_data["metadata"] = m.set_metadata( "PipelineMetadata") return Response(response_data, status=response_status) else: return Response(status=status.HTTP_401_UNAUTHORIZED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create(self, request): """ POST request that creates a new project. :param request: POST request :return: New project object """ dataset_inputs = load_request(request) serializer = self.serializer_class(data=dataset_inputs, context={'request': request}) if serializer.is_valid(): serializer.save() project = serializer.data p = Project.objects.get(id=project["id"]) if "metadata" not in dataset_inputs.keys(): dataset_inputs["metadata"] = None m = Metadata(p, dataset_inputs["metadata"]) meta = m.set_metadata("ProjectMetadata") project["metadata"] = meta return Response(project, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create(self, request): """ POST request that creates a new analytical model. :param request: POST request :return: New analytical object """ amodel_inputs = request.data.dict() serializer = self.serializer_class(data=amodel_inputs, context={'request': request}) if serializer.is_valid(): serializer.save() amodel_inputs = serializer.data if "metadata" in amodel_inputs.keys(): amodel_inputs["metadata"] = None m = Metadata(amodel_inputs, amodel_inputs["metadata"]) meta = m.set_metadata("ModelMetadata") amodel_inputs["metadata"] = m.get_metadata("ModelMetadata") if amodel_inputs: return Response(amodel_inputs, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create(self, request): """ POST request that creates a new location. :param request: POST request :return: New location object """ dataset_inputs = load_request(request) serializer = self.serializer_class(data=dataset_inputs, context={'request': request}) # TODO: Add project existence and ownership check if serializer.is_valid(): location = serializer.save() location_data = serializer.data if "metadata" not in dataset_inputs.keys(): dataset_inputs["metadata"] = None m = Metadata(location, dataset_inputs["metadata"]) meta = m.set_metadata("LocationMetadata") if meta: location_data["metadata"] = meta if location: return Response(location_data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)