Skip to content

Commit

Permalink
#1152 - allow for facet counts to be undefined so we can choose not t…
Browse files Browse the repository at this point in the history
…o display facet counts

- currently only added the instrument fix to DatafileSearchTable
  • Loading branch information
louise-davies committed Oct 4, 2023
1 parent 5618130 commit cdc01f2
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 177 deletions.
19 changes: 8 additions & 11 deletions packages/datagateway-common/src/api/lucene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ interface RangeFacetResponse {
}

export interface SearchFacet {
[dimension: string]: { [label: string]: number | RangeFacetResponse };
[dimension: string]: {
[label: string]: number | RangeFacetResponse | undefined;
};
}

export interface SearchResponse {
Expand Down Expand Up @@ -337,22 +339,17 @@ export const useLuceneSearchInfinite = (
// and we are searching investigation, only investigation.type.name will be added to the final filter object.
luceneParams.filters = Object.entries(facetFilters).reduce<FiltersType>(
(filters, [filterKey, filterValue]) => {
if (new RegExp(`^${datasearchType}.*`, 'i').test(filterKey)) {
const k = filterKey[0].toLocaleLowerCase() + filterKey.substring(1);
filters[k] = filterValue;
}
// if (new RegExp(`^${datasearchType}.*`, 'i').test(filterKey)) {
const k = filterKey[0].toLocaleLowerCase() + filterKey.substring(1);
filters[k] = filterValue;
// }
return filters;
},
{}
);
}

return useInfiniteQuery<
SearchResponse,
AxiosError<LuceneError>,
SearchResponse,
[string, DatasearchType, LuceneSearchParams]
>(
return useInfiniteQuery(
['search', datasearchType, luceneParams],
() => fetchLuceneData(datasearchType, luceneParams, { icatUrl }),
{
Expand Down
54 changes: 16 additions & 38 deletions packages/datagateway-search/public/res/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,14 @@
"facetDimensionLabel": {
"Investigation.type.name": "Type",
"InvestigationParameter.type.name": "Parameter name",
"Sample.type.name": "Sample",
"Sample.sample.type.name": "Sample",
"InvestigationInstrument.instrument.name": "Instrument",
"Dataset.type.name": "Type",
"Dataset.sample.type.name": "Sample",
"DatasetParameter.type.name": "Parameter name",
"Datafile.datafileFormat.name": "Format",
"DatafileParameter.type.name": "Parameter name"
"DatafileParameter.type.name": "Parameter name",
"Datafile.sample.type.name": "Sample"
},
"parameterFilters": {
"title": "Parameter filters",
Expand Down Expand Up @@ -270,48 +274,22 @@
"show": "Show Advanced Filters",
"hide": "Hide Advanced Filters",
"icons": {
"title": [
"Title",
"Name",
"Type"
],
"fingerprint": [
"Visit ID",
"Name"
],
"public": [
"DOI"
],
"confirmation_number": [
"Dataset Count",
" Datafile Count"
],
"assessment": [
"Instrument",
"Beamline"
],
"title": ["Title", "Name", "Type"],
"fingerprint": ["Visit ID", "Name"],
"public": ["DOI"],
"confirmation_number": ["Dataset Count", " Datafile Count"],
"assessment": ["Instrument", "Beamline"],
"calendar_today": [
"Start Date",
"End Date",
"Create Time",
"Modified Time"
],
"explore": [
"Location"
],
"save": [
"Size"
],
"description": [
"Description",
"Summary"
],
"link": [
"URL"
],
"person": [
"Principal Investigator"
]
"explore": ["Location"],
"save": ["Size"],
"description": ["Description", "Summary"],
"link": ["URL"],
"person": ["Principal Investigator"]
}
},
"entity_card": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
import {
AddToCartButton,
CardView,
type CVCustomFilters,
DatasetDetailsPanel,
DLSDatasetDetailsPanel,
DownloadButton,
Expand Down Expand Up @@ -66,8 +65,16 @@ const DatasetCardView = (props: DatasetCardViewProps): React.ReactElement => {
() => parseSearchToQuery(location.search),
[location.search]
);
const { startDate, endDate, page, results, sort, filters, restrict } =
queryParams;
const {
startDate,
endDate,
page,
results,
sort,
filters,
restrict,
dataset,
} = queryParams;
const searchText = queryParams.searchText ? queryParams.searchText : '';

const minNumResults = useSelector(
Expand Down Expand Up @@ -101,7 +108,8 @@ const DatasetCardView = (props: DatasetCardViewProps): React.ReactElement => {
},
],
},
filters
filters,
{ enabled: dataset }
);

const {
Expand All @@ -127,50 +135,6 @@ const DatasetCardView = (props: DatasetCardViewProps): React.ReactElement => {
return response.results?.map((result) => result.id) ?? [];
}

const mapFacets = React.useCallback(
(responses: SearchResponse[]): CVCustomFilters[] => {
const filters: { [dimension: string]: { [label: string]: number } } = {};
responses.forEach((response) => {
if (response.dimensions !== undefined) {
Object.entries(response.dimensions).forEach((dimension) => {
const dimensionKey = dimension[0];
const dimensionValue = dimension[1];
if (!Object.keys(filters).includes(dimensionKey)) {
filters[dimensionKey] = {};
}
Object.entries(dimensionValue).forEach((labelValue) => {
const label = labelValue[0];
const count =
typeof labelValue[1] === 'number'
? labelValue[1]
: labelValue[1].count;
if (Object.keys(filters[dimensionKey]).includes(label)) {
filters[dimensionKey][label] += count;
} else {
filters[dimensionKey][label] = count;
}
});
});
}
});
return Object.entries(filters).map((dimension) => {
const dimensionKey = dimension[0].toLocaleLowerCase();
const dimensionValue = dimension[1];
return {
label: t(dimensionKey),
dataKey: dimensionKey,
dataKeySearch: dimensionKey.replace('dataset.', ''),
filterItems: Object.entries(dimensionValue).map((labelValue) => ({
name: labelValue[0],
count: labelValue[1].toString(),
})),
prefixLabel: true,
};
});
},
[t]
);

const { paginatedSource, aggregatedIds, aborted } = React.useMemo(() => {
if (data) {
const aggregatedIds = data.pages
Expand All @@ -187,18 +151,16 @@ const DatasetCardView = (props: DatasetCardViewProps): React.ReactElement => {
return {
paginatedSource: aggregatedSource.slice(minResult, maxResult),
aggregatedIds: aggregatedIds,
customFilters: mapFacets(data.pages),
aborted: data.pages[data.pages.length - 1].aborted,
};
} else {
return {
paginatedSource: [],
aggregatedIds: [],
customFilters: [],
aborted: false,
};
}
}, [data, fetchNextPage, hasNextPage, mapFacets, page, results]);
}, [data, fetchNextPage, hasNextPage, page, results]);

const handleSort = useSort();
const pushFilter = usePushDatasetFilter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import {
import { useSelector } from 'react-redux';
import { useHistory, useLocation } from 'react-router-dom';
import { StateType } from '../state/app.types';
import { CVCustomFilters } from 'datagateway-common/lib/card/cardView.component';
import useFacetFilters from '../facet/useFacetFilters';
import FacetPanel from '../facet/components/facetPanel/facetPanel.component';
import { facetClassificationFromSearchResponses } from '../facet/facet';
Expand Down Expand Up @@ -76,8 +75,16 @@ const InvestigationCardView = (
() => parseSearchToQuery(location.search),
[location.search]
);
const { filters, sort, page, results, startDate, endDate, restrict } =
queryParams;
const {
filters,
sort,
page,
results,
startDate,
endDate,
restrict,
investigation,
} = queryParams;
const searchText = queryParams.searchText ? queryParams.searchText : '';

const handleSort = useSort();
Expand Down Expand Up @@ -120,7 +127,8 @@ const InvestigationCardView = (
},
],
},
filters
filters,
{ enabled: investigation }
);

function mapSource(response: SearchResponse): SearchResultSource[] {
Expand All @@ -131,55 +139,6 @@ const InvestigationCardView = (
return response.results?.map((result) => result.id) ?? [];
}

const mapFacets = React.useCallback(
(responses: SearchResponse[]): CVCustomFilters[] => {
// Aggregate pages
const filters: { [dimension: string]: { [label: string]: number } } = {};
responses.forEach((response) => {
if (response.dimensions !== undefined) {
Object.entries(response.dimensions).forEach((dimension) => {
const dimensionKey = dimension[0];
const dimensionValue = dimension[1];
if (!Object.keys(filters).includes(dimensionKey)) {
filters[dimensionKey] = {};
}
Object.entries(dimensionValue).forEach((labelValue) => {
const label = labelValue[0];
const count =
typeof labelValue[1] === 'number'
? labelValue[1]
: labelValue[1].count;
if (Object.keys(filters[dimensionKey]).includes(label)) {
filters[dimensionKey][label] += count;
} else {
filters[dimensionKey][label] = count;
}
});
});
}
});
// Convert to custom filters
return Object.entries(filters).map((dimension) => {
const dimensionKey = dimension[0].toLocaleLowerCase();
const dimensionValue = dimension[1];
return {
label: t(dimensionKey),
dataKey: dimensionKey,
dataKeySearch: dimensionKey
.replace('investigation.', '')
.replace('investigationparameter.', 'investigationparameter ')
.replace('sample.', 'sample '),
filterItems: Object.entries(dimensionValue).map((labelValue) => ({
name: labelValue[0],
count: labelValue[1].toString(),
})),
prefixLabel: true,
};
});
},
[t]
);

const {
selectedFacetFilters,
addFacetFilter,
Expand Down Expand Up @@ -211,18 +170,16 @@ const InvestigationCardView = (
return {
paginatedSource: aggregatedSource.slice(minResult, maxResult),
aggregatedIds: aggregatedIds,
customFilters: mapFacets(data.pages),
aborted: data.pages[data.pages.length - 1].aborted,
};
} else {
return {
paginatedSource: [],
aggregatedIds: [],
customFilters: [],
aborted: false,
};
}
}, [data, fetchNextPage, hasNextPage, mapFacets, page, results]);
}, [data, fetchNextPage, hasNextPage, page, results]);

