Skip to content

Commit d6e6972

Browse files
committed
[ML] Fixing custom urls to dashboards
1 parent 7f901f9 commit d6e6972

4 files changed

Lines changed: 83 additions & 71 deletions

File tree

x-pack/plugins/ml/public/application/app.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const App: FC<AppProps> = ({ coreStart, deps, appMountParams }) => {
4141
application: coreStart.application,
4242
http: coreStart.http,
4343
security: deps.security,
44+
urlGenerators: deps.share.urlGenerators,
4445
});
4546

4647
const mlLicense = setLicenseCache(deps.licensing);

x-pack/plugins/ml/public/application/jobs/components/custom_url_editor/utils.js

Lines changed: 68 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
import { TIME_RANGE_TYPE, URL_TYPE } from './constants';
88

99
import rison from 'rison-node';
10-
// import url from 'url';
10+
import url from 'url';
1111

12-
// import { npStart } from 'ui/new_platform';
13-
// import { DASHBOARD_APP_URL_GENERATOR } from '../../../../../../../../src/plugins/dashboard_embeddable_container/public';
12+
import { DASHBOARD_APP_URL_GENERATOR } from '../../../../../../../../src/plugins/dashboard/public';
1413

1514
import { ML_RESULTS_INDEX_PATTERN } from '../../../../../common/constants/index_patterns';
1615
import { getPartitioningFieldNames } from '../../../../../common/util/job_utils';
@@ -19,7 +18,7 @@ import { replaceTokensInUrlValue, isValidLabel } from '../../../util/custom_url_
1918
import { ml } from '../../../services/ml_api_service';
2019
import { mlJobService } from '../../../services/job_service';
2120
import { escapeForElasticsearchQuery } from '../../../util/string_utils';
22-
// import { getSavedObjectsClient } from '../../../util/dependency_cache';
21+
import { getSavedObjectsClient, getGetUrlGenerator } from '../../../util/dependency_cache';
2322

