Skip to content

Commit

Permalink
[Drilldowns] improve dashboard link generator to support dashboard-to…
Browse files Browse the repository at this point in the history
…-dashboard drilldown (#61307)

* improve dashboard link generator to support dashboard-to-dashboard drilldown

* any -> unknown

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
Dosant and elasticmachine authored Mar 31, 2020
1 parent 99a9288 commit 25a1119
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
17 changes: 15 additions & 2 deletions src/plugins/dashboard/public/url_generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { createDirectAccessDashboardLinkGenerator } from './url_generator';
import { hashedItemStore } from '../../kibana_utils/public';
// eslint-disable-next-line
import { mockStorage } from '../../kibana_utils/public/storage/hashed_item_store/mock';
import { esFilters } from '../../data/public';

const APP_BASE_PATH: string = 'xyz/app/kibana';

Expand Down Expand Up @@ -50,12 +51,13 @@ describe('dashboard url generator', () => {
);
});

test('creates a link with filters, time range and query to a saved object', async () => {
test('creates a link with filters, time range, refresh interval and query to a saved object', async () => {
const generator = createDirectAccessDashboardLinkGenerator(() =>
Promise.resolve({ appBasePath: APP_BASE_PATH, useHashedUrl: false })
);
const url = await generator.createUrl!({
timeRange: { to: 'now', from: 'now-15m', mode: 'relative' },
refreshInterval: { pause: false, value: 300 },
dashboardId: '123',
filters: [
{
Expand All @@ -66,11 +68,22 @@ describe('dashboard url generator', () => {
},
query: { query: 'hi' },
},
{
meta: {
alias: null,
disabled: false,
negate: false,
},
query: { query: 'hi' },
$state: {
store: esFilters.FilterStateStore.GLOBAL_STATE,
},
},
],
query: { query: 'bye', language: 'kuery' },
});
expect(url).toMatchInlineSnapshot(
`"xyz/app/kibana#/dashboard/123?_a=(filters:!((meta:(alias:!n,disabled:!f,negate:!f),query:(query:hi))),query:(language:kuery,query:bye))&_g=(time:(from:now-15m,mode:relative,to:now))"`
`"xyz/app/kibana#/dashboard/123?_a=(filters:!((meta:(alias:!n,disabled:!f,negate:!f),query:(query:hi))),query:(language:kuery,query:bye))&_g=(filters:!(('$state':(store:globalState),meta:(alias:!n,disabled:!f,negate:!f),query:(query:hi))),refreshInterval:(pause:!f,value:300),time:(from:now-15m,mode:relative,to:now))"`
);
});

Expand Down
41 changes: 32 additions & 9 deletions src/plugins/dashboard/public/url_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@
* under the License.
*/

import { TimeRange, Filter, Query } from '../../data/public';
import {
TimeRange,
Filter,
Query,
esFilters,
QueryState,
RefreshInterval,
} from '../../data/public';
import { setStateToKbnUrl } from '../../kibana_utils/public';
import { UrlGeneratorsDefinition, UrlGeneratorState } from '../../share/public';

Expand All @@ -36,10 +43,15 @@ export type DashboardAppLinkGeneratorState = UrlGeneratorState<{
* Optionally set the time range in the time picker.
*/
timeRange?: TimeRange;

/**
* Optionally set the refresh interval.
*/
refreshInterval?: RefreshInterval;

/**
* Optionally apply filers. NOTE: if given and used in conjunction with `dashboardId`, and the
* saved dashboard has filters saved with it, this will _replace_ those filters. This will set
* app filters, not global filters.
* saved dashboard has filters saved with it, this will _replace_ those filters.
*/
filters?: Filter[];
/**
Expand All @@ -64,21 +76,32 @@ export const createDirectAccessDashboardLinkGenerator = (
const appBasePath = startServices.appBasePath;
const hash = state.dashboardId ? `dashboard/${state.dashboardId}` : `dashboard`;

const cleanEmptyKeys = (stateObj: Record<string, unknown>) => {
Object.keys(stateObj).forEach(key => {
if (stateObj[key] === undefined) {
delete stateObj[key];
}
});
return stateObj;
};

const appStateUrl = setStateToKbnUrl(
STATE_STORAGE_KEY,
{
cleanEmptyKeys({
query: state.query,
filters: state.filters,
},
filters: state.filters?.filter(f => !esFilters.isFilterPinned(f)),
}),
{ useHash },
`${appBasePath}#/${hash}`
);

return setStateToKbnUrl(
return setStateToKbnUrl<QueryState>(
GLOBAL_STATE_STORAGE_KEY,
{
cleanEmptyKeys({
time: state.timeRange,
},
filters: state.filters?.filter(f => esFilters.isFilterPinned(f)),
refreshInterval: state.refreshInterval,
}),
{ useHash },
appStateUrl
);
Expand Down

0 comments on commit 25a1119

Please sign in to comment.