From 81b10d126a6d51b3381df1f187b819225f2b3473 Mon Sep 17 00:00:00 2001 From: Palash Gupta Date: Mon, 2 Oct 2023 05:04:04 +0000 Subject: [PATCH] feat: has and nhas filters is now enabled (#3567) --- .../src/container/LogDetailedView/config.ts | 13 ++++ .../container/LogDetailedView/util.test.ts | 4 +- .../src/container/LogDetailedView/utils.tsx | 77 +++++++++++-------- .../filters/QueryBuilderSearch/index.tsx | 5 +- 4 files changed, 59 insertions(+), 40 deletions(-) create mode 100644 frontend/src/container/LogDetailedView/config.ts diff --git a/frontend/src/container/LogDetailedView/config.ts b/frontend/src/container/LogDetailedView/config.ts new file mode 100644 index 0000000000..cd34023699 --- /dev/null +++ b/frontend/src/container/LogDetailedView/config.ts @@ -0,0 +1,13 @@ +import { DataTypes } from 'types/api/queryBuilder/queryAutocompleteResponse'; + +export const typeToArrayTypeMapper: { [key in DataTypes]: DataTypes } = { + [DataTypes.String]: DataTypes.ArrayString, + [DataTypes.Float64]: DataTypes.ArrayFloat64, + [DataTypes.Int64]: DataTypes.ArrayInt64, + [DataTypes.bool]: DataTypes.ArrayBool, + [DataTypes.EMPTY]: DataTypes.EMPTY, + [DataTypes.ArrayFloat64]: DataTypes.ArrayFloat64, + [DataTypes.ArrayInt64]: DataTypes.ArrayInt64, + [DataTypes.ArrayString]: DataTypes.ArrayString, + [DataTypes.ArrayBool]: DataTypes.ArrayBool, +}; diff --git a/frontend/src/container/LogDetailedView/util.test.ts b/frontend/src/container/LogDetailedView/util.test.ts index 4f080e23c1..d5918f2bca 100644 --- a/frontend/src/container/LogDetailedView/util.test.ts +++ b/frontend/src/container/LogDetailedView/util.test.ts @@ -176,8 +176,8 @@ describe('Get Data Types utils', () => { }); // Edge cases - it('should return Int64 for empty array input', () => { - expect(getDataTypes([])).toBe(DataTypes.Int64); + it('should return Empty for empty array input', () => { + expect(getDataTypes([])).toBe(DataTypes.EMPTY); }); it('should handle mixed array (return based on first element)', () => { diff --git a/frontend/src/container/LogDetailedView/utils.tsx b/frontend/src/container/LogDetailedView/utils.tsx index 02890e7dc9..5530c970d3 100644 --- a/frontend/src/container/LogDetailedView/utils.tsx +++ b/frontend/src/container/LogDetailedView/utils.tsx @@ -5,6 +5,7 @@ import { ILog, ILogAggregateAttributesResources } from 'types/api/logs/log'; import { DataTypes } from 'types/api/queryBuilder/queryAutocompleteResponse'; import BodyTitleRenderer from './BodyTitleRenderer'; +import { typeToArrayTypeMapper } from './config'; import { AnyObject, IFieldAttributes } from './LogDetailedView.types'; export const recursiveParseJSON = (obj: string): Record => { @@ -107,40 +108,6 @@ export function flattenObject(obj: AnyObject, prefix = ''): AnyObject { }, {}); } -const isFloat = (num: number): boolean => num % 1 !== 0; - -export const getDataTypes = (value: unknown): DataTypes => { - if (typeof value === 'string') { - return DataTypes.String; - } - - if (typeof value === 'number') { - return isFloat(value) ? DataTypes.Float64 : DataTypes.Int64; - } - - if (typeof value === 'boolean') { - return DataTypes.bool; - } - - if (Array.isArray(value)) { - const firstElement = value[0]; - - if (typeof firstElement === 'string') { - return DataTypes.ArrayString; - } - - if (typeof firstElement === 'boolean') { - return DataTypes.ArrayBool; - } - - if (typeof firstElement === 'number') { - return isFloat(firstElement) ? DataTypes.ArrayFloat64 : DataTypes.ArrayInt64; - } - } - - return DataTypes.Int64; -}; - export const generateFieldKeyForArray = ( fieldKey: string, dataType: DataTypes, @@ -217,3 +184,45 @@ export const aggregateAttributesResourcesToString = (logData: ILog): string => { return JSON.stringify(outputJson, null, 2); }; + +const isFloat = (num: number): boolean => num % 1 !== 0; + +const isBooleanString = (str: string): boolean => + str.toLowerCase() === 'true' || str.toLowerCase() === 'false'; + +const determineType = (val: unknown): DataTypes => { + if (typeof val === 'string') { + if (isBooleanString(val)) { + return DataTypes.bool; + } + + const numberValue = parseFloat(val); + + if (!Number.isNaN(numberValue)) { + return isFloat(numberValue) ? DataTypes.Float64 : DataTypes.Int64; + } + + return DataTypes.String; + } + + if (typeof val === 'number') { + return isFloat(val) ? DataTypes.Float64 : DataTypes.Int64; + } + + if (typeof val === 'boolean') { + return DataTypes.bool; + } + + return DataTypes.EMPTY; +}; + +export const getDataTypes = (value: unknown): DataTypes => { + const getArrayType = (elementType: DataTypes): DataTypes => + typeToArrayTypeMapper[elementType] || DataTypes.EMPTY; + + if (Array.isArray(value)) { + return getArrayType(determineType(value[0])); + } + + return determineType(value); +}; diff --git a/frontend/src/container/QueryBuilder/filters/QueryBuilderSearch/index.tsx b/frontend/src/container/QueryBuilder/filters/QueryBuilderSearch/index.tsx index 4b2c4fe6b3..59c693b6e6 100644 --- a/frontend/src/container/QueryBuilder/filters/QueryBuilderSearch/index.tsx +++ b/frontend/src/container/QueryBuilder/filters/QueryBuilderSearch/index.tsx @@ -87,10 +87,7 @@ function QueryBuilderSearch({ handleSearch(value); }; - const isDisabled = - !!searchValue || - OPERATORS.HAS === tagOperator || - OPERATORS.NHAS === tagOperator; + const isDisabled = !!searchValue; return (