-
Notifications
You must be signed in to change notification settings - Fork 6
/
builds.js
349 lines (331 loc) · 11.6 KB
/
builds.js
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
const utils = require('./utils')
const fetch = require('node-fetch');
function getApps(all_apps, api_key, next, callback) {
return fetch(utils.BASE_URL + (next ? '?next=' + next : ''), utils.getHeaders(api_key))
.then(res => res.json())
.then((apps) => {
apps.data.forEach((app)=>{
all_apps[app.slug] = {
app
};
})
if(apps.paging.next){
getApps(all_apps, api_key, apps.paging.next, callback)
} else {
callback(all_apps);
}
});
}
function getBuilds(api_key, all_builds, from, to, appSlug, next, callback) {
let fromDate = new Date(from);
let toDate = new Date(to);
let fromTimestamp = parseInt(fromDate.getTime()/1000);
let toTimestamp = parseInt(toDate.getTime()/1000);
let nextStr = next ? '?after='+fromTimestamp+'&before='+toTimestamp+'&next='+next : '?after='+fromTimestamp+'&before='+toTimestamp;
let url = utils.BASE_URL+'/'+appSlug+'/builds'+nextStr;
return fetch(url, utils.getHeaders(api_key))
.then(res => res.json())
.then((builds) => {
if(!builds.paging){
console.log('Error:', builds);
}
all_builds = all_builds.concat(builds.data)
if(builds.paging.next){
getBuilds(api_key, all_builds, from, to, appSlug, builds.paging.next, callback)
} else {
callback(all_builds);
}
});
}
module.exports = {
getAllData: (appSlugsFilter, api_key, from, to, callback) => {
let all_apps = {};
getApps(all_apps, api_key, null, (apps) => {
let appSlugs = Object.keys(apps);
let complete = 0;
appSlugs.forEach((appSlug)=>{
if(!appSlugsFilter || appSlugsFilter.indexOf(appSlug) != -1){
all_apps[appSlug] = apps[appSlug];
let all_builds = [];
getBuilds(api_key, all_builds, from, to, appSlug, 0, (builds) => {
complete++;
all_apps[appSlug].builds = builds;
if(complete == appSlugs.length || (appSlugsFilter && complete == appSlugsFilter.length)){
callback(all_apps);
}
})
}
});
});
},
getBuildTableData: (appSlugsFilter, data) => {
let table_data = [];
let appSlugs = Object.keys(data);
appSlugs.forEach((appSlug) => {
if(!appSlugsFilter || appSlugsFilter.indexOf(appSlug) != -1){
let app = data[appSlug];
app.builds.forEach((build) => {
let now = new Date();
let triggered_at = new Date(build.triggered_at);
let queue_duration = 0;
if(build.started_on_worker_at){
let started_on_worker_at = new Date(build.started_on_worker_at);
queue_duration = (started_on_worker_at.getTime() - triggered_at.getTime()) / 60000;
}else{
queue_duration = (now.getTime() - triggered_at.getTime()) / 60000;
}
if(build.status_text == 'in-progress' || !build.started_on_worker_at){
duration = (now.getTime() - triggered_at.getTime()) / 60000;
} else {
let finished_at = new Date(build.finished_at);
let started_on_worker_at = new Date(build.started_on_worker_at);
duration = (finished_at.getTime() - started_on_worker_at.getTime()) / 60000;
}
let name = app.app.title;
let buildNum = parseInt(build.build_number);
let workflow = build.triggered_workflow;
let stack = build.stack_identifier;
let branch = build.branch;
let status = build.status_text;
let slug = build.slug;
table_data.push([
triggered_at,
'<a href="https://app.bitrise.io/app/'+appSlug+'#/builds" target="_blank">'+name+'</a>',
'<a href="https://app.bitrise.io/build/'+slug+'#?tab=log" target="_blank">'+buildNum+'</a>',
'<a href="https://app.bitrise.io/app/'+appSlug+'/workflow_editor#!/workflows?workflow_id='+workflow+'" target="_blank">'+workflow+'</a>',
stack,
branch,
status,
duration.toFixed(1) + ' mins',
queue_duration.toFixed(1) + ' mins'
])
})
}
})
table_data.sort((a,b) => b[0].getTime() - a[0].getTime())
return [
{
"type":"table",
"columns":[
{
"text":"Started At",
"type":"time"
},
{
"text":"App",
"type":"string"
},
{
"text":"Build #",
"type":"int"
},
{
"text":"Workflow",
"type":"string"
},
{
"text":"Stack",
"type":"string"
},
{
"text":"Branch",
"type":"string"
},
{
"text":"Status",
"type":"string"
},
{
"text":"Duration",
"type":"string"
},
{
"text":"Queued",
"type":"string"
}
],
"rows": table_data
}
];
},
getStatsTableData: (appSlugsFilter, data) => {
let table_data = [];
let appSlugs = Object.keys(data);
let stats = {};
appSlugs.forEach((appSlug) => {
if(!appSlugsFilter || appSlugsFilter.indexOf(appSlug) != -1){
let app = data[appSlug];
app.builds.forEach((build) => {
if(build.status_text != 'in-progress' && build.started_on_worker_at){
let now = new Date();
let triggered_at = new Date(build.triggered_at);
let started_on_worker_at = new Date(build.started_on_worker_at);
let queue_duration = (started_on_worker_at.getTime() - triggered_at.getTime()) / 60000;
let finished_at = new Date(build.finished_at);
let duration = (finished_at.getTime() - started_on_worker_at.getTime()) / 60000;
let name = app.app.title;
let workflow = build.triggered_workflow;
let stack = build.stack_identifier;
let branch = build.branch;
let status = build.status_text;
let slug = build.slug;
// Group Everything except master && develop into other group
// if (branch != 'master' && branch != 'develop') {
// branch = 'other';
// }
let key = appSlug + '_' + workflow + '_' + branch + '_' + stack + '_' + status;
if (!stats[key]) {
stats[key] = {};
}
stats[key].app = name;
stats[key].workflow = workflow;
stats[key].stack = stack;
stats[key].branch = branch;
stats[key].status = status;
stats[key].count = stats[key].count ? stats[key].count + 1 : 1;
stats[key].total = stats[key].total ? stats[key].total + duration : duration;
stats[key].queue_total = stats[key].queue_total ? stats[key].queue_total + queue_duration : queue_duration;
let allKey = 'All_' + status;
if (!stats[allKey]) {
stats[allKey] = {};
}
stats[allKey].app = 'All';
stats[allKey].workflow = 'All';
stats[allKey].stack = 'All';
stats[allKey].branch = 'All';
stats[allKey].status = status;
stats[allKey].count = stats[allKey].count ? stats[allKey].count + 1 : 1;
stats[allKey].total = stats[allKey].total ? stats[allKey].total + duration : duration;
stats[allKey].queue_total = stats[allKey].queue_total ? stats[allKey].queue_total + queue_duration : queue_duration;
}
})
}
})
let allKeys = Object.keys(stats).filter((key) => key.indexOf('All_') == 0);
allKeys.forEach((key) => {
let average = stats[key].total / stats[key].count;
let averageQueue = stats[key].queue_total / stats[key].count;
table_data.push([
stats[key].app,
stats[key].workflow,
stats[key].stack,
stats[key].branch,
stats[key].status,
stats[key].count,
average.toFixed(2),
averageQueue.toFixed(2)
])
});
let keys = Object.keys(stats);
keys.forEach((key) => {
if (stats[key].app != 'All') {
let average = stats[key].total / stats[key].count;
let averageQueue = stats[key].queue_total / stats[key].count;
table_data.push([
stats[key].app,
stats[key].workflow,
stats[key].stack,
stats[key].branch,
stats[key].status,
stats[key].count,
average.toFixed(2),
averageQueue.toFixed(2)
])
}
});
// table_data.sort((a,b) => b[0].getTime() - a[0].getTime())
return [
{
"type":"table",
"columns":[
{
"text":"App",
"type":"string"
},
{
"text":"Workflow",
"type":"string"
},
{
"text":"Stack",
"type":"string"
},
{
"text":"Branch",
"type":"string"
},
{
"text":"Status",
"type":"string"
},
{
"text":"Count",
"type":"int"
},
{
"text":"Avg Build Time",
"type":"string"
},
{
"text":"Avg Queue Time",
"type":"string"
}
],
"rows": table_data
}
];
},
getBuildTimeseriesData: (appSlugsFilter, data) => {
let timeseries_data = [];
let appSlugs = Object.keys(data);
appSlugs.forEach((appSlug) => {
if(!appSlugsFilter || appSlugsFilter.indexOf(appSlug) != -1){
let buildDurations = [];
let app = data[appSlug];
app.builds.forEach((build) => {
if((build.status_text == 'success' || build.status_text == 'error') && build.started_on_worker_at){
let started_on_worker_at = new Date(build.started_on_worker_at);
let finished_at = new Date(build.finished_at);
let build_duration = (finished_at.getTime() - started_on_worker_at.getTime()) / 60000;
buildDurations.push([build_duration.toFixed(1),(finished_at.getTime()), 'ABC'])
}
})
buildDurations.sort((a, b)=> b[1] - a[1]);
if(buildDurations.length > 0){
timeseries_data.push({
target: app.app.title,
datapoints: buildDurations
})
}
}
})
return timeseries_data;
},
getQueueTimeseriesData: (appSlugsFilter, data) => {
let timeseries_data = [];
let appSlugs = Object.keys(data);
appSlugs.forEach((appSlug) => {
if(!appSlugsFilter || appSlugsFilter.indexOf(appSlug) != -1){
let buildDurations = [];
let app = data[appSlug];
app.builds.forEach((build) => {
if((build.status_text == 'success' || build.status_text == 'error') && build.started_on_worker_at){
let triggered_at = new Date(build.triggered_at);
let started_on_worker_at = new Date(build.started_on_worker_at);
let finished_at = new Date(build.finished_at);
let queue_duration = (started_on_worker_at.getTime() - triggered_at.getTime()) / 60000;
buildDurations.push([queue_duration.toFixed(1),(finished_at.getTime())])
}
})
buildDurations.sort((a, b)=> b[1] - a[1]);
if(buildDurations.length > 0){
timeseries_data.push({
target: app.app.title,
datapoints: buildDurations
})
}
}
})
return timeseries_data;
}
}