Name

global.GroupCapacityCalculator

Description

No description available

Script

var GroupCapacityCalculator = Class.create();
GroupCapacityCalculator.prototype = {
  initialize: function(group, release) {
  	if (JSUtil.nil(group)) 
  		return;
  		
  	this.groupId = group;
  	
  	this.releaseGroupAssociation = new GlideRecord('m2m_release_group');
  	this.releaseGroupAssociation.addQuery('group', this.groupId);
  	this.releaseGroupAssociation.addQuery('release.active', 'true');
  	this.releaseGroupAssociation.addNotNullQuery('start_sprint');
  	this.releaseGroupAssociation.addNotNullQuery('end_sprint');
  	
  	if (JSUtil.notNil(release)) 
  		this.releaseGroupAssociation.addQuery('release', release);
  		
  	this.releaseGroupAssociation.query();
  	this.isGroupAssociatedToRelease = this.releaseGroupAssociation.hasNext();
  },
  
  countSprintsByGroup: function(groupId, startDate, endDate) {
  	var ga = new GlideAggregate('rm_sprint');
  	
  	ga.addQuery('assignment_group', groupId);
  	ga.addQuery('start_date', '>=', startDate);
  	ga.addQuery('end_date', '<=', endDate);
  	ga.addAggregate('COUNT');
  	ga.setGroup(false);
  	ga.query();
  	
  	return ga.next() ? ga.getAggregate('COUNT') : 0;
  },
  
  sumGroupCapacityByRelease: function(releaseId) {
  	var ga = new GlideAggregate('m2m_release_group');
  	
  	ga.addQuery('release', releaseId);
  	ga.addAggregate('SUM', 'group_capacity');
  	ga.setGroup(false);
  	ga.query();
  	
  	return ga.next() ? ga.getAggregate('SUM', 'group_capacity') : 0;
  },
  
  recalculateGroupCapacity: function() {
  	if (!this.isGroupAssociatedToRelease) 
  		return;
  		
  	while (this.releaseGroupAssociation.next()) {
  		var releaseId = this.releaseGroupAssociation.getValue('release');
  		var startDate = this.releaseGroupAssociation.start_sprint.getRefRecord().getValue('start_date');
  		var endDate = this.releaseGroupAssociation.end_sprint.getRefRecord().getValue('end_date');
  		var numOfSprints = this.countSprintsByGroup(this.groupId, startDate, endDate);
  		var points = this.releaseGroupAssociation.getValue('points');
  		var groupCapacity = numOfSprints * points;
  		
  		this.releaseGroupAssociation.setValue('group_capacity', groupCapacity);
  		this.releaseGroupAssociation.setWorkflow(false);
  		this.releaseGroupAssociation.update();
  		
  		this.recalculateReleaseCapacity(releaseId);
  	}
  },
  
  recalculateReleaseCapacity: function(releaseId) {
  	var releaseCapacity = this.sumGroupCapacityByRelease(releaseId);
  	var release = new GlideRecord('rm_release_scrum');
  	
  	release.get(releaseId);
  	release.setValue('release_capacity', releaseCapacity);
  	release.setWorkflow(false);
  	release.update();
  },
  
  type: 'GroupCapacityCalculator'
};

Sys ID

b2a28b9d0b532200d3fced3ab4673aa5

Offical Documentation

Official Docs: