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

# Backport

This will backport the following commits from `main` to `8.8`:
- [[Discover][Alerts] Make alert links shorter
(#158582)](https://github.com/elastic/kibana/pull/158582)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Julia
Rechkunova","email":"julia.rechkunova@elastic.co"},"sourceCommit":{"committedDate":"2023-05-31T05:28:35Z","message":"[Discover][Alerts]
Make alert links shorter (#158582)\n\n- Addresses
https://github.com/elastic/kibana/issues/158262\r\n\r\n##
Summary\r\n\r\nThis PR makes alert links shorter by removing redundant
props from the\r\nencoded state. We should trim it down more in the
future. Backporting a\r\nsmall fix for now.\r\n\r\nFor
testing:\r\nPlease follow instructions from this PR
description\r\nhttps://github.com/elastic/kibana/pull/146403","sha":"ef07c978689872d2ae3037aa06a0f2f7b23c3582","branchLabelMapping":{"^v8.9.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:DataDiscovery","backport:prev-minor","v8.9.0"],"number":158582,"url":"https://github.com/elastic/kibana/pull/158582","mergeCommit":{"message":"[Discover][Alerts]
Make alert links shorter (#158582)\n\n- Addresses
https://github.com/elastic/kibana/issues/158262\r\n\r\n##
Summary\r\n\r\nThis PR makes alert links shorter by removing redundant
props from the\r\nencoded state. We should trim it down more in the
future. Backporting a\r\nsmall fix for now.\r\n\r\nFor
testing:\r\nPlease follow instructions from this PR
description\r\nhttps://github.com/elastic/kibana/pull/146403","sha":"ef07c978689872d2ae3037aa06a0f2f7b23c3582"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.9.0","labelRegex":"^v8.9.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/158582","number":158582,"mergeCommit":{"message":"[Discover][Alerts]
Make alert links shorter (#158582)\n\n- Addresses
https://github.com/elastic/kibana/issues/158262\r\n\r\n##
Summary\r\n\r\nThis PR makes alert links shorter by removing redundant
props from the\r\nencoded state. We should trim it down more in the
future. Backporting a\r\nsmall fix for now.\r\n\r\nFor
testing:\r\nPlease follow instructions from this PR
description\r\nhttps://github.com/elastic/kibana/pull/146403","sha":"ef07c978689872d2ae3037aa06a0f2f7b23c3582"}}]}]
BACKPORT-->

Co-authored-by: Julia Rechkunova <julia.rechkunova@elastic.co>
This commit is contained in:
Kibana Machine 2023-05-31 03:24:39 -04:00 committed by GitHub
parent 19194f9daf
commit a4a3562091
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;
}