Skip to content

Commit e46a3e4

Browse files
committed
[Monitoring] Use async/await (#81200)
* Use async/await * Make sure we tell angular what's up
1 parent 6b6084b commit e46a3e4

File tree

2 files changed

+48
-46
lines changed

2 files changed

+48
-46
lines changed

x-pack/plugins/monitoring/public/services/clusters.js

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ let once = false;
2525
let inTransit = false;
2626

2727
export function monitoringClustersProvider($injector) {
28-
return (clusterUuid, ccs, codePaths) => {
28+
return async (clusterUuid, ccs, codePaths) => {
2929
const { min, max } = Legacy.shims.timefilter.getBounds();
3030

3131
// append clusterUuid if the parameter is given
@@ -36,74 +36,73 @@ export function monitoringClustersProvider($injector) {
3636

3737
const $http = $injector.get('$http');
3838

39-
function getClusters() {
40-
return $http
41-
.post(url, {
39+
async function getClusters() {
40+
try {
41+
const response = await $http.post(url, {
4242
ccs,
4343
timeRange: {
4444
min: min.toISOString(),
4545
max: max.toISOString(),
4646
},
4747
codePaths,
48-
})
49-
.then((response) => response.data)
50-
.then((data) => {
51-
return formatClusters(data); // return set of clusters
52-
})
53-
.catch((err) => {
54-
const Private = $injector.get('Private');
55-
const ajaxErrorHandlers = Private(ajaxErrorHandlersProvider);
56-
return ajaxErrorHandlers(err);
5748
});
49+
return formatClusters(response.data);
50+
} catch (err) {
51+
const Private = $injector.get('Private');
52+
const ajaxErrorHandlers = Private(ajaxErrorHandlersProvider);
53+
return ajaxErrorHandlers(err);
54+
}
5855
}
5956

60-
function ensureAlertsEnabled() {
61-
return $http.post('../api/monitoring/v1/alerts/enable', {}).catch((err) => {
57+
async function ensureAlertsEnabled() {
58+
try {
59+
return $http.post('../api/monitoring/v1/alerts/enable', {});
60+
} catch (err) {
6261
const Private = $injector.get('Private');
6362
const ajaxErrorHandlers = Private(ajaxErrorHandlersProvider);
6463
return ajaxErrorHandlers(err);
65-
});
64+
}
6665
}
6766

68-
function ensureMetricbeatEnabled() {
67+
async function ensureMetricbeatEnabled() {
6968
if (Legacy.shims.isCloud) {
70-
return Promise.resolve();
69+
return;
7170
}
7271
const globalState = $injector.get('globalState');
73-
return $http
74-
.post('../api/monitoring/v1/elasticsearch_settings/check/internal_monitoring', {
75-
ccs: globalState.ccs,
76-
})
77-
.then(({ data }) => {
78-
showInternalMonitoringToast({
79-
legacyIndices: data.legacy_indices,
80-
metricbeatIndices: data.mb_indices,
81-
});
82-
})
83-
.catch((err) => {
84-
const Private = $injector.get('Private');
85-
const ajaxErrorHandlers = Private(ajaxErrorHandlersProvider);
86-
return ajaxErrorHandlers(err);
72+
try {
73+
const response = await $http.post(
74+
'../api/monitoring/v1/elasticsearch_settings/check/internal_monitoring',
75+
{
76+
ccs: globalState.ccs,
77+
}
78+
);
79+
const { data } = response;
80+
showInternalMonitoringToast({
81+
legacyIndices: data.legacy_indices,
82+
metricbeatIndices: data.mb_indices,
8783
});
84+
} catch (err) {
85+
const Private = $injector.get('Private');
86+
const ajaxErrorHandlers = Private(ajaxErrorHandlersProvider);
87+
return ajaxErrorHandlers(err);
88+
}
8889
}
8990

9091
if (!once && !inTransit) {
9192
inTransit = true;
92-
return getClusters().then((clusters) => {
93-
if (clusters.length) {
94-
Promise.all([ensureAlertsEnabled(), ensureMetricbeatEnabled()])
95-
.then(([{ data }]) => {
96-
showSecurityToast(data);
97-
once = true;
98-
})
99-
.catch(() => {
100-
// Intentionally swallow the error as this will retry the next page load
101-
})
102-
.finally(() => (inTransit = false));
93+
const clusters = await getClusters();
94+
if (clusters.length) {
95+
try {
96+
const [{ data }] = await Promise.all([ensureAlertsEnabled(), ensureMetricbeatEnabled()]);
97+
showSecurityToast(data);
98+
once = true;
99+
} catch (_err) {
100+
// Intentionally swallow the error as this will retry the next page load
103101
}
104-
return clusters;
105-
});
102+
inTransit = false;
103+
}
104+
return clusters;
106105
}
107-
return getClusters();
106+
return await getClusters();
108107
};
109108
}

x-pack/plugins/monitoring/public/views/loading/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,18 @@ uiRoutes.when('/loading', {
5353
(clusters) => {
5454
if (!clusters || !clusters.length) {
5555
window.location.hash = '#/no-data';
56+
$scope.$apply();
5657
return;
5758
}
5859
if (clusters.length === 1) {
5960
// Bypass the cluster listing if there is just 1 cluster
6061
window.history.replaceState(null, null, '#/overview');
62+
$scope.$apply();
6163
return;
6264
}
6365

6466
window.history.replaceState(null, null, '#/home');
67+
$scope.$apply();
6568
}
6669
);
6770
}

0 commit comments

Comments
 (0)