Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Monitoring] Use async/await #81200

Merged
merged 3 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 45 additions & 46 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,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();
};
}
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