Skip to content

Commit

Permalink
Replacing URL regex parsing with legacy URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
Maja Grubic committed Jan 10, 2020
1 parent fe9ab85 commit 25010ff
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 232 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* 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'
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ export { IInjector } from 'ui/chrome';
export { SavedObjectLoader } from 'ui/saved_objects';
export { VISUALIZE_EMBEDDABLE_TYPE } from '../visualize_embeddable';
export { registerTimefilterWithGlobalStateFactory } from 'ui/timefilter/setup_router';
export { absoluteToParsedUrl } from 'ui/url/absolute_to_parsed_url';
102 changes: 102 additions & 0 deletions src/legacy/core_plugins/kibana/public/dashboard/np_ready/url_helper.ts
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,
id: string
): string | null {
if (!dashboardAbsoluteUrl || !basePath) {
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&param2=y&param3=z
* output:http://localhost:5601/lib/app/kibana#/dashboard
* input: http://localhost:5601/lib/app/kibana#/dashboard/39292992?param1=x&param2=y&param3=z
* output: http://localhost:5601/lib/app/kibana#/dashboard/39292992
* @param url url to parse
*/
function getUrlWithoutQueryParams(url: string): string {
return url.split('?')[0];
}
44 changes: 24 additions & 20 deletions x-pack/legacy/plugins/lens/public/app_plugin/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ import { NOT_INTERNATIONALIZED_PRODUCT_NAME } from '../../common';
import { KibanaLegacySetup } from '../../../../../../src/plugins/kibana_legacy/public';
import { EditorFrameStart } from '../types';
import {
getKibanaBasePathFromDashboardUrl,
addEmbeddableToDashboardUrl,
getDashboardUrlWithQueryParams,
getUrlVars,
} from './url_helper';
getLensUrlFromDashboardAbsoluteUrl,
} from '../../../../../../src/legacy/core_plugins/kibana/public/dashboard/np_ready/url_helper';

export interface LensPluginSetupDependencies {
kibana_legacy: KibanaLegacySetup;
Expand Down Expand Up @@ -95,7 +94,6 @@ export class AppPlugin {
}
const { data, savedObjectsClient, editorFrame } = this.startDependencies;
addHelpMenuToAppChrome(context.core.chrome);

const instance = editorFrame.createInstance({});

setReportManager(
Expand Down Expand Up @@ -126,25 +124,31 @@ export class AppPlugin {
routeProps.history.push(`/lens/edit/${id}`);
const url = context.core.chrome.navLinks.get('kibana:dashboard');
if (!url) {
return;
throw new Error('Cannot get last dashboard url');
}
const lastDashboardAbsoluteUrl = url.url;
const lensUrl = `${getKibanaBasePathFromDashboardUrl(
lastDashboardAbsoluteUrl
)}/lens/edit/${id}`;
if (lastDashboardAbsoluteUrl && lensUrl) {
const urlVars = getUrlVars(lastDashboardAbsoluteUrl);
updateUrlTime(urlVars);
window.history.pushState({}, '', lensUrl);
const dashboardUrl = getDashboardUrlWithQueryParams(
lastDashboardAbsoluteUrl,
urlVars
);
const dashboardParsedUrl = addEmbeddableToDashboardUrl(dashboardUrl, id, 'lens');
if (dashboardParsedUrl) {
window.history.pushState({}, '', dashboardParsedUrl);
}
const basePath = context.core.http.basePath.get();
const lensUrl = getLensUrlFromDashboardAbsoluteUrl(
lastDashboardAbsoluteUrl,
basePath,
id
);
if (!lastDashboardAbsoluteUrl || !lensUrl) {
throw new Error('Cannot get last dashboard url');
}
window.history.pushState({}, '', lensUrl);
const urlVars = getUrlVars(lastDashboardAbsoluteUrl);
updateUrlTime(urlVars); // we need to pass in timerange in query params directly
const dashboardParsedUrl = addEmbeddableToDashboardUrl(
lastDashboardAbsoluteUrl,
basePath,
id,
urlVars
);
if (!dashboardParsedUrl) {
throw new Error('Problem parsing dashboard url');
}
window.history.pushState({}, '', dashboardParsedUrl);
}
};

Expand Down
102 changes: 0 additions & 102 deletions x-pack/legacy/plugins/lens/public/app_plugin/url_helper.test.ts

This file was deleted.

Loading

0 comments on commit 25010ff

Please sign in to comment.