From eb0dade59528e02271848de36b77869a9ef2c13a Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Mon, 30 Nov 2020 14:17:11 +0100 Subject: [PATCH 1/4] refactor getContextUrl to seperate file --- .../helpers/get_context_url.test.ts | 34 ++++++++++++ .../application/helpers/get_context_url.tsx | 52 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 src/plugins/discover/public/application/helpers/get_context_url.test.ts create mode 100644 src/plugins/discover/public/application/helpers/get_context_url.tsx diff --git a/src/plugins/discover/public/application/helpers/get_context_url.test.ts b/src/plugins/discover/public/application/helpers/get_context_url.test.ts new file mode 100644 index 0000000000000..9ff7e11a31247 --- /dev/null +++ b/src/plugins/discover/public/application/helpers/get_context_url.test.ts @@ -0,0 +1,34 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { getContextUrl } from './get_context_url'; +import { FilterManager } from '../../../../data/public/query/filter_manager'; + +describe('Get context url', () => { + test('returns a valid context url', async () => { + const filterManager = ({ + getGlobalFilters: () => [], + getAppFilters: () => [], + } as unknown) as FilterManager; + const url = await getContextUrl('docId', 'ipId', ['test1', 'test2'], filterManager); + expect(url).toMatchInlineSnapshot( + `"#/context/ipId/docId?_g=(filters:!())&_a=(columns:!(test1,test2),filters:!())"` + ); + }); +}); diff --git a/src/plugins/discover/public/application/helpers/get_context_url.tsx b/src/plugins/discover/public/application/helpers/get_context_url.tsx new file mode 100644 index 0000000000000..b159341cbe28d --- /dev/null +++ b/src/plugins/discover/public/application/helpers/get_context_url.tsx @@ -0,0 +1,52 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { stringify } from 'query-string'; +import rison from 'rison-node'; +import { url } from '../../../../kibana_utils/common'; +import { esFilters, FilterManager } from '../../../../data/public'; + +/** + * Helper function to generate an URL to a document in Discover's context view + */ +export function getContextUrl( + documentId: string, + indexPatternId: string, + columns: string[], + filterManager: FilterManager +) { + const globalFilters = filterManager.getGlobalFilters(); + const appFilters = filterManager.getAppFilters(); + + const hash = stringify( + url.encodeQuery({ + _g: rison.encode({ + filters: globalFilters || [], + }), + _a: rison.encode({ + columns, + filters: (appFilters || []).map(esFilters.disableFilter), + }), + }), + { encode: false, sort: false } + ); + + return `#/context/${encodeURIComponent(indexPatternId)}/${encodeURIComponent( + documentId + )}?${hash}`; +} From af7ca07046b0457eac38d044b5c7e25f87c54e35 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Tue, 1 Dec 2020 11:35:05 +0100 Subject: [PATCH 2/4] Use getContextUrl in legacy doc table --- .../angular/doc_table/components/table_row.ts | 28 ++----------------- .../helpers/get_context_url.test.ts | 17 +++++++---- 2 files changed, 14 insertions(+), 31 deletions(-) diff --git a/src/plugins/discover/public/application/angular/doc_table/components/table_row.ts b/src/plugins/discover/public/application/angular/doc_table/components/table_row.ts index 17f3199b75b15..d8a47c9e38dd0 100644 --- a/src/plugins/discover/public/application/angular/doc_table/components/table_row.ts +++ b/src/plugins/discover/public/application/angular/doc_table/components/table_row.ts @@ -18,19 +18,13 @@ */ import { find, template } from 'lodash'; -import { stringify } from 'query-string'; import $ from 'jquery'; -import rison from 'rison-node'; -import '../../doc_viewer'; - import openRowHtml from './table_row/open.html'; import detailsHtml from './table_row/details.html'; - -import { dispatchRenderComplete, url } from '../../../../../../kibana_utils/public'; +import { dispatchRenderComplete } from '../../../../../../kibana_utils/public'; import { DOC_HIDE_TIME_COLUMN_SETTING } from '../../../../../common'; import cellTemplateHtml from '../components/table_row/cell.html'; import truncateByHeightTemplateHtml from '../components/table_row/truncate_by_height.html'; -import { esFilters } from '../../../../../../data/public'; import { getServices } from '../../../../kibana_services'; const TAGS_WITH_WS = />\s+ { - const globalFilters: any = getServices().filterManager.getGlobalFilters(); - const appFilters: any = getServices().filterManager.getAppFilters(); - - const hash = stringify( - url.encodeQuery({ - _g: rison.encode({ - filters: globalFilters || [], - }), - _a: rison.encode({ - columns: $scope.columns, - filters: (appFilters || []).map(esFilters.disableFilter), - }), - }), - { encode: false, sort: false } - ); - - return `#/context/${encodeURIComponent($scope.indexPattern.id)}/${encodeURIComponent( - $scope.row._id - )}?${hash}`; + return getContextUrl($scope.indexPattern.id, $scope.row._id, getServices().filterManager); }; // create a tr element that lists the value for each *column* diff --git a/src/plugins/discover/public/application/helpers/get_context_url.test.ts b/src/plugins/discover/public/application/helpers/get_context_url.test.ts index 9ff7e11a31247..481ea6b1a5b4f 100644 --- a/src/plugins/discover/public/application/helpers/get_context_url.test.ts +++ b/src/plugins/discover/public/application/helpers/get_context_url.test.ts @@ -19,16 +19,23 @@ import { getContextUrl } from './get_context_url'; import { FilterManager } from '../../../../data/public/query/filter_manager'; +const filterManager = ({ + getGlobalFilters: () => [], + getAppFilters: () => [], +} as unknown) as FilterManager; describe('Get context url', () => { - test('returns a valid context url', async () => { - const filterManager = ({ - getGlobalFilters: () => [], - getAppFilters: () => [], - } as unknown) as FilterManager; + test('returning a valid context url', async () => { const url = await getContextUrl('docId', 'ipId', ['test1', 'test2'], filterManager); expect(url).toMatchInlineSnapshot( `"#/context/ipId/docId?_g=(filters:!())&_a=(columns:!(test1,test2),filters:!())"` ); }); + + test('returning a valid context url when docId contains whitespace', async () => { + const url = await getContextUrl('doc Id', 'ipId', ['test1', 'test2'], filterManager); + expect(url).toMatchInlineSnapshot( + `"#/context/ipId/doc%20Id?_g=(filters:!())&_a=(columns:!(test1,test2),filters:!())"` + ); + }); }); From d25abd02aa9112604b7e0370b47adc9ad2fdb533 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Tue, 1 Dec 2020 16:10:00 +0100 Subject: [PATCH 3/4] Fix tests --- .../application/angular/doc_table/components/table_row.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/plugins/discover/public/application/angular/doc_table/components/table_row.ts b/src/plugins/discover/public/application/angular/doc_table/components/table_row.ts index d8a47c9e38dd0..07bb06b7a0dea 100644 --- a/src/plugins/discover/public/application/angular/doc_table/components/table_row.ts +++ b/src/plugins/discover/public/application/angular/doc_table/components/table_row.ts @@ -26,6 +26,7 @@ import { DOC_HIDE_TIME_COLUMN_SETTING } from '../../../../../common'; import cellTemplateHtml from '../components/table_row/cell.html'; import truncateByHeightTemplateHtml from '../components/table_row/truncate_by_height.html'; import { getServices } from '../../../../kibana_services'; +import { getContextUrl } from '../../../helpers/get_context_url'; const TAGS_WITH_WS = />\s+ { - return getContextUrl($scope.indexPattern.id, $scope.row._id, getServices().filterManager); + return getContextUrl( + $scope.indexPattern.id, + $scope.row._id, + $scope.columns, + getServices().filterManager + ); }; // create a tr element that lists the value for each *column* From 3d48e096b45e558268647c3049a989a9e3266f73 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Tue, 1 Dec 2020 17:35:53 +0100 Subject: [PATCH 4/4] Switch params in called function --- .../application/angular/doc_table/components/table_row.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/discover/public/application/angular/doc_table/components/table_row.ts b/src/plugins/discover/public/application/angular/doc_table/components/table_row.ts index 07bb06b7a0dea..e45f18606e3fc 100644 --- a/src/plugins/discover/public/application/angular/doc_table/components/table_row.ts +++ b/src/plugins/discover/public/application/angular/doc_table/components/table_row.ts @@ -111,8 +111,8 @@ export function createTableRowDirective($compile: ng.ICompileService) { $scope.getContextAppHref = () => { return getContextUrl( - $scope.indexPattern.id, $scope.row._id, + $scope.indexPattern.id, $scope.columns, getServices().filterManager );