Skip to content

Commit

Permalink
[ML] Fix analytics results page.
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed Dec 1, 2020
1 parent 5412b39 commit 1af2323
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function getDefaultDatafeedQuery() {

export function createSearchItems(
kibanaConfig: IUiSettingsClient,
indexPattern: IIndexPattern,
indexPattern: IIndexPattern | undefined,
savedSearch: SavedSearchSavedObject | null
) {
// query is only used by the data visualizer as it needs
Expand Down
65 changes: 37 additions & 28 deletions x-pack/plugins/ml/public/application/routing/use_resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
getIndexPatternById,
getIndexPatternsContract,
getIndexPatternAndSavedSearch,
IndexPatternAndSavedSearch,
} from '../util/index_utils';
import { createSearchItems } from '../jobs/new_job/utils/new_job_utils';
import { ResolverResults, Resolvers } from './resolvers';
Expand Down Expand Up @@ -52,36 +53,44 @@ export const useResolver = (
return;
}

if (indexPatternId !== undefined || savedSearchId !== undefined) {
try {
// note, currently we're using our own kibana context that requires a current index pattern to be set
// this means, if the page uses this context, useResolver must be passed a string for the index pattern id
// and loadIndexPatterns must be part of the resolvers.
const { indexPattern, savedSearch } =
savedSearchId !== undefined
? await getIndexPatternAndSavedSearch(savedSearchId)
: { savedSearch: null, indexPattern: await getIndexPatternById(indexPatternId!) };

const { combinedQuery } = createSearchItems(config, indexPattern!, savedSearch);
try {
// note, currently we're using our own kibana context that requires a current index pattern to be set
// this means, if the page uses this context, useResolver must be passed a string for the index pattern id
// and loadIndexPatterns must be part of the resolvers.
let indexPatternAndSavedSearch: IndexPatternAndSavedSearch = {
savedSearch: null,
indexPattern: null,
};

setContext({
combinedQuery,
currentIndexPattern: indexPattern,
currentSavedSearch: savedSearch,
indexPatterns: getIndexPatternsContract()!,
kibanaConfig: config,
});
} catch (error) {
// an unexpected error has occurred. This could be caused by an incorrect index pattern or saved search ID
notifications.toasts.addError(new Error(error), {
title: i18n.translate('xpack.ml.useResolver.errorTitle', {
defaultMessage: 'An error has occurred',
}),
});
await redirectToJobsManagementPage();
if (savedSearchId !== undefined) {
indexPatternAndSavedSearch = await getIndexPatternAndSavedSearch(savedSearchId);
} else if (indexPatternId !== undefined) {
indexPatternAndSavedSearch.indexPattern = await getIndexPatternById(indexPatternId);
}
} else {
setContext({});

const { savedSearch, indexPattern } = indexPatternAndSavedSearch;

const { combinedQuery } = createSearchItems(
config,
indexPattern !== null ? indexPattern : undefined,
savedSearch
);

setContext({
combinedQuery,
currentIndexPattern: indexPattern,
currentSavedSearch: savedSearch,
indexPatterns: getIndexPatternsContract(),
kibanaConfig: config,
});
} catch (error) {
// an unexpected error has occurred. This could be caused by an incorrect index pattern or saved search ID
notifications.toasts.addError(new Error(error), {
title: i18n.translate('xpack.ml.useResolver.errorTitle', {
defaultMessage: 'An error has occurred',
}),
});
await redirectToJobsManagementPage();
}
})();
}, []);
Expand Down
7 changes: 5 additions & 2 deletions x-pack/plugins/ml/public/application/util/index_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ export function getIndexPatternIdFromName(name: string) {
}
return null;
}

export interface IndexPatternAndSavedSearch {
savedSearch: SavedSearchSavedObject | null;
indexPattern: IIndexPattern | null;
}
export async function getIndexPatternAndSavedSearch(savedSearchId: string) {
const resp: { savedSearch: SavedSearchSavedObject | null; indexPattern: IIndexPattern | null } = {
const resp: IndexPatternAndSavedSearch = {
savedSearch: null,
indexPattern: null,
};
Expand Down

0 comments on commit 1af2323

Please sign in to comment.