-
Notifications
You must be signed in to change notification settings - Fork 0
/
V2- Jenkins builds stats.groovy
63 lines (42 loc) · 1.53 KB
/
V2- Jenkins builds stats.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.text.SimpleDateFormat
import com.cloudbees.hudson.plugins.folder.*
import groovyjarjarasm.asm.Item
String startDate = "07/01/2018 00:00";
String endDate = "07/31/2018 23:59";
String dateFormatPattern = "MM/dd/yyyy HH:mm";
Calendar startDateCal = parseDate(startDate,dateFormatPattern);
Calendar endDateCal = parseDate(endDate,dateFormatPattern);
getTotalBuildsCount();
getActiveBuildsCount(startDateCal,endDateCal);
def getActiveBuildsCount(Calendar startRef,Calendar endRef) {
int count =0;
for(jobItm in Jenkins.instance.getAllItems()) {
if(jobItm instanceof Job && jobItm.getLastBuild() != null) {
for(build in jobItm.builds) {
Date buildDate = build.getTime();
Calendar buildCal = Calendar.getInstance();
buildCal.setTime(buildDate);
if(buildCal.before(endRef) && buildCal.after(startRef)) {
count++;
}
}
}
}
println("Number of active builds: " + count);
}
def getTotalBuildsCount(){
int count = 0;
for(jobRoot in Jenkins.instance.getAllItems()) {
if(jobRoot instanceof Job && jobRoot.getLastBuild() != null) {
count += jobRoot.getLastBuild().number;
}
}
println("Total builds: " + count);
}
def parseDate(String dateIn,String pattern){
SimpleDateFormat sf = new SimpleDateFormat(pattern);
Calendar referenceDate = Calendar.getInstance();
Date parsed = sf.parse(dateIn);
referenceDate.setTime(parsed);
return referenceDate;
}