aboutsummaryrefslogtreecommitdiffstats
path: root/lib/srtgui/static/js/mrjsection.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/srtgui/static/js/mrjsection.js')
-rwxr-xr-xlib/srtgui/static/js/mrjsection.js131
1 files changed, 131 insertions, 0 deletions
diff --git a/lib/srtgui/static/js/mrjsection.js b/lib/srtgui/static/js/mrjsection.js
new file mode 100755
index 00000000..800f0e6f
--- /dev/null
+++ b/lib/srtgui/static/js/mrjsection.js
@@ -0,0 +1,131 @@
+
+function mrjSectionInit(ctx){
+ $('#latest-jobs').on('click', '.cancel-job-btn', function(e){
+ e.stopImmediatePropagation();
+ e.preventDefault();
+
+ var url = $(this).data('request-url');
+ var jobReqIds = $(this).data('jobrequest-id');
+
+ libtoaster.cancelAJob(url, jobReqIds, function () {
+ alert("CANCEL JOB");
+ window.location.reload();
+ }, null);
+ });
+
+ // cached version of jobData, so we can determine whether a job has
+ // changed since it was last fetched, and update the DOM appropriately
+ var jobData = {};
+
+ // returns the cached version of this job, or {} is there isn't a cached one
+ function getCached(job) {
+ return jobData[job.id] || {};
+ }
+
+ // returns true if a job's state changed to "Success", "Errors"
+ // or "Cancelled" from some other value
+ function jobFinished(job) {
+ var cached = getCached(job);
+ return cached.state &&
+ cached.state !== job.state &&
+ (job.state == 'Success' || job.state == 'Errors' ||
+ job.state == 'Cancelled');
+ }
+
+ // returns true if the state changed
+ function stateChanged(job) {
+ var cached = getCached(job);
+ return (cached.state !== job.state);
+ }
+
+ // returns true if the tasks_complete_percentage changed
+ function tasksProgressChanged(job) {
+ var cached = getCached(job);
+ var a = cached.tasks_complete_percentage;
+ var b = job.tasks_complete_percentage;
+ var c = cached.tasks_complete_percentage !== job.tasks_complete_percentage;
+ return (cached.tasks_complete_percentage !== job.tasks_complete_percentage);
+ }
+
+ // Auto-refresh 1500 ms AFTER its last successful refresh, to avoid refresh race conditions
+ function refreshMostRecentJobs(){
+ libtoaster.getMostRecentJobs(
+ libtoaster.ctx.mostRecentJobsUrl,
+
+ // success callback
+ function (data) {
+ var job;
+ var tmpl;
+ var container;
+ var selector;
+ var colourClass;
+ var elements;
+
+ for (var i = 0; i < data.length; i++) {
+ job = data[i];
+
+ var jobEle = document.getElementById("job-instance-"+job.id);
+ if (null == jobEle) {
+ // Job's display instance does not exist, so force refresh of page's Job MRU
+ // DISABLE THESE LINES TO Avoid a race condition loop
+// alert("NO JOB");
+ setTimeout(() => { console.log("NO_JOB_YET_DELAY!"); }, 2000);
+ window.location.reload();
+ return;
+ }
+ else if (jobFinished(job)) {
+ // a job finished: reload the whole page so that the job
+ // shows up in the jobs table
+// alert("DONE JOB");
+ window.location.reload();
+ return;
+ }
+ else if (stateChanged(job)) {
+ // update the whole template
+ job.warnings_pluralise = (job.warnings !== 1 ? 's' : '');
+ job.errors_pluralise = (job.errors !== 1 ? 's' : '');
+
+ tmpl = $.templates("#job-template");
+
+ html = $(tmpl.render(job));
+
+ selector = '[data-latest-job-result="' + job.id + '"] ' +
+ '[data-role="job-status-container"]';
+ container = $(selector);
+
+ // initialize bootstrap tooltips in the new HTML
+ html.find('span.glyphicon-question-sign').tooltip();
+
+ container.html(html);
+ }
+ else if (tasksProgressChanged(job)) {
+ // update the task progress text
+ selector = '#job-pc-done-' + job.id;
+ $(selector).html(job.tasks_complete_percentage);
+ selector = '#job-message-done-' + job.id;
+ $(selector).html(job.targets);
+
+ // update the task progress bar
+ selector = '#job-pc-done-bar-' + job.id;
+ $(selector).width(job.tasks_complete_percentage + '%');
+ }
+
+ jobData[job.id] = job;
+ }
+ },
+
+ // fail callback
+ function (data) {
+ console.error(data);
+ }
+ );
+ window.setTimeout(refreshMostRecentJobs, 1500);
+ var msg = "REFRESH:"+Date.now();
+ console.log(msg);
+ }
+
+ // window.setInterval(refreshMostRecentJobs, 1500);
+
+ // Self refresh every 1500 ms
+ refreshMostRecentJobs();
+}