Skip to content

Commit

Permalink
[ML] Fixing custom urls to dashboards (#60355) (#60434)
Browse files Browse the repository at this point in the history
* [ML] Fixing custom urls to dashboards

* missing file

Co-authored-by: Elastic Machine <[email protected]>

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
jgowdyelastic and elasticmachine authored Mar 18, 2020
1 parent 7f93862 commit 5acd83c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 72 deletions.
3 changes: 2 additions & 1 deletion x-pack/plugins/ml/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"features",
"home",
"licensing",
"usageCollection"
"usageCollection",
"share"
],
"optionalPlugins": [
"security",
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/ml/public/application/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const App: FC<AppProps> = ({ coreStart, deps, appMountParams }) => {
application: coreStart.application,
http: coreStart.http,
security: deps.security,
urlGenerators: deps.share.urlGenerators,
});

const mlLicense = setLicenseCache(deps.licensing);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
import { TIME_RANGE_TYPE, URL_TYPE } from './constants';

import rison from 'rison-node';
// import url from 'url';
import url from 'url';

// import { npStart } from 'ui/new_platform';
// import { DASHBOARD_APP_URL_GENERATOR } from '../../../../../../../../src/plugins/dashboard_embeddable_container/public';
import { DASHBOARD_APP_URL_GENERATOR } from '../../../../../../../../src/plugins/dashboard/public';

import { ML_RESULTS_INDEX_PATTERN } from '../../../../../common/constants/index_patterns';
import { getPartitioningFieldNames } from '../../../../../common/util/job_utils';
Expand All @@ -19,7 +18,7 @@ import { replaceTokensInUrlValue, isValidLabel } from '../../../util/custom_url_
import { ml } from '../../../services/ml_api_service';
import { mlJobService } from '../../../services/job_service';
import { escapeForElasticsearchQuery } from '../../../util/string_utils';
// import { getSavedObjectsClient } from '../../../util/dependency_cache';
import { getSavedObjectsClient, getGetUrlGenerator } from '../../../util/dependency_cache';

