Skip to content

Commit 32d1448

Browse files
authored
fix: update the auto-complete API reading logic (#7254)
1 parent e614d6b commit 32d1448

File tree

7 files changed

+80
-18
lines changed

7 files changed

+80
-18
lines changed

frontend/src/components/CeleryTask/CeleryTaskConfigOptions/useGetCeleryFilters.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable react-hooks/exhaustive-deps */
22
import { DefaultOptionType } from 'antd/es/select';
33
import { getAttributesValues } from 'api/queryBuilder/getAttributesValues';
4+
import { DATA_TYPE_VS_ATTRIBUTE_VALUES_KEY } from 'constants/queryBuilder';
45
import { REACT_QUERY_KEY } from 'constants/reactQueryKeys';
56
import { useQuery } from 'react-query';
67
import { useSelector } from 'react-redux';
@@ -67,9 +68,13 @@ export function useGetAllFilters(props: Filters): GetAllFiltersResponse {
6768

6869
const uniqueValues = [
6970
...new Set(
70-
responses.flatMap(
71-
({ payload }) => Object.values(payload || {}).find((el) => !!el) || [],
72-
),
71+
responses.flatMap(({ payload }) => {
72+
if (!payload) return [];
73+
74+
const dataType = filterAttributeKeyDataType || DataTypes.String;
75+
const key = DATA_TYPE_VS_ATTRIBUTE_VALUES_KEY[dataType];
76+
return key ? payload[key] || [] : [];
77+
}),
7378
),
7479
];
7580

frontend/src/components/QuickFilters/FilterRenderers/Checkbox/Checkbox.tsx

+10-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import {
1010
IQuickFiltersConfig,
1111
QuickFiltersSource,
1212
} from 'components/QuickFilters/types';
13-
import { OPERATORS } from 'constants/queryBuilder';
13+
import {
14+
DATA_TYPE_VS_ATTRIBUTE_VALUES_KEY,
15+
OPERATORS,
16+
} from 'constants/queryBuilder';
1417
import { DEBOUNCE_DELAY } from 'constants/queryBuilderFilterConfig';
1518
import { getOperatorValue } from 'container/QueryBuilder/filters/QueryBuilderSearch/utils';
1619
import { useGetAggregateValues } from 'hooks/queryBuilder/useGetAggregateValues';
@@ -76,12 +79,12 @@ export default function CheckboxFilter(props: ICheckboxProps): JSX.Element {
7679
},
7780
);
7881

79-
const attributeValues: string[] = useMemo(
80-
() =>
81-
((Object.values(data?.payload || {}).find((el) => !!el) ||
82-
[]) as string[]).filter((val) => !isEmpty(val)),
83-
[data?.payload],
84-
);
82+
const attributeValues: string[] = useMemo(() => {
83+
const dataType = filter.attributeKey.dataType || DataTypes.String;
84+
const key = DATA_TYPE_VS_ATTRIBUTE_VALUES_KEY[dataType];
85+
return (data?.payload?.[key] || []).filter((val) => !isEmpty(val));
86+
}, [data?.payload, filter.attributeKey.dataType]);
87+
8588
const currentAttributeKeys = attributeValues.slice(0, visibleItemsCount);
8689

8790
const setSearchTextDebounced = useDebouncedFn((...args) => {

frontend/src/constants/queryBuilder.ts

+16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// ** Helpers
22
import { createIdFromObjectFields } from 'lib/createIdFromObjectFields';
33
import { createNewBuilderItemName } from 'lib/newQueryBuilder/createNewBuilderItemName';
4+
import { IAttributeValuesResponse } from 'types/api/queryBuilder/getAttributesValues';
45
import {
56
AutocompleteType,
67
BaseAutocompleteData,
@@ -417,3 +418,18 @@ export enum PanelDisplay {
417418
PIE = 'Pie',
418419
HISTOGRAM = 'Histogram',
419420
}
421+
422+
export const DATA_TYPE_VS_ATTRIBUTE_VALUES_KEY: Record<
423+
DataTypes,
424+
keyof IAttributeValuesResponse
425+
> = {
426+
[DataTypes.String]: 'stringAttributeValues',
427+
[DataTypes.Float64]: 'numberAttributeValues',
428+
[DataTypes.Int64]: 'numberAttributeValues',
429+
[DataTypes.bool]: 'boolAttributeValues',
430+
[DataTypes.ArrayFloat64]: 'numberAttributeValues',
431+
[DataTypes.ArrayInt64]: 'numberAttributeValues',
432+
[DataTypes.ArrayString]: 'stringAttributeValues',
433+
[DataTypes.ArrayBool]: 'boolAttributeValues',
434+
[DataTypes.EMPTY]: 'stringAttributeValues',
435+
};

frontend/src/container/QueryBuilder/filters/QueryBuilderSearchV2/QueryBuilderSearchV2.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import './QueryBuilderSearchV2.styles.scss';
44
import { Select, Spin, Tag, Tooltip } from 'antd';
55
import cx from 'classnames';
66
import {
7+
DATA_TYPE_VS_ATTRIBUTE_VALUES_KEY,
78
OPERATORS,
89
QUERY_BUILDER_OPERATORS_BY_TYPES,
910
QUERY_BUILDER_SEARCH_VALUES,
@@ -737,9 +738,11 @@ function QueryBuilderSearchV2(
737738
values.push(tagValue[tagValue.length - 1]);
738739
} else if (!isEmpty(tagValue)) values.push(tagValue);
739740

740-
values.push(
741-
...(Object.values(attributeValues?.payload || {}).find((el) => !!el) || []),
742-
);
741+
if (attributeValues?.payload) {
742+
const dataType = currentFilterItem?.key?.dataType || DataTypes.String;
743+
const key = DATA_TYPE_VS_ATTRIBUTE_VALUES_KEY[dataType];
744+
values.push(...(attributeValues?.payload?.[key] || []));
745+
}
743746

744747
setDropdownOptions(
745748
values.map((val) => ({

frontend/src/hooks/queryBuilder/useFetchKeysAndValues.ts

+35-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable sonarjs/cognitive-complexity */
22
import { getMetricsListFilterValues } from 'api/metricsExplorer/getMetricsListFilterValues';
33
import { getAttributesValues } from 'api/queryBuilder/getAttributesValues';
4+
import { DATA_TYPE_VS_ATTRIBUTE_VALUES_KEY } from 'constants/queryBuilder';
45
import { DEBOUNCE_DELAY } from 'constants/queryBuilderFilterConfig';
56
import {
67
K8sCategory,
@@ -16,6 +17,7 @@ import useDebounceValue from 'hooks/useDebounce';
1617
import { cloneDeep, isEqual, uniqWith, unset } from 'lodash-es';
1718
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
1819
import { useDebounce } from 'react-use';
20+
import { IAttributeValuesResponse } from 'types/api/queryBuilder/getAttributesValues';
1921
import {
2022
BaseAutocompleteData,
2123
DataTypes,
@@ -157,6 +159,28 @@ export const useFetchKeysAndValues = (
157159
enabled: isMetricsExplorer && isQueryEnabled && !shouldUseSuggestions,
158160
});
159161

162+
function isAttributeValuesResponse(
163+
payload: any,
164+
): payload is IAttributeValuesResponse {
165+
return (
166+
payload &&
167+
(Array.isArray(payload.stringAttributeValues) ||
168+
payload.stringAttributeValues === null ||
169+
Array.isArray(payload.numberAttributeValues) ||
170+
payload.numberAttributeValues === null ||
171+
Array.isArray(payload.boolAttributeValues) ||
172+
payload.boolAttributeValues === null)
173+
);
174+
}
175+
176+
function isMetricsListFilterValuesData(
177+
payload: any,
178+
): payload is { filterValues: string[] } {
179+
return (
180+
payload && 'filterValues' in payload && Array.isArray(payload.filterValues)
181+
);
182+
}
183+
160184
/**
161185
* Fetches the options to be displayed based on the selected value
162186
* @param value - the selected value
@@ -226,8 +250,17 @@ export const useFetchKeysAndValues = (
226250
}
227251

228252
if (payload) {
229-
const values = Object.values(payload).find((el) => !!el) || [];
230-
setResults(values);
253+
if (isAttributeValuesResponse(payload)) {
254+
const dataType = filterAttributeKey?.dataType ?? DataTypes.String;
255+
const key = DATA_TYPE_VS_ATTRIBUTE_VALUES_KEY[dataType];
256+
setResults(key ? payload[key] || [] : []);
257+
return;
258+
}
259+
260+
if (isMetricsExplorer && isMetricsListFilterValuesData(payload)) {
261+
setResults(payload.filterValues || []);
262+
return;
263+
}
231264
}
232265
} catch (e) {
233266
console.error(e);

frontend/src/pages/MessagingQueues/MQGraph/useGetAllConfigOptions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function useGetAllConfigOptions(
3434
});
3535

3636
if (payload) {
37-
const values = Object.values(payload).find((el) => !!el) || [];
37+
const values = payload.stringAttributeValues || [];
3838
const options: DefaultOptionType[] = values.map((val: string) => ({
3939
label: val,
4040
value: val,

frontend/src/pages/TracesExplorer/Filter/filterUtils.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable react-hooks/exhaustive-deps */
22
import { getAttributesValues } from 'api/queryBuilder/getAttributesValues';
3+
import { DATA_TYPE_VS_ATTRIBUTE_VALUES_KEY } from 'constants/queryBuilder';
34
import { Dispatch, SetStateAction, useEffect, useState } from 'react';
45
import {
56
BaseAutocompleteData,
@@ -327,8 +328,9 @@ export function useGetAggregateValues(
327328
});
328329

329330
if (payload) {
330-
const values = Object.values(payload).find((el) => !!el) || [];
331-
setResults(values);
331+
const key =
332+
DATA_TYPE_VS_ATTRIBUTE_VALUES_KEY[keyData.dataType as Partial<DataTypes>];
333+
setResults(key ? payload[key] || [] : []);
332334
}
333335
} catch (e) {
334336
console.error(e);

0 commit comments

Comments
 (0)