Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

feat(filter): add group by filter (#2538)

Yevhen Shevchenko5f73a82d9f94
diff --git a/frontend/src/api/queryBuilder/getAttributeKeys.ts b/frontend/src/api/queryBuilder/getAttributeKeys.ts
index b42566a75e..439c20f132 100644
--- a/frontend/src/api/queryBuilder/getAttributeKeys.ts
+++ b/frontend/src/api/queryBuilder/getAttributeKeys.ts
@@ -11,6 +11,7 @@ export const getAggregateKeys = async ({
searchText,
dataSource,
aggregateAttribute,
+ tagType,
}: IGetAttributeKeysPayload): Promise<
SuccessResponse<IQueryAutocompleteResponse> | ErrorResponse
> => {
@@ -18,7 +19,7 @@ export const getAggregateKeys = async ({
const response: AxiosResponse<{
data: IQueryAutocompleteResponse;
}> = await ApiV3Instance.get(
- `autocomplete/attribute_keys?aggregateOperator=${aggregateOperator}&dataSource=${dataSource}&aggregateAttribute=${aggregateAttribute}&searchText=${searchText}`,
+ `autocomplete/attribute_keys?aggregateOperator=${aggregateOperator}&dataSource=${dataSource}&aggregateAttribute=${aggregateAttribute}&tagType=${tagType}&searchText=${searchText}`,
);
return {
diff --git a/frontend/src/constants/useQueryKeys.ts b/frontend/src/constants/useQueryKeys.ts
index 705d5ef350..c765c17748 100644
--- a/frontend/src/constants/useQueryKeys.ts
+++ b/frontend/src/constants/useQueryKeys.ts
@@ -1,3 +1,4 @@
export enum QueryBuilderKeys {
GET_AGGREGATE_ATTRIBUTE = 'GET_AGGREGATE_ATTRIBUTE',
+ GET_AGGREGATE_KEYS = 'GET_AGGREGATE_KEYS',
}
diff --git a/frontend/src/container/QueryBuilder/components/FilterLabel/FilterLabel.styled.ts b/frontend/src/container/QueryBuilder/components/FilterLabel/FilterLabel.styled.ts
index 8eb97a32f3..d379f6cbbe 100644
--- a/frontend/src/container/QueryBuilder/components/FilterLabel/FilterLabel.styled.ts
+++ b/frontend/src/container/QueryBuilder/components/FilterLabel/FilterLabel.styled.ts
@@ -2,7 +2,6 @@ import styled from 'styled-components';
export const StyledLabel = styled.div`
padding: 0 0.6875rem;
- min-width: 6.5rem;
width: fit-content;
min-height: 2rem;
display: inline-flex;
diff --git a/frontend/src/container/QueryBuilder/components/ListMarker/ListMarker.styled.ts b/frontend/src/container/QueryBuilder/components/ListMarker/ListMarker.styled.ts
index f876af973b..6344456d0b 100644
--- a/frontend/src/container/QueryBuilder/components/ListMarker/ListMarker.styled.ts
+++ b/frontend/src/container/QueryBuilder/components/ListMarker/ListMarker.styled.ts
@@ -1,9 +1,12 @@
import { Button } from 'antd';
import styled from 'styled-components';
-export const StyledButton = styled(Button)`
+export const StyledButton = styled(Button)<{ isAvailableToDisable: boolean }>`
min-width: 2rem;
height: 2.25rem;
padding: 0.125rem;
border-radius: 0.375rem;
+ margin-right: 0.1rem;
+ pointer-events: ${(props): string =>
+ props.isAvailableToDisable ? 'default' : 'none'};
`;
diff --git a/frontend/src/container/QueryBuilder/components/ListMarker/ListMarker.tsx b/frontend/src/container/QueryBuilder/components/ListMarker/ListMarker.tsx
index 3af9d3aa37..8573579be7 100644
--- a/frontend/src/container/QueryBuilder/components/ListMarker/ListMarker.tsx
+++ b/frontend/src/container/QueryBuilder/components/ListMarker/ListMarker.tsx
@@ -30,7 +30,8 @@ export function ListMarker({
icon={buttonProps.icon}
onClick={buttonProps.onClick}
className={className}
- style={{ marginRight: '0.1rem', ...style }}
+ isAvailableToDisable={isAvailableToDisable}
+ style={style}
>
{labelName}
</StyledButton>
diff --git a/frontend/src/container/QueryBuilder/components/Query/Query.tsx b/frontend/src/container/QueryBuilder/components/Query/Query.tsx
index 81427bc260..a41eb51e5d 100644
--- a/frontend/src/container/QueryBuilder/components/Query/Query.tsx
+++ b/frontend/src/container/QueryBuilder/components/Query/Query.tsx
@@ -8,13 +8,14 @@ import {
} from 'container/QueryBuilder/components';
import {
AggregatorFilter,
+ GroupByFilter,
OperatorsSelect,
} from 'container/QueryBuilder/filters';
// Context
import { useQueryBuilder } from 'hooks/useQueryBuilder';
// ** Hooks
import React from 'react';
-import { AutocompleteData } from 'types/api/queryBuilder/queryAutocompleteResponse';
+import { BaseAutocompleteData } from 'types/api/queryBuilder/queryAutocompleteResponse';
import { DataSource } from 'types/common/queryBuilder';
// ** Constants
import {
@@ -55,40 +56,66 @@ export function Query({
handleSetQueryData(index, { disabled: !query.disabled });
};
- const handleChangeAggregatorAttribute = (value: AutocompleteData): void => {
+ const handleChangeAggregatorAttribute = (
+ value: BaseAutocompleteData,
+ ): void => {
handleSetQueryData(index, { aggregateAttribute: value });
};
+ const handleChangeGroupByKeys = (values: BaseAutocompleteData[]): void => {
+ handleSetQueryData(index, { groupBy: values });
+ };
+
return (
- <Row style={{ gap: '0.75rem' }}>
- <Col span={12}>
- <ListMarker
- isDisabled={query.disabled}
- toggleDisabled={handleToggleDisableQuery}
- labelName={query.queryName}
- index={index}
- isAvailableToDisable={isAvailableToDisable}
- />
- {queryVariant === 'dropdown' ? (
- <DataSourceDropdown
- onChange={handleChangeDataSource}
- value={query.dataSource}
- />
- ) : (
- <FilterLabel label={transformToUpperCase(query.dataSource)} />
- )}
- </Col>
+ <Row gutter={[0, 15]}>
<Col span={24}>
- <OperatorsSelect
- value={query.aggregateOperator || currentListOfOperators[0]}
- onChange={handleChangeOperator}
- operators={currentListOfOperators}
- style={{ minWidth: 104, marginRight: '0.75rem' }}
- />
- <AggregatorFilter
- onChange={handleChangeAggregatorAttribute}
- query={query}
- />
+ <Row wrap={false} align="middle">
+ <Col span={24}>
+ <ListMarker
+ isDisabled={query.disabled}
+ toggleDisabled={handleToggleDisableQuery}
+ labelName={query.queryName}
+ index={index}
+ isAvailableToDisable={isAvailableToDisable}
+ />
+ {queryVariant === 'dropdown' ? (
+ <DataSourceDropdown
+ onChange={handleChangeDataSource}
+ value={query.dataSource}
+ />
+ ) : (
+ <FilterLabel label={transformToUpperCase(query.dataSource)} />
+ )}
+ {/* TODO: here will be search */}
+ </Col>
+ </Row>
+ </Col>
+ <Col span={11}>
+ <Row gutter={[11, 5]}>
+ <Col flex="95px">
+ <OperatorsSelect
+ value={query.aggregateOperator || currentListOfOperators[0]}
+ onChange={handleChangeOperator}
+ operators={currentListOfOperators}
+ />
+ </Col>
+ <Col flex="1 1 200px">
+ <AggregatorFilter
+ onChange={handleChangeAggregatorAttribute}
+ query={query}
+ />
+ </Col>
+ </Row>
+ </Col>
+ <Col span={11} offset={2}>
+ <Row gutter={[11, 5]}>
+ <Col flex="95px">
+ <FilterLabel label="Group by" />
+ </Col>
+ <Col flex="1 1 200px">
+ <GroupByFilter query={query} onChange={handleChangeGroupByKeys} />
+ </Col>
+ </Row>
</Col>
</Row>
);
diff --git a/frontend/src/container/QueryBuilder/filters/AggregatorFilter/AggregatorFilter.intefaces.ts b/frontend/src/container/QueryBuilder/filters/AggregatorFilter/AggregatorFilter.intefaces.ts
index 81ba34a2c2..e8e4f072fb 100644
--- a/frontend/src/container/QueryBuilder/filters/AggregatorFilter/AggregatorFilter.intefaces.ts
+++ b/frontend/src/container/QueryBuilder/filters/AggregatorFilter/AggregatorFilter.intefaces.ts
@@ -1,7 +1,7 @@
-import { AutocompleteData } from 'types/api/queryBuilder/queryAutocompleteResponse';
+import { BaseAutocompleteData } from 'types/api/queryBuilder/queryAutocompleteResponse';
import { IBuilderQueryForm } from 'types/api/queryBuilder/queryBuilderData';
export type AgregatorFilterProps = {
- onChange: (value: AutocompleteData) => void;
+ onChange: (value: BaseAutocompleteData) => void;
query: IBuilderQueryForm;
};
diff --git a/frontend/src/container/QueryBuilder/filters/AggregatorFilter/AggregatorFilter.tsx b/frontend/src/container/QueryBuilder/filters/AggregatorFilter/AggregatorFilter.tsx
index 13a25216ce..6eebe2f955 100644
--- a/frontend/src/container/QueryBuilder/filters/AggregatorFilter/AggregatorFilter.tsx
+++ b/frontend/src/container/QueryBuilder/filters/AggregatorFilter/AggregatorFilter.tsx
@@ -68,10 +68,8 @@ export function AggregatorFilter({
return (
<AutoComplete
showSearch
- placeholder={`${transformToUpperCase(
- query.dataSource,
- )} Name (Start typing to get suggestions)`}
- style={{ flex: 1, minWidth: 200 }}
+ placeholder={`${transformToUpperCase(query.dataSource)} name`}
+ style={{ width: '100%' }}
showArrow={false}
filterOption={false}
onSearch={handleSearchAttribute}
diff --git a/frontend/src/container/QueryBuilder/filters/GroupByFilter/GroupByFilter.interfaces.ts b/frontend/src/container/QueryBuilder/filters/GroupByFilter/GroupByFilter.interfaces.ts
new file mode 100644
index 0000000000..24b82d855e
--- /dev/null
+++ b/frontend/src/container/QueryBuilder/filters/GroupByFilter/GroupByFilter.interfaces.ts
@@ -0,0 +1,15 @@
+import { BaseAutocompleteData } from 'types/api/queryBuilder/queryAutocompleteResponse';
+import { IBuilderQueryForm } from 'types/api/queryBuilder/queryBuilderData';
+
+export type GroupByFilterProps = {
+ query: IBuilderQueryForm;
+ onChange: (values: BaseAutocompleteData[]) => void;
+};
+
+export type GroupByFilterValue = {
+ disabled: boolean | undefined;
+ key: string;
+ label: string;
+ title: string | undefined;
+ value: string;
+};
diff --git a/frontend/src/container/QueryBuilder/filters/GroupByFilter/GroupByFilter.tsx b/frontend/src/container/QueryBuilder/filters/GroupByFilter/GroupByFilter.tsx
new file mode 100644
index 0000000000..3cda80d704
--- /dev/null
+++ b/frontend/src/container/QueryBuilder/filters/GroupByFilter/GroupByFilter.tsx
@@ -0,0 +1,100 @@
+import { Select, Spin } from 'antd';
+// ** Api
+import { getAggregateKeys } from 'api/queryBuilder/getAttributeKeys';
+// ** Constants
+import { QueryBuilderKeys } from 'constants/useQueryKeys';
+// ** Components
+// ** Helpers
+import { transformStringWithPrefix } from 'lib/query/transformStringWithPrefix';
+import React, { useState } from 'react';
+import { useQuery } from 'react-query';
+import { BaseAutocompleteData } from 'types/api/queryBuilder/queryAutocompleteResponse';
+import { SelectOption } from 'types/common/select';
+
+// ** Types
+import {
+ GroupByFilterProps,
+ GroupByFilterValue,
+} from './GroupByFilter.interfaces';
+
+export function GroupByFilter({
+ query,
+ onChange,
+}: GroupByFilterProps): JSX.Element {
+ const [searchText, setSearchText] = useState<string>('');
+
+ const { data, isFetching } = useQuery(
+ [QueryBuilderKeys.GET_AGGREGATE_KEYS, searchText],
+ async () =>
+ getAggregateKeys({
+ aggregateAttribute: query.aggregateAttribute.key,
+ tagType: query.aggregateAttribute.type,
+ dataSource: query.dataSource,
+ aggregateOperator: query.aggregateOperator,
+ searchText,
+ }),
+ { enabled: !!query.aggregateAttribute.key, keepPreviousData: true },
+ );
+
+ const handleSearchKeys = (searchText: string): void => {
+ setSearchText(searchText);
+ };
+
+ const optionsData: SelectOption<string, string>[] =
+ data?.payload?.attributeKeys?.map((item) => ({
+ label: transformStringWithPrefix({
+ str: item.key,
+ prefix: item.type || '',
+ condition: !item.isColumn,
+ }),
+ value: item.key,
+ })) || [];
+
+ const handleChange = (values: GroupByFilterValue[]): void => {
+ const groupByValues: BaseAutocompleteData[] = values.map((item) => {
+ const iterationArray = data?.payload?.attributeKeys || query.groupBy;
+ const existGroup = iterationArray.find((group) => group.key === item.value);
+ if (existGroup) {
+ return existGroup;
+ }
+
+ return {
+ isColumn: null,
+ key: item.value,
+ dataType: null,
+ type: null,
+ };
+ });
+
+ onChange(groupByValues);
+ };
+
+ const values: GroupByFilterValue[] = query.groupBy.map((item) => ({
+ label: transformStringWithPrefix({
+ str: item.key,
+ prefix: item.type || '',
+ condition: !item.isColumn,
+ }),
+ key: item.key,
+ value: item.key,
+ disabled: undefined,
+ title: undefined,
+ }));
+
+ return (
+ <Select
+ mode="tags"
+ style={{ width: '100%' }}
+ onSearch={handleSearchKeys}
+ showSearch
+ disabled={!query.aggregateAttribute.key}
+ showArrow={false}
+ filterOption={false}
+ options={optionsData}
+ labelInValue
+ value={values}
+ notFoundContent={isFetching ? <Spin size="small" /> : null}
+ onChange={handleChange}
+ />
+ );
+}
diff --git a/frontend/src/container/QueryBuilder/filters/GroupByFilter/index.ts b/frontend/src/container/QueryBuilder/filters/GroupByFilter/index.ts
new file mode 100644
index 0000000000..4377822177
--- /dev/null
+++ b/frontend/src/container/QueryBuilder/filters/GroupByFilter/index.ts
@@ -0,0 +1 @@
+export { GroupByFilter } from './GroupByFilter';
diff --git a/frontend/src/container/QueryBuilder/filters/OperatorsSelect/OperatorsSelect.tsx b/frontend/src/container/QueryBuilder/filters/OperatorsSelect/OperatorsSelect.tsx
index 979bac1dcc..e0bf89e6d2 100644
--- a/frontend/src/container/QueryBuilder/filters/OperatorsSelect/OperatorsSelect.tsx
+++ b/frontend/src/container/QueryBuilder/filters/OperatorsSelect/OperatorsSelect.tsx
@@ -25,6 +25,7 @@ export function OperatorsSelect({
options={operatorsOptions}
value={value}
onChange={onChange}
+ style={{ width: '100%' }}
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
/>
diff --git a/frontend/src/container/QueryBuilder/filters/index.ts b/frontend/src/container/QueryBuilder/filters/index.ts
index 79fc32a7a4..61d937c193 100644
--- a/frontend/src/container/QueryBuilder/filters/index.ts
+++ b/frontend/src/container/QueryBuilder/filters/index.ts
@@ -1,2 +1,3 @@
export { AggregatorFilter } from './AggregatorFilter';
+export { GroupByFilter } from './GroupByFilter';
export { OperatorsSelect } from './OperatorsSelect';
diff --git a/frontend/src/providers/QueryBuilder.tsx b/frontend/src/providers/QueryBuilder.tsx
index 0686828318..1d816ef70b 100644
--- a/frontend/src/providers/QueryBuilder.tsx
+++ b/frontend/src/providers/QueryBuilder.tsx
@@ -53,6 +53,7 @@ export function QueryBuilderProvider({
isColumn: null,
type: null,
},
+ groupBy: [],
},
],
queryFormulas: [],
diff --git a/frontend/src/types/api/queryBuilder/getAttributeKeys.ts b/frontend/src/types/api/queryBuilder/getAttributeKeys.ts
index e28623cad1..b3452aef26 100644
--- a/frontend/src/types/api/queryBuilder/getAttributeKeys.ts
+++ b/frontend/src/types/api/queryBuilder/getAttributeKeys.ts
@@ -1,8 +1,11 @@
import { DataSource } from 'types/common/queryBuilder';
+import { BaseAutocompleteData } from './queryAutocompleteResponse';
+
export interface IGetAttributeKeysPayload {
aggregateOperator: string;
dataSource: DataSource;
searchText: string;
aggregateAttribute: string;
+ tagType: BaseAutocompleteData['type'];
}
diff --git a/frontend/src/types/api/queryBuilder/queryAutocompleteResponse.ts b/frontend/src/types/api/queryBuilder/queryAutocompleteResponse.ts
index 847fb6bcd3..d032d3ad7a 100644
--- a/frontend/src/types/api/queryBuilder/queryAutocompleteResponse.ts
+++ b/frontend/src/types/api/queryBuilder/queryAutocompleteResponse.ts
@@ -1,4 +1,4 @@
-export interface AutocompleteData {
+export interface BaseAutocompleteData {
dataType: 'number' | 'string' | 'boolean' | null;
isColumn: boolean | null;
key: string;
@@ -6,5 +6,5 @@ export interface AutocompleteData {
}
export interface IQueryAutocompleteResponse {
- attributeKeys: AutocompleteData[];
+ attributeKeys: BaseAutocompleteData[];
}
diff --git a/frontend/src/types/api/queryBuilder/queryBuilderData.ts b/frontend/src/types/api/queryBuilder/queryBuilderData.ts
index afddce2814..67c63d4bde 100644
--- a/frontend/src/types/api/queryBuilder/queryBuilderData.ts
+++ b/frontend/src/types/api/queryBuilder/queryBuilderData.ts
@@ -1,6 +1,6 @@
import { DataSource } from 'types/common/queryBuilder';
-import { AutocompleteData } from './queryAutocompleteResponse';
+import { BaseAutocompleteData } from './queryAutocompleteResponse';
// Type for Formula
export interface IBuilderFormula {
@@ -30,7 +30,7 @@ export type IBuilderQuery = {
aggregateOperator: string;
aggregateAttribute: string;
tagFilters: TagFilter[];
- groupBy: string[];
+ groupBy: BaseAutocompleteData[];
expression: string;
disabled: boolean;
having?: string;
@@ -40,5 +40,5 @@ export type IBuilderQuery = {
};
export type IBuilderQueryForm = Omit<IBuilderQuery, 'aggregateAttribute'> & {
- aggregateAttribute: AutocompleteData;
+ aggregateAttribute: BaseAutocompleteData;
};