export function getNewCustomUrlDefaults(job, dashboards, indexPatterns) {
// Returns the settings object in the format used by the custom URL editor
Expand Down Expand Up @@ -120,7 +119,7 @@ export function buildCustomUrlFromSettings(settings) {
// Dashboard URL returns a Promise as a query is made to obtain the full dashboard config.
// So wrap the other two return types in a Promise for consistent return type.
if (settings.type === URL_TYPE.KIBANA_DASHBOARD) {
// return buildDashboardUrlFromSettings(settings);
return buildDashboardUrlFromSettings(settings);
} else if (settings.type === URL_TYPE.KIBANA_DISCOVER) {
return Promise.resolve(buildDiscoverUrlFromSettings(settings));
} else {
Expand All @@ -133,72 +132,70 @@ export function buildCustomUrlFromSettings(settings) {
}
}

// function buildDashboardUrlFromSettings(settings) {
// // Get the complete list of attributes for the selected dashboard (query, filters).
// return new Promise((resolve, reject) => {
// const { dashboardId, queryFieldNames } = settings.kibanaSettings;

// const savedObjectsClient = getSavedObjectsClient();
// savedObjectsClient
// .get('dashboard', dashboardId)
// .then(response => {
// // Use the filters from the saved dashboard if there are any.
// // let filters = [];

// // Use the query from the dashboard only if no job entities are selected.
// let query = undefined;

// const searchSourceJSON = response.get('kibanaSavedObjectMeta.searchSourceJSON');
// if (searchSourceJSON !== undefined) {
// const searchSourceData = JSON.parse(searchSourceJSON);
// if (searchSourceData.filter !== undefined) {
// filters = searchSourceData.filter;
// }
// query = searchSourceData.query;
// }

// const queryFromEntityFieldNames = buildAppStateQueryParam(queryFieldNames);
// if (queryFromEntityFieldNames !== undefined) {
// query = queryFromEntityFieldNames;
// }

// const generator = npStart.plugins.share.urlGenerators.getUrlGenerator(
// DASHBOARD_APP_URL_GENERATOR
// );

// return generator
// .createUrl({
// dashboardId,
// timeRange: {
// from: '$earliest$',
// to: '$latest$',
// mode: 'absolute',
// },
// filters,
// query,
// // Don't hash the URL since this string will be 1. shown to the user and 2. used as a
// // template to inject the time parameters.
// useHash: false,
// })
// .then(urlValue => {
// const urlToAdd = {
// url_name: settings.label,
// url_value: decodeURIComponent(`kibana${url.parse(urlValue).hash}`),
// time_range: TIME_RANGE_TYPE.AUTO,
// };

// if (settings.timeRange.type === TIME_RANGE_TYPE.INTERVAL) {
// urlToAdd.time_range = settings.timeRange.interval;
// }

// resolve(urlToAdd);
// });
// })
// .catch(resp => {
// reject(resp);
// });
// });
// }
function buildDashboardUrlFromSettings(settings) {
// Get the complete list of attributes for the selected dashboard (query, filters).
return new Promise((resolve, reject) => {
const { dashboardId, queryFieldNames } = settings.kibanaSettings;

const savedObjectsClient = getSavedObjectsClient();
savedObjectsClient
.get('dashboard', dashboardId)
.then(response => {
// Use the filters from the saved dashboard if there are any.
let filters = [];

// Use the query from the dashboard only if no job entities are selected.
let query = undefined;

const searchSourceJSON = response.get('kibanaSavedObjectMeta.searchSourceJSON');
if (searchSourceJSON !== undefined) {
const searchSourceData = JSON.parse(searchSourceJSON);
if (searchSourceData.filter !== undefined) {
filters = searchSourceData.filter;
}
query = searchSourceData.query;
}

const queryFromEntityFieldNames = buildAppStateQueryParam(queryFieldNames);
if (queryFromEntityFieldNames !== undefined) {
query = queryFromEntityFieldNames;
}

const getUrlGenerator = getGetUrlGenerator();
const generator = getUrlGenerator(DASHBOARD_APP_URL_GENERATOR);
return generator
.createUrl({
dashboardId,
timeRange: {
from: '$earliest$',
to: '$latest$',
mode: 'absolute',
},
filters,
query,
// Don't hash the URL since this string will be 1. shown to the user and 2. used as a
// template to inject the time parameters.
useHash: false,
})
.then(urlValue => {
const urlToAdd = {
url_name: settings.label,
url_value: decodeURIComponent(`kibana${url.parse(urlValue).hash}`),
time_range: TIME_RANGE_TYPE.AUTO,
};

if (settings.timeRange.type === TIME_RANGE_TYPE.INTERVAL) {
urlToAdd.time_range = settings.timeRange.interval;
}

resolve(urlToAdd);
});
})
.catch(resp => {
reject(resp);
});
});
}

function buildDiscoverUrlFromSettings(settings) {
const { discoverIndexPatternId, queryFieldNames } = settings.kibanaSettings;
Expand Down
11 changes: 11 additions & 0 deletions x-pack/plugins/ml/public/application/util/dependency_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
ChromeRecentlyAccessed,
IBasePath,
} from 'kibana/public';
import { SharePluginStart } from 'src/plugins/share/public';
import { SecurityPluginSetup } from '../../../../security/public';

export interface DependencyCache {
Expand All @@ -40,6 +41,7 @@ export interface DependencyCache {
http: HttpStart | null;
security: SecurityPluginSetup | null;
i18n: I18nStart | null;
urlGenerators: SharePluginStart['urlGenerators'] | null;
}

const cache: DependencyCache = {
Expand All @@ -59,6 +61,7 @@ const cache: DependencyCache = {
http: null,
security: null,
i18n: null,
urlGenerators: null,
};

export function setDependencyCache(deps: Partial<DependencyCache>) {
Expand All @@ -78,6 +81,7 @@ export function setDependencyCache(deps: Partial<DependencyCache>) {
cache.http = deps.http || null;
cache.security = deps.security || null;
cache.i18n = deps.i18n || null;
cache.urlGenerators = deps.urlGenerators || null;
}

export function getTimefilter() {
Expand Down Expand Up @@ -191,6 +195,13 @@ export function getI18n() {
return cache.i18n;
}

export function getGetUrlGenerator() {
if (cache.urlGenerators === null) {
throw new Error("urlGenerators hasn't been initialized");
}
return cache.urlGenerators.getUrlGenerator;
}

export function clearCache() {
console.log('clearing dependency cache'); // eslint-disable-line no-console
Object.keys(cache).forEach(k => {
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/ml/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { i18n } from '@kbn/i18n';
import { Plugin, CoreStart, CoreSetup, AppMountParameters } from 'kibana/public';
import { ManagementSetup } from 'src/plugins/management/public';
import { SharePluginStart } from 'src/plugins/share/public';

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

export interface MlStartDependencies {
data: DataPublicPluginStart;
share: SharePluginStart;
}
export interface MlSetupDependencies {
security: SecurityPluginSetup;
Expand All @@ -41,6 +43,7 @@ export class MlPlugin implements Plugin<Setup, Start> {
coreStart,
{
data: pluginsStart.data,
share: pluginsStart.share,
security: pluginsSetup.security,
licensing: pluginsSetup.licensing,
management: pluginsSetup.management,
Expand Down

0 comments on commit 5acd83c

Please sign in to comment.