2423
export function getNewCustomUrlDefaults(job, dashboards, indexPatterns) {
2524
// Returns the settings object in the format used by the custom URL editor
@@ -119,7 +118,7 @@ export function buildCustomUrlFromSettings(settings) {
119118
// Dashboard URL returns a Promise as a query is made to obtain the full dashboard config.
120119
// So wrap the other two return types in a Promise for consistent return type.
121120
if (settings.type === URL_TYPE.KIBANA_DASHBOARD) {
122-
// return buildDashboardUrlFromSettings(settings);
121+
return buildDashboardUrlFromSettings(settings);
123122
} else if (settings.type === URL_TYPE.KIBANA_DISCOVER) {
124123
return Promise.resolve(buildDiscoverUrlFromSettings(settings));
125124
} else {
@@ -132,72 +131,70 @@ export function buildCustomUrlFromSettings(settings) {
132131
}
133132
}
134133

135-
// function buildDashboardUrlFromSettings(settings) {
136-
// // Get the complete list of attributes for the selected dashboard (query, filters).
137-
// return new Promise((resolve, reject) => {
138-
// const { dashboardId, queryFieldNames } = settings.kibanaSettings;
139-
140-
// const savedObjectsClient = getSavedObjectsClient();
141-
// savedObjectsClient
142-
// .get('dashboard', dashboardId)
143-
// .then(response => {
144-
// // Use the filters from the saved dashboard if there are any.
145-
// // let filters = [];
146-
147-
// // Use the query from the dashboard only if no job entities are selected.
148-
// let query = undefined;
149-
150-
// const searchSourceJSON = response.get('kibanaSavedObjectMeta.searchSourceJSON');
151-
// if (searchSourceJSON !== undefined) {
152-
// const searchSourceData = JSON.parse(searchSourceJSON);
153-
// if (searchSourceData.filter !== undefined) {
154-
// filters = searchSourceData.filter;
155-
// }
156-
// query = searchSourceData.query;
157-
// }
158-
159-
// const queryFromEntityFieldNames = buildAppStateQueryParam(queryFieldNames);
160-
// if (queryFromEntityFieldNames !== undefined) {
161-
// query = queryFromEntityFieldNames;
162-
// }
163-
164-
// const generator = npStart.plugins.share.urlGenerators.getUrlGenerator(
165-
// DASHBOARD_APP_URL_GENERATOR
166-
// );
167-
168-
// return generator
169-
// .createUrl({
170-
// dashboardId,
171-
// timeRange: {
172-
// from: '$earliest$',
173-
// to: '$latest$',
174-
// mode: 'absolute',
175-
// },
176-
// filters,
177-
// query,
178-
// // Don't hash the URL since this string will be 1. shown to the user and 2. used as a
179-
// // template to inject the time parameters.
180-
// useHash: false,
181-
// })
182-
// .then(urlValue => {
183-
// const urlToAdd = {
184-
// url_name: settings.label,
185-
// url_value: decodeURIComponent(`kibana${url.parse(urlValue).hash}`),
186-
// time_range: TIME_RANGE_TYPE.AUTO,
187-
// };
188-
189-
// if (settings.timeRange.type === TIME_RANGE_TYPE.INTERVAL) {
190-
// urlToAdd.time_range = settings.timeRange.interval;
191-
// }
192-
193-
// resolve(urlToAdd);
194-
// });
195-
// })
196-
// .catch(resp => {
197-
// reject(resp);
198-
// });
199-
// });
200-
// }
134+
function buildDashboardUrlFromSettings(settings) {
135+
// Get the complete list of attributes for the selected dashboard (query, filters).
136+
return new Promise((resolve, reject) => {
137+
const { dashboardId, queryFieldNames } = settings.kibanaSettings;
138+
139+
const savedObjectsClient = getSavedObjectsClient();
140+
savedObjectsClient
141+
.get('dashboard', dashboardId)
142+
.then(response => {
143+
// Use the filters from the saved dashboard if there are any.
144+
let filters = [];
145+
146+
// Use the query from the dashboard only if no job entities are selected.
147+
let query = undefined;
148+
149+
const searchSourceJSON = response.get('kibanaSavedObjectMeta.searchSourceJSON');
150+
if (searchSourceJSON !== undefined) {
151+
const searchSourceData = JSON.parse(searchSourceJSON);
152+
if (searchSourceData.filter !== undefined) {
153+
filters = searchSourceData.filter;
154+
}
155+
query = searchSourceData.query;
156+
}
157+
158+
const queryFromEntityFieldNames = buildAppStateQueryParam(queryFieldNames);
159+
if (queryFromEntityFieldNames !== undefined) {
160+
query = queryFromEntityFieldNames;
161+
}
162+
163+
const getUrlGenerator = getGetUrlGenerator();
164+
const generator = getUrlGenerator(DASHBOARD_APP_URL_GENERATOR);
165+
return generator
166+
.createUrl({
167+
dashboardId,
168+
timeRange: {
169+
from: '$earliest$',
170+
to: '$latest$',
171+
mode: 'absolute',
172+
},
173+
filters,
174+
query,
175+
// Don't hash the URL since this string will be 1. shown to the user and 2. used as a
176+
// template to inject the time parameters.
177+
useHash: false,
178+
})
179+
.then(urlValue => {
180+
const urlToAdd = {
181+
url_name: settings.label,
182+
url_value: decodeURIComponent(`kibana${url.parse(urlValue).hash}`),
183+
time_range: TIME_RANGE_TYPE.AUTO,
184+
};
185+
186+
if (settings.timeRange.type === TIME_RANGE_TYPE.INTERVAL) {
187+
urlToAdd.time_range = settings.timeRange.interval;
188+
}
189+
190+
resolve(urlToAdd);
191+
});
192+
})
193+
.catch(resp => {
194+
reject(resp);
195+
});
196+
});
197+
}
201198

202199
function buildDiscoverUrlFromSettings(settings) {
203200
const { discoverIndexPatternId, queryFieldNames } = settings.kibanaSettings;

x-pack/plugins/ml/public/application/util/dependency_cache.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
ChromeRecentlyAccessed,
2222
IBasePath,
2323
} from 'kibana/public';
24+
import { SharePluginStart } from 'src/plugins/share/public';
2425
import { SecurityPluginSetup } from '../../../../security/public';
2526

2627
export interface DependencyCache {
@@ -40,6 +41,7 @@ export interface DependencyCache {
4041
http: HttpStart | null;
4142
security: SecurityPluginSetup | null;
4243
i18n: I18nStart | null;
44+
urlGenerators: SharePluginStart['urlGenerators'] | null;
4345
}
4446

4547
const cache: DependencyCache = {
@@ -59,6 +61,7 @@ const cache: DependencyCache = {
5961
http: null,
6062
security: null,
6163
i18n: null,
64+
urlGenerators: null,
6265
};
6366

6467
export function setDependencyCache(deps: Partial<DependencyCache>) {
@@ -78,6 +81,7 @@ export function setDependencyCache(deps: Partial<DependencyCache>) {
7881
cache.http = deps.http || null;
7982
cache.security = deps.security || null;
8083
cache.i18n = deps.i18n || null;
84+
cache.urlGenerators = deps.urlGenerators || null;
8185
}
8286

8387
export function getTimefilter() {
@@ -191,6 +195,13 @@ export function getI18n() {
191195
return cache.i18n;
192196
}
193197

198+
export function getGetUrlGenerator() {
199+
if (cache.urlGenerators === null) {
200+
throw new Error("urlGenerators hasn't been initialized");
201+
}
202+
return cache.urlGenerators.getUrlGenerator;
203+
}
204+
194205
export function clearCache() {
195206
console.log('clearing dependency cache'); // eslint-disable-line no-console
196207
Object.keys(cache).forEach(k => {

x-pack/plugins/ml/public/plugin.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import { i18n } from '@kbn/i18n';
88
import { Plugin, CoreStart, CoreSetup, AppMountParameters } from 'kibana/public';
99
import { ManagementSetup } from 'src/plugins/management/public';
10+
import { SharePluginStart } from 'src/plugins/share/public';
1011

1112
import { DataPublicPluginStart } from 'src/plugins/data/public';
1213
import { SecurityPluginSetup } from '../../security/public';
@@ -17,6 +18,7 @@ import { PLUGIN_ID, PLUGIN_ICON } from '../common/constants/app';
1718

1819
export interface MlStartDependencies {
1920
data: DataPublicPluginStart;
21+
share: SharePluginStart;
2022
}
2123
export interface MlSetupDependencies {
2224
security: SecurityPluginSetup;
@@ -41,6 +43,7 @@ export class MlPlugin implements Plugin<Setup, Start> {
4143
coreStart,
4244
{
4345
data: pluginsStart.data,
46+
share: pluginsStart.share,
4447
security: pluginsSetup.security,
4548
licensing: pluginsSetup.licensing,
4649
management: pluginsSetup.management,

0 commit comments

Comments
 (0)