diff --git a/x-pack/platform/plugins/shared/stack_alerts/public/rule_types/es_query/expression/es_query_expression.test.tsx b/x-pack/platform/plugins/shared/stack_alerts/public/rule_types/es_query/expression/es_query_expression.test.tsx index 45447d58726f4..69d1d84f102d8 100644 --- a/x-pack/platform/plugins/shared/stack_alerts/public/rule_types/es_query/expression/es_query_expression.test.tsx +++ b/x-pack/platform/plugins/shared/stack_alerts/public/rule_types/es_query/expression/es_query_expression.test.tsx @@ -149,6 +149,8 @@ describe('EsQueryRuleTypeExpression', () => { size: [], timeField: [], timeWindowSize: [], + termSize: [], + termField: [], }; const wrapper = mountWithIntl( @@ -223,6 +225,22 @@ describe('EsQueryRuleTypeExpression', () => { expect(excludeMatchesCheckBox.prop('checked')).toBe(false); }); + test('should render EsQueryRuleTypeExpression with chosen runtime group field', async () => { + const result = await setup({ + ...defaultEsQueryExpressionParams, + esQuery: + '{\n "query":{\n "match_all" : {}\n },\n "runtime_mappings": {\n "day_of_week": {\n "type": "keyword",\n "script": {\n "source": "emit(doc[\'@timestamp\'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ENGLISH))"\n }\n }\n }\n }', + groupBy: 'top', + termField: 'day_of_week', + termSize: 3, + } as unknown as EsQueryRuleParams); + + fireEvent.click(screen.getByTestId('groupByExpression')); + expect(await screen.findByRole('dialog')).toBeInTheDocument(); + + expect(result.getByTestId('fieldsExpressionSelect')).toHaveTextContent('day_of_week'); + }); + test('should show success message if ungrouped Test Query is successful', async () => { const searchResponseMock$ = of({ rawResponse: { diff --git a/x-pack/platform/plugins/shared/stack_alerts/public/rule_types/es_query/expression/es_query_expression.tsx b/x-pack/platform/plugins/shared/stack_alerts/public/rule_types/es_query/expression/es_query_expression.tsx index e00b7b6c1156d..790ed3b6a03ac 100644 --- a/x-pack/platform/plugins/shared/stack_alerts/public/rule_types/es_query/expression/es_query_expression.tsx +++ b/x-pack/platform/plugins/shared/stack_alerts/public/rule_types/es_query/expression/es_query_expression.tsx @@ -98,10 +98,12 @@ export const EsQueryExpression: React.FC< const setDefaultExpressionValues = async () => { setRuleProperty('params', currentRuleParams); - setXJson(esQuery ?? DEFAULT_VALUES.QUERY); + const query = esQuery ?? DEFAULT_VALUES.QUERY; + setXJson(query); if (index && index.length > 0) { - await refreshEsFields(index); + const initialRuntimeFields = getRuntimeFields(query); + await refreshEsFields(index, initialRuntimeFields); } }; @@ -110,10 +112,14 @@ export const EsQueryExpression: React.FC< // eslint-disable-next-line react-hooks/exhaustive-deps }, []); - const refreshEsFields = async (indices: string[]) => { + const refreshEsFields = async (indices: string[], initialRuntimeFields?: FieldOption[]) => { const currentEsFields = await getFields(http, indices); setEsFields(currentEsFields); - setCombinedFields(sortBy(currentEsFields.concat(runtimeFields), 'name')); + + const combined = currentEsFields.concat( + initialRuntimeFields !== undefined ? initialRuntimeFields : runtimeFields + ); + setCombinedFields(sortBy(combined, 'name')); }; const getRuntimeFields = (xjson: string) => { @@ -127,6 +133,7 @@ export const EsQueryExpression: React.FC< const currentRuntimeFields = convertRawRuntimeFieldtoFieldOption(runtimeMappings); setRuntimeFields(currentRuntimeFields); setCombinedFields(sortBy(esFields.concat(currentRuntimeFields), 'name')); + return currentRuntimeFields; } };