From e46a3e47e5cb9ed443f05d5fe79d48d3816a8c77 Mon Sep 17 00:00:00 2001 From: Chris Roberson Date: Wed, 21 Oct 2020 14:01:02 -0400 Subject: [PATCH] [Monitoring] Use async/await (#81200) * Use async/await * Make sure we tell angular what's up --- .../monitoring/public/services/clusters.js | 91 +++++++++---------- .../monitoring/public/views/loading/index.js | 3 + 2 files changed, 48 insertions(+), 46 deletions(-) diff --git a/x-pack/plugins/monitoring/public/services/clusters.js b/x-pack/plugins/monitoring/public/services/clusters.js index d0ca1bc6bbde6..ef97d78b4f745 100644 --- a/x-pack/plugins/monitoring/public/services/clusters.js +++ b/x-pack/plugins/monitoring/public/services/clusters.js @@ -25,7 +25,7 @@ let once = false; let inTransit = false; export function monitoringClustersProvider($injector) { - return (clusterUuid, ccs, codePaths) => { + return async (clusterUuid, ccs, codePaths) => { const { min, max } = Legacy.shims.timefilter.getBounds(); // append clusterUuid if the parameter is given @@ -36,74 +36,73 @@ export function monitoringClustersProvider($injector) { const $http = $injector.get('$http'); - function getClusters() { - return $http - .post(url, { + async function getClusters() { + try { + const response = await $http.post(url, { ccs, timeRange: { min: min.toISOString(), max: max.toISOString(), }, codePaths, - }) - .then((response) => response.data) - .then((data) => { - return formatClusters(data); // return set of clusters - }) - .catch((err) => { - const Private = $injector.get('Private'); - const ajaxErrorHandlers = Private(ajaxErrorHandlersProvider); - return ajaxErrorHandlers(err); }); + return formatClusters(response.data); + } catch (err) { + const Private = $injector.get('Private'); + const ajaxErrorHandlers = Private(ajaxErrorHandlersProvider); + return ajaxErrorHandlers(err); + } } - function ensureAlertsEnabled() { - return $http.post('../api/monitoring/v1/alerts/enable', {}).catch((err) => { + async function ensureAlertsEnabled() { + try { + return $http.post('../api/monitoring/v1/alerts/enable', {}); + } catch (err) { const Private = $injector.get('Private'); const ajaxErrorHandlers = Private(ajaxErrorHandlersProvider); return ajaxErrorHandlers(err); - }); + } } - function ensureMetricbeatEnabled() { + async function ensureMetricbeatEnabled() { if (Legacy.shims.isCloud) { - return Promise.resolve(); + return; } const globalState = $injector.get('globalState'); - return $http - .post('../api/monitoring/v1/elasticsearch_settings/check/internal_monitoring', { - ccs: globalState.ccs, - }) - .then(({ data }) => { - showInternalMonitoringToast({ - legacyIndices: data.legacy_indices, - metricbeatIndices: data.mb_indices, - }); - }) - .catch((err) => { - const Private = $injector.get('Private'); - const ajaxErrorHandlers = Private(ajaxErrorHandlersProvider); - return ajaxErrorHandlers(err); + try { + const response = await $http.post( + '../api/monitoring/v1/elasticsearch_settings/check/internal_monitoring', + { + ccs: globalState.ccs, + } + ); + const { data } = response; + showInternalMonitoringToast({ + legacyIndices: data.legacy_indices, + metricbeatIndices: data.mb_indices, }); + } catch (err) { + const Private = $injector.get('Private'); + const ajaxErrorHandlers = Private(ajaxErrorHandlersProvider); + return ajaxErrorHandlers(err); + } } if (!once && !inTransit) { inTransit = true; - return getClusters().then((clusters) => { - if (clusters.length) { - Promise.all([ensureAlertsEnabled(), ensureMetricbeatEnabled()]) - .then(([{ data }]) => { - showSecurityToast(data); - once = true; - }) - .catch(() => { - // Intentionally swallow the error as this will retry the next page load - }) - .finally(() => (inTransit = false)); + const clusters = await getClusters(); + if (clusters.length) { + try { + const [{ data }] = await Promise.all([ensureAlertsEnabled(), ensureMetricbeatEnabled()]); + showSecurityToast(data); + once = true; + } catch (_err) { + // Intentionally swallow the error as this will retry the next page load } - return clusters; - }); + inTransit = false; + } + return clusters; } - return getClusters(); + return await getClusters(); }; } diff --git a/x-pack/plugins/monitoring/public/views/loading/index.js b/x-pack/plugins/monitoring/public/views/loading/index.js index 5ca523899067d..b5d79e1d2b652 100644 --- a/x-pack/plugins/monitoring/public/views/loading/index.js +++ b/x-pack/plugins/monitoring/public/views/loading/index.js @@ -53,15 +53,18 @@ uiRoutes.when('/loading', { (clusters) => { if (!clusters || !clusters.length) { window.location.hash = '#/no-data'; + $scope.$apply(); return; } if (clusters.length === 1) { // Bypass the cluster listing if there is just 1 cluster window.history.replaceState(null, null, '#/overview'); + $scope.$apply(); return; } window.history.replaceState(null, null, '#/home'); + $scope.$apply(); } ); }