Skip to content

Commit

Permalink
Removing timefilter from Dashboard URL
Browse files Browse the repository at this point in the history
  • Loading branch information
Maja Grubic committed Jan 7, 2020
1 parent 1bfec16 commit bbfcc34
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
9 changes: 7 additions & 2 deletions x-pack/legacy/plugins/lens/public/app_plugin/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ import {
import { NOT_INTERNATIONALIZED_PRODUCT_NAME } from '../../common';
import { KibanaLegacySetup } from '../../../../../../src/plugins/kibana_legacy/public';
import { EditorFrameStart } from '../types';
import { getKibanaBasePathFromDashboardUrl, addEmbeddableToDashboardUrl } from './url_helper';
import {
getKibanaBasePathFromDashboardUrl,
addEmbeddableToDashboardUrl,
getDashboardUrlWithoutTime,
} from './url_helper';

export interface LensPluginSetupDependencies {
kibana_legacy: KibanaLegacySetup;
Expand Down Expand Up @@ -116,8 +120,9 @@ export class AppPlugin {
)}/lens/edit/${id}`;
if (lensUrl) {
window.history.pushState({}, '', lensUrl);
const dashboardUrlWithoutTime = getDashboardUrlWithoutTime(lastDashboardAbsoluteUrl);
const dashboardParsedUrl = addEmbeddableToDashboardUrl(
lastDashboardAbsoluteUrl,
dashboardUrlWithoutTime,
id,
'lens'
);
Expand Down
31 changes: 30 additions & 1 deletion x-pack/legacy/plugins/lens/public/app_plugin/url_helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ jest.mock('../../../../../../src/legacy/core_plugins/kibana/public/dashboard', (
},
}));

import { addEmbeddableToDashboardUrl, getKibanaBasePathFromDashboardUrl } from './url_helper';
import {
addEmbeddableToDashboardUrl,
getKibanaBasePathFromDashboardUrl,
getDashboardUrlWithoutTime,
} from './url_helper';

describe('Lens URL Helper', () => {
it('getKibanaBasePathFromDashboardUrl', () => {
Expand Down Expand Up @@ -53,4 +57,29 @@ describe('Lens URL Helper', () => {
`http://localhost:5601/app/kibana#/dashboard/777182?addEmbeddableType=${type}&addEmbeddableId=${id}&_g=(refreshInterval:(pause:!t,value:0),time:(from:now-4h,to:now))&_a=(description:'',filters:!()`
);
});

it('getDashboardUrlWithoutTime', () => {
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:!()`
);
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:!()`
);
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:!()`
);
});
});
21 changes: 21 additions & 0 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,6 +7,8 @@ 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 Down Expand Up @@ -56,3 +58,22 @@ export function addEmbeddableToDashboardUrl(
const dashboardState = regex[2];
return `${base}${DashboardConstants.ADD_EMBEDDABLE_TYPE}=${embeddableType}&${DashboardConstants.ADD_EMBEDDABLE_ID}=${embeddableId}&${dashboardState}`;
}

/**
* Returns dashboard URL without time parameter
* 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))
* @param url dashboard absolute url
*/
export function getDashboardUrlWithoutTime(url: string | undefined): string {
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;
}

0 comments on commit bbfcc34

Please sign in to comment.