updateLastRaportProgress.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. var DBG = DBG || false
  2. var DBG_FAKE_ANIM = DBG_FAKE_ANIM || false
  3. if (!URL_FETCH_BI_AUDIT_PROGRESS) throw "Missing URL_FETCH_BI_AUDIT_PROGRESS"
  4. if (!jQuery('#last-raport-state').length) return;
  5. // 'URL_BI_AUDIT_RESULT' => Router::getRoute('UrlAction_BiAuditGenerate')->getLink(''),
  6. // 'ID_ARG_BI_AUDIT_RESULT' => 'ID_BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA'
  7. // index.php?_route=UrlAction_BiAuditGenerate&ID_BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA=102
  8. var IS_STARTED = false
  9. var DBG_COUNTER = 0;
  10. function updateLastRaportView(respJson) {
  11. if (!respJson.body || !respJson.body.ids || !respJson.body.progress) return;
  12. if (!jQuery('#last-raport-state').length) return;
  13. if(DBG)console.log('DBG:updateLastRaportView', {respJson: respJson, tblStruct: tblStruct});
  14. var idx = 0;
  15. var respId = respJson.body.ids[idx]
  16. var progress = respJson.body.progress[idx]
  17. var percent = Math.round(100 * progress)
  18. if (DBG_FAKE_ANIM && percent > 0 && percent < 100) percent += (DBG_COUNTER * 10)
  19. var status = (respJson.body.statuses[idx]) ? respJson.body.statuses[idx] : ''
  20. if (DBG_FAKE_ANIM) status = (percent < 100) ? 'IN_PROGRES' : 'DONE'
  21. var statusInfo = (respJson.body.statusesInfo[idx]) ? respJson.body.statusesInfo[idx] : ''
  22. jQuery('#last-raport-state').get(0).innerHTML = '';
  23. var el = jQuery('<span style="display:inline-block; width:140px; text-align:center">' + status + '</span>');
  24. jQuery('#last-raport-state').append(el);
  25. if ('GENERATED' === status) {
  26. var resultLink = URL_BI_AUDIT_RESULT + '&' + ID_ARG_BI_AUDIT_RESULT + '=' + respId;
  27. jQuery('#last-raport-state').append('<a style="margin-left:8px; padding:6px 12px; vertical-align:top" class="btn btn-default btn-link" href="'+resultLink+'">Wyniki</a>');
  28. }
  29. jQuery('#last-raport-state').append('<span style="display:block; font-size:12px; color:#c0c0c0; line-height:16px"> '+statusInfo+' </span>');
  30. if(DBG)console.log('DBG:updateLastRaportView loop ('+idx+', el)', {el: el, progress: progress, pr: percent });
  31. updatePercentView(el, {
  32. percent: percent,
  33. status: status,
  34. })
  35. }
  36. function updatePercentView(el, props) {
  37. var hrNode = jQuery(el).find('hr')
  38. if (!hrNode.length) hrNode = jQuery('<hr>').appendTo(el)
  39. if (props.percent > 100) props.percent = 100
  40. hrNode.attr('style', "margin:0; padding:0; border-bottom:3px solid #f00; width:" + props.percent + "%")
  41. if (props.status) jQuery(el).find('span').text(props.status)
  42. }
  43. function updateLastRaportProgress() {
  44. DBG_COUNTER += 1
  45. if(DBG)console.log('updateLastRaportProgress ('+DBG_COUNTER+')');
  46. global.fetch(URL_FETCH_BI_AUDIT_PROGRESS, {
  47. method: 'POST',
  48. credentials: 'same-origin',
  49. body: JSON.stringify({
  50. ids: 'last'
  51. })
  52. }).then(function (response) {
  53. return response.json()
  54. }).then(function (respJson) {
  55. updateLastRaportView(respJson)
  56. IS_STARTED = false;
  57. startUpdateLastRaportProgress()
  58. }).catch(function (e) {
  59. if(DBG)console.warn('e:', e)
  60. IS_STARTED = false
  61. })
  62. IS_STARTED = false
  63. }
  64. function startUpdateLastRaportProgress() {
  65. if (IS_STARTED) return;
  66. IS_STARTED = true
  67. if(DBG)console.log('START updateLastRaportProgress ...');
  68. global.setTimeout(updateLastRaportProgress, 1000)
  69. }
  70. setTimeout(startUpdateLastRaportProgress, 100);