diff --git a/SAS/ResourceAssignment/ResourceAssignmentDatabase/radb.py b/SAS/ResourceAssignment/ResourceAssignmentDatabase/radb.py index 26beeb5150fa7f0b6c71791ac3f6e4dd816176bc..55115b0e128cd4719e4e6251a3bec7e9a2181a53 100644 --- a/SAS/ResourceAssignment/ResourceAssignmentDatabase/radb.py +++ b/SAS/ResourceAssignment/ResourceAssignmentDatabase/radb.py @@ -680,7 +680,7 @@ class RADatabase: if isinstance(resource_ids, int): # just a single id conditions.append('id = %s') qargs.append(resource_ids) - else: #assume a list/enumerable of id's + elif resource_ids: #assume a list/enumerable of id's conditions.append('id in %s') qargs.append(tuple(resource_ids)) @@ -705,10 +705,13 @@ class RADatabase: resources = list(self._executeQuery(query, qargs, fetch=_FETCH_ALL)) - if lower_bound and upper_bound and isinstance(lower_bound, datetime) and isinstance(upper_bound, datetime): - # TODO: possibility for performance improvement: make single db call instead of looping. - for resource in resources: - resource['claimable_capacity'] = self.get_resource_claimable_capacity(resource['id'], lower_bound, upper_bound) + if lower_bound or upper_bound: + if isinstance(lower_bound, datetime) and isinstance(upper_bound, datetime): + # TODO: possibility for performance improvement: make single db call instead of looping. + for resource in resources: + resource['claimable_capacity'] = self.get_resource_claimable_capacity(resource['id'], lower_bound, upper_bound) + else: + raise ValueError('you should supply both lower_bound and upper_bound (as datetimes)') return resources @@ -1202,20 +1205,18 @@ class RADatabase: def updateResourceClaim(self, resource_claim_id, resource_id=None, task_id=None, starttime=None, endtime=None, status=None, claim_size=None, username=None, used_rcus=None, user_id=None, commit=True): - return self.updateResourceClaims([resource_claim_id], resource_id, task_id, starttime, endtime, status, + return self.updateResourceClaims([resource_claim_id], None, resource_id, task_id, starttime, endtime, status, claim_size, username, used_rcus, user_id, commit) - def updateResourceClaims(self, resource_claim_ids, resource_id=None, task_id=None, starttime=None, endtime=None, + def updateResourceClaims(self, where_resource_claim_ids=None, where_task_ids=None, resource_id=None, task_id=None, starttime=None, endtime=None, status=None, claim_size=None, username=None, used_rcus=None, user_id=None, commit=True): - if not resource_claim_ids: - return True - - logger.info("updateResourceClaims for %d claims" % len(resource_claim_ids)) + logger.info("updateResourceClaims") + status_id = status if status is not None and isinstance(status, basestring): #convert status string to status.id - status = self.getResourceClaimStatusId(status) + status_id = self.getResourceClaimStatusId(status) fields = [] values = [] @@ -1236,9 +1237,9 @@ class RADatabase: fields.append('endtime') values.append(endtime) - if status is not None: + if status_id is not None: fields.append('status_id') - values.append(status) + values.append(status_id) if claim_size is not None: fields.append('claim_size') @@ -1256,13 +1257,32 @@ class RADatabase: fields.append('user_id') values.append(user_id) - values.append(tuple(resource_claim_ids)) - query = '''UPDATE resource_allocation.resource_claim - SET ({fields}) = ({value_placeholders}) - WHERE resource_allocation.resource_claim.id in {rc_ids_placeholder};'''.format(fields=', '.join(fields), - value_placeholders=', '.join('%s' for x in fields), - rc_ids_placeholder='%s') + SET ({fields}) = ({value_placeholders})'''.format(fields=', '.join(fields), + value_placeholders=', '.join('%s' for x in fields)) + + if where_resource_claim_ids is None and where_task_ids is None: + raise ValueError('please provide either "where_resource_claim_ids" and/or "where_task_ids" argument for updateResourceClaims') + + conditions = [] + + if where_resource_claim_ids is not None: + if isinstance(where_resource_claim_ids, int): # just a single id + conditions.append('id = %s') + values.append(where_resource_claim_ids) + elif len(where_resource_claim_ids): #assume a list/enumerable of id's + conditions.append('id in %s') + values.append(tuple(where_resource_claim_ids)) + + if where_task_ids is not None: + if isinstance(where_task_ids, int): # just a single id + conditions.append('task_id = %s') + values.append(where_task_ids) + elif len(where_task_ids): #assume a list/enumerable of id's + conditions.append('task_id in %s') + values.append(tuple(where_task_ids)) + + query += ' WHERE ' + ' AND '.join(conditions) self._executeQuery(query, values) @@ -1273,27 +1293,19 @@ class RADatabase: def updateTaskAndResourceClaims(self, task_id, starttime=None, endtime=None, task_status=None, claim_status=None, username=None, used_rcus=None, user_id=None, commit=True): - claimsBeforeUpdate = self.getResourceClaims(task_ids=task_id) - - if claim_status is not None and isinstance(claim_status, basestring): - #convert status string to status.id - claim_status = self.getResourceClaimStatusId(claim_status) - updated = True if (starttime or endtime or claim_status is not None or username is not None or used_rcus is not None or user_id is not None): # update the claims as well - claim_ids = [c['id'] for c in claimsBeforeUpdate] - if claim_ids: - updated &= self.updateResourceClaims(claim_ids, - starttime=starttime, - endtime=endtime, - status=claim_status, - username=username, - used_rcus=used_rcus, - user_id=user_id, - commit=False) + updated &= self.updateResourceClaims(where_task_ids=task_id, + starttime=starttime, + endtime=endtime, + status=claim_status, + username=username, + used_rcus=used_rcus, + user_id=user_id, + commit=False) if task_status is not None : updated &= self.updateTask(task_id, task_status=task_status, commit=False)