[Discover][Alerts] Make alert links shorter (#158582)

- Addresses https://github.com/elastic/kibana/issues/158262

## Summary

This PR makes alert links shorter by removing redundant props from the
encoded state. We should trim it down more in the future. Backporting a
small fix for now.

For testing:
Please follow instructions from this PR description
https://github.com/elastic/kibana/pull/146403
This commit is contained in:
Julia Rechkunova 2023-05-31 07:28:35 +02:00 committed by GitHub
parent ef264f0ac4
commit ef07c97868
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 147 additions and 3 deletions

View file

@ -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",
},
}
`);
});
});
});

View file

@ -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;
}