Skip to content

Commit bf8b5b4

Browse files
committed
Refactor CohortBuilder and chart components: simplify CohortPanel props, update multi-index aggregation query, and modularize MultiTrackHorizontalBarChart.
1 parent e1c2329 commit bf8b5b4

File tree

7 files changed

+270
-200
lines changed

7 files changed

+270
-200
lines changed

packages/core/src/features/guppy/guppySlice.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,20 @@ interface MultiIndexFieldQueryRequest {
111111
accessibility?: Accessibility;
112112
}
113113

114+
interface FieldAggregation {
115+
histogram: AggregationsData;
116+
totalCount: number;
117+
}
118+
119+
interface IndexAggregation {
120+
[field: string]: FieldAggregation | number;
121+
totalCount: number;
122+
}
123+
124+
interface MultiIndexFieldAggregationResponse {
125+
[index: string]: IndexAggregation;
126+
}
127+
114128
export const explorerTags = guppyApi.enhanceEndpoints({
115129
addTagTypes: ['AGGS', 'COUNTS', 'STATS', 'TABLE_DATA', 'RAW_DATA'] as const,
116130
});
@@ -463,8 +477,10 @@ export const explorerApi = explorerTags.injectEndpoints({
463477
_meta,
464478
args,
465479
): number => {
480+
if (!('data' in response) || !response.data) return 0;
481+
466482
return (
467-
response?.data[`${args?.indexPrefix ?? ''}_aggregation`][args.type]
483+
response?.data?.[`${args?.indexPrefix ?? ''}_aggregation`][args.type]
468484
?._totalCount ?? 0
469485
);
470486
},
@@ -617,7 +633,7 @@ export const explorerApi = explorerTags.injectEndpoints({
617633
},
618634
}),
619635
getGetMultiIndexAggregation: builder.query<
620-
Record<string, any>,
636+
MultiIndexFieldAggregationResponse,
621637
MultiIndexFieldQueryRequest
622638
>({
623639
query: ({
@@ -632,17 +648,23 @@ export const explorerApi = explorerTags.injectEndpoints({
632648
},
633649
{} as Record<string, GQLFilter>,
634650
);
635-
const query = buildMultiIndexAggregationQuery(indexAndFields);
651+
const query = buildMultiIndexAggregationQuery(
652+
indexAndFields,
653+
accessibility,
654+
);
636655
return {
637656
query,
638657
variables: {
639658
...filters,
640-
accessibility,
641659
},
642660
};
643661
},
644-
transformResponse: (response: Record<string, any>) => {
645-
return response?.data?._aggregation ?? {};
662+
transformResponse: (response: {
663+
data: { _aggregation?: IndexAggregation };
664+
}) => {
665+
if (!response?.data?._aggregation) return {};
666+
667+
return response.data._aggregation;
646668
},
647669
}),
648670
generalGQL: builder.query<Record<string, unknown>, guppyApiSliceRequest>({
@@ -676,18 +698,18 @@ export const useGetIndexFields = (index: string, indexPrefix = '') => {
676698

677699
const buildMultiIndexAggregationQuery = (
678700
indexesAndFields: Array<QueryAggsParams>,
701+
accessibility = Accessibility.ALL,
679702
filterBasename: string = 'filter',
680703
) => {
681704
const filterNames = indexesAndFields.reduce(
682705
(acc, curr) => acc + `$${curr.type}${filterBasename}: JSON,`,
683706
'',
684707
);
685708

686-
let queryString = `query getAggs (${filterNames}) {`;
709+
let queryString = `query getMultiIndexAggs (${filterNames}) {`;
687710

688711
indexesAndFields.forEach((indexQuery) => {
689-
queryString += ` ${indexQuery.type}_aggregation {
690-
${indexQuery.type} (filter: ${indexQuery.type}${filterBasename}, filterSelf: ${indexQuery.filterSelf ? 'true' : 'false'}, accessibility: ${indexQuery.accessibility}) { _totalCount
712+
queryString += ` ${indexQuery.indexPrefix ?? ''}_aggregation { ${indexQuery.type} (filter: $${indexQuery.type}${filterBasename}, filterSelf: ${indexQuery.filterSelf ? 'true' : 'false'}, accessibility: ${accessibility}) { totalCount:_totalCount
691713
`;
692714
queryString += indexQuery.fields.map((field: string) =>
693715
histogramQueryStrForEachField(field),

packages/core/src/features/guppy/queryGenerators.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const histogramQueryStrForEachField = (field: string): string => {
2323
if (splittedFieldArray.length === 0) {
2424
return `
2525
${splittedField} {
26-
_totalCount
26+
totalCount: _totalCount
2727
histogram {
2828
key
2929
count

packages/frontend/src/components/Content/ChartContent.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import { createChart } from '../charts/createChart';
33
import { ChartProps } from '../charts';
44
import {
5+
Accessibility,
56
EmptyFilterSet,
67
HistogramDataArray,
78
useGetGetMultiIndexAggregationQuery,
@@ -19,15 +20,17 @@ const ChartContent = ({ chartType, parameters }: ChartContentProps) => {
1920
const chart: ChartProps = parameters?.chart;
2021
const dataFunctionParameters = parameters?.dataFetch;
2122

22-
const processedDataFunctionParameters = dataFunctionParameters?.map(
23-
(props: any) => ({
23+
const processedDataFunctionParameters =
24+
dataFunctionParameters?.indexAndFields?.map((props: any) => ({
2425
...props,
2526
filters: EmptyFilterSet,
26-
}),
27-
);
27+
}));
2828

2929
const { data, isFetching, isError } = useGetGetMultiIndexAggregationQuery(
30-
processedDataFunctionParameters,
30+
{
31+
indexAndFields: processedDataFunctionParameters,
32+
accessibility: dataFunctionParameters?.accessibility ?? Accessibility.ALL,
33+
},
3134
{ skip: !processedDataFunctionParameters },
3235
);
3336

@@ -39,6 +42,7 @@ const ChartContent = ({ chartType, parameters }: ChartContentProps) => {
3942
return <ErrorCard message="Error fetching chart data" />;
4043
}
4144

45+
console.log('chart data', data);
4246
const chartComponent = createChart(chartType, {
4347
data: data === undefined ? [] : (data as HistogramDataArray),
4448
total: 1,

0 commit comments

Comments
 (0)