2020-07-24 12:09:34 +00:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import statusCodes from '~/lib/utils/http_status';
|
|
|
|
import { backOff } from '~/lib/utils/common_utils';
|
|
|
|
import { PROMETHEUS_TIMEOUT } from '../constants';
|
|
|
|
|
2020-08-11 00:10:18 +00:00
|
|
|
const cancellableBackOffRequest = makeRequestCallback =>
|
2020-07-24 12:09:34 +00:00
|
|
|
backOff((next, stop) => {
|
|
|
|
makeRequestCallback()
|
|
|
|
.then(resp => {
|
|
|
|
if (resp.status === statusCodes.NO_CONTENT) {
|
|
|
|
next();
|
|
|
|
} else {
|
|
|
|
stop(resp);
|
|
|
|
}
|
|
|
|
})
|
2020-08-11 00:10:18 +00:00
|
|
|
// If the request is cancelled by axios
|
|
|
|
// then consider it as noop so that its not
|
|
|
|
// caught by subsequent catches
|
|
|
|
.catch(thrown => (axios.isCancel(thrown) ? undefined : stop(thrown)));
|
2020-07-24 12:09:34 +00:00
|
|
|
}, PROMETHEUS_TIMEOUT);
|
|
|
|
|
|
|
|
export const getDashboard = (dashboardEndpoint, params) =>
|
2020-08-11 00:10:18 +00:00
|
|
|
cancellableBackOffRequest(() => axios.get(dashboardEndpoint, { params })).then(
|
2020-07-24 12:09:34 +00:00
|
|
|
axiosResponse => axiosResponse.data,
|
|
|
|
);
|
|
|
|
|
2020-08-11 00:10:18 +00:00
|
|
|
export const getPrometheusQueryData = (prometheusEndpoint, params, opts) =>
|
|
|
|
cancellableBackOffRequest(() => axios.get(prometheusEndpoint, { params, ...opts }))
|
2020-07-24 12:09:34 +00:00
|
|
|
.then(axiosResponse => axiosResponse.data)
|
|
|
|
.then(prometheusResponse => prometheusResponse.data)
|
|
|
|
.catch(error => {
|
|
|
|
// Prometheus returns errors in specific cases
|
|
|
|
// https://prometheus.io/docs/prometheus/latest/querying/api/#format-overview
|
|
|
|
const { response = {} } = error;
|
|
|
|
if (
|
|
|
|
response.status === statusCodes.BAD_REQUEST ||
|
|
|
|
response.status === statusCodes.UNPROCESSABLE_ENTITY ||
|
|
|
|
response.status === statusCodes.SERVICE_UNAVAILABLE
|
|
|
|
) {
|
|
|
|
const { data } = response;
|
|
|
|
if (data?.status === 'error' && data?.error) {
|
|
|
|
throw new Error(data.error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
});
|