-
Notifications
You must be signed in to change notification settings - Fork 0
/
jenkins_data.rb
193 lines (156 loc) · 4.83 KB
/
jenkins_data.rb
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env ruby
require 'json'
require 'open-uri'
#
# Set start timestamp, end timestamp, jenkins master
#
# http://www.unixtimestamp.com/index.php
# start timestamp: midnight January 1 2016 UTC
start_timestamp = 1451606400000
# end timestamp: midnight February 1 2016 UTC
end_timestamp = 1454284800000
# name of jenkins master
jenkins_master='wilson'
jenkins_url="http://#{jenkins_master}.ci.chef.co/api/json"
def json_result(url)
JSON.parse(open(url).read)
end
class JenkinsJob
attr_reader :name, :type, :url, :pipeline
def initialize(name, type, url, pipeline)
@name = name
@type = type
@url = url
@pipeline = pipeline
end
end
class BuildData
attr_reader :job_url, :start_timestamp, :end_timestamp
def initialize(job_url, start_timestamp, end_timestamp)
@job_url = job_url
@start_timestamp = start_timestamp
@end_timestamp = end_timestamp
end
def mean
durations = build_data.reject { |j| !j['result'].eql?('SUCCESS') }.collect { |j| j['duration'] }
total = durations.inject { |sum, d| sum + d }
return total / successes unless successes == 0
return "No successful build in the specified time period"
end
def numbuilds
build_data.length
end
def successes
count('SUCCESS')
end
def failures
count('FAILURE')
end
def aborts
count('ABORTED')
end
def unstables
count('UNSTABLE')
end
def get_good
all_timestamps.select { |t| t['timestamp'].to_i >= start_timestamp && t['timestamp'].to_i <= end_timestamp }.collect { |t| t['number'] }.sort
end
def good
@good ||= get_good
end
private
def all_timestamps
@all_timestamps ||= get_all_timestamps
end
def build_data
@build_data ||= get_build_data
end
def get_build_data
# http://manhattan.ci.chef.co/job/chefdk-build/api/json?tree=allBuilds[number,actions[causes[upstreamProject]]]%7B0,69%7D
# gives you causes
range_url = @job_url + "api/json?tree=allBuilds[number,duration,result,actions[causes[upstreamProject]]]%7B#{left},#{right}%7D"
all_builds = json_result(range_url)['allBuilds']
prune(all_builds)
end
# prune ad hoc builds
def prune(bd)
pruned = []
bd.each do |build|
causes = build['actions'].find_all { |elt| elt.key?('causes') }
upstream = causes.reject { |elt| elt['causes'].eql?([{}]) }
ad_hoc_builds = upstream.find_all { |u| u['causes'][0]['upstreamProject'].include?('ad_hoc') }
next unless ad_hoc_builds.empty?
pruned.push(build)
end
pruned
end
def count(thing)
build_data.find_all { |j| j['result'].eql?(thing) }.length
end
def get_all_timestamps
ts_url = @job_url + '/api/json?tree=allBuilds[number,timestamp]'
json_result(ts_url)['allBuilds']
end
def get_latest_build
last_build_url = @job_url + 'lastBuild/api/json?tree=number'
last_build_number = json_result(last_build_url)
last_build_number['number'].to_i
end
def get_left
latest_build - good[-1]
end
def get_right
left + good.length
end
def latest_build
@latest_build ||= get_latest_build
end
def left
@left ||= get_left
end
def right
@right ||= get_right
end
end
jobs = []
json_result(jenkins_url)['jobs'].each do |j|
next if j['name'].include?('trigger')
%w(build test release).each do |job_type|
if j['name'].include?(job_type)
jobs.push(JenkinsJob.new(j['name'], job_type, j['url'], j['name'].gsub("-#{job_type}",'')))
end
end
end
pipeline_names = []
pipeline_results = []
puts "Job Name,Job Type,Pipeline,NumBuilds,Mean Duration of Successful Run(ms),Successes,Failures,Aborts,Unstable"
jobs.each do |job|
job_stuff = BuildData.new(job.url, start_timestamp, end_timestamp)
if job_stuff.good.empty?
puts "#{job.name},No builds during the specified time period"
next
end
puts "#{job.name},#{job.type},#{job.pipeline},#{job_stuff.numbuilds},#{job_stuff.mean},#{job_stuff.successes},#{job_stuff.failures},#{job_stuff.aborts},#{job_stuff.unstables}"
pipeline_names.push(job.pipeline)
pipeline_results.push([job.pipeline,job.type,job_stuff.successes,job_stuff.mean])
end
puts "\nPipeline,Mean Successful Run Duration(ms)"
pn = pipeline_names.sort.uniq
pn.each do |name|
# Take the weighted average of the build/test/release components of the pipeline
pr = pipeline_results.find_all { |result| result[0].eql?(name) }
if pr.any? { |result| result[3].eql?('No successful build in the specified time period') }
puts "#{name},No successful pipeline runs during the specified time period"
next
end
cs = 0
qu = 0
denominator = pr.collect { |r| r[2] }.inject { |qu,r| qu + r }
if denominator == 0
puts "#{name},No successful pipeline runs during the specified time period"
next
end
numerator = pr.collect { |r| r[2] * r[3] }.inject { |cs,r| cs +r }
quotient = numerator / denominator
puts "#{name},#{quotient}"
end