Commit 86847820 authored by Magnus Westergaard's avatar Magnus Westergaard
Browse files

DEICH-5630: Simplify data fetching for related content from series....

DEICH-5630: Simplify data fetching for related content from series. Consolidate workseries item sorting.
parent 2293e724
......@@ -7,7 +7,7 @@ import PropTypes from "prop-types";
const YEAR_THRESHOLD = 1500;
const NumberInSeries = ({ wsPartNumber }) => {
if (!wsPartNumber && !Number.isInteger(wsPartNumber)) {
if (!wsPartNumber) {
return null;
}
let displayText = wsPartNumber;
......
......@@ -51,7 +51,7 @@ class WorkSeriesPublicationList extends React.Component {
const res = await fetch(
`/api/resources/workseries/${
workSeries.id
}/publications${mediaTypeQuery}&size=${size}&excludeSatellites=excludeSatellites`
}/publications${mediaTypeQuery}&size=${size}`
);
const results = await res.json();
......
......@@ -8,7 +8,7 @@ import { toggleFavourites, unfreezeBody } from "../../store/application";
import { hideSearchOverlay } from "../../store/search";
import { extractIdFromSlug } from "../../utilities/slug";
import { workSeriesWorksSort } from "../../server/utils/sortWorkSeriesWorks";
import { worksSort } from "../../server/utils/sortWorkSeriesItems";
import Head from "../../components/Head";
import FullScreen from "../../components/FullScreen";
......@@ -250,7 +250,7 @@ function publicationsFromWorks(
otherPublications
) {
return works
.sort(workSeriesWorksSort)
.sort(worksSort)
.map((work, index) => {
let otherPub = { work: {} };
if (!publications[work.uri] && otherPublications[work.uri]) {
......@@ -270,8 +270,7 @@ function publicationsFromWorks(
id: work.uri,
mediaType,
workLanguage: work.language,
partNumber: work.partNumber,
partNumberAsInteger: work.partNumberAsInteger,
wsPartNumber: work.wsPartNumber,
images: {
fallbackColor: `col-ok-${(index % 6) + 1}`,
hasAllSizes: false,
......@@ -290,10 +289,7 @@ function publicationsFromWorks(
}
function DisplaySeriesNumber({ data }) {
const number = Number.isInteger(data.partNumberAsInteger)
? data.partNumberAsInteger
: data.partNumber;
return <NumberInSeries wsPartNumber={number} />;
return <NumberInSeries wsPartNumber={data.wsPartNumber} />;
}
function DisplayNotAvailableInLanguage(requestedLanguage, { data }) {
......
......@@ -7,7 +7,7 @@ const {
} = require("../utils/resourceHelpers");
const { translations } = require("../../constants/translations");
const { timeout } = require("../utils/apiUtils");
const { workSeriesWorksSort } = require("../utils/sortWorkSeriesWorks");
const { publicationsSort } = require("../utils/sortWorkSeriesItems");
const logger = require("../../logger")(__filename);
const CALL_ID_HEADER = "Deichman-CallID";
......@@ -391,45 +391,26 @@ routes.get("/workseries/:workSeriesId", async (req, res) => {
// Get workSeries publications (in order)
routes.get("/workseries/:workSeriesId/publications", async (req, res) => {
const { filter, excludeSatellites, size } = req.query;
let query = filter ? `?filter=${encodeURIComponent(filter)}` : "";
if (excludeSatellites === "excludeSatellites") {
query += "&excludeSatellites=excludeSatellites";
}
const { filter, size } = req.query;
const query = filter ? `&filter=${encodeURIComponent(filter)}` : "";
const { workSeriesId } = req.params;
const deichmanCallId = req.headers[CALL_ID_HEADER];
logger.info(
`Fetching publications for workseries ${req.params.workSeriesId}`,
{
workSeries_id: req.params.workSeriesId,
call_id: deichmanCallId
}
);
logger.info(`Fetching publications for workseries ${workSeriesId}`, {
workSeries_id: workSeriesId,
call_id: deichmanCallId
});
try {
const results = await fetch(
`${INTERNAL_URL_EULER}/api/augmented/workSeries/${
req.params.workSeriesId
}`
);
const { works } = await results.json();
const workUris = works.map(work => work.uri);
const sibylRes = await timeout(
TIMEOUT_MS,
fetch(`${INTERNAL_URL_SIBYL}/search/publicationByWorkUris${query}`, {
method: "POST",
body: JSON.stringify({ ids: workUris })
})
);
const { hits, totalHits } = await sibylRes.json();
works.sort(workSeriesWorksSort);
hits.sort(
(pubA, pubB) =>
works.findIndex(w => pubA.work.uri === w.uri) -
works.findIndex(w => pubB.work.uri === w.uri)
fetch(
`${INTERNAL_URL_SIBYL}/search/publication?q=workSeriesInfo.id:${workSeriesId}${query}&excludeSatellites=excludeSatellites&pageSize=100&excludeAggregations=excludeAggregations&includeWithoutItems=includeWithoutItems`
)
);
const { hits = [], totalHits } = await sibylRes.json();
hits.sort(publicationsSort(workSeriesId));
res.status(200).send({ orderedHits: hits.slice(0, size), totalHits });
} catch (error) {
logger.warn(`ERROR calling get workseries publications ${error.message}`, {
......
/*
Sort works in a workSeries according to
1. partNumber if it's an integer (the one from the WorkSeriesPart relation, not the on which is part of the work's title)
WorkSeries items sorted by
1. partNumber if it's an integer (the one from the WorkSeriesPart relation, not the one which is part of the work's title)
2. publication year
3. main title
*/
function workSeriesWorksSort(a, b) {
// partNumberAsInteger might be 0 (which is valid)
const aHasIntegerPartNumber = Number.isInteger(a.partNumberAsInteger);
const bHasIntegerPartNumber = Number.isInteger(b.partNumberAsInteger);
function workSeriesWorkSort(a, b) {
// partNumber might be 0 (which is valid)
const aHasIntegerPartNumber = Number.isInteger(a.partNumber);
const bHasIntegerPartNumber = Number.isInteger(b.partNumber);
if (aHasIntegerPartNumber && !bHasIntegerPartNumber) {
return -1;
}
......@@ -15,7 +15,7 @@ function workSeriesWorksSort(a, b) {
return 1;
}
if (aHasIntegerPartNumber && bHasIntegerPartNumber) {
const diff = a.partNumberAsInteger - b.partNumberAsInteger;
const diff = a.partNumber - b.partNumber;
if (diff !== 0) {
return diff;
}
......@@ -34,4 +34,42 @@ function workSeriesWorksSort(a, b) {
return `${a.mainTitle}`.localeCompare(`${b.mainTitle}`);
}
module.exports = { workSeriesWorksSort };
function asNumberIfOnlyDigits(s) {
const isOnlyDigits = /^[0-9]+$/.test(s);
return isOnlyDigits ? Number(s) : undefined;
}
// extracts relevant sorting data from a publication (search result)
function pubToSortable(pub, workSeriesId) {
const workSeries = pub.workSeriesInfo.find(ws => ws.id === workSeriesId);
return {
partNumber: asNumberIfOnlyDigits(workSeries?.number),
publicationYear: pub.work.publicationYear,
mainTitle: pub.work.mainTitle
};
}
function workToSortable(work) {
return {
partNumber: asNumberIfOnlyDigits(work.wsPartNumber),
publicationYear: work.publicationYear,
mainTitle: work.mainTitle
};
}
function worksSort(workA, workB) {
const sortableA = workToSortable(workA);
const sortableB = workToSortable(workB);
return workSeriesWorkSort(sortableA, sortableB);
}
function publicationsSort(workSeriesId) {
return (pubA, pubB) => {
const sortableA = pubToSortable(pubA, workSeriesId);
const sortableB = pubToSortable(pubB, workSeriesId);
return workSeriesWorkSort(sortableA, sortableB);
};
}
module.exports = { worksSort, publicationsSort };
......@@ -31,14 +31,14 @@ public class AugmentedWorkSeries {
String mainTitle = tuple.get("mainTitle");
String publicationYear = tuple.get("publicationYear");
String language = tuple.get("language");
String partNumber = tuple.get("partNumber");
String wsPpartNumber = tuple.get("wsPartNumber");
boolean isNewWork = !worksPerUri.containsKey(workUri);
WorkSeriesWork wsWork = isNewWork ? new WorkSeriesWork(workUri, mainTitle) : worksPerUri.get(workUri);
if (publicationYear != null) {
wsWork.setPublicationYear(publicationYear);
}
wsWork.setPartNumber(partNumber);
wsWork.setWsPartNumber(wsPpartNumber);
wsWork.getLanguage().add(language);
if (isNewWork) {
......@@ -78,9 +78,8 @@ public class AugmentedWorkSeries {
String uri;
String mainTitle;
Integer publicationYear;
String partNumber;
String wsPartNumber;
Set<String> language = new HashSet<>();
Integer partNumberAsInteger;
WorkSeriesWork(String uri, String mainTitle) {
this.uri = uri;
......@@ -94,14 +93,5 @@ public class AugmentedWorkSeries {
log.warn("publicationYear '{}' is not a valid integer (work={})", publicationYear, this.uri);
}
}
void setPartNumber(String partNumber) {
try {
this.partNumberAsInteger = Integer.valueOf(partNumber);
} catch (NumberFormatException e) {
// don't do anything, it's normal for partnumbers to contain text
this.partNumber = partNumber;
}
}
}
}
......@@ -2,7 +2,7 @@ PREFIX :<http://data.deichman.no/ontology#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
WITH <https://katalog.deichman.no>
SELECT DISTINCT ?work ?mainTitle ?language ?partNumber ?publicationYear
SELECT DISTINCT ?work ?mainTitle ?language ?wsPartNumber ?publicationYear
WHERE {
?work :isPartOfWorkSeries ?wsp ;
:mainTitle ?mainTitle .
......@@ -13,5 +13,5 @@ WHERE {
BIND(STR(?_language) AS ?language) .
}
OPTIONAL { ?work :publicationYear ?publicationYear }
OPTIONAL { ?wsp :partNumber ?partNumber }
OPTIONAL { ?wsp :partNumber ?wsPartNumber }
}
......@@ -134,8 +134,7 @@ func (p *publicationSearchClient) SearchByWorkUris(uris []string, params url.Val
return SearchResults{}
}
excludeSatellites := params.Get("excludeSatellites") == "excludeSatellites"
parsedResults := transformElasticResponse(result, size, excludeAdditionalMediaTypes, excludeSatellites, DebugFlags{})
parsedResults := transformElasticResponse(result, size, excludeAdditionalMediaTypes, false, DebugFlags{})
return parsedResults
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment