2016-08-13 14:51:01 -05:00
|
|
|
/*
|
|
|
|
* status.js
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-12-23 00:02:45 -06:00
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
2016-08-13 14:51:01 -05:00
|
|
|
*/
|
|
|
|
|
2017-01-02 03:34:01 -06:00
|
|
|
/** global: jobImportUrl, langImportSingleError, langImportMultiError, jobStartUrl, langImportTimeOutError, langImportFinished, langImportFatalError */
|
|
|
|
|
2017-06-10 08:09:41 -05:00
|
|
|
var timeOutId;
|
2017-06-23 22:49:33 -05:00
|
|
|
var startInterval = 1000;
|
2016-08-13 14:51:01 -05:00
|
|
|
var interval = 500;
|
2017-06-22 14:50:10 -05:00
|
|
|
|
2017-06-23 22:49:33 -05:00
|
|
|
// these vars are used to detect a stalled job:
|
|
|
|
var numberOfSteps = 0;
|
|
|
|
var numberOfReports = 0;
|
|
|
|
var jobFailed = false;
|
|
|
|
|
|
|
|
// counts how many errors have been detected
|
|
|
|
var knownErrors = 0;
|
2017-06-22 14:50:10 -05:00
|
|
|
|
2016-08-13 14:51:01 -05:00
|
|
|
$(function () {
|
|
|
|
"use strict";
|
2017-06-10 08:09:41 -05:00
|
|
|
timeOutId = setTimeout(checkImportStatus, startInterval);
|
|
|
|
$('.start-job').click(startJob);
|
2016-08-13 14:51:01 -05:00
|
|
|
});
|
|
|
|
|
2017-06-23 22:49:33 -05:00
|
|
|
/**
|
|
|
|
* Downloads some JSON and responds to its content to see what the status is of the current import.
|
|
|
|
*/
|
2016-08-13 14:51:01 -05:00
|
|
|
function checkImportStatus() {
|
2016-09-16 06:29:56 -05:00
|
|
|
$.getJSON(jobImportUrl).done(reportOnJobImport).fail(failedJobImport);
|
2016-08-13 14:51:01 -05:00
|
|
|
}
|
|
|
|
|
2017-06-23 22:49:33 -05:00
|
|
|
/**
|
|
|
|
* This method is called when the JSON query returns an error. If possible, this error is relayed to the user.
|
|
|
|
*/
|
|
|
|
function failedJobImport(jqxhr, textStatus, error) {
|
|
|
|
// hide all possible boxes:
|
|
|
|
$('.statusbox').hide();
|
2017-06-22 14:50:10 -05:00
|
|
|
|
2017-06-23 22:49:33 -05:00
|
|
|
// fill in some details:
|
|
|
|
var errorMessage = textStatus + " " + error;
|
|
|
|
|
2017-06-23 23:57:24 -05:00
|
|
|
$('.fatal_error_txt').text(errorMessage);
|
2017-06-23 22:49:33 -05:00
|
|
|
|
2017-06-23 23:57:24 -05:00
|
|
|
// show the fatal error box:
|
|
|
|
$('.fatal_error').show();
|
2017-06-22 14:50:10 -05:00
|
|
|
}
|
|
|
|
|
2017-06-23 22:49:33 -05:00
|
|
|
/**
|
|
|
|
* This method is called when the job enquiry (JSON) returns some info.
|
|
|
|
* It also decides whether or not to check again.
|
|
|
|
*
|
|
|
|
* @param data
|
|
|
|
*/
|
2017-06-10 08:09:41 -05:00
|
|
|
function reportOnJobImport(data) {
|
2017-06-23 22:49:33 -05:00
|
|
|
|
|
|
|
switch (data.status) {
|
2017-07-23 01:16:11 -05:00
|
|
|
default:
|
|
|
|
break;
|
2017-06-23 22:49:33 -05:00
|
|
|
case "configured":
|
|
|
|
// job is ready. Do not check again, just show the start-box. Hide the rest.
|
|
|
|
$('.statusbox').hide();
|
|
|
|
$('.status_configured').show();
|
|
|
|
break;
|
|
|
|
case "running":
|
|
|
|
// job is running! Show the running box:
|
|
|
|
$('.statusbox').hide();
|
|
|
|
$('.status_running').show();
|
|
|
|
|
|
|
|
// update the bar
|
|
|
|
updateBar(data);
|
|
|
|
|
|
|
|
// update the status text:
|
|
|
|
updateStatusText(data);
|
|
|
|
|
|
|
|
// report on detected errors:
|
|
|
|
reportOnErrors(data);
|
|
|
|
|
|
|
|
if (jobIsStalled(data)) {
|
|
|
|
// do something
|
|
|
|
showStalledBox();
|
|
|
|
} else {
|
|
|
|
// check again in 500ms
|
|
|
|
timeOutId = setTimeout(checkImportStatus, interval);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "finished":
|
|
|
|
$('.statusbox').hide();
|
|
|
|
$('.status_finished').show();
|
|
|
|
// show text:
|
2017-06-23 23:57:24 -05:00
|
|
|
$('#import-status-more-info').html(data.finishedText);
|
2017-06-23 22:49:33 -05:00
|
|
|
|
|
|
|
|
|
|
|
break;
|
2017-06-22 14:50:10 -05:00
|
|
|
}
|
2017-06-23 22:49:33 -05:00
|
|
|
}
|
2017-06-22 14:50:10 -05:00
|
|
|
|
2017-06-23 22:49:33 -05:00
|
|
|
/**
|
2017-06-23 23:57:24 -05:00
|
|
|
* Shows a fatal error when the job seems to be stalled.
|
2017-06-23 22:49:33 -05:00
|
|
|
*/
|
|
|
|
function showStalledBox() {
|
|
|
|
$('.statusbox').hide();
|
2017-06-23 23:57:24 -05:00
|
|
|
$('.fatal_error').show();
|
|
|
|
$('.fatal_error_txt').text(langImportTimeOutError);
|
2017-06-23 22:49:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detects if a job is frozen.
|
|
|
|
*
|
|
|
|
* @param data
|
|
|
|
*/
|
|
|
|
function jobIsStalled(data) {
|
2017-06-24 01:37:09 -05:00
|
|
|
if (data.done === numberOfSteps) {
|
2017-06-23 22:49:33 -05:00
|
|
|
numberOfReports++;
|
2017-06-22 14:50:10 -05:00
|
|
|
}
|
2017-06-23 22:49:33 -05:00
|
|
|
if (data.done !== numberOfSteps) {
|
|
|
|
numberOfReports = 0;
|
2017-06-22 14:50:10 -05:00
|
|
|
}
|
2017-06-23 22:49:33 -05:00
|
|
|
if (numberOfReports > 20) {
|
|
|
|
return true;
|
2017-06-22 14:50:10 -05:00
|
|
|
}
|
2017-06-23 22:49:33 -05:00
|
|
|
numberOfSteps = data.done;
|
2017-06-10 08:09:41 -05:00
|
|
|
|
2017-06-23 22:49:33 -05:00
|
|
|
return false;
|
2017-06-10 08:09:41 -05:00
|
|
|
}
|
|
|
|
|
2017-06-23 22:49:33 -05:00
|
|
|
/**
|
|
|
|
* This function tells Firefly start the job. It will also initialize a re-check in 500ms time.
|
|
|
|
*/
|
|
|
|
function startJob() {
|
|
|
|
// disable the button, add loading thing.
|
|
|
|
$('.start-job').prop('disabled', true).text('...');
|
|
|
|
$.post(jobStartUrl).fail(reportOnSubmitError);
|
2017-06-10 08:09:41 -05:00
|
|
|
|
2017-06-23 22:49:33 -05:00
|
|
|
// check status, every 500 ms.
|
|
|
|
timeOutId = setTimeout(checkImportStatus, startInterval);
|
|
|
|
}
|
2017-06-10 08:09:41 -05:00
|
|
|
|
2017-06-23 22:49:33 -05:00
|
|
|
function reportOnSubmitError() {
|
|
|
|
// stop the refresh thing
|
|
|
|
clearTimeout(timeOutId);
|
2017-06-10 08:09:41 -05:00
|
|
|
|
2017-06-23 22:49:33 -05:00
|
|
|
// hide all possible boxes:
|
|
|
|
$('.statusbox').hide();
|
2017-06-10 08:09:41 -05:00
|
|
|
|
2017-06-23 22:49:33 -05:00
|
|
|
// fill in some details:
|
2017-06-23 23:57:24 -05:00
|
|
|
var errorMessage = "Time out while waiting for job to finish.";
|
2017-06-10 08:09:41 -05:00
|
|
|
|
2017-06-23 23:57:24 -05:00
|
|
|
$('.fatal_error_txt').text(errorMessage);
|
2017-06-10 08:09:41 -05:00
|
|
|
|
2017-06-23 23:57:24 -05:00
|
|
|
// show the fatal error box:
|
|
|
|
$('.fatal_error').show();
|
2017-06-23 22:49:33 -05:00
|
|
|
jobFailed = true;
|
2017-06-10 08:09:41 -05:00
|
|
|
|
2016-08-13 16:28:01 -05:00
|
|
|
}
|
2016-08-13 14:51:01 -05:00
|
|
|
|
2017-06-23 22:49:33 -05:00
|
|
|
/**
|
|
|
|
* This method updates the percentage bar thing if the job is running!
|
|
|
|
*/
|
2016-08-13 16:28:01 -05:00
|
|
|
function updateBar(data) {
|
2016-08-13 14:51:01 -05:00
|
|
|
var bar = $('#import-status-bar');
|
2017-06-23 23:57:24 -05:00
|
|
|
if (data.show_percentage) {
|
2016-08-13 14:51:01 -05:00
|
|
|
bar.addClass('progress-bar-success').removeClass('progress-bar-info');
|
|
|
|
bar.attr('aria-valuenow', data.percentage);
|
|
|
|
bar.css('width', data.percentage + '%');
|
2017-06-23 22:49:33 -05:00
|
|
|
$('#import-status-bar').text(data.done + '/' + data.steps);
|
|
|
|
return true;
|
2016-08-13 16:28:01 -05:00
|
|
|
}
|
|
|
|
// dont show percentage:
|
|
|
|
bar.removeClass('progress-bar-success').addClass('progress-bar-info');
|
|
|
|
bar.attr('aria-valuenow', 100);
|
|
|
|
bar.css('width', '100%');
|
2017-07-23 01:16:11 -05:00
|
|
|
return true;
|
2016-08-13 16:28:01 -05:00
|
|
|
}
|
2017-06-23 22:49:33 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add text with current import status.
|
|
|
|
* @param data
|
|
|
|
*/
|
|
|
|
function updateStatusText(data) {
|
2016-08-13 16:28:01 -05:00
|
|
|
"use strict";
|
2016-08-13 14:51:01 -05:00
|
|
|
$('#import-status-txt').removeClass('text-danger').text(data.statusText);
|
2016-08-13 16:28:01 -05:00
|
|
|
}
|
2017-06-23 22:49:33 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Report on errors found in import:
|
|
|
|
* @param data
|
|
|
|
*/
|
|
|
|
function reportOnErrors(data) {
|
|
|
|
if (knownErrors === data.errors.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (data.errors.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.errors.length === 1) {
|
|
|
|
$('#import-status-error-intro').text(langImportSingleError);
|
|
|
|
//'An error has occured during the import. The import can continue, however.'
|
|
|
|
}
|
|
|
|
if (data.errors.length > 1) {
|
|
|
|
// 'Errors have occured during the import. The import can continue, however.'
|
|
|
|
$('#import-status-error-intro').text(langImportMultiError);
|
|
|
|
}
|
|
|
|
$('.info_errors').show();
|
|
|
|
// fill the list with error texts
|
|
|
|
$('#import-status-error-list').empty();
|
|
|
|
for (var i = 0; i < data.errors.length; i++) {
|
|
|
|
var errorSet = data.errors[i];
|
|
|
|
for (var j = 0; j < errorSet.length; j++) {
|
|
|
|
var item = $('<li>').html(errorSet[j]);
|
|
|
|
$('#import-status-error-list').append(item);
|
|
|
|
}
|
|
|
|
}
|
2017-06-24 06:03:09 -05:00
|
|
|
}
|