def _create(self, request): data = { 'algo_key': request.data.get('algo_key'), 'rank': request.data.get('rank'), 'compute_plan_id': request.data.get('compute_plan_id', ''), 'in_models_keys': request.data.getlist('in_models_keys'), 'worker': request.data.get('worker'), 'tag': request.data.get('tag', '') } serializer = self.get_serializer(data=data) serializer.is_valid(raise_exception=True) # Get aggregatetuple pkhash to handle 408 timeout in invoke_ledger args = serializer.get_args(serializer.validated_data) try: data = query_ledger(fcn='createAggregatetuple', args=args) except LedgerConflict as e: raise LedgerException({ 'message': str(e.msg), 'pkhash': e.pkhash }, e.status) except LedgerError as e: raise LedgerException({'message': str(e.msg)}, e.status) else: pkhash = data.get('key') return self.commit(serializer, pkhash)
def commit(self, serializer, request): # create on local db try: instance = self.perform_create(serializer) except IntegrityError as e: try: pkhash = re.search(r'\(pkhash\)=\((\w+)\)', e.args[0]).group(1) except IndexError: pkhash = '' err_msg = 'A objective with this description file already exists.' return {'message': err_msg, 'pkhash': pkhash}, status.HTTP_409_CONFLICT except Exception as e: raise Exception(e.args) # init ledger serializer ledger_data = { 'test_data_sample_keys': request.data.getlist('test_data_sample_keys', []), 'test_data_manager_key': request.data.get('test_data_manager_key', ''), 'name': request.data.get('name'), # XXX workaround because input is a QueryDict and not a JSON object. This # is due to the fact that we are sending file object and body in a # single HTTP request 'permissions': { 'public': request.data.get('permissions_public'), 'authorized_ids': request.data.getlist('permissions_authorized_ids', []), }, 'metrics_name': request.data.get('metrics_name'), } ledger_data.update({'instance': instance}) ledger_serializer = LedgerObjectiveSerializer(data=ledger_data, context={'request': request}) if not ledger_serializer.is_valid(): # delete instance instance.delete() raise ValidationError(ledger_serializer.errors) # create on ledger try: data = ledger_serializer.create(ledger_serializer.validated_data) except LedgerTimeout as e: data = {'pkhash': [x['pkhash'] for x in serializer.data], 'validated': False} raise LedgerException(data, e.status) except LedgerConflict as e: raise ValidationException(e.msg, e.pkhash, e.status) except LedgerError as e: instance.delete() raise LedgerException(str(e.msg), e.status) except Exception: instance.delete() raise d = dict(serializer.data) d.update(data) return d
def commit(self, serializer, request): # create on local db try: instance = self.perform_create(serializer) except Exception as e: raise Exception(e.args) # init ledger serializer ledger_data = { 'test_data_sample_keys': request.data.get('test_data_sample_keys') or [], 'test_data_manager_key': request.data.get('test_data_manager_key'), 'name': request.data.get('name'), 'permissions': request.data.get('permissions'), 'metrics_name': request.data.get('metrics_name'), 'metadata': request.data.get('metadata') } ledger_data.update({'instance': instance}) ledger_serializer = LedgerObjectiveSerializer( data=ledger_data, context={'request': request}) if not ledger_serializer.is_valid(): # delete instance instance.delete() raise ValidationError(ledger_serializer.errors) # create on ledger try: data = ledger_serializer.create(get_channel_name(request), ledger_serializer.validated_data) except LedgerTimeout as e: raise LedgerException('timeout', e.status) except LedgerConflict as e: raise ValidationException(e.msg, e.key, e.status) except LedgerError as e: instance.delete() raise LedgerException(str(e.msg), e.status) except Exception: instance.delete() raise d = dict(serializer.data) d.update(data) return d
def commit(self, serializer, channel_name): # create on ledger try: data = serializer.validated_data if data['rank'] == 0 and not data['compute_plan_key']: # Auto-create compute plan res = create_compute_plan(channel_name, data={}) data['compute_plan_key'] = res['key'] data = serializer.create(channel_name, data) except LedgerConflict as e: raise LedgerException({'message': str(e.msg), 'key': e.key}, e.status) except LedgerError as e: raise LedgerException({'message': str(e.msg)}, e.status) else: return data
def commit(self, serializer, request): # create on db instance = self.perform_create(serializer) ledger_data = { 'name': request.data.get('name'), # XXX workaround because input is a QueryDict and not a JSON object. This # is due to the fact that we are sending file object and body in a # single HTTP request 'permissions': { 'public': request.data.get('permissions_public'), 'authorized_ids': request.data.getlist('permissions_authorized_ids', []), }, } # init ledger serializer ledger_data.update({'instance': instance}) ledger_serializer = LedgerCompositeAlgoSerializer( data=ledger_data, context={'request': request}) if not ledger_serializer.is_valid(): # delete instance instance.delete() raise ValidationError(ledger_serializer.errors) # create on ledger try: data = ledger_serializer.create(ledger_serializer.validated_data) except LedgerTimeout as e: data = { 'pkhash': [x['pkhash'] for x in serializer.data], 'validated': False } raise LedgerException(data, e.status) except LedgerConflict as e: raise ValidationException(e.msg, e.pkhash, e.status) except LedgerError as e: instance.delete() raise LedgerException(str(e.msg), e.status) except Exception: instance.delete() raise d = dict(serializer.data) d.update(data) return d
def commit(self, serializer, pkhash): # create on ledger try: data = serializer.create(serializer.validated_data) except LedgerError as e: raise LedgerException({'message': str(e.msg), 'pkhash': pkhash}, e.status) else: return data
def commit(self, serializer, channel_name): # create on ledger try: data = serializer.create(channel_name, serializer.validated_data) except LedgerError as e: raise LedgerException({'message': str(e.msg)}, e.status) else: return data
def _create(self, request): data = { 'algo_key': request.data.get('algo_key'), 'data_manager_key': request.data.get('data_manager_key'), 'rank': request.data.get('rank'), 'compute_plan_id': request.data.get('compute_plan_id', ''), 'in_head_model_key': request.data.get('in_head_model_key', ''), 'in_trunk_model_key': request.data.get('in_trunk_model_key', ''), 'out_trunk_model_permissions': { 'public': False, 'authorized_ids': request.data.getlist( 'out_trunk_model_permissions_authorized_ids', []), }, 'train_data_sample_keys': request.data.getlist('train_data_sample_keys'), 'tag': request.data.get('tag', '') } serializer = self.get_serializer(data=data) serializer.is_valid(raise_exception=True) # Get compositetraintuple pkhash to handle 408 timeout in invoke_ledger args = serializer.get_args(serializer.validated_data) try: data = query_ledger(fcn='createCompositeTraintuple', args=args) except LedgerConflict as e: raise LedgerException({ 'message': str(e.msg), 'pkhash': e.pkhash }, e.status) except LedgerError as e: raise LedgerException({'message': str(e.msg)}, e.status) else: pkhash = data.get('key') return self.commit(serializer, pkhash)
def commit(self, serializer, request): # create on db instance = self.perform_create(serializer) ledger_data = { 'name': request.data.get('name'), 'permissions': request.data.get('permissions'), 'metadata': request.data.get('metadata') } # init ledger serializer ledger_data.update({'instance': instance}) ledger_serializer = LedgerAlgoSerializer(data=ledger_data, context={'request': request}) if not ledger_serializer.is_valid(): # delete instance instance.delete() raise ValidationError(ledger_serializer.errors) # create on ledger try: data = ledger_serializer.create(get_channel_name(request), ledger_serializer.validated_data) except LedgerTimeout as e: if isinstance(serializer.data, list): key = [x['key'] for x in serializer.data] else: key = [serializer.data['key']] data = {'key': key, 'validated': False} raise LedgerException(data, e.status) except LedgerConflict as e: raise ValidationException(e.msg, e.key, e.status) except LedgerError as e: instance.delete() raise LedgerException(str(e.msg), e.status) except Exception: instance.delete() raise d = dict(serializer.data) d.update(data) return d
def commit(serializer, ledger_data): instances = serializer.save() # init ledger serializer ledger_data.update({'instances': instances}) ledger_serializer = LedgerDataSampleSerializer(data=ledger_data) if not ledger_serializer.is_valid(): # delete instance for instance in instances: instance.delete() raise ValidationError(ledger_serializer.errors) # create on ledger try: data = ledger_serializer.create(ledger_serializer.validated_data) except LedgerTimeout as e: data = { 'pkhash': [x['pkhash'] for x in serializer.data], 'validated': False } raise LedgerException(data, e.status) except LedgerConflict as e: raise ValidationException(e.msg, e.pkhash, e.status) except LedgerError as e: for instance in instances: instance.delete() raise LedgerException(str(e.msg), e.status) except Exception: for instance in instances: instance.delete() raise st = get_success_create_code() # update validated to True in response if 'pkhash' in data and data['validated']: for d in serializer.data: if d['pkhash'] in data['pkhash']: d.update({'validated': data['validated']}) return serializer.data, st
def _create(self, request): data = { 'objective_key': request.data.get('objective_key'), 'traintuple_key': request.data.get('traintuple_key'), 'data_manager_key': request.data.get('data_manager_key', ''), 'test_data_sample_keys': request.data.getlist('test_data_sample_keys'), 'tag': request.data.get('tag', '') } serializer = self.get_serializer(data=data) serializer.is_valid(raise_exception=True) # Get traintuple pkhash to handle 408 timeout in invoke_ledger args = serializer.get_args(serializer.validated_data) try: data = query_ledger(fcn='createTesttuple', args=args) except LedgerConflict as e: raise LedgerException({'message': str(e.msg), 'pkhash': e.pkhash}, e.status) except LedgerError as e: raise LedgerException({'message': str(e.msg)}, e.status) else: pkhash = data.get('key') return self.commit(serializer, pkhash)