Skip to content

Commit 30659fc

Browse files
committed
fix: go back to making other queries also use limit=0
Plus, some refactoring to pull out common code.
1 parent 43ec5df commit 30659fc

File tree

1 file changed

+49
-56
lines changed

1 file changed

+49
-56
lines changed

Code.js

Lines changed: 49 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ function showItemsForCallNumberRange(firstCN, lastCN, locationId) {
404404
} else {
405405
// Get the location name so we can write it in the error message.
406406
const locName = getLocationsList().find(el => (el.id == locationId)).name;
407-
quit('No results for given call number range and location',
407+
quit('No results for this combination of call number range and location',
408408
`Searching FOLIO for the call number range ${firstCN}${lastCN}` +
409409
` (in either order) at location "${locName}" produced no results.` +
410410
' Please verify the call numbers (paying special attention to any' +
@@ -418,37 +418,18 @@ function showItemsForCallNumberRange(firstCN, lastCN, locationId) {
418418

419419
// Don't start downloading results if we won't be able to write them.
420420
const numColumns = getEnabledFields().length;
421-
const maxRecords = Math.trunc(maxGoogleSheetCells/numColumns) - 1;
422-
if (expected.totalRecords > maxRecords) {
421+
const maxRows = Math.trunc(maxGoogleSheetCells/numColumns) - 1;
422+
if (expected.totalRecords > maxRows) {
423423
quit('This query exceeds the maximum number of results that can be written',
424-
`The number of records this produced (${maxRecords.toLocaleString()})` +
425-
` times the number of currently-selected data fields (${numColumns})` +
426-
` exceeds the number of spreadsheet cells that Google Sheets allow in` +
427-
` a single empty spreadsheet.`);
424+
`The number of records this produced (${maxRows.toLocaleString()})` +
425+
` times the number of currently-selected data fields (${numColumns})` +
426+
` exceeds the number of cells that a Google spreadsheet can contain.`);
428427
}
429428

430429
// Now get the records.
431-
let records = [];
432-
let results;
433430
note(`Fetching ${expected.totalRecords.toLocaleString()} records from FOLIO …`, 30);
434-
for (let offset = 0; offset < expected.totalRecords; offset += 100) {
435-
endpoint = makeRangeQuery(firstESO, lastESO, 100, offset);
436-
results = fetchJSON(endpoint, tenantId, token);
437-
if (results.items) {
438-
records.push.apply(records, results.items);
439-
} else {
440-
quit('Failed to get complete set of records',
441-
`While downloading the item records for ${firstCN}${lastCN},` +
442-
' Boffo unexpectedly received an empty batch from FOLIO. It may' +
443-
' be due to a sudden network glitch or other temporary failure,' +
444-
' or it may indicate a deeper problem. Please wait a short time' +
445-
' then try the command again. If this situation repeats, please' +
446-
' report it to the developers.');
447-
}
448-
if (offset > 0 && (offset % 5000) == 0) {
449-
note(`Fetched ${offset.toLocaleString()} records so far and still going …`, 30);
450-
}
451-
}
431+
const makeQuery = makeRangeQuery.bind(null, firstESO, lastESO);
432+
const records = getRecordsForQuery(makeQuery, expected.totalRecords, tenantId, token);
452433

453434
// And we're done.
454435
writeResultsSheet(sortByShelvingOrder(records));
@@ -483,41 +464,22 @@ function getItemsForCN(cn, locationId) {
483464
const {folioUrl, tenantId, token} = getStoredCredentials();
484465
const baseUrl = `${folioUrl}/inventory/items`;
485466

486-
function makeQuery(offset = 0) {
467+
function makeQuery(limit = 0, offset = 0) {
487468
// Search on the wildcarded call number at the given location.
488469
// 100 is the max that the Folio API will return for this query.
489-
return baseUrl + `?limit=100&offset=${offset}&query=` +
470+
return baseUrl + `?limit=${limit}&offset=${offset}&query=` +
490471
encodeURI(`effectiveLocationId==${locationId} AND ` +
491472
`effectiveCallNumberComponents.callNumber=="${cn}"`);
492473
}
493474

494-
let endpoint = makeQuery(0);
495-
let records = fetchJSON(endpoint, tenantId, token);
496-
log(`FOLIO has ${records.totalRecords} items for ${cn} at location ${locationId}`);
497-
498-
if (records.totalRecords > 0 && records.totalRecords <= 100) {
499-
return sortByShelvingOrder(records.items);
500-
} else if (records.totalRecords > 100) {
501-
// The call above didn't get all of the records.
502-
log('iterating to get all records');
503-
let itemRecords = records.items;
504-
let results;
505-
for (let offset = 100; offset <= records.totalRecords; offset += 100) {
506-
endpoint = makeQuery(offset);
507-
results = fetchJSON(endpoint, tenantId, token);
508-
if (results.items) {
509-
itemRecords.push.apply(itemRecords, results.items);
510-
} else {
511-
quit(`Failed to get complete set of records for ${givenCN}.`,
512-
' Boffo unexpectedly received an empty batch from FOLIO. It' +
513-
' may be due to a sudden network glitch or other temporary' +
514-
' failure, or it may indicate a deeper problem. Please wait' +
515-
' a few seconds, then try again. If this situation repeats,' +
516-
' please report it to the developers.');
517-
}
518-
}
519-
log(`got ${itemRecords.length} item records`);
520-
return sortByShelvingOrder(itemRecords);
475+
// Do preliminary query to get the number of records.
476+
let endpoint = makeQuery();
477+
let results = fetchJSON(endpoint, tenantId, token);
478+
log(`FOLIO has ${results.totalRecords} items for ${cn} at location ${locationId}`);
479+
480+
// Now get the records.
481+
if (results.totalRecords > 0) {
482+
return getRecordsForQuery(makeQuery, results.totalRecords, tenantId, token);
521483
} else {
522484
// Get the location name so we can write it in the error message.
523485
const locName = getLocationsList().find(el => (el.id == locationId)).name;
@@ -535,6 +497,37 @@ function getItemsForCN(cn, locationId) {
535497
}
536498
}
537499

500+
/**
501+
* Takes a function object, a total numbef of records to get, and the
502+
* tenant ID and token, then iterates to get all the Folio records, and
503+
* finally returns the array of record objects.
504+
*
505+
* The first argument (makeQuery) must be a function object that takes
506+
* two parameters: the "limit" value to a Folio call, and the "offset"
507+
* value.
508+
*/
509+
function getRecordsForQuery(makeQuery, totalRecords, tenantId, token) {
510+
let records = [];
511+
let results;
512+
for (let offset = 0; offset <= totalRecords; offset += 100) {
513+
results = fetchJSON(makeQuery(100, offset), tenantId, token);
514+
if (results.items) {
515+
records.push.apply(records, results.items);
516+
} else {
517+
quit(`Failed to get complete set of records`,
518+
' Boffo unexpectedly received an empty batch from FOLIO. It' +
519+
' may be due to a sudden network glitch or other temporary' +
520+
' failure, or it may indicate a deeper problem. Please wait' +
521+
' a few seconds, then try again. If this situation repeats,' +
522+
' please report it to the developers.');
523+
}
524+
if (offset > 0 && (offset % 5000) == 0) {
525+
note(`Fetched ${offset.toLocaleString()} records so far and still going …`, 30);
526+
}
527+
}
528+
return records;
529+
}
530+
538531
/**
539532
* Takes a list of items (assumed to be sorted by effectiveShelvingOrder) and
540533
* returns the first value of effectiveShelvingOrder found.

0 commit comments

Comments
 (0)