def update_ledger(self, request, *args, **kwargs): lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field pk = self.kwargs[lookup_url_kwarg] try: validate_pk(pk) except Exception as e: return Response({'message': str(e)}, status=status.HTTP_400_BAD_REQUEST) objective_key = request.data.get('objective_key') args = { 'dataManagerKey': pk, 'objectiveKey': objective_key, } if getattr(settings, 'LEDGER_SYNC_ENABLED'): try: data = updateLedgerDataManager(args, sync=True) except LedgerError as e: return Response({'message': str(e.msg)}, status=e.status) st = status.HTTP_200_OK else: # use a celery task, as we are in an http request transaction updateLedgerDataManagerAsync.delay(args) data = { 'message': 'The substra network has been notified for updating this DataManager' } st = status.HTTP_202_ACCEPTED return Response(data, status=st)
def _retrieve(self, request, pk): validate_pk(pk) data = get_object_from_ledger(pk, self.ledger_query_call) # do not cache if node has not process permission if node_has_process_permission(data): # try to get it from local db to check if description exists try: instance = self.get_object() except Http404: instance = None finally: # check if instance has description if not instance or not instance.description: instance = self.create_or_update_algo(data, pk) # For security reason, do not give access to local file address # Restrain data to some fields # TODO: do we need to send creation date and/or last modified date ? serializer = self.get_serializer(instance, fields=('owner', 'pkhash')) data.update(serializer.data) replace_storage_addresses(request, data) return data
def _retrieve(self, pk): validate_pk(pk) data = get_object_from_ledger(pk, self.ledger_query_call) if not data or not data.get('traintuple'): raise Exception('Invalid model: missing traintuple field') if data['traintuple'].get('status') != "done": raise Exception("Invalid model: traintuple must be at status done") # Try to get it from local db, else create it in local db try: instance = self.get_object() except Http404: instance = None if not instance or not instance.file: instance = self.create_or_update_model( data['traintuple'], data['traintuple']['outModel']['hash']) # For security reason, do not give access to local file address # Restrain data to some fields # TODO: do we need to send creation date and/or last modified date ? serializer = self.get_serializer(instance, fields=('owner', 'pkhash')) data.update(serializer.data) return data
def cancel(self, request, pk): validate_pk(pk) try: compute_plan = invoke_ledger(fcn='cancelComputePlan', args={'key': pk}, only_pkhash=False) except LedgerError as e: return Response({'message': str(e.msg)}, status=e.status) return Response(compute_plan, status=status.HTTP_200_OK)
def retrieve(self, request, *args, **kwargs): lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field pk = self.kwargs[lookup_url_kwarg] try: validate_pk(pk) data = get_object_from_ledger(pk, 'queryComputePlan') except LedgerError as e: return Response({'message': str(e.msg)}, status=e.status) except Exception as e: return Response({'message': str(e)}, status=status.HTTP_400_BAD_REQUEST) else: return Response(data, status=status.HTTP_200_OK)
def leaderboard(self, request, pk): sort = request.query_params.get('sort', 'desc') try: validate_pk(pk) except Exception as e: return Response({'message': str(e)}, status=status.HTTP_400_BAD_REQUEST) try: validate_sort(sort) except Exception as e: return Response({'message': str(e)}, status=status.HTTP_400_BAD_REQUEST) try: leaderboard = query_ledger(fcn='queryObjectiveLeaderboard', args={ 'objectiveKey': pk, 'ascendingOrder': sort == 'asc', }) except LedgerError as e: return Response({'message': str(e.msg)}, status=e.status) return Response(leaderboard, status=status.HTTP_200_OK)
def _retrieve(self, request, pk): validate_pk(pk) # get instance from remote node data = get_object_from_ledger(pk, 'queryDataset') # do not cache if node has not process permission if node_has_process_permission(data): # try to get it from local db to check if description exists try: instance = self.get_object() except Http404: instance = None finally: # check if instance has description or data_opener if not instance or not instance.description or not instance.data_opener: instance = self.create_or_update_datamanager(instance, data, pk) # do not give access to local files address serializer = self.get_serializer(instance, fields=('owner', 'pkhash', 'creation_date', 'last_modified')) data.update(serializer.data) replace_storage_addresses(request, data) return data
def _retrieve(self, pk): validate_pk(pk) return get_object_from_ledger(pk, self.ledger_query_call)