View Javadoc
1   package org.kuali.common.devops.jenkins.scan;
2   
3   import static com.google.common.collect.Lists.newArrayList;
4   import static org.kuali.common.devops.jenkins.scan.Stats.buildSummaryFromIntegers;
5   import static org.kuali.common.devops.jenkins.scan.Stats.buildSummaryFromLongs;
6   
7   import java.util.Arrays;
8   import java.util.List;
9   
10  import com.google.common.collect.ImmutableList;
11  
12  public class Jobs {
13  
14  	public static SummaryStatistics getStartTimeStats(List<Job> jobs) {
15  		List<Long> longs = newArrayList();
16  		for (Job job : jobs) {
17  			for (JobExecution execution : job.getExecutions()) {
18  				longs.add(execution.getStartTime());
19  			}
20  		}
21  		return buildSummaryFromLongs(longs);
22  	}
23  
24  	public static int getLastBuildNumber(Job job) {
25  		List<Integer> integers = newArrayList();
26  		for (JobExecution execution : job.getExecutions()) {
27  			integers.add(execution.getNumber());
28  		}
29  		SummaryStatistics summary = buildSummaryFromIntegers(integers);
30  		return new Double(summary.getMax()).intValue();
31  	}
32  
33  	public static long getTotalExecutions(List<Job> jobs) {
34  		int builds = 0;
35  		for (Job job : jobs) {
36  			builds += getLastBuildNumber(job);
37  		}
38  		return builds;
39  	}
40  
41  	public static JobDurationStatistics getRecordedDurationStatistics(Job job) {
42  		List<Long> durations = newArrayList();
43  		for (JobExecution execution : job.getExecutions()) {
44  			durations.add(execution.getDuration());
45  		}
46  		SummaryStatistics summary = buildSummaryFromLongs(durations);
47  		return JobDurationStatistics.builder().withName(job.getName()).withSummary(summary).withValues(durations).build();
48  	}
49  
50  	public static SummaryStatistics getCalculatedDurationStatistics(List<Job> jobs) {
51  		List<Long> durations = newArrayList();
52  		for (Job job : jobs) {
53  			JobDurationStatistics jds = getCalculatedDurationStatistics(job);
54  			durations.addAll(jds.getValues());
55  		}
56  		return buildSummaryFromLongs(durations);
57  	}
58  
59  	public static JobDurationStatistics getCalculatedDurationStatistics(Job job) {
60  		JobDurationStatistics recorded = getRecordedDurationStatistics(job);
61  		List<JobExecution> executions = job.getExecutions();
62  		if (executions.isEmpty()) {
63  			return recorded;
64  		}
65  		int size = executions.size();
66  		JobExecution last = executions.get(size - 1);
67  		int latestBuildNumber = last.getNumber();
68  		Long[] values = new Long[latestBuildNumber];
69  		long duration = new Double(recorded.getSummary().getAvg()).longValue();
70  		Arrays.fill(values, duration);
71  		List<Long> durations = ImmutableList.copyOf(values);
72  		SummaryStatistics summary = buildSummaryFromLongs(durations);
73  		return JobDurationStatistics.builder().withName(job.getName()).withSummary(summary).withValues(durations).build();
74  	}
75  
76  }