Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1 | |
| 2 | function mrbSectionInit(ctx){ |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 3 | $('#latest-builds').on('click', '.cancel-build-btn', function(e){ |
| 4 | e.stopImmediatePropagation(); |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 5 | e.preventDefault(); |
| 6 | |
| 7 | var url = $(this).data('request-url'); |
| 8 | var buildReqIds = $(this).data('buildrequest-id'); |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 9 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 10 | libtoaster.cancelABuild(url, buildReqIds, function () { |
| 11 | window.location.reload(); |
| 12 | }, null); |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 13 | }); |
| 14 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 15 | $('#latest-builds').on('click', '.rebuild-btn', function(e){ |
| 16 | e.stopImmediatePropagation(); |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 17 | 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 27 | // 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 30 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 31 | // 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 35 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 36 | // 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 45 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 46 | // returns true if the state changed |
| 47 | function stateChanged(build) { |
| 48 | var cached = getCached(build); |
| 49 | return (cached.state !== build.state); |
| 50 | } |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 51 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 52 | // 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 57 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 58 | // 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 63 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 64 | // 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 70 | function refreshMostRecentBuilds(){ |
| 71 | libtoaster.getMostRecentBuilds( |
| 72 | libtoaster.ctx.mostRecentBuildsUrl, |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 73 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 74 | // 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 Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 89 | window.location.reload(true); |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 90 | } |
| 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 Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 109 | 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 Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 113 | selector = '#repos-cloned-progressitem-' + build.id; |
| 114 | $(selector).html('('+build.progress_item+')'); |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 115 | |
| 116 | // update the recipe progress bar |
| 117 | selector = '#repos-cloned-percentage-bar-' + build.id; |
| 118 | $(selector).width(build.repos_cloned_percentage + '%'); |
| 119 | } |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 120 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 137 | } |
| 138 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 139 | buildData[build.id] = build; |
| 140 | } |
| 141 | }, |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 142 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 143 | // fail callback |
| 144 | function (data) { |
| 145 | console.error(data); |
| 146 | } |
| 147 | ); |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 148 | } |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 149 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 150 | window.setInterval(refreshMostRecentBuilds, 1500); |
| 151 | refreshMostRecentBuilds(); |
| 152 | } |