-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathuse_resolver.ts
114 lines (102 loc) · 4.01 KB
/
use_resolver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { useEffect, useState } from 'react';
import { IUiSettingsClient } from '@kbn/core/public';
import { i18n } from '@kbn/i18n';
import type { DataViewsContract } from '@kbn/data-views-plugin/public';
import {
getDataViewById,
getDataViewAndSavedSearch,
DataViewAndSavedSearch,
} from '../util/index_utils';
import { createSearchItems } from '../jobs/new_job/utils/new_job_utils';
import { ResolverResults, Resolvers } from './resolvers';
import { MlContextValue } from '../contexts/ml';
import { useNotifications } from '../contexts/kibana';
import { useCreateAndNavigateToMlLink } from '../contexts/kibana/use_create_url';
import { ML_PAGES } from '../../../common/constants/locator';
/**
* Hook to resolve route specific requirements
* @param dataViewId optional Kibana data view id, used for wizards
* @param savedSearchId optional Kibana saved search id, used for wizards
* @param config Kibana UI Settings
* @param resolvers an array of resolvers to be executed for the route
* @return { context, results } returns the ML context and resolver results
*/
export const useResolver = (
dataViewId: string | undefined,
savedSearchId: string | undefined,
config: IUiSettingsClient,
dataViewsContract: DataViewsContract,
resolvers: Resolvers
): { context: MlContextValue; results: ResolverResults } => {
const notifications = useNotifications();
const funcNames = Object.keys(resolvers); // Object.entries gets this wrong?!
const funcs = Object.values(resolvers); // Object.entries gets this wrong?!
const tempResults = funcNames.reduce((p, c) => {
p[c] = {};
return p;
}, {} as ResolverResults);
const [context, setContext] = useState<any | null>(null);
const [results, setResults] = useState(tempResults);
const redirectToJobsManagementPage = useCreateAndNavigateToMlLink(
ML_PAGES.ANOMALY_DETECTION_JOBS_MANAGE
);
useEffect(() => {
(async () => {
try {
const res = await Promise.all(funcs.map((r) => r()));
res.forEach((r, i) => (tempResults[funcNames[i]] = r));
setResults(tempResults);
} catch (error) {
// quietly fail. Errors being thrown here are expected as a way to handle privilege or license check failures.
// The user will be redirected by the failed resolver.
return;
}
try {
if (dataViewId === '') {
throw new Error(
i18n.translate('xpack.ml.useResolver.errorIndexPatternIdEmptyString', {
defaultMessage: 'dataViewId must not be empty string.',
})
);
}
let dataViewAndSavedSearch: DataViewAndSavedSearch = {
savedSearch: null,
dataView: null,
};
if (savedSearchId !== undefined) {
dataViewAndSavedSearch = await getDataViewAndSavedSearch(savedSearchId);
} else if (dataViewId !== undefined) {
dataViewAndSavedSearch.dataView = await getDataViewById(dataViewId);
}
const { savedSearch, dataView } = dataViewAndSavedSearch;
const { combinedQuery } = createSearchItems(
config,
dataView !== null ? dataView : undefined,
savedSearch
);
setContext({
combinedQuery,
currentDataView: dataView,
currentSavedSearch: savedSearch,
dataViewsContract,
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();
}
})();
}, []);
return { context, results };
};