Skip to content

Commit

Permalink
Replacing time parsing with rison
Browse files Browse the repository at this point in the history
  • Loading branch information
Maja Grubic committed Jan 8, 2020
1 parent 8022cea commit 64e6ceb
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 38 deletions.
31 changes: 23 additions & 8 deletions x-pack/legacy/plugins/lens/public/app_plugin/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { HashRouter, Route, RouteComponentProps, Switch } from 'react-router-dom
import { render, unmountComponentAtNode } from 'react-dom';
import { CoreSetup, CoreStart, SavedObjectsClientContract } from 'src/core/public';
import { DataPublicPluginStart } from 'src/plugins/data/public';
import rison, { RisonObject, RisonValue } from 'rison-node';
import { isObject } from 'lodash';
import { DataStart } from '../../../../../../src/legacy/core_plugins/data/public';
import { Storage } from '../../../../../../src/plugins/kibana_utils/public';
import { editorFrameSetup, editorFrameStart, editorFrameStop } from '../editor_frame_plugin';
Expand All @@ -44,7 +46,8 @@ import { EditorFrameStart } from '../types';
import {
getKibanaBasePathFromDashboardUrl,
addEmbeddableToDashboardUrl,
getDashboardUrlWithoutTime,
getDashboardUrlWithQueryParams,
getUrlVars,
} from './url_helper';

