예제 #1
0
    def _retrieve(self, request, key):
        validate_key(key)
        data = get_object_from_ledger(get_channel_name(request), key,
                                      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(
                        get_channel_name(request), data, key)

                # 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'))
                data.update(serializer.data)

        replace_storage_addresses(request, data)

        return data
예제 #2
0
    def update_ledger(self, request, *args, **kwargs):

        lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
        key = self.kwargs[lookup_url_kwarg]

        validate_key(key)

        objective_key = request.data.get('objective_key')
        args = {
            'data_manager_key': key,
            'objective_key': objective_key,
        }

        if settings.LEDGER_SYNC_ENABLED:
            st = status.HTTP_200_OK
        else:
            st = status.HTTP_202_ACCEPTED

        try:
            data = ledger.assets.update_datamanager(get_channel_name(request),
                                                    args)
        except LedgerError as e:
            return Response({'message': str(e.msg)}, status=e.status)

        return Response(data, status=st)
예제 #3
0
    def _retrieve(self, request, key):
        validate_key(key)
        # get instance from remote node
        data = get_object_from_ledger(get_channel_name(request), key,
                                      '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(
                        get_channel_name(request), instance, data, key)

                # do not give access to local files address
                serializer = self.get_serializer(instance, fields=('owner'))
                data.update(serializer.data)

        replace_storage_addresses(request, data)

        return data
예제 #4
0
    def retrieve(self, request, *args, **kwargs):
        lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
        key = self.kwargs[lookup_url_kwarg]
        validate_key(key)

        try:
            data = get_object_from_ledger(get_channel_name(request), key,
                                          'queryComputePlan')
        except LedgerError as e:
            return Response({'message': str(e.msg)}, status=e.status)
        else:
            return Response(data, status=status.HTTP_200_OK)
예제 #5
0
    def cancel(self, request, *args, **kwargs):
        lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
        key = self.kwargs[lookup_url_kwarg]
        validate_key(key)

        try:
            compute_plan = invoke_ledger(get_channel_name(request),
                                         fcn='cancelComputePlan',
                                         args={'key': key},
                                         only_key=False)
        except LedgerError as e:
            return Response({'message': str(e.msg)}, status=e.status)
        return Response(compute_plan, status=status.HTTP_200_OK)
예제 #6
0
    def _retrieve(self, channel_name, key):
        validate_key(key)

        data = get_object_from_ledger(channel_name, key, self.ledger_query_call)

        compatible_tuple_types = ['traintuple', 'composite_traintuple', 'aggregatetuple']
        any_data = any(list(map(lambda x: x in data, compatible_tuple_types)))

        if not any_data:
            raise Exception(
                'Invalid model: missing traintuple, composite_traintuple or aggregatetuple field'
            )

        return data
예제 #7
0
    def update_ledger(self, request, *args, **kwargs):
        lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
        key = self.kwargs[lookup_url_kwarg]

        validate_key(key)

        serializer = self.get_serializer(data=dict(request.data))
        serializer.is_valid(raise_exception=True)

        # update compute plan in ledger
        try:
            data = serializer.update(get_channel_name(request), key,
                                     serializer.validated_data)
        except LedgerError as e:
            error = {'message': str(e.msg), 'key': key}
            return Response(error, status=e.status)

        # send successful response
        headers = self.get_success_headers(data)
        status = get_success_create_code()
        return Response(data, status=status, headers=headers)
예제 #8
0
    def leaderboard(self, request, *args, **kwargs):
        lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
        key = self.kwargs[lookup_url_kwarg]
        validate_key(key)
        sort = request.query_params.get('sort', 'desc')

        try:
            validate_sort(sort)
        except Exception as e:
            return Response({'message': str(e)},
                            status=status.HTTP_400_BAD_REQUEST)

        try:
            leaderboard = query_ledger(get_channel_name(request),
                                       fcn='queryObjectiveLeaderboard',
                                       args={
                                           'objective_key': key,
                                           'ascendingOrder': sort == 'asc',
                                       })
        except LedgerError as e:
            return Response({'message': str(e.msg)}, status=e.status)

        return Response(leaderboard, status=status.HTTP_200_OK)
예제 #9
0
 def _retrieve(self, channel_name, key):
     validate_key(key)
     return get_object_from_ledger(channel_name, key,
                                   self.ledger_query_call)