Skip to content

Commit

Permalink
Project SaveAs now correctly copies all project values (#345)
Browse files Browse the repository at this point in the history
* project saveAs now correctly copies all project values

* update to use modern Java

* unneeded file

---------

Co-authored-by: zkofiro <[email protected]>
  • Loading branch information
Zakaria-Kofiro and zkofiro authored Nov 14, 2024
1 parent ba7c20b commit dbcc0cf
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 42 deletions.
58 changes: 42 additions & 16 deletions web/web_support/src/main/java/com/intuit/tank/ProjectBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
*/

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

import com.intuit.tank.project.*;
import jakarta.enterprise.context.ConversationScoped;
import jakarta.enterprise.context.Conversation;
import jakarta.enterprise.event.Event;
Expand All @@ -34,15 +34,6 @@
import com.intuit.tank.auth.Security;
import com.intuit.tank.dao.ProjectDao;
import com.intuit.tank.dao.WorkloadDao;
import com.intuit.tank.project.AssociateDataFileBean;
import com.intuit.tank.project.JobConfiguration;
import com.intuit.tank.project.JobMaker;
import com.intuit.tank.project.NotificationsEditor;
import com.intuit.tank.project.Project;
import com.intuit.tank.project.ProjectVariableEditor;
import com.intuit.tank.project.UsersAndTimes;
import com.intuit.tank.project.Workload;
import com.intuit.tank.project.WorkloadScripts;
import com.intuit.tank.qualifier.Modified;
import com.intuit.tank.util.ExceptionHandler;
import com.intuit.tank.vm.api.enumerated.IncrementStrategy;
Expand Down Expand Up @@ -298,23 +289,58 @@ private Project copyProject() {
Project ret = new Project();
Workload workload = new Workload();
workload.setParent(ret);
List<Workload> workloads = new ArrayList<Workload>();
workload.setName(saveAsName);
workloads.add(workload);
ret.setWorkloads(workloads);
ret.setWorkloads(List.of(workload));
ret.setComments(project.getComments());
ret.setCreator(securityContext.getCallerPrincipal().getName());
ret.setName(saveAsName);
ret.setProductName(project.getProductName());
ret.setScriptDriver(project.getScriptDriver());

JobConfiguration originalJobConfig = project.getWorkloads().get(0).getJobConfiguration();
JobConfiguration newJobConfig = copyJobConfiguration(originalJobConfig, workload);
workload.setJobConfiguration(newJobConfig);

usersAndTimes.copyTo(workload);
workloadScripts.copyTo(workload);

notificationsEditor.copyTo(workload);
return ret;
}

private JobConfiguration copyJobConfiguration(JobConfiguration original, Workload newParent) {
JobConfiguration copy = new JobConfiguration();
copy.setBaselineVirtualUsers(original.getBaselineVirtualUsers());
copy.setTargetRampRate(original.getTargetRampRate());
copy.setDataFileIds(Set.copyOf(original.getDataFileIds()));
copy.setIncrementStrategy(original.getIncrementStrategy());
copy.setLocation(original.getLocation());
copy.setReportingMode(original.getReportingMode());
copy.setSimulationTime(original.getSimulationTime());
copy.setSimulationTimeExpression(original.getSimulationTimeExpression());
copy.setRampTime(original.getRampTime());
copy.setRampTimeExpression(original.getRampTimeExpression());
copy.setTerminationPolicy(original.getTerminationPolicy());
copy.setUserIntervalIncrement(original.getUserIntervalIncrement());
copy.setNumUsersPerAgent(original.getNumUsersPerAgent());
copy.setTargetRatePerAgent(original.getTargetRatePerAgent());
copy.setStopBehavior(original.getStopBehavior());
copy.setVmInstanceType(original.getVmInstanceType());
copy.setVariables(Map.copyOf(original.getVariables()));
copy.setJobRegions(original.getJobRegions().stream()
.map(this::copyRegion)
.collect(Collectors.toSet()));
copy.setParent(newParent);
return copy;
}

private JobRegion copyRegion(JobRegion original) {
JobRegion copy = new JobRegion();
copy.setRegion(original.getRegion());
copy.setUsers(original.getUsers());
copy.setPercentage(original.getPercentage());
return copy;
}

/**
* Saves the Project object in the database.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@
*/

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

import jakarta.enterprise.context.ConversationScoped;
Expand Down Expand Up @@ -228,32 +224,46 @@ public void save() {

public void copyTo(Workload copyTo) {
JobConfiguration jobConfiguration = projectBean.getJobConfiguration();
JobConfiguration jc = copyTo.getJobConfiguration();
jc.setBaselineVirtualUsers(jobConfiguration.getBaselineVirtualUsers());
jc.setDataFileIds(new HashSet<Integer>(jobConfiguration.getDataFileIds()));
jc.setIncrementStrategy(jobConfiguration.getIncrementStrategy());
jc.setLocation(jobConfiguration.getLocation());
jc.setRampTime(jobConfiguration.getRampTime());
jc.setReportingMode(jobConfiguration.getReportingMode());
jc.setSimulationTime(jobConfiguration.getSimulationTime());
jc.setTerminationPolicy(jobConfiguration.getTerminationPolicy());
jc.setUserIntervalIncrement(jobConfiguration.getUserIntervalIncrement());
jc.setVariables(jobConfiguration.getVariables());
Set<JobRegion> regions = jobConfiguration.getJobRegions().stream().map(this::copyRegion).collect(Collectors.toSet());
jc.setJobRegions(regions);
jc.setParent(copyTo);
JobConfiguration jc = copyJobConfiguration(jobConfiguration, copyTo);
copyTo.setJobConfiguration(jc);
}

private JobConfiguration copyJobConfiguration(JobConfiguration original, Workload newParent) {
JobConfiguration copy = new JobConfiguration();
copy.setBaselineVirtualUsers(original.getBaselineVirtualUsers());
copy.setTargetRampRate(original.getTargetRampRate());
copy.setDataFileIds(Set.copyOf(original.getDataFileIds()));
copy.setIncrementStrategy(original.getIncrementStrategy());
copy.setLocation(original.getLocation());
copy.setReportingMode(original.getReportingMode());
copy.setSimulationTime(original.getSimulationTime());
copy.setSimulationTimeExpression(original.getSimulationTimeExpression());
copy.setRampTime(original.getRampTime());
copy.setRampTimeExpression(original.getRampTimeExpression());
copy.setTerminationPolicy(original.getTerminationPolicy());
copy.setUserIntervalIncrement(original.getUserIntervalIncrement());
copy.setNumUsersPerAgent(original.getNumUsersPerAgent());
copy.setTargetRatePerAgent(original.getTargetRatePerAgent());
copy.setStopBehavior(original.getStopBehavior());
copy.setVmInstanceType(original.getVmInstanceType());
copy.setVariables(Map.copyOf(original.getVariables()));
copy.setJobRegions(original.getJobRegions().stream()
.map(this::copyRegion)
.collect(Collectors.toSet()));
copy.setParent(newParent);
return copy;
}

/**
* @param jr
* @return
* @param original
* @return copy
*/
private JobRegion copyRegion(JobRegion jr) {
JobRegion ret = new JobRegion();
ret.setRegion(jr.getRegion());
ret.setUsers(jr.getUsers());
return ret;
private JobRegion copyRegion(JobRegion original) {
JobRegion copy = new JobRegion();
copy.setRegion(original.getRegion());
copy.setUsers(original.getUsers());
copy.setPercentage(original.getPercentage());
return copy;
}

/**
Expand Down

0 comments on commit dbcc0cf

Please sign in to comment.