2019-09-04 21:01:54 +05:30
|
|
|
import * as types from './mutation_types';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import createFlash from '~/flash';
|
2020-01-01 13:55:28 +05:30
|
|
|
import trackDashboardLoad from '../monitoring_tracking_helper';
|
2019-09-04 21:01:54 +05:30
|
|
|
import statusCodes from '../../lib/utils/http_status';
|
|
|
|
import { backOff } from '../../lib/utils/common_utils';
|
2020-01-01 13:55:28 +05:30
|
|
|
import { s__, sprintf } from '../../locale';
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
import { PROMETHEUS_TIMEOUT } from '../constants';
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
function backOffRequest(makeRequestCallback) {
|
2019-09-04 21:01:54 +05:30
|
|
|
return backOff((next, stop) => {
|
|
|
|
makeRequestCallback()
|
|
|
|
.then(resp => {
|
|
|
|
if (resp.status === statusCodes.NO_CONTENT) {
|
2020-01-01 13:55:28 +05:30
|
|
|
next();
|
2019-09-04 21:01:54 +05:30
|
|
|
} else {
|
|
|
|
stop(resp);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(stop);
|
2020-01-01 13:55:28 +05:30
|
|
|
}, PROMETHEUS_TIMEOUT);
|
2019-09-04 21:01:54 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export const setGettingStartedEmptyState = ({ commit }) => {
|
|
|
|
commit(types.SET_GETTING_STARTED_EMPTY_STATE);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const setEndpoints = ({ commit }, endpoints) => {
|
|
|
|
commit(types.SET_ENDPOINTS, endpoints);
|
|
|
|
};
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
export const setShowErrorBanner = ({ commit }, enabled) => {
|
|
|
|
commit(types.SET_SHOW_ERROR_BANNER, enabled);
|
|
|
|
};
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
export const requestMetricsDashboard = ({ commit }) => {
|
|
|
|
commit(types.REQUEST_METRICS_DATA);
|
|
|
|
};
|
2019-12-21 20:55:43 +05:30
|
|
|
export const receiveMetricsDashboardSuccess = ({ commit, dispatch }, { response, params }) => {
|
|
|
|
commit(types.SET_ALL_DASHBOARDS, response.all_dashboards);
|
2019-09-04 21:01:54 +05:30
|
|
|
commit(types.RECEIVE_METRICS_DATA_SUCCESS, response.dashboard.panel_groups);
|
2020-01-01 13:55:28 +05:30
|
|
|
return dispatch('fetchPrometheusMetrics', params);
|
2019-09-04 21:01:54 +05:30
|
|
|
};
|
|
|
|
export const receiveMetricsDashboardFailure = ({ commit }, error) => {
|
|
|
|
commit(types.RECEIVE_METRICS_DATA_FAILURE, error);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const receiveDeploymentsDataSuccess = ({ commit }, data) =>
|
|
|
|
commit(types.RECEIVE_DEPLOYMENTS_DATA_SUCCESS, data);
|
|
|
|
export const receiveDeploymentsDataFailure = ({ commit }) =>
|
|
|
|
commit(types.RECEIVE_DEPLOYMENTS_DATA_FAILURE);
|
|
|
|
export const receiveEnvironmentsDataSuccess = ({ commit }, data) =>
|
|
|
|
commit(types.RECEIVE_ENVIRONMENTS_DATA_SUCCESS, data);
|
|
|
|
export const receiveEnvironmentsDataFailure = ({ commit }) =>
|
|
|
|
commit(types.RECEIVE_ENVIRONMENTS_DATA_FAILURE);
|
|
|
|
|
|
|
|
export const fetchData = ({ dispatch }, params) => {
|
|
|
|
dispatch('fetchMetricsData', params);
|
|
|
|
dispatch('fetchDeploymentsData');
|
|
|
|
dispatch('fetchEnvironmentsData');
|
|
|
|
};
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
export const fetchMetricsData = ({ dispatch }, params) => dispatch('fetchDashboard', params);
|
2019-09-04 21:01:54 +05:30
|
|
|
|
|
|
|
export const fetchDashboard = ({ state, dispatch }, params) => {
|
|
|
|
dispatch('requestMetricsDashboard');
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
if (state.currentDashboard) {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
params.dashboard = state.currentDashboard;
|
|
|
|
}
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
return backOffRequest(() => axios.get(state.dashboardEndpoint, { params }))
|
2019-09-04 21:01:54 +05:30
|
|
|
.then(resp => resp.data)
|
2020-01-01 13:55:28 +05:30
|
|
|
.then(response => dispatch('receiveMetricsDashboardSuccess', { response, params }))
|
|
|
|
.catch(e => {
|
|
|
|
dispatch('receiveMetricsDashboardFailure', e);
|
|
|
|
if (state.showErrorBanner) {
|
|
|
|
if (e.response.data && e.response.data.message) {
|
|
|
|
const { message } = e.response.data;
|
|
|
|
createFlash(
|
|
|
|
sprintf(
|
|
|
|
s__('Metrics|There was an error while retrieving metrics. %{message}'),
|
|
|
|
{ message },
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
createFlash(s__('Metrics|There was an error while retrieving metrics'));
|
|
|
|
}
|
2019-10-12 21:52:04 +05:30
|
|
|
}
|
2019-09-04 21:01:54 +05:30
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
function fetchPrometheusResult(prometheusEndpoint, params) {
|
|
|
|
return backOffRequest(() => axios.get(prometheusEndpoint, { params }))
|
|
|
|
.then(res => res.data)
|
|
|
|
.then(response => {
|
|
|
|
if (response.status === 'error') {
|
|
|
|
throw new Error(response.error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.data.result;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns list of metrics in data.result
|
|
|
|
* {"status":"success", "data":{"resultType":"matrix","result":[]}}
|
|
|
|
*
|
|
|
|
* @param {metric} metric
|
|
|
|
*/
|
|
|
|
export const fetchPrometheusMetric = ({ commit }, { metric, params }) => {
|
|
|
|
const { start, end } = params;
|
2019-10-12 21:52:04 +05:30
|
|
|
const timeDiff = (new Date(end) - new Date(start)) / 1000;
|
2019-09-04 21:01:54 +05:30
|
|
|
|
|
|
|
const minStep = 60;
|
|
|
|
const queryDataPoints = 600;
|
|
|
|
const step = Math.max(minStep, Math.ceil(timeDiff / queryDataPoints));
|
|
|
|
|
|
|
|
const queryParams = {
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
step,
|
|
|
|
};
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
commit(types.REQUEST_METRIC_RESULT, { metricId: metric.metric_id });
|
|
|
|
|
|
|
|
return fetchPrometheusResult(metric.prometheus_endpoint_path, queryParams)
|
|
|
|
.then(result => {
|
|
|
|
commit(types.RECEIVE_METRIC_RESULT_SUCCESS, { metricId: metric.metric_id, result });
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
commit(types.RECEIVE_METRIC_RESULT_FAILURE, { metricId: metric.metric_id, error });
|
|
|
|
// Continue to throw error so the dashboard can notify using createFlash
|
|
|
|
throw error;
|
|
|
|
});
|
2019-09-04 21:01:54 +05:30
|
|
|
};
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
export const fetchPrometheusMetrics = ({ state, commit, dispatch, getters }, params) => {
|
2019-09-04 21:01:54 +05:30
|
|
|
commit(types.REQUEST_METRICS_DATA);
|
|
|
|
|
|
|
|
const promises = [];
|
2019-12-26 22:10:19 +05:30
|
|
|
state.dashboard.panel_groups.forEach(group => {
|
2019-09-04 21:01:54 +05:30
|
|
|
group.panels.forEach(panel => {
|
|
|
|
panel.metrics.forEach(metric => {
|
|
|
|
promises.push(dispatch('fetchPrometheusMetric', { metric, params }));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
return Promise.all(promises)
|
|
|
|
.then(() => {
|
|
|
|
const dashboardType = state.currentDashboard === '' ? 'default' : 'custom';
|
|
|
|
trackDashboardLoad({
|
|
|
|
label: `${dashboardType}_metrics_dashboard`,
|
|
|
|
value: getters.metricsWithData().length,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
createFlash(s__(`Metrics|There was an error while retrieving metrics`), 'warning');
|
|
|
|
});
|
2019-09-04 21:01:54 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
export const fetchDeploymentsData = ({ state, dispatch }) => {
|
|
|
|
if (!state.deploymentsEndpoint) {
|
|
|
|
return Promise.resolve([]);
|
|
|
|
}
|
2020-01-01 13:55:28 +05:30
|
|
|
return axios
|
|
|
|
.get(state.deploymentsEndpoint)
|
2019-09-04 21:01:54 +05:30
|
|
|
.then(resp => resp.data)
|
|
|
|
.then(response => {
|
|
|
|
if (!response || !response.deployments) {
|
|
|
|
createFlash(s__('Metrics|Unexpected deployment data response from prometheus endpoint'));
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch('receiveDeploymentsDataSuccess', response.deployments);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
dispatch('receiveDeploymentsDataFailure');
|
|
|
|
createFlash(s__('Metrics|There was an error getting deployment information.'));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const fetchEnvironmentsData = ({ state, dispatch }) => {
|
|
|
|
if (!state.environmentsEndpoint) {
|
|
|
|
return Promise.resolve([]);
|
|
|
|
}
|
|
|
|
return axios
|
|
|
|
.get(state.environmentsEndpoint)
|
|
|
|
.then(resp => resp.data)
|
|
|
|
.then(response => {
|
|
|
|
if (!response || !response.environments) {
|
|
|
|
createFlash(
|
|
|
|
s__('Metrics|There was an error fetching the environments data, please try again'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
dispatch('receiveEnvironmentsDataSuccess', response.environments);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
dispatch('receiveEnvironmentsDataFailure');
|
|
|
|
createFlash(s__('Metrics|There was an error getting environments information.'));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
/**
|
|
|
|
* Set a new array of metrics to a panel group
|
|
|
|
* @param {*} data An object containing
|
|
|
|
* - `key` with a unique panel key
|
|
|
|
* - `metrics` with the metrics array
|
|
|
|
*/
|
|
|
|
export const setPanelGroupMetrics = ({ commit }, data) => {
|
|
|
|
commit(types.SET_PANEL_GROUP_METRICS, data);
|
|
|
|
};
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
// prevent babel-plugin-rewire from generating an invalid default during karma tests
|
|
|
|
export default () => {};
|