-
Notifications
You must be signed in to change notification settings - Fork 0
/
JobsActiveInTimeFrame.groovy
88 lines (72 loc) · 2.89 KB
/
JobsActiveInTimeFrame.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* Jenkins, Groovy script for obtain all jobs, and statistics about usage per given period
*
* REFMONTHAGO - offset of period to the past in months (be sure to use minus sign)
* REFDAYSAGO - offset of period to the past in days (be sure to use minus sign)
*
* PRINTALLJOBS - print all found jobs in Jenkins
* PRINTACTIVEJOBS - print all found jobs in Jenkins which were build in given frame period
*
* PRINTINACTIVEJOBS - print all found jobs in Jenkins which were build, but not in given frame period
* PRINTNOBUILDJOBS - print all found jobs in Jenkins which were not never built
*/
import groovyjarjarasm.asm.Item
//init
int REFMONTHAGO = -1;
int REFDAYSAGO = 0;
boolean PRINTALLJOBS = false;
boolean PRINTACTIVEJOBS = false;
boolean PRINTINACTIVEJOBS = true;
boolean PRINTNOBUILDJOBS = true;
String dateFormatPattern = "MM/dd/yyyy HH:mm";
Calendar referenceDate = Calendar.getInstance();
referenceDate.set(Calendar.HOUR_OF_DAY,23);
referenceDate.set(Calendar.MINUTE,59);
referenceDate.set(Calendar.SECOND,59);
referenceDate.set(Calendar.MILLISECOND,59);
//set relevant time frame
referenceDate.add(Calendar.MONTH,REFMONTHAGO);
referenceDate.add(Calendar.DAY_OF_MONTH,REFDAYSAGO);
Calendar itemLastBuildDateCalendar = Calendar.getInstance();
//load all jobs
List<Item> allJobs = Jenkins.instance.getAllItems();
//candidate list
List<Item> jBuildedInRef = new ArrayList<Item>();
for(int i = 0; i < allJobs.size() ; i++){
def item = allJobs.get(i);
//if we want to print all jobs, print its name
if(PRINTALLJOBS && item != null) {
println("all #"+i+": "+item.fullName);
}
try{
//never built or is folder eg.
if(item != null && item.getLastBuild() != null){
Date lastBuildDate = item.getLastBuild().getTime();
itemLastBuildDateCalendar.setTime(lastBuildDate);
if(itemLastBuildDateCalendar.after(referenceDate)){
jBuildedInRef.add(item);
if(PRINTACTIVEJOBS) {
println("active #"+i+": "+ item.fullName +", last build: " + lastBuildDate.format(dateFormatPattern));
}
}else {
if(PRINTINACTIVEJOBS) {
println("inactive #"+i+": "+ item.fullName +", last build: " + lastBuildDate.format(dateFormatPattern));
}
}
}else if(item != null) {
if(PRINTNOBUILDJOBS) {
println("inactive #"+i+": "+ item.fullName +", without build");
}
}
}catch(Exception e){
//groovy.lang.MissingMethodException,...
}
}
println "JOBS INFO:"
println "total: \t\t\t" + allJobs.size();
println "ref. period active: \t" + jBuildedInRef.size();
double percentage = (jBuildedInRef.size()/allJobs.size())*100;
percentage = percentage*100;
percentage = Math.round(percentage);
percentage = percentage/100;
println "percentage: \t\t" + percentage +" %";