Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bibdk2021 2849 facets events #462

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/middlewares/dataHubMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,43 @@ export function dataHubMiddleware(req, res, next) {
req.datasources.getLoader("datahub").load(event);
}

async function createInspirationEvent({ input = {}, result = {} }) {
const context = await getContext();
if (!shouldSendEvent(context)) {
return;
}

const variables = {
limit: input.limit || 10,
filters: input.filter?.map(({ category, subCategories }) => ({
category,
subCategories,
})),
};

const inspiration = {
categories: result.data.map((category) => ({
title: category.category,
subCategories: category.subCategories.map((sub) => ({
title: sub.title,
identifiers: sub.result.map((work) => ({
identifier: work.work,
traceId: work.traceId,
})),
})),
})),
};

const event = {
context,
kind: "INSPIRATION",
variables,
result: { inspiration },
};

req.datasources.getLoader("datahub").load(event);
}

if (!req.onOperationComplete) {
req.onOperationComplete = [];
}
Expand Down Expand Up @@ -373,6 +410,7 @@ export function dataHubMiddleware(req, res, next) {
createSeriesEvent,
createUniverseEvent,
createComplexSearchEvent,
createInspirationEvent,
};

next();
Expand Down
5 changes: 4 additions & 1 deletion src/schema/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export const resolvers = {
const limit = args.limit || 10;
const result = await Promise.all(
parent.result.slice(0, limit).map(async (entry) => {
const work = await resolveWork({ id: entry.work }, context);
const work = await resolveWork(
{ id: entry.work, traceId: entry.traceId },
context
);
const manifestation = resolveManifestation(
{ pid: entry.pid },
context
Expand Down
67 changes: 45 additions & 22 deletions src/schema/complexsearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ A Facet value in response
type ComplexSearchFacetValue{
key: String!
score: Int!
traceId: String

}

"""
Expand Down Expand Up @@ -180,13 +182,48 @@ function setPost(parent, context, args) {
};
}

//will add traceIDs to the facets and send datahub event
async function traceFacets({ response, context, parent, args }) {
console.log("\n\n\nresponse", response);
const facetsWithTraceIds = response?.facets?.map((facet) => ({
...facet,
values: facet?.values?.map((value) => {
return { ...value, traceId: createTraceId() };
}),
}));

if (facetsWithTraceIds?.length > 0) {
await context?.dataHub?.createComplexSearchEvent({
input: { ...parent, ...args, profile: context.profile },
result: {
works: response?.works?.map((id) => ({
workId: id,
// do we need this?? traceId: createTraceId(),//todo move to datasource?
})),
facets: facetsWithTraceIds,
},
});
}

return facetsWithTraceIds;
}

export const resolvers = {
ComplexFacetResponse: {
async facets(parent, args, context) {
const res = await context.datasources
.getLoader("complexFacets")
.load(setPost(parent, context, args));
return res?.facets;

const facetsWithTraceIds = await traceFacets({
response: res,
parent,
context,
args,
});
console.log("\n\n\n\n\n\nfacetsWithTraceIds", facetsWithTraceIds);

return facetsWithTraceIds;
},
async hitcount(parent, args, context) {
const res = await context.datasources
Expand All @@ -212,28 +249,14 @@ export const resolvers = {
const res = await context.datasources
.getLoader("complexsearch")
.load(setPost(parent, context, args));
const facetsWithTraceIds = await traceFacets({
response: res,
parent,
context,
args,
});

const facetsWithTraceIds = res?.facets?.map((facet) => ({
...facet,
values: facet.values?.map((value) => {
return { ...value, traceId: createTraceId() };
}),
}));

if (facetsWithTraceIds?.length > 0) {
await context?.dataHub?.createComplexSearchEvent({
input: { ...parent, ...args, profile: context.profile },
result: {
works: res?.works?.map((id) => ({
workId: id,
traceId: createTraceId(),//todo move to datasource?
})),
facets: facetsWithTraceIds,
},
});
}

return res?.facets;
return facetsWithTraceIds;
},
async works(parent, args, context) {
const res = await context.datasources
Expand Down
18 changes: 16 additions & 2 deletions src/schema/inspiration.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*
*/

import { createTraceId } from "../utils/trace";

export const typeDef = `
input CategoryFilterInput {
category: CategoryFiltersEnum!
Expand Down Expand Up @@ -58,7 +60,7 @@ export const resolvers = {
return [];
}

return args?.filter?.map(({ category, subCategories }) => {
const result = args?.filter?.map(({ category, subCategories }) => {
const key = mapEnums[category];
// Some data keys differs from enum types - e.g. We do not use _ in api
const data = res[mapKeys[key] || key];
Expand All @@ -70,7 +72,14 @@ export const resolvers = {
type: category,
subCategories: subCategories
.map((sub) => data.find(({ title }) => title === sub))
.filter((element) => element !== undefined),
.filter((element) => element !== undefined)
.map((category) => ({
...category,
result: category.result.map((item) => ({
...item,
traceId: createTraceId(),
})),
})),
};
}

Expand All @@ -80,6 +89,11 @@ export const resolvers = {
subCategories: data,
};
});
await context?.dataHub?.createInspirationEvent({
input: { ...parent, ...args, profile: context.profile },
result: { data: result },
});
return result;
},
},
};
2 changes: 1 addition & 1 deletion src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ export async function resolveWork(args, context) {
});

if (w) {
const withTraceId = { ...w, traceId: createTraceId() };
const withTraceId = { ...w, traceId: args.traceId || createTraceId() };
withTraceId.manifestations = {
bestRepresentations: w?.manifestations?.bestRepresentations?.map((m) => ({
...m,
Expand Down