Skip to content

Commit

Permalink
Merge branch '8.8' into backport/8.8/pr-158371
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine committed May 31, 2023
2 parents ae2a153 + a4a3562 commit 1f8537f
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/plugins/discover/public/application/doc/locator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,17 @@ describe('Discover single doc url generator', () => {
`"#/doc/c367b774-a4c2-11ea-bb37-0242ac130002/mock-row-index?id=mock-row-id"`
);
});

it('should URL encode rowId', async () => {
const { locator } = setup();
const { path } = await locator.getLocation({
index: dataViewId,
rowId: 'id with special characters: &?#+',
rowIndex: 'mock-row-index',
referrer: 'mock-referrer',
});
expect(path).toMatchInlineSnapshot(
`"#/doc/c367b774-a4c2-11ea-bb37-0242ac130002/mock-row-index?id=id%20with%20special%20characters%3A%20%26%3F%23%2B"`
);
});
});
2 changes: 1 addition & 1 deletion src/plugins/discover/public/application/doc/locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class DiscoverSingleDocLocatorDefinition
dataViewId = index;
}

const path = `#/doc/${dataViewId}/${rowIndex}?id=${rowId}`;
const path = `#/doc/${dataViewId}/${rowIndex}?id=${encodeURIComponent(rowId)}`;

return {
app: 'discover',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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",
},
}
`);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { omit, pickBy, mapValues } from 'lodash';
import { buildRangeFilter, Filter } from '@kbn/es-query';
import {
DataView,
Expand Down Expand Up @@ -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');

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

0 comments on commit 1f8537f

Please sign in to comment.