Skip to content

Commit

Permalink
[Monitoring] Use async/await (#81200) (#81370)
Browse files Browse the repository at this point in the history
* Use async/await

* Make sure we tell angular what's up
# Conflicts:
#	x-pack/plugins/monitoring/public/services/clusters.js
  • Loading branch information
chrisronline authored Oct 21, 2020
1 parent 3386293 commit 70c73ff
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 45 deletions.
91 changes: 46 additions & 45 deletions x-pack/plugins/monitoring/public/services/clusters.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,72 +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;
}

return $http
.get('../api/monitoring/v1/elasticsearch_settings/check/internal_monitoring')
.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);
const globalState = $injector.get('globalState');
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();
};
}
3 changes: 3 additions & 0 deletions x-pack/plugins/monitoring/public/views/loading/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
);
}
Expand Down

0 comments on commit 70c73ff

Please sign in to comment.