updateRaportProgress.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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('#BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA').length) return;
  5. var IS_STARTED = false
  6. var DBG_COUNTER = 0;
  7. function getTableAjaxStruct() {
  8. var tblNode = jQuery('#BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA > table.AjaxTable')
  9. var fieldCells = tblNode.find('> thead > tr.tblAjax__head__sort > th > span')
  10. var idCells = tblNode.find('> tbody > tr > td:nth-child(3)')
  11. // if(DBG)console.log('updateRaportProgress ('+DBG_COUNTER+') idCells: ', idCells);
  12. if (!idCells.length || !fieldCells.length) {
  13. return null;
  14. }
  15. // FILE_STATUS -> 'IN_PROGRESS'
  16. // FILE_STATUS_info -> 'W trakcie generowania powiązań'
  17. var fields = fieldCells.toArray().map(function (el) {
  18. // if(DBG)console.log('updateRaportProgress ('+DBG_COUNTER+') fields loop ('+idx+'): el', el.title);
  19. if (!el.title) return '';
  20. var title = el.title
  21. var fieldName = ''
  22. if (')' === title.substr(-1)) {
  23. var pos = title.lastIndexOf('(')
  24. if (-1 !== pos) {
  25. fieldName = title.substring(pos + 1, title.length - 1)
  26. }
  27. }
  28. return fieldName;
  29. })
  30. if(DBG)console.log('getTableAjaxStruct ('+DBG_COUNTER+') fields: ', fields);
  31. var fieldStatusIdx = fields.indexOf('FILE_STATUS');
  32. var fieldStatusInfoIdx = fields.indexOf('FILE_STATUS_info');
  33. if(DBG)console.log('getTableAjaxStruct ('+DBG_COUNTER+') idx('+fieldStatusIdx+','+fieldStatusInfoIdx+')');
  34. var ids = idCells.toArray().map(function (el) {
  35. return parseInt(jQuery(el).text());
  36. })
  37. if(DBG)console.log('getTableAjaxStruct ('+DBG_COUNTER+') ids: ', ids);
  38. var statusNodes = (fieldStatusIdx > 0) ? tblNode.find('> tbody > tr > td:nth-child(' + (3 + fieldStatusIdx) + ')').toArray() : []
  39. var statusInfoNodes = (fieldStatusInfoIdx > 0) ? tblNode.find('> tbody > tr > td:nth-child(' + (3 + fieldStatusInfoIdx) + ')').toArray() : []
  40. var statuses = statusNodes.map(function (el) {
  41. return jQuery(el).text()
  42. })
  43. return {
  44. fields: fields,
  45. fieldStatusIdx: fieldStatusIdx,
  46. fieldStatusInfoIdx: fieldStatusInfoIdx,
  47. ids: ids,
  48. statusNodes: statusNodes,
  49. statusInfoNodes: statusInfoNodes,
  50. statuses: statuses,
  51. }
  52. }
  53. function updateTableAjaxView(respJson) {
  54. if (!respJson.body || !respJson.body.ids || !respJson.body.progress) return;
  55. var tblStruct = getTableAjaxStruct()
  56. if (!tblStruct) return;
  57. if(DBG)console.log('DBG:updateTableAjaxView', {respJson: respJson, tblStruct: tblStruct});
  58. tblStruct.statusNodes.forEach(function (el, idx) {
  59. var id = tblStruct.ids[idx]
  60. var respId = respJson.body.ids[idx]
  61. if (id !== respId) return; // view updated before response
  62. var progress = respJson.body.progress[idx]
  63. var percent = Math.round(100 * progress)
  64. if (DBG_FAKE_ANIM && percent > 0 && percent < 100) percent += (DBG_COUNTER * 10)
  65. if(DBG)console.log('DBG:updateTableAjaxView loop ('+idx+', el)', {el: el, id: id, progress: progress, pr: percent });
  66. var status = (respJson.body.statuses[idx]) ? respJson.body.statuses[idx] : null
  67. if (DBG_FAKE_ANIM) status = (percent < 100) ? 'IN_PROGRES' : 'DONE'
  68. updatePercentView(el, {
  69. percent: percent,
  70. status: status,
  71. })
  72. var statusInfo = (respJson.body.statusesInfo[idx]) ? respJson.body.statusesInfo[idx] : null
  73. if (statusInfo && tblStruct.statusInfoNodes && tblStruct.statusInfoNodes[idx]) updateStatusInfoView(tblStruct.statusInfoNodes[idx], statusInfo)
  74. })
  75. }
  76. function updateStatusInfoView(el, statusInfo) {
  77. if (statusInfo) jQuery(el).find('span').text(statusInfo)
  78. }
  79. function updatePercentView(el, props) {
  80. var hrNode = jQuery(el).find('hr')
  81. if (!hrNode.length) hrNode = jQuery('<hr>').appendTo(el)
  82. if (props.percent > 100) props.percent = 100
  83. hrNode.attr('style', "margin:0; padding:0; border-bottom:3px solid #f00; width:" + props.percent + "%")
  84. if (props.status) jQuery(el).find('span').text(props.status)
  85. }
  86. function updateRaportProgress() {
  87. DBG_COUNTER += 1
  88. if(DBG)console.log('updateRaportProgress ('+DBG_COUNTER+')');
  89. var tblStruct = getTableAjaxStruct()
  90. if(DBG)console.log('updateRaportProgress ('+DBG_COUNTER+') tblStruct', tblStruct);
  91. if (!tblStruct) {
  92. global.setTimeout(updateRaportProgress, 3000)
  93. return;
  94. }
  95. global.fetch(URL_FETCH_BI_AUDIT_PROGRESS, {
  96. method: 'POST',
  97. credentials: 'same-origin',
  98. body: JSON.stringify({
  99. ids: tblStruct.ids,
  100. })
  101. }).then(function (response) {
  102. return response.json()
  103. }).then(function (respJson) {
  104. updateTableAjaxView(respJson)
  105. IS_STARTED = false;
  106. startUpdateRaportProgress()
  107. }).catch(function (e) {
  108. if(DBG)console.warn('e:', e)
  109. IS_STARTED = false
  110. })
  111. IS_STARTED = false
  112. }
  113. function startUpdateRaportProgress() {
  114. if (IS_STARTED) return;
  115. IS_STARTED = true
  116. if(DBG)console.log('START updateRaportProgress ...');
  117. global.setTimeout(updateRaportProgress, 1000)
  118. }
  119. jQuery('#BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA').on('TableAjax:render', startUpdateRaportProgress)