diff --git a/x-pack/plugins/stack_alerts/server/rule_types/es_query/lib/fetch_search_source_query.test.ts b/x-pack/plugins/stack_alerts/server/rule_types/es_query/lib/fetch_search_source_query.test.ts index 9afffb7c10ceb..94a8ea648c1e9 100644 --- a/x-pack/plugins/stack_alerts/server/rule_types/es_query/lib/fetch_search_source_query.test.ts +++ b/x-pack/plugins/stack_alerts/server/rule_types/es_query/lib/fetch_search_source_query.test.ts @@ -7,8 +7,11 @@ import { OnlySearchSourceRuleParams } from '../types'; import { createSearchSourceMock } from '@kbn/data-plugin/common/search/search_source/mocks'; -import { updateSearchSource } from './fetch_search_source_query'; -import { stubbedSavedObjectIndexPattern } from '@kbn/data-views-plugin/common/data_view.stub'; +import { updateSearchSource, getSmallerDataViewSpec } from './fetch_search_source_query'; +import { + createStubDataView, + stubbedSavedObjectIndexPattern, +} from '@kbn/data-views-plugin/common/data_view.stub'; import { DataView } from '@kbn/data-views-plugin/common'; import { fieldFormatsMock } from '@kbn/field-formats-plugin/common/mocks'; import { Comparator } from '../../../../common/comparator_types'; @@ -282,4 +285,121 @@ describe('fetchSearchSourceQuery', () => { `); }); }); + + describe('getSmallerDataViewSpec', () => { + it('should remove "count"s but keep other props like "customLabel"', async () => { + const fieldsMap = { + test1: { + name: 'test1', + type: 'keyword', + aggregatable: true, + searchable: true, + readFromDocValues: false, + }, + test2: { + name: 'test2', + type: 'keyword', + aggregatable: true, + searchable: true, + readFromDocValues: false, + }, + test3: { + name: 'test3', + type: 'keyword', + aggregatable: true, + searchable: true, + readFromDocValues: false, + }, + }; + expect( + getSmallerDataViewSpec( + createStubDataView({ + spec: { + id: 'test', + title: 'test*', + fields: fieldsMap, + fieldAttrs: undefined, + }, + }) + )?.fieldAttrs + ).toBeUndefined(); + expect( + getSmallerDataViewSpec( + createStubDataView({ + spec: { + id: 'test', + title: 'test*', + fields: fieldsMap, + fieldAttrs: { + test1: { + count: 11, + }, + test2: { + count: 12, + }, + }, + }, + }) + )?.fieldAttrs + ).toBeUndefined(); + expect( + getSmallerDataViewSpec( + createStubDataView({ + spec: { + id: 'test', + title: 'test*', + fields: fieldsMap, + fieldAttrs: { + test1: { + count: 11, + customLabel: 'test11', + }, + test2: { + count: 12, + }, + }, + }, + }) + )?.fieldAttrs + ).toMatchInlineSnapshot(` + Object { + "test1": Object { + "customLabel": "test11", + }, + } + `); + expect( + getSmallerDataViewSpec( + createStubDataView({ + spec: { + id: 'test', + title: 'test*', + fields: fieldsMap, + fieldAttrs: { + test1: { + count: 11, + customLabel: 'test11', + }, + test2: { + customLabel: 'test12', + }, + test3: { + count: 30, + }, + }, + }, + }) + )?.fieldAttrs + ).toMatchInlineSnapshot(` + Object { + "test1": Object { + "customLabel": "test11", + }, + "test2": Object { + "customLabel": "test12", + }, + } + `); + }); + }); }); diff --git a/x-pack/plugins/stack_alerts/server/rule_types/es_query/lib/fetch_search_source_query.ts b/x-pack/plugins/stack_alerts/server/rule_types/es_query/lib/fetch_search_source_query.ts index bc22a228ce988..1ae217f96c326 100644 --- a/x-pack/plugins/stack_alerts/server/rule_types/es_query/lib/fetch_search_source_query.ts +++ b/x-pack/plugins/stack_alerts/server/rule_types/es_query/lib/fetch_search_source_query.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { omit, pickBy, mapValues } from 'lodash'; import { buildRangeFilter, Filter } from '@kbn/es-query'; import { DataView, @@ -186,12 +187,15 @@ async function generateLink( const updatedFilters = updateFilterReferences(prevFilters, dataViewToUpdate.id!, newDataView.id!); const redirectUrlParams: DiscoverAppLocatorParams = { - dataViewSpec: newDataView.toSpec(false), + dataViewSpec: getSmallerDataViewSpec(newDataView), filters: updatedFilters, query: searchSource.getField('query'), timeRange: { from: dateStart, to: dateEnd }, isAlertResults: true, }; + + // use `lzCompress` flag for making the link readable during debugging/testing + // const redirectUrl = discoverLocator!.getRedirectUrl(redirectUrlParams, { lzCompress: false }); const redirectUrl = discoverLocator!.getRedirectUrl(redirectUrlParams); const [start, end] = redirectUrl.split('/app'); @@ -213,3 +217,23 @@ function updateFilterReferences(filters: Filter[], fromDataView: string, toDataV } }); } + +export function getSmallerDataViewSpec( + dataView: DataView +): DiscoverAppLocatorParams['dataViewSpec'] { + const dataViewSpec = dataView.toSpec(false); + + if (dataViewSpec.fieldAttrs) { + // remove `count` props + dataViewSpec.fieldAttrs = pickBy( + mapValues(dataViewSpec.fieldAttrs, (fieldAttrs) => omit(fieldAttrs, 'count')), + (trimmedFieldAttrs) => Object.keys(trimmedFieldAttrs).length > 0 + ); + + if (Object.keys(dataViewSpec.fieldAttrs).length === 0) { + dataViewSpec.fieldAttrs = undefined; + } + } + + return dataViewSpec; +}