signoz/frontend/src/hooks/queryBuilder/useQueryOperations.ts
Chintan Sudani 76331001b7
fix: issues on WHERE search filter (#2629)
* fix: search filter validation on data source

* fix: value search not working for in/nin

* fix: unwanted key api while searching value & disabled tag

* fix: unnecessary , at end of in/nin value

* fix: added space after operator to get value

* fix: custom value not being selected

* fix: space after tag and value

* fix: api call debounce duration

* fix: suggested changes

* fix: updated query params

* fix: search filter data for logs and traces

* fix: search filter value data type issue

* fix: search filter value tag type

* fix: chip & iscolumn issue

* fix: null handled

* fix: label in list of search filter component

* fix: label in list of search filter component

* fix: code level changes

* fix: incorrect filter operators

* fix: key selection dancing

* fix: missing suggestion

* fix: keys are not getting updated

* fix: strange behaviour - removed duplicate options

* fix: driver id exclusion not working

* fix: loader when 0 options

* fix: exists/not-exists tag value issue

* fix: some weird behaviour about exists

* fix: added duplicate option remove logic at hook level

* fix: removed empty options from list

* fix: closable chip handler on edit

* fix: search filter validation on data source

* fix: value search not working for in/nin

* fix: unwanted key api while searching value & disabled tag

* fix: unnecessary , at end of in/nin value

* fix: added space after operator to get value

* fix: custom value not being selected

* fix: space after tag and value

* fix: api call debounce duration

* fix: suggested changes

* fix: updated query params

* fix: search filter data for logs and traces

* fix: search filter value data type issue

* fix: search filter value tag type

* fix: chip & iscolumn issue

* fix: null handled

* fix: label in list of search filter component

* fix: label in list of search filter component

* fix: code level changes

* fix: incorrect filter operators

* fix: key selection dancing

* fix: missing suggestion

* fix: keys are not getting updated

* fix: strange behaviour - removed duplicate options

* fix: driver id exclusion not working

* fix: loader when 0 options

* fix: exists/not-exists tag value issue

* fix: some weird behaviour about exists

* fix: added duplicate option remove logic at hook level

* fix: removed empty options from list

* fix: closable chip handler on edit

* fix: search filter validation on data source

* fix: lint issues is fixed

* fix: chip & iscolumn issue

* fix: lint changes are updated

* fix: undefined case handled

* fix: undefined case handled

* fix: removed settimeout

* fix: delete chip getting value undefined

* fix: payload correctness

* fix: incorrect value selection

* fix: key text typing doesn't change anything

* fix: search value issue

* fix: payload updated

* fix: auto populate value issue

* fix: payload updated & populate values

* fix: split value for in/nin

* fix: split value getting undefined

* fix: new version of search filter using papaparse library

* fix: removed unwanted space before operator

* fix: added exact find method & removed includes logic

* fix: issue when user create chip for exists not exists operator

* fix: white space logic removed

* fix: allow custom key in from list

* fix: issue when user create chip for exists not exists operator

* fix: removed unwanted includes

* fix: removed unwanted utils function

* fix: replaced join with papa unparse

* fix: removed get count of space utils

* fix: resolved build issue

* fix: code level fixes

* fix: space after key

* fix: quote a value if comma present

* fix: handle custom key object onchange

* chore: coverted into string

* Merge branch 'develop' into fix/issue-search-filter

* chore: eslint rule disabling is removed

* fix: serviceName contains sql

* chore: less restrictive expression

* fix: custom key selection issue

* chore: papa parse version is made exact

---------

Co-authored-by: Palash Gupta <palashgdev@gmail.com>
Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>
2023-05-12 12:30:00 +05:30

149 lines
4.0 KiB
TypeScript

import {
initialAggregateAttribute,
initialQueryBuilderFormValues,
mapOfFilters,
} from 'constants/queryBuilder';
import { useQueryBuilder } from 'hooks/queryBuilder/useQueryBuilder';
import { getOperatorsBySourceAndPanelType } from 'lib/newQueryBuilder/getOperatorsBySourceAndPanelType';
import { findDataTypeOfOperator } from 'lib/query/findDataTypeOfOperator';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { BaseAutocompleteData } from 'types/api/queryBuilder/queryAutocompleteResponse';
import { IBuilderQuery } from 'types/api/queryBuilder/queryBuilderData';
import {
HandleChangeQueryData,
UseQueryOperations,
} from 'types/common/operations.types';
import { DataSource } from 'types/common/queryBuilder';
export const useQueryOperations: UseQueryOperations = ({ query, index }) => {
const {
handleSetQueryData,
removeEntityByIndex,
panelType,
} = useQueryBuilder();
const [operators, setOperators] = useState<string[]>([]);
const [listOfAdditionalFilters, setListOfAdditionalFilters] = useState<
string[]
>([]);
const { dataSource, aggregateOperator } = query;
const handleChangeOperator = useCallback(
(value: string): void => {
const aggregateDataType: BaseAutocompleteData['dataType'] =
query.aggregateAttribute.dataType;
const typeOfValue = findDataTypeOfOperator(value);
const shouldResetAggregateAttribute =
(aggregateDataType === 'string' || aggregateDataType === 'bool') &&
typeOfValue === 'number';
const newQuery: IBuilderQuery = {
...query,
aggregateOperator: value,
having: [],
orderBy: [],
limit: null,
filters: { items: [], op: 'AND' },
...(shouldResetAggregateAttribute
? { aggregateAttribute: initialAggregateAttribute }
: {}),
};
handleSetQueryData(index, newQuery);
},
[index, query, handleSetQueryData],
);
const getNewListOfAdditionalFilters = useCallback(
(dataSource: DataSource): string[] =>
mapOfFilters[dataSource].map((item) => item.text),
[],
);
const handleChangeAggregatorAttribute = useCallback(
(value: BaseAutocompleteData): void => {
const newQuery: IBuilderQuery = {
...query,
aggregateAttribute: value,
having: [],
};
handleSetQueryData(index, newQuery);
},
[index, query, handleSetQueryData],
);
const handleChangeDataSource = useCallback(
(nextSource: DataSource): void => {
const newOperators = getOperatorsBySourceAndPanelType({
dataSource: nextSource,
panelType,
});
const entries = Object.entries(initialQueryBuilderFormValues).filter(
([key]) => key !== 'queryName' && key !== 'expression',
);
const initCopyResult = Object.fromEntries(entries);
const newQuery: IBuilderQuery = {
...query,
...initCopyResult,
dataSource: nextSource,
aggregateOperator: newOperators[0],
};
setOperators(newOperators);
handleSetQueryData(index, newQuery);
},
[index, query, panelType, handleSetQueryData],
);
const handleDeleteQuery = useCallback(() => {
removeEntityByIndex('queryData', index);
}, [removeEntityByIndex, index]);
const handleChangeQueryData: HandleChangeQueryData = useCallback(
(key, value) => {
const newQuery: IBuilderQuery = {
...query,
[key]: value,
};
handleSetQueryData(index, newQuery);
},
[query, index, handleSetQueryData],
);
const isMetricsDataSource = useMemo(
() => query.dataSource === DataSource.METRICS,
[query.dataSource],
);
useEffect(() => {
const initialOperators = getOperatorsBySourceAndPanelType({
dataSource,
panelType,
});
setOperators(initialOperators);
}, [dataSource, panelType]);
useEffect(() => {
const additionalFilters = getNewListOfAdditionalFilters(dataSource);
setListOfAdditionalFilters(additionalFilters);
}, [dataSource, aggregateOperator, getNewListOfAdditionalFilters]);
return {
isMetricsDataSource,
operators,
listOfAdditionalFilters,
handleChangeOperator,
handleChangeAggregatorAttribute,
handleChangeDataSource,
handleDeleteQuery,
handleChangeQueryData,
};
};