blob: f07ccf8181c4e194dc3a559a2da38011bdc898e6 [file] [log] [blame]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001
2function mrbSectionInit(ctx){
Patrick Williamsc0f7c042017-02-23 20:41:17 -06003 $('#latest-builds').on('click', '.cancel-build-btn', function(e){
4 e.stopImmediatePropagation();
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05005 e.preventDefault();
6
7 var url = $(this).data('request-url');
8 var buildReqIds = $(this).data('buildrequest-id');
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05009
Patrick Williamsc0f7c042017-02-23 20:41:17 -060010 libtoaster.cancelABuild(url, buildReqIds, function () {
11 window.location.reload();
12 }, null);
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050013 });
14
Patrick Williamsc0f7c042017-02-23 20:41:17 -060015 $('#latest-builds').on('click', '.rebuild-btn', function(e){
16 e.stopImmediatePropagation();
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050017 e.preventDefault();
18
19 var url = $(this).data('request-url');
20 var target = $(this).data('target');
21
22 libtoaster.startABuild(url, target, function(){
23 window.location.reload();
24 }, null);
25 });
26
Patrick Williamsc0f7c042017-02-23 20:41:17 -060027 // cached version of buildData, so we can determine whether a build has
28 // changed since it was last fetched, and update the DOM appropriately
29 var buildData = {};
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050030
Patrick Williamsc0f7c042017-02-23 20:41:17 -060031 // returns the cached version of this build, or {} is there isn't a cached one
32 function getCached(build) {
33 return buildData[build.id] || {};
34 }
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050035
Patrick Williamsc0f7c042017-02-23 20:41:17 -060036 // returns true if a build's state changed to "Succeeded", "Failed"
37 // or "Cancelled" from some other value
38 function buildFinished(build) {
39 var cached = getCached(build);
40 return cached.state &&
41 cached.state !== build.state &&
42 (build.state == 'Succeeded' || build.state == 'Failed' ||
43 build.state == 'Cancelled');
44 }
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050045
Patrick Williamsc0f7c042017-02-23 20:41:17 -060046 // returns true if the state changed
47 function stateChanged(build) {
48 var cached = getCached(build);
49 return (cached.state !== build.state);
50 }
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050051
Patrick Williamsc0f7c042017-02-23 20:41:17 -060052 // returns true if the tasks_complete_percentage changed
53 function tasksProgressChanged(build) {
54 var cached = getCached(build);
55 return (cached.tasks_complete_percentage !== build.tasks_complete_percentage);
56 }
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050057
Patrick Williamsc0f7c042017-02-23 20:41:17 -060058 // returns true if the number of recipes parsed/to parse changed
59 function recipeProgressChanged(build) {
60 var cached = getCached(build);
61 return (cached.recipes_parsed_percentage !== build.recipes_parsed_percentage);
62 }
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050063
Brad Bishopd7bf8c12018-02-25 22:55:05 -050064 // returns true if the number of repos cloned/to clone changed
65 function cloneProgressChanged(build) {
66 var cached = getCached(build);
67 return (cached.repos_cloned_percentage !== build.repos_cloned_percentage);
68 }
69
Patrick Williamsc0f7c042017-02-23 20:41:17 -060070 function refreshMostRecentBuilds(){
71 libtoaster.getMostRecentBuilds(
72 libtoaster.ctx.mostRecentBuildsUrl,
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050073
Patrick Williamsc0f7c042017-02-23 20:41:17 -060074 // success callback
75 function (data) {
76 var build;
77 var tmpl;
78 var container;
79 var selector;
80 var colourClass;
81 var elements;
82
83 for (var i = 0; i < data.length; i++) {
84 build = data[i];
85
86 if (buildFinished(build)) {
87 // a build finished: reload the whole page so that the build
88 // shows up in the builds table
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080089 window.location.reload(true);
Patrick Williamsc0f7c042017-02-23 20:41:17 -060090 }
91 else if (stateChanged(build)) {
92 // update the whole template
93 build.warnings_pluralise = (build.warnings !== 1 ? 's' : '');
94 build.errors_pluralise = (build.errors !== 1 ? 's' : '');
95
96 tmpl = $.templates("#build-template");
97
98 html = $(tmpl.render(build));
99
100 selector = '[data-latest-build-result="' + build.id + '"] ' +
101 '[data-role="build-status-container"]';
102 container = $(selector);
103
104 // initialize bootstrap tooltips in the new HTML
105 html.find('span.glyphicon-question-sign').tooltip();
106
107 container.html(html);
108 }
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500109 else if (cloneProgressChanged(build)) {
110 // update the clone progress text
111 selector = '#repos-cloned-percentage-' + build.id;
112 $(selector).html(build.repos_cloned_percentage);
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800113 selector = '#repos-cloned-progressitem-' + build.id;
114 $(selector).html('('+build.progress_item+')');
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500115
116 // update the recipe progress bar
117 selector = '#repos-cloned-percentage-bar-' + build.id;
118 $(selector).width(build.repos_cloned_percentage + '%');
119 }
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600120 else if (tasksProgressChanged(build)) {
121 // update the task progress text
122 selector = '#build-pc-done-' + build.id;
123 $(selector).html(build.tasks_complete_percentage);
124
125 // update the task progress bar
126 selector = '#build-pc-done-bar-' + build.id;
127 $(selector).width(build.tasks_complete_percentage + '%');
128 }
129 else if (recipeProgressChanged(build)) {
130 // update the recipe progress text
131 selector = '#recipes-parsed-percentage-' + build.id;
132 $(selector).html(build.recipes_parsed_percentage);
133
134 // update the recipe progress bar
135 selector = '#recipes-parsed-percentage-bar-' + build.id;
136 $(selector).width(build.recipes_parsed_percentage + '%');
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500137 }
138
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600139 buildData[build.id] = build;
140 }
141 },
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500142
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600143 // fail callback
144 function (data) {
145 console.error(data);
146 }
147 );
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500148 }
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500149
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600150 window.setInterval(refreshMostRecentBuilds, 1500);
151 refreshMostRecentBuilds();
152}