1
1
/* eslint-disable sonarjs/cognitive-complexity */
2
2
import { getMetricsListFilterValues } from 'api/metricsExplorer/getMetricsListFilterValues' ;
3
3
import { getAttributesValues } from 'api/queryBuilder/getAttributesValues' ;
4
+ import { DATA_TYPE_VS_ATTRIBUTE_VALUES_KEY } from 'constants/queryBuilder' ;
4
5
import { DEBOUNCE_DELAY } from 'constants/queryBuilderFilterConfig' ;
5
6
import {
6
7
K8sCategory ,
@@ -16,6 +17,7 @@ import useDebounceValue from 'hooks/useDebounce';
16
17
import { cloneDeep , isEqual , uniqWith , unset } from 'lodash-es' ;
17
18
import { useCallback , useEffect , useMemo , useRef , useState } from 'react' ;
18
19
import { useDebounce } from 'react-use' ;
20
+ import { IAttributeValuesResponse } from 'types/api/queryBuilder/getAttributesValues' ;
19
21
import {
20
22
BaseAutocompleteData ,
21
23
DataTypes ,
@@ -157,6 +159,28 @@ export const useFetchKeysAndValues = (
157
159
enabled : isMetricsExplorer && isQueryEnabled && ! shouldUseSuggestions ,
158
160
} ) ;
159
161
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
+
160
184
/**
161
185
* Fetches the options to be displayed based on the selected value
162
186
* @param value - the selected value
@@ -226,8 +250,17 @@ export const useFetchKeysAndValues = (
226
250
}
227
251
228
252
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
+ }
231
264
}
232
265
} catch ( e ) {
233
266
console . error ( e ) ;
0 commit comments