Koha/koha-tmpl/intranet-tmpl/prog/js/job_progess.js
Kyle Hall d2e4a6ec7d
Bug 31666: Add job progress bar to stage-marc-import.pl
It would be nice if we had progress bars to indicate the progress of background jobs for scripts that utilize them.

This patch implements a reusable bootstrap based progess bar.

Test Plan:
1) Apply this patch
2) Stage a marc batch ( preferrably a large one to show the progress updating )
3) Note the new progess bar, verify it functions correctly.

Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
2022-10-05 16:08:11 -03:00

34 lines
1.6 KiB
JavaScript

function updateProgress(job_id) {
$.getJSON('/api/v1/jobs/' + job_id, function(job){
let recheck = true;
if ( job.status == "new" ) {
$(`#job-percent-${job_id}`).text(0);
$(`#job-status-${job_id}`).text(JOB_PROGRESS_NOT_STARTED);
$(`#progress-bar-${job_id}`).attr('aria-valuenow', 0).css("width", "100%");
} else if ( job.status == "started" ) {
const progress = job["progress"];
const size = job["size"];
const percent = progress > 0 ? ( progress / size ) * 100 : 0;
$(`#job-percent-${job_id}`).text(percent.toFixed(2));
$(`#job-status-${job_id}`).text(JOB_PROGRESS_STARTED);
$(`#progress-bar-${job_id}`).attr('aria-valuenow', percent).css("width", `${percent}%`);
} else if ( job.status == "finished" ) {
$(`#job-percent-${job_id}`).text(100);
$(`#job-status-${job_id}`).text(JOB_PROGRESS_FINISHED);
$(`#progress-bar-${job_id}`).addClass("progress-bar-success");
$(`#progress-bar-${job_id}`).attr('aria-valuenow', 100).css("width", "100%");
recheck = false;
} else if ( job.status == "failed" ) {
$(`#job-percent-${job_id}`).text(0);
$(`#job-status-${job_id}`).text(JOB_PROGRESS_FAILED);
$(`#progress-bar-${job_id}`).addClass("progress-bar-danger");
$(`#progress-bar-${job_id}`).attr('aria-valuenow', 0).css("width", "100%");
recheck = false;
}
if ( recheck ) {
setTimeout(function(){updateProgress(job_id)}, 1 * 1000);
}
});
}