From b93867a52294eecdd6cd7ab0e3060c8254b680f2 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Mon, 17 Jul 2023 15:56:39 +0000 Subject: [PATCH] Bug 34282: Follow-up to fix multiple requests causing high server load, squash later --- .../intranet-tmpl/prog/js/ill-batch-modal.js | 103 ++++++++---------- 1 file changed, 45 insertions(+), 58 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/js/ill-batch-modal.js b/koha-tmpl/intranet-tmpl/prog/js/ill-batch-modal.js index 20d54781bc..ed53610578 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/ill-batch-modal.js +++ b/koha-tmpl/intranet-tmpl/prog/js/ill-batch-modal.js @@ -86,9 +86,6 @@ updateTable(); updateRowCount(); updateProcessTotals(); - if (ill_check_availability_syspref == 1){ - checkAvailability(); - } } } ); @@ -237,55 +234,28 @@ }); }; - // Identify items that can have their availability checked, and do it - function checkAvailability() { - // Only proceed if we've got services that can check availability - if (!batch_availability_services || batch_availability_services.length === 0) return; - var toCheck = tableContent.data; - toCheck.forEach(function (row) { - if ( - !row.url && - Object.keys(row.metadata).length > 0 && - !availabilitySent[row.value] - ) { - availabilitySent[row.value] = 1; - getAvailability(row); - } - }); - }; - - // Check availability services for immediate availability, if found, - // create a link in the table linking to the item - function getAvailability(row) { + async function populateAvailability(row) { let metadata = row.metadata; - // Prep the metadata for passing to the availability plugins + let availability_object = {}; if (metadata.issn) availability_object['issn'] = metadata.issn; if (metadata.doi) availability_object['doi'] = metadata.doi; if (metadata.pubmedid) availability_object['pubmedid'] = metadata.pubmedid; - var prepped = encodeURIComponent(base64EncodeUnicode(JSON.stringify(availability_object))); - row.av_hits = []; - batch_availability_services.map(async (service) => { - window.doApiRequest( - service.endpoint + prepped - ) - .then(function (response) { - return response.json(); - }) - .then(function (data) { - if (data.results.search_results && data.results.search_results.length > 0) { - var result = data.results.search_results[0]; - row.av_hits.push({name:service.name, url:result.url}); - return row; - }else { - row.av_hits.push({ name: service.name, empty: 1 }) - } - }) - .then(function (response) { - updateTable(); - }); - }); - // } + + // Check each service and use the first results we get, if any + var av_hits = []; + for (const service of batch_availability_services){ + var prepped = encodeURIComponent(base64EncodeUnicode(JSON.stringify(availability_object))); + + var endpoint = service.endpoint + prepped; + var availability = await getAvailability(endpoint); + if (availability.results.search_results && availability.results.search_results.length > 0) { + av_hits.push({name: service.name, url: availability.results.search_results[0].url}); + }else{ + av_hits.push({ name: service.name, empty:1 }); + } + }; + return av_hits; }; // Help btoa with > 8 bit strings @@ -689,6 +659,7 @@ } else if (alreadyInDeduped.length === 0) { row.metadata = {}; row.failed = {}; + row.availability_hits = {}; row.requestId = null; deduped.push(row); } @@ -739,6 +710,16 @@ } catch (e) { row.failed = ill_populate_failed; } + + if (ill_check_availability_syspref == 1){ + try { + var availability = await populateAvailability(row); + row.availability_hits = availability || {}; + } catch (e) { + //do nothing + } + } + newData[i] = row; tableContent.data = newData; } @@ -774,6 +755,11 @@ } }; + async function getAvailability(endpoint) { + var response = await debounce(doApiRequest)(endpoint); + return response.json(); + }; + async function getMetadata(endpoint) { var response = await debounce(doApiRequest)(endpoint); return response.json(); @@ -933,21 +919,22 @@ return data.failed; } + if (Object.keys(data.availability_hits).length === 0){ + return ill_populate_waiting; + } + let str = ''; let has_some = false; - if(typeof data.av_hits !== 'undefined' && data.av_hits.length>0){ - data.av_hits.forEach((av_hit) => { - if(!av_hit.empty){ - has_some = true; - str += '
  • ' + av_hit.name + '
  • ' - } - }); - - if(!has_some){ - str = ill_batch_none; + for (i = 0; i < data.availability_hits.length; i++) { + if (!data.availability_hits[i].empty){ + has_some = true; + str += "
  • " + data.availability_hits[i].name + "
  • " } + } + if(!has_some){ + str = ill_batch_none; } - return str || ill_populate_waiting; + return str; }; function buildTable(identifiers) { -- 2.30.2