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
|
function mrbSectionInit(ctx){
$('#latest-builds').on('click', '.cancel-build-btn', function(e){
e.stopImmediatePropagation();
e.preventDefault();
var url = $(this).data('request-url');
var buildReqIds = $(this).data('buildrequest-id');
libtoaster.cancelABuild(url, buildReqIds, function () {
window.location.reload();
}, null);
});
$('#latest-builds').on('click', '.rebuild-btn', function(e){
e.stopImmediatePropagation();
e.preventDefault();
var url = $(this).data('request-url');
var target = $(this).data('target');
libtoaster.startABuild(url, target, function(){
window.location.reload();
}, null);
});
// cached version of buildData, so we can determine whether a build has
// changed since it was last fetched, and update the DOM appropriately
var buildData = {};
// returns the cached version of this build, or {} is there isn't a cached one
function getCached(build) {
return buildData[build.id] || {};
}
// returns true if a build's state changed to "Succeeded", "Failed"
// or "Cancelled" from some other value
function buildFinished(build) {
var cached = getCached(build);
return cached.state &&
cached.state !== build.state &&
(build.state == 'Succeeded' || build.state == 'Failed' ||
build.state == 'Cancelled');
}
// returns true if the state changed
function stateChanged(build) {
var cached = getCached(build);
return (cached.state !== build.state);
}
// returns true if the tasks_complete_percentage changed
function tasksProgressChanged(build) {
var cached = getCached(build);
return (cached.tasks_complete_percentage !== build.tasks_complete_percentage);
}
// returns true if the number of recipes parsed/to parse changed
function recipeProgressChanged(build) {
var cached = getCached(build);
return (cached.recipes_parsed_percentage !== build.recipes_parsed_percentage);
}
function refreshMostRecentBuilds(){
libtoaster.getMostRecentBuilds(
libtoaster.ctx.mostRecentBuildsUrl,
// success callback
function (data) {
var build;
var tmpl;
var container;
var selector;
var colourClass;
var elements;
for (var i = 0; i < data.length; i++) {
build = data[i];
if (buildFinished(build)) {
// a build finished: reload the whole page so that the build
// shows up in the builds table
window.location.reload();
}
else if (stateChanged(build)) {
// update the whole template
build.warnings_pluralise = (build.warnings !== 1 ? 's' : '');
build.errors_pluralise = (build.errors !== 1 ? 's' : '');
tmpl = $.templates("#build-template");
html = $(tmpl.render(build));
selector = '[data-latest-build-result="' + build.id + '"] ' +
'[data-role="build-status-container"]';
container = $(selector);
// initialize bootstrap tooltips in the new HTML
html.find('span.glyphicon-question-sign').tooltip();
container.html(html);
}
else if (tasksProgressChanged(build)) {
// update the task progress text
selector = '#build-pc-done-' + build.id;
$(selector).html(build.tasks_complete_percentage);
// update the task progress bar
selector = '#build-pc-done-bar-' + build.id;
$(selector).width(build.tasks_complete_percentage + '%');
}
else if (recipeProgressChanged(build)) {
// update the recipe progress text
selector = '#recipes-parsed-percentage-' + build.id;
$(selector).html(build.recipes_parsed_percentage);
// update the recipe progress bar
selector = '#recipes-parsed-percentage-bar-' + build.id;
$(selector).width(build.recipes_parsed_percentage + '%');
}
buildData[build.id] = build;
}
},
// fail callback
function (data) {
console.error(data);
}
);
}
window.setInterval(refreshMostRecentBuilds, 1500);
refreshMostRecentBuilds();
}
|