def retrieve(self, request, pk=None): """ GET request for the data of a dataset, specified by dataset id :param request: GET request, containing the dataset id :param pk: Dataset id :return: Dataset data and relevant statistics """ if pk: try: dataset = Dataset.objects.get(pk=pk) except Dataset.DoesNotExist: return Response("No dataset found for id: {}".format(pk), status=status.HTTP_400_BAD_REQUEST) if not IsOwner().has_object_permission(request, self, dataset): return Response(status=status.HTTP_401_UNAUTHORIZED) serializer = self.serializer_class(dataset, many=False) response_data = serializer.data m = Metadata(dataset) meta = m.get_metadata("DatasetMetadata") response = "Response" if meta: response_data["metadata"] = meta response = meta["target"] response_data["data"] = load_dataset(pk) if response not in response_data["data"]: response = response_data["data"].columns.tolist()[0] response_data["statistics"] = DatasetStatistics(response_data["data"]).calculate_statistics(response) return Response(response_data, status=status.HTTP_200_OK) else: return Response( "Required id for the dataset was not found.", status=status.HTTP_400_BAD_REQUEST )
def retrieve(self, request, pk=None): """ GET request for the data of a dataset, specified by dataset id :param request: GET request, containing the dataset id :param pk: Dataset id :return: Dataset data and relevant statistics """ if pk: dataset = Dataset.objects.get(pk=pk) serializer = self.serializer_class(dataset, many=False) response_data = serializer.data m = Metadata(dataset) meta = m.get_metadata("DatasetMetadata") response = "Response" if meta: response_data["metadata"] = meta response = meta["response"] response_data["data"] = pd.read_csv( StringIO(bytes(dataset.data).decode())) if response not in response_data["data"]: response = response_data["data"].columns.tolist()[0] response_data["statistics"] = DatasetStatistics( response_data["data"]).calculate_statistics(response) return Response(response_data, status=status.HTTP_200_OK) else: return Response("Required id for the dataset id was not found.", 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 list(self, request): """ GET request that lists all the locations owned by the user. :param request: GET request :return: List of locations """ locations = Location.objects.filter(owner=request.user) # TODO: Add ACL access objects serializer = self.serializer_class(locations, many=True) response_data = serializer.data for l in response_data: loc = Location.objects.get(pk=int(l["id"])) m = Metadata(loc, None) l["metadata"] = m.get_metadata("LocationMetadata") return Response(response_data, status=status.HTTP_200_OK)
def list(self, request, pk=None): """ GET request that lists all the projects :param request: GET request :return: List of projects """ projects = Project.objects.filter(owner=request.user) # TODO: Add ACL access objects serializer = self.serializer_class(projects, many=True) response_data = serializer.data for d in response_data: p = Project.objects.get(id=d["id"]) m = Metadata(p, None) meta = m.get_metadata("ProjectMetadata") d["metadata"] = meta return Response(serializer.data, status=status.HTTP_200_OK)
def predict(self, request): input_data = load_request(request) required_parameters = ["project_id", "model_id", "data"] 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) response = {} data = str(input_data["data"]) results = DaskTasks.predict(project.id, model.id, data) response["project_id"] = project.id response["pipeline_id"] = model.pipeline.id response["model_id"] = model.id m = Metadata(model) response["model_metadata"] = m.get_metadata("ModelMetadata") response["dataset_id"] = project.dataset response["results"] = results 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 list(self, request): """ GET request that lists all the pipeline for a specific project id :param request: GET request, containing the project id as 'project' :return: List of analytical models """ if 'project' in self.request.query_params.keys(): pipeline = Pipeline.objects.filter( project=int(self.request.query_params.get('project'))) serializer = self.serializer_class(pipeline, many=True) response_data = serializer.data for l in response_data: a = Pipeline.objects.get(pk=int(l["id"])) m = Metadata(a, None) l["metadata"] = m.get_metadata("PipelineMetadata") return Response(response_data, status=status.HTTP_200_OK) return Response( "Required 'project' parameter for the pipeline was not found.", 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 data(self, request): inputs = request.data.dict() required_parameters = ["workflow_id", "model_id"] if set(required_parameters).issubset(inputs.keys()): try: workflow = Workflow.objects.get(id=int(inputs["workflow_id"])) except ObjectDoesNotExist: workflow = None try: amodel = AnalyticalModel.objects.get(id=int(inputs["model_id"])) except ObjectDoesNotExist: amodel = None if workflow is None or amodel is None: message = [] message = message if workflow else message.append("No workflow found for id: {}".format(inputs["workflow_id"])) message = message if amodel else message.append("No analytical model found for id: {}".format(inputs["amodel_id"])) return Response(",".join(message), status=status.HTTP_400_BAD_REQUEST) elif IsOwnerOfLocationChild().has_object_permission(request, self, workflow): response = {} meta = Metadata(parent=amodel) metadata = meta.get_metadata("ModelMetadata", ['status', 'stage', 'message']) response["metadata"] = metadata completed = False if "stage" in metadata.keys(): i = metadata["stage"].split("/") if int(i[0]) == int(i[1]): completed = True if completed: if amodel.model: data = None if "data" in inputs.keys(): data = pd.read_csv(StringIO(inputs["data"])) response["data"] = DaskTasks.make_prediction(amodel.id, data) response["dataset_id"] = amodel.dataset response["analytical_model_id"] = amodel.id response["workflow_id"] = workflow.id 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 status(self, request): input_data = self.request.query_params required_parameters = ["project_id", "pipeline_id"] 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: pipeline = Pipeline.objects.get( id=int(input_data["pipeline_id"])) if not IsOwnerOfPipeline().has_object_permission( request, self, pipeline): permissions.append("Unauthorized to access pipeline") except Pipeline.DoesNotExist: pipeline = None if len(permissions) > 0: return Response(permissions, status=status.HTTP_401_UNAUTHORIZED) if pipeline is None or project is None: message = [] if project is None: message.append("No project found for id: {}".format( input_data["project_id"])) if pipeline is None: message.append("No pipeline found for id: {}".format( input_data["pipeline_id"])) return Response(", ".join(message), status=status.HTTP_400_BAD_REQUEST) response = {} meta = Metadata(parent=pipeline) metadata = meta.get_metadata("PipelineMetadata", ['status', 'stage', 'message']) response["metadata"] = metadata completed = False if "stage" in metadata.keys(): i = metadata["stage"].split("/") try: if int(i[0]) == int(i[1]): completed = True except Exception as e: logger.error("Request Error: {}".format(e)) logger.error("STAGE: {}".format(metadata["stage"])) if completed: # TODO: Add additional pipeline completion results models = Model.objects.filter(pipeline=pipeline) model_details = [] for m in models: model_details.append({ "id": m.id, "name": m.name, "description": m.description }) response["models"] = model_details response["project_id"] = project.id response["dataset_id"] = project.dataset response["pipeline_id"] = pipeline.id 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)