-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathJenkins_ReportJobUsage.groovy
57 lines (49 loc) · 1.34 KB
/
Jenkins_ReportJobUsage.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
/*** BEGIN META {
"name" : "Jenkins_ReportJobUsage",
"comment" : "Creates a report of a job usage (counts) by user over a selectable period of days (default is 365)",
"parameters" : [ 'vJobName','vDaySpan'],
"core": "1.596",
"authors" : [
{ name : "Ioannis K. Moutsatsos" }
]
} END META**/
import hudson.model.*
def jobName=vJobName
def job = hudson.model.Hudson.instance.getJob(jobName)
users=[]
uniqueUsers=[]
tSpan=(vDaySpan!='')?vDaySpan as Integer:365 //one year period
now= new Date()
build=job.getBuilds()
build.each{
buildDate= new Date(it.getStartTimeInMillis())
if(now-buildDate<=tSpan) {
def allActions = it.getAllActions()
allActions.each{
if(it.class== hudson.model.CauseAction){
// println it.class
causes= it.getCauses()
causes.each{ c->
if(c.class==hudson.model.Cause$UserIdCause){
users.add( c.getShortDescription()-'Started by user ')
}
}
//
}
}
}//end tSpan logic
}//end each
uniqueUsers=uniqueUsers+users
byNumList=[] //a list for sorting jobs by count
println "${job.name} Builds"
println "Reporting: Last $tSpan days from $now"
println '___________'
uniqueUsers.unique().each{us->
occurs=users.count{it.startsWith(us)}
byNumList.add( "${String.format('%03d', occurs)}\t$us")
}
byNumList.sort().reverse().each{
println it
}
println '___________'
println "${users.size()}:TOTAL"