export interface LensPluginSetupDependencies {
Expand All @@ -56,6 +59,9 @@ export interface LensPluginStartDependencies {
dataShim: DataStart;
}

export const isRisonObject = (value: RisonValue): value is RisonObject => {
return isObject(value);
};
export class AppPlugin {
private startDependencies: {
data: DataPublicPluginStart;
Expand Down Expand Up @@ -98,7 +104,15 @@ export class AppPlugin {
http: core.http,
})
);

const updateUrlTime = (urlVars: Record<string, string>): void => {
const decoded: RisonObject = rison.decode(urlVars._g) as RisonObject;
if (!decoded) {
return;
}
// @ts-ignore
decoded.time = data.query.timefilter.timefilter.getTime();
urlVars._g = rison.encode((decoded as unknown) as RisonObject);
};
const redirectTo = (
routeProps: RouteComponentProps<{ id?: string }>,
addToDashboardMode: boolean,
Expand All @@ -118,14 +132,15 @@ export class AppPlugin {
const lensUrl = `${getKibanaBasePathFromDashboardUrl(
lastDashboardAbsoluteUrl
)}/lens/edit/${id}`;
const dashboardUrlWithoutTime = getDashboardUrlWithoutTime(lastDashboardAbsoluteUrl);
if (lensUrl && dashboardUrlWithoutTime) {
if (lastDashboardAbsoluteUrl && lensUrl) {
const urlVars = getUrlVars(lastDashboardAbsoluteUrl);
updateUrlTime(urlVars);
window.history.pushState({}, '', lensUrl);
const dashboardParsedUrl = addEmbeddableToDashboardUrl(
dashboardUrlWithoutTime,
id,
'lens'
const dashboardUrl = getDashboardUrlWithQueryParams(
lastDashboardAbsoluteUrl,
urlVars
);
const dashboardParsedUrl = addEmbeddableToDashboardUrl(dashboardUrl, id, 'lens');
if (dashboardParsedUrl) {
window.history.pushState({}, '', dashboardParsedUrl);
}
Expand Down
53 changes: 35 additions & 18 deletions x-pack/legacy/plugins/lens/public/app_plugin/url_helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ jest.mock('../../../../../../src/legacy/core_plugins/kibana/public/dashboard', (
import {
addEmbeddableToDashboardUrl,
getKibanaBasePathFromDashboardUrl,
getDashboardUrlWithoutTime,
getUrlVars,
getDashboardUrlWithQueryParams,
} from './url_helper';

describe('Lens URL Helper', () => {
Expand Down Expand Up @@ -58,28 +59,44 @@ describe('Lens URL Helper', () => {
);
});

it('getDashboardUrlWithoutTime', () => {
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(getDashboardUrlWithoutTime(url)).toEqual(
"http://localhost:5601/app/kibana#/dashboard?_g=(refreshInterval:(pause:!t,value:0))&_a=(description:'',filters:!()"
);
url =
"http://mybusiness.mydomain.com/app/kibana#/dashboard?_g=(refreshInterval:(pause:!t,value:0))&_a=(description:'',filters:!()";
expect(getDashboardUrlWithoutTime(url)).toEqual(
`http://mybusiness.mydomain.com/app/kibana#/dashboard?_g=(refreshInterval:(pause:!t,value:0))&_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('getDashboardUrlWithQueryParams', () => {
const queryParams = {
_g: '(refreshInterval:(pause:!t,value:0),time:(from:now-45m,to:now))',
_a: "(description:'',filters:!()",
};
let url =
"http://localhost:5601/app/kibana#/dashboard?_g=(refreshInterval:(pause:!t,value:0),time:(from:now-15m,to:now))&_a=(description:'',filters:!()";
expect(getDashboardUrlWithQueryParams(url, queryParams)).toEqual(
"http://localhost:5601/app/kibana#/dashboard?_a=(description:'',filters:!()&_g=(refreshInterval:(pause:!t,value:0),time:(from:now-45m,to:now))"
);

url =
"http://mybusiness.mydomain.com/app/kibana#/dashboard?_g=(time:(from:now-15m,to:now),refreshInterval:(pause:!t,value:0))&_a=(description:'',filters:!()";
expect(getDashboardUrlWithoutTime(url)).toEqual(
`http://mybusiness.mydomain.com/app/kibana#/dashboard?_g=(refreshInterval:(pause:!t,value:0))&_a=(description:\'\',filters:!()`
"http://localhost:5601/app/kibana#/dashboard/625357282?_a=(description:'',filters:!()&_g=(refreshInterval:(pause:!t,value:0),time:(from:now-15m,to:now))";
expect(getDashboardUrlWithQueryParams(url, queryParams)).toEqual(
"http://localhost:5601/app/kibana#/dashboard/625357282?_a=(description:'',filters:!()&_g=(refreshInterval:(pause:!t,value:0),time:(from:now-45m,to:now))"
);
url = 'http://notDashboarUrl';
expect(getDashboardUrlWithoutTime(url)).toBe('http://notDashboarUrl');
url =
"http://localhost:5601/app/kibana#/dashboard/777182?_g=(refreshInterval:(pause:!t,value:0),time:(from:now-4h,to:now))&_a=(description:'',filters:!()";
expect(getDashboardUrlWithoutTime(url)).toBe(
`http://localhost:5601/app/kibana#/dashboard/777182?_g=(refreshInterval:(pause:!t,value:0))&_a=(description:'',filters:!()`

url = 'http://localhost:5601/app/kibana#/dashboard/777182';
expect(getDashboardUrlWithQueryParams(url, queryParams)).toEqual(
"http://localhost:5601/app/kibana#/dashboard/777182?_a=(description:'',filters:!()&_g=(refreshInterval:(pause:!t,value:0),time:(from:now-45m,to:now))"
);
});
});
55 changes: 43 additions & 12 deletions x-pack/legacy/plugins/lens/public/app_plugin/url_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { DashboardConstants } from '../../../../../../src/legacy/core_plugins/ki

const EMPTY_DASHBOARD_PATTERN = /(.*#\/dashboard\?)(.*)/;
const DASHBOARD_WITH_ID_PATTERN = /(.*#\/dashboard\/.*\?)(.*)/;
const TIME_PATTERN_1 = /(.*)(,time:[^)]+\))(.*)/;
const TIME_PATTERN_2 = /(.*)(time:[^)]+\),)(.*)/; // same as TIME_PATTERN_1, but comma follows, not preceeds

/** *
* Returns base path from dashboard url
Expand All @@ -32,6 +30,42 @@ export function getKibanaBasePathFromDashboardUrl(url: string | undefined): stri
return null;
}

/**
* Returns dashboard url with given query params. If query params already exist in the url, they will be replaced
* eg.
* input: http://localhost:5601/lib/app/kibana#/dashboard, {_a: {...}, _g: {...}}
* output: http://localhost:5601/lib/app/kibana#/dashboard??_g=(refreshInterval:(pause:!t,value:0),time:(from:now-15m,to:now))&_a=(description:'',filters:!(),fullScreenMode:!f,options:(hidePanelTitles:!f,useMargins:!t),panels:!(),query:(language:kuery,query:''),timeRestore:!f,title:'',viewMode:edit)
* @param absoluteUrl dashboard absolute url
* @param urlParams query params to append to the url
*/
export function getDashboardUrlWithQueryParams(
absoluteUrl: string,
urlParams: Record<string, string>
): string {
let dashboardUrl = getUrlWithoutQueryParams(absoluteUrl);
if (!dashboardUrl) {
return absoluteUrl;
}
dashboardUrl += '?';
const keys = Object.keys(urlParams).sort();
keys.forEach((key, index) => {
dashboardUrl += `${key}=${urlParams[key]}`;
if (index !== keys.length - 1) {
dashboardUrl += '&';
}
});
return dashboardUrl;
}

export function getUrlVars(url: string): Record<string, string> {
const vars: Record<string, string> = {};
// @ts-ignore
url.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(_, key, value) {
vars[key] = value;
});
return vars;
}

/** *
* Returns dashboard URL with added embeddableType and embeddableId query params
* eg.
Expand Down Expand Up @@ -60,20 +94,17 @@ export function addEmbeddableToDashboardUrl(
}

/**
* Returns dashboard URL without time parameter
* Returns the portion of the URL without query params
* eg.
* input: http://localhost:5601/lib/app/kibana#/dashboard?_g=(refreshInterval:(pause:!t,value:0),time:(from:now-15m,to:now))
* output: http://localhost:5601/lib/app/kibana#/dashboard?_g=(refreshInterval:(pause:!t,value:0))
* 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 dashboard absolute url
*/
export function getDashboardUrlWithoutTime(url: string | undefined): string | null {
function getUrlWithoutQueryParams(url: string | undefined): string | null {
if (!url) {
return null;
}
let regex = RegExp(TIME_PATTERN_1).exec(url);
regex = regex || RegExp(TIME_PATTERN_2).exec(url);
if (regex) {
return `${regex[1]}${regex[3]}`;
}
return url;
return url.split('?')[0];
}

0 comments on commit 64e6ceb

Please sign in to comment.