Skip to content

Commit

Permalink
feat(filters): Add number of occurrences, close #54
Browse files Browse the repository at this point in the history
  • Loading branch information
annelhote committed Jan 12, 2024
1 parent f8b8f58 commit 94d23cc
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 40 deletions.
26 changes: 13 additions & 13 deletions client/src/pages/datasetsTab.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export default function DatasetsTab({ datasets, publishers, selectedDatasets, se

useEffect(() => {
setFilteredDatasets(datasets);
setFilteredPublishers(publishers);
setFilteredYears(years);
setFilteredTypes(types);
setFilteredPublishers(Object.keys(publishers));
setFilteredYears(Object.keys(years));
setFilteredTypes(Object.keys(types));
}, [datasets, publishers, types, years]);

useEffect(() => {
Expand All @@ -40,7 +40,7 @@ export default function DatasetsTab({ datasets, publishers, selectedDatasets, se
&& filteredPublishers.includes(dataset.publisher)
&& filteredStatus.includes(dataset.status)
&& filteredTypes.includes(dataset.type)
&& filteredYears.includes(Number(dataset.year)));
&& filteredYears.includes(dataset.year));
setFilteredDatasets(filteredDatasetsTmp);
}, 500);
setTimer(timerTmp);
Expand Down Expand Up @@ -142,11 +142,11 @@ export default function DatasetsTab({ datasets, publishers, selectedDatasets, se
hint="Filter publications on selected years"
legend="Years"
>
{years.map((year) => (
{Object.keys(years).sort().reverse().map((year) => (
<Checkbox
checked={filteredYears.includes(year)}
key={year}
label={year.toString()}
label={`${year} (${years[year]})`}
onChange={() => onYearsChange(year)}
size="sm"
/>
Expand All @@ -156,11 +156,11 @@ export default function DatasetsTab({ datasets, publishers, selectedDatasets, se
hint="Filter publications on selected types"
legend="Types"
>
{types.map((type) => (
{Object.keys(types).map((type) => (
<Checkbox
checked={filteredTypes.includes(type)}
key={type}
label={type.toString()}
label={`${type} (${types[type]})`}
onChange={() => onTypesChange(type)}
size="sm"
/>
Expand All @@ -170,11 +170,11 @@ export default function DatasetsTab({ datasets, publishers, selectedDatasets, se
hint="Filter publications on selected publishers"
legend="Publishers"
>
{publishers.map((publisher) => (
{Object.keys(publishers).map((publisher) => (
<Checkbox
checked={filteredPublishers.includes(publisher)}
key={publisher}
label={publisher.toString()}
label={`${publisher} (${publishers[publisher]})`}
onChange={() => onPublishersChange(publisher)}
size="sm"
/>
Expand Down Expand Up @@ -209,7 +209,7 @@ DatasetsTab.propTypes = {
status: PropTypes.string.isRequired,
type: PropTypes.string.isRequired,
})).isRequired,
publishers: PropTypes.arrayOf(PropTypes.string).isRequired,
publishers: PropTypes.object.isRequired,
selectedDatasets: PropTypes.arrayOf(PropTypes.shape({
affiliations: PropTypes.arrayOf(PropTypes.string).isRequired,
allIds: PropTypes.arrayOf(PropTypes.object).isRequired,
Expand All @@ -222,6 +222,6 @@ DatasetsTab.propTypes = {
})).isRequired,
setSelectedDatasets: PropTypes.func.isRequired,
tagDatasets: PropTypes.func.isRequired,
types: PropTypes.arrayOf(PropTypes.string).isRequired,
years: PropTypes.arrayOf(PropTypes.number).isRequired,
types: PropTypes.object.isRequired,
years: PropTypes.object.isRequired,
};
1 change: 0 additions & 1 deletion client/src/pages/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ export default function Home() {
</Row>
</Container>
<Container className="fr-mx-5w" as="section" fluid>
{/* TODO add Datasets ? */}
<Actions
allAffiliations={allAffiliations}
allPublications={allPublications}
Expand Down
24 changes: 12 additions & 12 deletions client/src/pages/publicationsTab.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export default function PublicationsTab({ publications, publishers, selectedPubl

useEffect(() => {
setFilteredPublications(publications);
setFilteredPublishers(publishers);
setFilteredYears(years);
setFilteredTypes(types);
setFilteredPublishers(Object.keys(publishers));
setFilteredYears(Object.keys(years));
setFilteredTypes(Object.keys(types));
}, [publications, publishers, types, years]);

useEffect(() => {
Expand Down Expand Up @@ -142,11 +142,11 @@ export default function PublicationsTab({ publications, publishers, selectedPubl
hint="Filter publications on selected years"
legend="Years"
>
{years.map((year) => (
{Object.keys(years).sort().reverse().map((year) => (
<Checkbox
checked={filteredYears.includes(year)}
key={year}
label={year.toString()}
label={`${year} (${years[year]})`}
onChange={() => onYearsChange(year)}
size="sm"
/>
Expand All @@ -156,11 +156,11 @@ export default function PublicationsTab({ publications, publishers, selectedPubl
hint="Filter publications on selected types"
legend="Types"
>
{types.map((type) => (
{Object.keys(types).map((type) => (
<Checkbox
checked={filteredTypes.includes(type)}
key={type}
label={type.toString()}
label={`${type} (${types[type]})`}
onChange={() => onTypesChange(type)}
size="sm"
/>
Expand All @@ -170,11 +170,11 @@ export default function PublicationsTab({ publications, publishers, selectedPubl
hint="Filter publications on selected publishers"
legend="Publishers"
>
{publishers.map((publisher) => (
{Object.keys(publishers).map((publisher) => (
<Checkbox
checked={filteredPublishers.includes(publisher)}
key={publisher}
label={publisher.toString()}
label={`${publisher} (${publishers[publisher]})`}
onChange={() => onPublishersChange(publisher)}
size="sm"
/>
Expand Down Expand Up @@ -209,7 +209,7 @@ PublicationsTab.propTypes = {
status: PropTypes.string.isRequired,
type: PropTypes.string.isRequired,
})).isRequired,
publishers: PropTypes.arrayOf(PropTypes.string).isRequired,
publishers: PropTypes.object.isRequired,
selectedPublications: PropTypes.arrayOf(PropTypes.shape({
affiliations: PropTypes.arrayOf(PropTypes.string).isRequired,
allIds: PropTypes.arrayOf(PropTypes.object).isRequired,
Expand All @@ -222,6 +222,6 @@ PublicationsTab.propTypes = {
})).isRequired,
setSelectedPublications: PropTypes.func.isRequired,
tagPublications: PropTypes.func.isRequired,
types: PropTypes.arrayOf(PropTypes.string).isRequired,
years: PropTypes.arrayOf(PropTypes.number).isRequired,
types: PropTypes.object.isRequired,
years: PropTypes.object.isRequired,
};
19 changes: 7 additions & 12 deletions server/src/routes/works.routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import express from 'express';

import { range } from '../utils/utils';
import { range, countUniqueValues } from '../utils/utils';
import { deduplicateWorks, getFosmWorks, getOpenAlexPublications, groupByAffiliations } from '../utils/works';
import webSocketServer from '../webSocketServer';

Expand Down Expand Up @@ -62,17 +62,12 @@ router.route('/works')
webSocketServer.broadcast('step_4');
// Compute distinct types & years for facet
console.time(`5. Facet ${options.affiliations}`);
// TODO chek if Set is optim
const publicationsYears = [...new Set(
publications.filter((publication) => !!publication?.year).map((publication) => Number(publication.year)),
)].sort((a, b) => b - a);
const datasetsYears = [...new Set(
datasets.filter((dataset) => !!dataset?.year).map((dataset) => Number(dataset.year)),
)].sort((a, b) => b - a);
const publicationsTypes = [...new Set(publications.map((publication) => publication?.type))];
const datasetsTypes = [...new Set(datasets.map((dataset) => dataset?.type))];
const publicationsPublishers = [...new Set(publications.map((publication) => publication?.publisher))];
const datasetsPublishers = [...new Set(datasets.map((dataset) => dataset?.publisher))];
const publicationsYears = countUniqueValues({ data: publications, field: 'year' });
const datasetsYears = countUniqueValues({ data: datasets, field: 'year' });
const publicationsTypes = countUniqueValues({ data: publications, field: 'type' });
const datasetsTypes = countUniqueValues({ data: datasets, field: 'type' });
const publicationsPublishers = countUniqueValues({ data: publications, field: 'publisher' });
const datasetsPublishers = countUniqueValues({ data: datasets, field: 'publisher' });
console.timeEnd(`5. Facet ${options.affiliations}`);
webSocketServer.broadcast('step_5');
// Build and serialize response
Expand Down
11 changes: 11 additions & 0 deletions server/src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ const cleanId = (id) => (
: null
);

const countUniqueValues = ({ data = [], field }) => {
const map = data
.map((item) => item?.[field] ?? '')
.reduce((acc, curr) => {
acc[curr] = acc?.[curr] ? acc[curr] + 1 : 1;
return acc;
}, {});
return Object.fromEntries(Object.entries(map).sort(([, a], [, b]) => b - a));
};

// See https://stackoverflow.com/a/18391901
const defaultDiacriticsRemovalMap = [
{ base: 'A', letters: '\u0041\u24B6\uFF21\u00C0\u00C1\u00C2\u1EA6\u1EA4\u1EAA\u1EA8\u00C3\u0100\u0102\u1EB0\u1EAE\u1EB4\u1EB2\u0226\u01E0\u00C4\u01DE\u1EA2\u00C5\u01FA\u01CD\u0200\u0202\u1EA0\u1EAC\u1EB6\u1E00\u0104\u023A\u2C6F' },
Expand Down Expand Up @@ -122,6 +132,7 @@ const range = (startYear, endYear = new Date().getFullYear()) => {

export {
cleanId,
countUniqueValues,
range,
removeDiacritics,
};
4 changes: 2 additions & 2 deletions server/src/utils/works.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const getFosmWorksByYear = async ({ results = [], options, pit, searchAfter }) =
publisher: result._source?.publisher_dissemination ?? result._source?.publisher ?? '',
title: result._source.title,
type: result._source?.genre_raw ?? result._source.genre,
year: result._source.year,
year: result?._source?.year?.toString() ?? '',
})));
if (hits.length > 0 && (Number(process.env.FOSM_MAX_SIZE) === 0 || results.length < Number(process.env.FOSM_MAX_SIZE))) {
// eslint-disable-next-line no-param-reassign
Expand Down Expand Up @@ -204,7 +204,7 @@ const getOpenAlexPublicationsByYear = (options, cursor = '*', previousResponse =
publisher: result?.primary_location?.source?.host_organization_name ?? '',
title: result?.display_name,
type: getTypeFromOpenAlex(result.type),
year: result?.publication_year,
year: result?.publication_year?.toString() ?? '',
})));
const nextCursor = response?.meta?.next_cursor;
if (nextCursor && hits.length > 0 && (Number(process.env.OPENALEX_MAX_SIZE) === 0 || results.length < Number(process.env.OPENALEX_MAX_SIZE))) {
Expand Down

0 comments on commit 94d23cc

Please sign in to comment.