// hierarchy === 'isis' ? data : undefined is a 'hack' to only perform
// the correct calculation queries for each facility
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ function FacetPanel({
classificationLabel={classificationLabel}
selected={isItemSelected}
onSelect={(classificationLabel, selected) => {
console.log('classificationLabel');
console.log('selected', selected);
if (selected) {
onAddFilter(dimension, classificationLabel);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ interface NewParameterFilterCreatorProps {
onClose: () => void;
}

interface ParameterValueFacet {
label: string;
count: number;
from?: number;
to?: number;
}

const PARAMETER_VALUE_SELECTOR: Record<
ParameterValueType,
(props: ParameterValueSelectorProps) => JSX.Element
Expand Down Expand Up @@ -196,4 +189,3 @@ function NewParameterFilterCreator({
}

export default NewParameterFilterCreator;
export type { ParameterValueFacet };
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ function parameterFacetsFromSearchResponse(

return Object.values(response.dimensions).flatMap((labelValues) =>
Object.entries(labelValues).map(([label, value]) =>
typeof value === 'number'
? { label, count: value }
: {
typeof value === 'object'
? {
label,
count: value.count,
from: value.from,
to: value.to,
}
: { label, count: value }
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface ParameterValueFilter {

interface ParameterValueFacet {
label: string;
count: number;
count?: number;
from?: number;
to?: number;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
type FiltersType,
useLuceneFacet,
} from 'datagateway-common';
import type { ParameterValueFacet } from '../newParameterFilterCreator.component';
import type { ParameterValueFacet } from '../parameterFilterTypes';
import parameterFacetsFromSearchResponse from '../parameterFacetsFromSearchResponse';
import ParameterValueSelectorProps from './parameterValueSelectorProps';
import { useTranslation } from 'react-i18next';
Expand Down
Loading

0 comments on commit cdc01f2

Please sign in to comment.