2020-10-24 23:57:45 +05:30
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import { backOff } from '~/lib/utils/common_utils';
|
2021-03-11 19:13:27 +05:30
|
|
|
import statusCodes from '~/lib/utils/http_status';
|
2020-10-24 23:57:45 +05:30
|
|
|
import { PROMETHEUS_TIMEOUT } from '../constants';
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
const cancellableBackOffRequest = (makeRequestCallback) =>
|
2020-10-24 23:57:45 +05:30
|
|
|
backOff((next, stop) => {
|
|
|
|
makeRequestCallback()
|
2021-03-08 18:12:59 +05:30
|
|
|
.then((resp) => {
|
2020-10-24 23:57:45 +05:30
|
|
|
if (resp.status === statusCodes.NO_CONTENT) {
|
|
|
|
next();
|
|
|
|
} else {
|
|
|
|
stop(resp);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// If the request is cancelled by axios
|
|
|
|
// then consider it as noop so that its not
|
|
|
|
// caught by subsequent catches
|
2021-03-08 18:12:59 +05:30
|
|
|
.catch((thrown) => (axios.isCancel(thrown) ? undefined : stop(thrown)));
|
2020-10-24 23:57:45 +05:30
|
|
|
}, PROMETHEUS_TIMEOUT);
|
|
|
|
|
|
|
|
export const getDashboard = (dashboardEndpoint, params) =>
|
|
|
|
cancellableBackOffRequest(() => axios.get(dashboardEndpoint, { params })).then(
|
2021-03-08 18:12:59 +05:30
|
|
|
(axiosResponse) => axiosResponse.data,
|
2020-10-24 23:57:45 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
export const getPrometheusQueryData = (prometheusEndpoint, params, opts) =>
|
|
|
|
cancellableBackOffRequest(() => axios.get(prometheusEndpoint, { params, ...opts }))
|
2021-03-08 18:12:59 +05:30
|
|
|
.then((axiosResponse) => axiosResponse.data)
|
|
|
|
.then((prometheusResponse) => prometheusResponse.data)
|
|
|
|
.catch((error) => {
|
2020-10-24 23:57:45 +05:30
|
|
|
// 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;
|
|
|
|
});
|