Skip to content

Commit d3884cc

Browse files
committed
Add multi-index aggregation query to Guppy API
1 parent 660ff50 commit d3884cc

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

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

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import useSWR, { Fetcher, SWRResponse } from 'swr';
22
import { AggregationsData, JSONObject, StatsData } from '../../types';
33
import { Accessibility, GEN3_GUPPY_API } from '../../constants';
4-
import {
5-
convertFilterSetToGqlFilter,
6-
FilterSet,
7-
GQLFilter,
8-
isFilterEmpty,
9-
} from '../filters';
4+
import { convertFilterSetToGqlFilter, FilterSet, GQLFilter, isFilterEmpty, } from '../filters';
105
import { guppyApi, guppyApiSliceRequest } from './guppyApi';
116
import { RangeQueryRequest, SharedFieldMapping } from './types';
127
import { groupSharedFields } from './utils';
@@ -111,6 +106,11 @@ interface ObjectIdQueryResponse {
111106
index: string;
112107
}
113108

109+
interface MultiIndexFieldQueryRequest {
110+
indexAndFields: Array<QueryAggsParams>;
111+
accessibility?: Accessibility;
112+
}
113+
114114
export const explorerTags = guppyApi.enhanceEndpoints({
115115
addTagTypes: ['AGGS', 'COUNTS', 'STATS', 'TABLE_DATA', 'RAW_DATA'] as const,
116116
});
@@ -616,6 +616,35 @@ export const explorerApi = explorerTags.injectEndpoints({
616616
};
617617
},
618618
}),
619+
getGetMultiIndexAggregation: builder.query<
620+
ObjectIdQueryResponse,
621+
MultiIndexFieldQueryRequest
622+
>({
623+
query: ({
624+
indexAndFields,
625+
accessibility = Accessibility.ALL,
626+
}: MultiIndexFieldQueryRequest) => {
627+
// build each query
628+
const filters = indexAndFields.reduce(
629+
(acc, curr) => {
630+
acc[curr.type] = convertFilterSetToGqlFilter(curr.filters);
631+
return acc;
632+
},
633+
{} as Record<string, GQLFilter>,
634+
);
635+
const query = buildMultiIndexAggregationQuery(indexAndFields);
636+
return {
637+
query,
638+
variables: {
639+
...filters,
640+
accessibility,
641+
},
642+
};
643+
},
644+
transformResponse: (response: Record<string, any>) => {
645+
return response?.data?._aggregation ?? {};
646+
},
647+
}),
619648
generalGQL: builder.query<Record<string, unknown>, guppyApiSliceRequest>({
620649
query: ({ query, variables }) => {
621650
return {
@@ -645,6 +674,32 @@ export const useGetIndexFields = (index: string, indexPrefix = '') => {
645674
return data ?? [];
646675
};
647676

677+
const buildMultiIndexAggregationQuery = (
678+
indexesAndFields: Array<QueryAggsParams>,
679+
filterBasename: string = 'filter',
680+
) => {
681+
const filterNames = indexesAndFields.reduce(
682+
(acc, curr) => acc + `$${curr.type}${filterBasename}: JSON,`,
683+
'',
684+
);
685+
686+
let queryString = `query getAggs (${filterNames}) {`;
687+
688+
indexesAndFields.forEach((indexQuery) => {
689+
queryString += ` ${indexQuery.type}_aggregation {
690+
${indexQuery.type} (filter: ${indexQuery.type}${filterBasename}, filterSelf: ${indexQuery.filterSelf ? 'true' : 'false'}, accessibility: ${indexQuery.accessibility}) { _totalCount
691+
`;
692+
queryString += indexQuery.fields.map((field: string) =>
693+
histogramQueryStrForEachField(field),
694+
);
695+
queryString += ' } }';
696+
});
697+
698+
queryString += '}';
699+
700+
return queryString;
701+
};
702+
648703
export const buildGetAggregationQuery = (
649704
type: string,
650705
fields: ReadonlyArray<string>,
@@ -730,4 +785,6 @@ export const {
730785
useLazyCustomRangeQuery,
731786
useGetObjectIdsQuery,
732787
useLazyGetObjectIdsQuery,
788+
useGetGetMultiIndexAggregationQuery,
789+
useLazyGetGetMultiIndexAggregationQuery,
733790
} = explorerApi;

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

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

0 commit comments

Comments
 (0)