-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Lens][Dashboard] Adding Lens to Dashboard (#53110)
* First version of adding Lens to dashboard * Fix failing unit test * Replacing explicit Lens query param with a more generic one * Fixing failing unit test * Adding a unit test for redirect * Do not show Save New if adding from Dashboard * Adding functional test * Adding functional test * Fixing type issues * Renaming query params * Fixing failing unit test * Removing unused constants * Fixing erroneous imports * Fixing erroneous import * Fixing import * Fix failing typecheck * Removing timefilter from Dashboard URL * Fixing type error * Replacing time parsing with rison * Replacing URL regex parsing with legacy URLs * Fixing failing test Co-authored-by: Elastic Machine <[email protected]>
- Loading branch information
1 parent
3311382
commit 1280d06
Showing
14 changed files
with
420 additions
and
28 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
src/legacy/core_plugins/kibana/public/dashboard/__tests__/url_helper.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
jest.mock('../', () => ({ | ||
DashboardConstants: { | ||
ADD_EMBEDDABLE_ID: 'addEmbeddableId', | ||
ADD_EMBEDDABLE_TYPE: 'addEmbeddableType', | ||
}, | ||
})); | ||
|
||
jest.mock('../legacy_imports', () => { | ||
return { | ||
absoluteToParsedUrl: jest.fn(() => { | ||
return { | ||
basePath: '/pep', | ||
appId: 'kibana', | ||
appPath: '/dashboard?addEmbeddableType=lens&addEmbeddableId=123eb456cd&x=1&y=2&z=3', | ||
hostname: 'localhost', | ||
port: 5601, | ||
protocol: 'http:', | ||
addQueryParameter: () => {}, | ||
getAbsoluteUrl: () => { | ||
return 'http://localhost:5601/pep/app/kibana#/dashboard?addEmbeddableType=lens&addEmbeddableId=123eb456cd&x=1&y=2&z=3'; | ||
}, | ||
}; | ||
}), | ||
}; | ||
}); | ||
|
||
import { | ||
addEmbeddableToDashboardUrl, | ||
getLensUrlFromDashboardAbsoluteUrl, | ||
getUrlVars, | ||
} from '../np_ready/url_helper'; | ||
|
||
describe('Dashboard URL Helper', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
}); | ||
|
||
it('addEmbeddableToDashboardUrl', () => { | ||
const id = '123eb456cd'; | ||
const type = 'lens'; | ||
const urlVars = { | ||
x: '1', | ||
y: '2', | ||
z: '3', | ||
}; | ||
const basePath = '/pep'; | ||
const url = | ||
"http://localhost:5601/pep/app/kibana#/dashboard?_g=(refreshInterval:(pause:!t,value:0),time:(from:now-15m,to:now))&_a=(description:'',filters:!()"; | ||
expect(addEmbeddableToDashboardUrl(url, basePath, id, urlVars, type)).toEqual( | ||
`http://localhost:5601/pep/app/kibana#/dashboard?addEmbeddableType=${type}&addEmbeddableId=${id}&x=1&y=2&z=3` | ||
); | ||
}); | ||
|
||
it('getUrlVars', () => { | ||
let url = | ||
"http://localhost:5601/app/kibana#/dashboard?_g=(refreshInterval:(pause:!t,value:0),time:(from:now-15m,to:now))&_a=(description:'',filters:!()"; | ||
expect(getUrlVars(url)).toEqual({ | ||
_g: '(refreshInterval:(pause:!t,value:0),time:(from:now-15m,to:now))', | ||
_a: "(description:'',filters:!()", | ||
}); | ||
url = 'http://mybusiness.mydomain.com/app/kibana#/dashboard?x=y&y=z'; | ||
expect(getUrlVars(url)).toEqual({ | ||
x: 'y', | ||
y: 'z', | ||
}); | ||
url = 'http://notDashboardUrl'; | ||
expect(getUrlVars(url)).toEqual({}); | ||
url = 'http://localhost:5601/app/kibana#/dashboard/777182'; | ||
expect(getUrlVars(url)).toEqual({}); | ||
}); | ||
|
||
it('getLensUrlFromDashboardAbsoluteUrl', () => { | ||
const id = '1244'; | ||
const basePath = '/wev'; | ||
let url = | ||
"http://localhost:5601/wev/app/kibana#/dashboard?_g=(refreshInterval:(pause:!t,value:0),time:(from:now-15m,to:now))&_a=(description:'',filters:!()"; | ||
expect(getLensUrlFromDashboardAbsoluteUrl(url, basePath, id)).toEqual( | ||
'http://localhost:5601/wev/app/kibana#/lens/edit/1244' | ||
); | ||
|
||
url = | ||
"http://localhost:5601/wev/app/kibana#/dashboard/625357282?_a=(description:'',filters:!()&_g=(refreshInterval:(pause:!t,value:0),time:(from:now-15m,to:now))"; | ||
expect(getLensUrlFromDashboardAbsoluteUrl(url, basePath, id)).toEqual( | ||
'http://localhost:5601/wev/app/kibana#/lens/edit/1244' | ||
); | ||
|
||
url = 'http://myserver.mydomain.com:5601/wev/app/kibana#/dashboard/777182'; | ||
expect(getLensUrlFromDashboardAbsoluteUrl(url, basePath, id)).toEqual( | ||
'http://myserver.mydomain.com:5601/wev/app/kibana#/lens/edit/1244' | ||
); | ||
|
||
url = | ||
"http://localhost:5601/app/kibana#/dashboard?_g=(refreshInterval:(pause:!t,value:0),time:(from:now-15m,to:now))&_a=(description:'',filters:!()"; | ||
expect(getLensUrlFromDashboardAbsoluteUrl(url, '', id)).toEqual( | ||
'http://localhost:5601/app/kibana#/lens/edit/1244' | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/legacy/core_plugins/kibana/public/dashboard/np_ready/url_helper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* 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 { parse } from 'url'; | ||
import { absoluteToParsedUrl } from '../legacy_imports'; | ||
import { DashboardConstants } from './dashboard_constants'; | ||
/** | ||
* Return query params from URL | ||
* @param url given url | ||
*/ | ||
export function getUrlVars(url: string): Record<string, string> { | ||
const vars: Record<string, string> = {}; | ||
// @ts-ignore | ||
url.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(_, key, value) { | ||
// @ts-ignore | ||
vars[key] = value; | ||
}); | ||
return vars; | ||
} | ||
|
||
/** * | ||
* Returns dashboard URL with added embeddableType and embeddableId query params | ||
* eg. | ||
* input: url: http://localhost:5601/lib/app/kibana#/dashboard?_g=(refreshInterval:(pause:!t,value:0),time:(from:now-15m,to:now)), embeddableId: 12345, embeddableType: 'lens' | ||
* output: http://localhost:5601/lib/app/kibana#dashboard?addEmbeddableType=lens&addEmbeddableId=12345&_g=(refreshInterval:(pause:!t,value:0),time:(from:now-15m,to:now)) | ||
* @param url dasbhoard absolute url | ||
* @param embeddableId id of the saved visualization | ||
* @param basePath current base path | ||
* @param urlVars url query params (optional) | ||
* @param embeddableType 'lens' or 'visualization' (optional, default is 'lens') | ||
*/ | ||
export function addEmbeddableToDashboardUrl( | ||
url: string | undefined, | ||
basePath: string, | ||
embeddableId: string, | ||
urlVars?: Record<string, string>, | ||
embeddableType?: string | ||
): string | null { | ||
if (!url) { | ||
return null; | ||
} | ||
const dashboardUrl = getUrlWithoutQueryParams(url); | ||
const dashboardParsedUrl = absoluteToParsedUrl(dashboardUrl, basePath); | ||
if (urlVars) { | ||
const keys = Object.keys(urlVars).sort(); | ||
keys.forEach(key => { | ||
dashboardParsedUrl.addQueryParameter(key, urlVars[key]); | ||
}); | ||
} | ||
dashboardParsedUrl.addQueryParameter( | ||
DashboardConstants.ADD_EMBEDDABLE_TYPE, | ||
embeddableType || 'lens' | ||
); | ||
dashboardParsedUrl.addQueryParameter(DashboardConstants.ADD_EMBEDDABLE_ID, embeddableId); | ||
return dashboardParsedUrl.getAbsoluteUrl(); | ||
} | ||
|
||
/** | ||
* Return Lens URL from dashboard absolute URL | ||
* @param dashboardAbsoluteUrl | ||
* @param basePath current base path | ||
* @param id Lens id | ||
*/ | ||
export function getLensUrlFromDashboardAbsoluteUrl( | ||
dashboardAbsoluteUrl: string | undefined | null, | ||
basePath: string | null | undefined, | ||
id: string | ||
): string | null { | ||
if (!dashboardAbsoluteUrl || basePath === null || basePath === undefined) { | ||
return null; | ||
} | ||
const { host, protocol } = parse(dashboardAbsoluteUrl); | ||
return `${protocol}//${host}${basePath}/app/kibana#/lens/edit/${id}`; | ||
} | ||
|
||
/** | ||
* Returns the portion of the URL without query params | ||
* eg. | ||
* input: http://localhost:5601/lib/app/kibana#/dashboard?param1=x¶m2=y¶m3=z | ||
* output:http://localhost:5601/lib/app/kibana#/dashboard | ||
* input: http://localhost:5601/lib/app/kibana#/dashboard/39292992?param1=x¶m2=y¶m3=z | ||
* output: http://localhost:5601/lib/app/kibana#/dashboard/39292992 | ||
* @param url url to parse | ||
*/ | ||
function getUrlWithoutQueryParams(url: string): string { | ||
return url.split('?')[0]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.