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

Suggest similar investigations based on the currently displayed investigation on investigation landing page #1522

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 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
99 changes: 99 additions & 0 deletions packages/datagateway-common/src/api/investigations.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ import {
useInvestigationSize,
useInvestigationSizes,
useInvestigationsPaginated,
useSimilarInvestigations,
} from './investigations';

import { SuggestedInvestigation } from '../app.types';

jest.mock('../handleICATError');

describe('investigation api functions', () => {
Expand Down Expand Up @@ -1253,4 +1256,100 @@ describe('investigation api functions', () => {
expect(document.body.appendChild).toHaveBeenCalledWith(link);
});
});

describe('useSimilarInvestigations', function () {
const mockSuggestions: SuggestedInvestigation[] = [
{
id: 1,
visitId: 'visitId',
name: 'Suggested investigation 1 name',
title: 'Suggested investigation 1',
summary: 'Suggested investigation 1 summary',
doi: 'doi1',
score: 0.1,
},
{
id: 2,
visitId: 'visitId',
name: 'Suggested investigation 2 name',
title: 'Suggested investigation 2',
summary: 'Suggested investigation 2 summary',
doi: 'doi2',
score: 0.9,
},
{
id: 3,
visitId: 'visitId',
name: 'Suggested investigation 3 name',
title: 'Suggested investigation 3',
summary: 'Suggested investigation 3 summary',
doi: 'doi3',
score: 0.5,
},
{
id: 4,
visitId: 'visitId',
name: 'Suggested investigation 4 name',
title: 'Suggested investigation 4',
summary: 'Suggested investigation 4 summary',
doi: 'doi4',
score: 0.4,
},
];

it('queries for investigations similar to the given investigation and sorts the result by their relevance from the most relevant to the least', async () => {
(axios.get as jest.Mock).mockResolvedValue({
data: mockSuggestions,
});

const { result, waitFor } = renderHook(
() =>
useSimilarInvestigations({
investigation: mockData[0],
}),
{ wrapper: createReactQueryWrapper() }
);

await waitFor(() => result.current.isSuccess);

expect(result.current.data).toEqual([
{
id: 2,
visitId: 'visitId',
name: 'Suggested investigation 2 name',
title: 'Suggested investigation 2',
summary: 'Suggested investigation 2 summary',
doi: 'doi2',
score: 0.9,
},
{
id: 3,
visitId: 'visitId',
name: 'Suggested investigation 3 name',
title: 'Suggested investigation 3',
summary: 'Suggested investigation 3 summary',
doi: 'doi3',
score: 0.5,
},
{
id: 4,
visitId: 'visitId',
name: 'Suggested investigation 4 name',
title: 'Suggested investigation 4',
summary: 'Suggested investigation 4 summary',
doi: 'doi4',
score: 0.4,
},
{
id: 1,
visitId: 'visitId',
name: 'Suggested investigation 1 name',
title: 'Suggested investigation 1',
summary: 'Suggested investigation 1 summary',
doi: 'doi1',
score: 0.1,
},
]);
});
});
});
38 changes: 38 additions & 0 deletions packages/datagateway-common/src/api/investigations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
FiltersType,
Investigation,
SortType,
SuggestedInvestigation,
} from '../app.types';
import { StateType } from '../state/app.types';
import {
Expand Down Expand Up @@ -542,3 +543,40 @@
link.click();
link.remove();
};

export const useSimilarInvestigations = ({
investigation,
}: {
investigation: Investigation;
}): UseQueryResult<SuggestedInvestigation[], AxiosError> => {
// TODO: Remove this hardcoded URL
const baseUrl = 'http://172.16.103.71:4001/api';

return useQuery<
SuggestedInvestigation[],
AxiosError,
SuggestedInvestigation[],
['similarInvestigations', Investigation['id']]
>(
['similarInvestigations', investigation.id],
() =>
axios
.get<SuggestedInvestigation[]>(
`/v2/similar/doc/?id=${investigation.id}`,
{
baseURL: baseUrl,
}
)
.then((response) => response.data),
{
select: (data) => {
data.sort((resultA, resultB) => resultB.score - resultA.score);
return data;
},
onError: (error) => {
handleICATError(error);

Check warning on line 577 in packages/datagateway-common/src/api/investigations.tsx

View check run for this annotation

Codecov / codecov/patch

packages/datagateway-common/src/api/investigations.tsx#L576-L577

Added lines #L576 - L577 were not covered by tests
},
retry: retryICATErrors,
}
);
};
12 changes: 12 additions & 0 deletions packages/datagateway-common/src/app.types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ export interface InvestigationInstrument {
investigation?: Investigation;
}

export interface SuggestedInvestigation {
doi: string;
name: string;
title: string;
id: number;
summary: string;
visitId: string;
startDate?: string;
endDate?: string;
score: number;
}

export interface Instrument {
id: number;
name: string;
Expand Down
13 changes: 13 additions & 0 deletions packages/datagateway-dataview/public/res/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,19 @@
},
"type": {
"id": "Type ID"
},
"landingPage": {
"extraInfo": "Extra info",
"similarInvestigations": "Similar investigations",
"findingSimilarInvestigations": "Finding similar investigations…",
"noSuggestion": "No suggestion available",
"similarInvestigationListPaginationLabel": {
"pageButton": "Go to page {{page}} of similar investigations",
"firstPageButton": "Go to the first page of similar investigations",
"lastPageButton": "Go to the last page of similar investigations",
"nextButton": "Go to the next page of similar investigations",
"prevButton": "Go to the previous page of similar investigations"
}
}
},
"datasets": {
Expand Down
Loading
Loading