debian-mirror-gitlab/app/assets/javascripts/monitoring/stores/mutations.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

274 lines
8.6 KiB
JavaScript
Raw Normal View History

2020-05-24 23:13:21 +05:30
import { pick } from 'lodash';
2021-03-11 19:13:27 +05:30
import Vue from 'vue';
2020-10-24 23:57:45 +05:30
import { BACKOFF_TIMEOUT } from '~/lib/utils/common_utils';
2021-03-11 19:13:27 +05:30
import httpStatusCodes from '~/lib/utils/http_status';
2020-07-28 23:09:34 +05:30
import { dashboardEmptyStates, endpointKeys, initialStateKeys, metricStates } from '../constants';
2021-03-11 19:13:27 +05:30
import * as types from './mutation_types';
import { mapToDashboardViewModel, mapPanelToViewModel, normalizeQueryResponseData } from './utils';
2020-07-28 23:09:34 +05:30
import { optionsFromSeriesData } from './variable_mapping';
2019-12-21 20:55:43 +05:30
2020-01-01 13:55:28 +05:30
/**
* Locate and return a metric in the dashboard by its id
* as generated by `uniqMetricsId()`.
* @param {String} metricId Unique id in the dashboard
* @param {Object} dashboard Full dashboard object
*/
const findMetricInDashboard = (metricId, dashboard) => {
let res = null;
2021-03-08 18:12:59 +05:30
dashboard.panelGroups.forEach((group) => {
group.panels.forEach((panel) => {
panel.metrics.forEach((metric) => {
2020-04-08 14:13:33 +05:30
if (metric.metricId === metricId) {
2020-01-01 13:55:28 +05:30
res = metric;
}
});
});
});
return res;
};
/**
* Maps a backened error state to a `metricStates` constant
* @param {Object} error - Error from backend response
*/
2021-03-08 18:12:59 +05:30
const emptyStateFromError = (error) => {
2020-01-01 13:55:28 +05:30
if (!error) {
return metricStates.UNKNOWN_ERROR;
}
// Special error responses
if (error.message === BACKOFF_TIMEOUT) {
return metricStates.TIMEOUT;
}
// Axios error responses
const { response } = error;
if (response && response.status === httpStatusCodes.SERVICE_UNAVAILABLE) {
return metricStates.CONNECTION_FAILED;
} else if (response && response.status === httpStatusCodes.BAD_REQUEST) {
// Note: "error.response.data.error" may contain Prometheus error information
return metricStates.BAD_QUERY;
}
return metricStates.UNKNOWN_ERROR;
};
2019-09-04 21:01:54 +05:30
2021-03-08 18:12:59 +05:30
export const metricStateFromData = (data) => {
2020-10-24 23:57:45 +05:30
if (data?.result?.length) {
const result = normalizeQueryResponseData(data);
return { state: metricStates.OK, result: Object.freeze(result) };
}
return { state: metricStates.NO_DATA, result: null };
};
2019-09-04 21:01:54 +05:30
export default {
2020-01-01 13:55:28 +05:30
/**
* Dashboard panels structure and global state
*/
2020-04-22 19:07:51 +05:30
[types.REQUEST_METRICS_DASHBOARD](state) {
2020-07-28 23:09:34 +05:30
state.emptyState = dashboardEmptyStates.LOADING;
2019-09-04 21:01:54 +05:30
},
2020-06-23 00:09:42 +05:30
[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, dashboardYML) {
const { dashboard, panelGroups, variables, links } = mapToDashboardViewModel(dashboardYML);
state.dashboard = {
dashboard,
panelGroups,
};
state.variables = variables;
state.links = links;
2019-09-04 21:01:54 +05:30
2020-04-08 14:13:33 +05:30
if (!state.dashboard.panelGroups.length) {
2020-07-28 23:09:34 +05:30
state.emptyState = dashboardEmptyStates.NO_DATA;
} else {
state.emptyState = null;
2019-09-04 21:01:54 +05:30
}
},
2020-04-22 19:07:51 +05:30
[types.RECEIVE_METRICS_DASHBOARD_FAILURE](state, error) {
2020-07-28 23:09:34 +05:30
state.emptyState = error
? dashboardEmptyStates.UNABLE_TO_CONNECT
: dashboardEmptyStates.NO_DATA;
2019-09-04 21:01:54 +05:30
},
2020-01-01 13:55:28 +05:30
2020-05-24 23:13:21 +05:30
[types.REQUEST_DASHBOARD_STARRING](state) {
state.isUpdatingStarredValue = true;
},
2020-06-23 00:09:42 +05:30
[types.RECEIVE_DASHBOARD_STARRING_SUCCESS](state, { selectedDashboard, newStarredValue }) {
2021-03-08 18:12:59 +05:30
const index = state.allDashboards.findIndex((d) => d === selectedDashboard);
2020-05-24 23:13:21 +05:30
state.isUpdatingStarredValue = false;
// Trigger state updates in the reactivity system for this change
// https://vuejs.org/v2/guide/reactivity.html#For-Arrays
2020-06-23 00:09:42 +05:30
Vue.set(state.allDashboards, index, { ...selectedDashboard, starred: newStarredValue });
2020-05-24 23:13:21 +05:30
},
[types.RECEIVE_DASHBOARD_STARRING_FAILURE](state) {
state.isUpdatingStarredValue = false;
},
2020-07-28 23:09:34 +05:30
[types.SET_CURRENT_DASHBOARD](state, currentDashboard) {
state.currentDashboard = currentDashboard;
},
2020-01-01 13:55:28 +05:30
/**
* Deployments and environments
*/
2019-09-04 21:01:54 +05:30
[types.RECEIVE_DEPLOYMENTS_DATA_SUCCESS](state, deployments) {
state.deploymentData = deployments;
},
[types.RECEIVE_DEPLOYMENTS_DATA_FAILURE](state) {
state.deploymentData = [];
},
2020-03-13 15:44:24 +05:30
[types.REQUEST_ENVIRONMENTS_DATA](state) {
state.environmentsLoading = true;
},
2019-09-04 21:01:54 +05:30
[types.RECEIVE_ENVIRONMENTS_DATA_SUCCESS](state, environments) {
2020-03-13 15:44:24 +05:30
state.environmentsLoading = false;
2019-09-04 21:01:54 +05:30
state.environments = environments;
},
[types.RECEIVE_ENVIRONMENTS_DATA_FAILURE](state) {
2020-03-13 15:44:24 +05:30
state.environmentsLoading = false;
2019-09-04 21:01:54 +05:30
state.environments = [];
},
2020-01-01 13:55:28 +05:30
2020-04-22 19:07:51 +05:30
/**
* Annotations
*/
[types.RECEIVE_ANNOTATIONS_SUCCESS](state, annotations) {
state.annotations = annotations;
},
[types.RECEIVE_ANNOTATIONS_FAILURE](state) {
state.annotations = [];
},
2020-07-28 23:09:34 +05:30
/**
* Dashboard Validation Warnings
*/
[types.RECEIVE_DASHBOARD_VALIDATION_WARNINGS_SUCCESS](state, hasDashboardValidationWarnings) {
state.hasDashboardValidationWarnings = hasDashboardValidationWarnings;
},
[types.RECEIVE_DASHBOARD_VALIDATION_WARNINGS_FAILURE](state) {
state.hasDashboardValidationWarnings = false;
},
2020-01-01 13:55:28 +05:30
/**
* Individual panel/metric results
*/
[types.REQUEST_METRIC_RESULT](state, { metricId }) {
const metric = findMetricInDashboard(metricId, state.dashboard);
2020-04-22 19:07:51 +05:30
metric.loading = true;
if (!metric.result) {
metric.state = metricStates.LOADING;
}
2020-01-01 13:55:28 +05:30
},
2020-07-28 23:09:34 +05:30
[types.RECEIVE_METRIC_RESULT_SUCCESS](state, { metricId, data }) {
2020-04-22 19:07:51 +05:30
const metric = findMetricInDashboard(metricId, state.dashboard);
2020-10-24 23:57:45 +05:30
const metricState = metricStateFromData(data);
2019-09-04 21:01:54 +05:30
2020-10-24 23:57:45 +05:30
metric.loading = false;
metric.state = metricState.state;
metric.result = metricState.result;
2020-01-01 13:55:28 +05:30
},
[types.RECEIVE_METRIC_RESULT_FAILURE](state, { metricId, error }) {
const metric = findMetricInDashboard(metricId, state.dashboard);
2020-04-22 19:07:51 +05:30
metric.state = emptyStateFromError(error);
metric.loading = false;
metric.result = null;
},
2020-05-24 23:13:21 +05:30
// Parameters and other information
2020-04-22 19:07:51 +05:30
[types.SET_INITIAL_STATE](state, initialState = {}) {
Object.assign(state, pick(initialState, initialStateKeys));
2019-09-04 21:01:54 +05:30
},
2020-03-13 15:44:24 +05:30
[types.SET_ENDPOINTS](state, endpoints = {}) {
2020-04-22 19:07:51 +05:30
Object.assign(state, pick(endpoints, endpointKeys));
2020-03-13 15:44:24 +05:30
},
[types.SET_TIME_RANGE](state, timeRange) {
state.timeRange = timeRange;
2019-09-04 21:01:54 +05:30
},
[types.SET_GETTING_STARTED_EMPTY_STATE](state) {
2020-07-28 23:09:34 +05:30
state.emptyState = dashboardEmptyStates.GETTING_STARTED;
2019-09-04 21:01:54 +05:30
},
2019-09-30 21:07:59 +05:30
[types.SET_ALL_DASHBOARDS](state, dashboards) {
2019-12-26 22:10:19 +05:30
state.allDashboards = dashboards || [];
2019-09-30 21:07:59 +05:30
},
2019-10-12 21:52:04 +05:30
[types.SET_SHOW_ERROR_BANNER](state, enabled) {
state.showErrorBanner = enabled;
},
2019-12-26 22:10:19 +05:30
[types.SET_PANEL_GROUP_METRICS](state, payload) {
2021-03-08 18:12:59 +05:30
const panelGroup = state.dashboard.panelGroups.find((pg) => payload.key === pg.key);
2020-01-01 13:55:28 +05:30
panelGroup.panels = payload.panels;
2019-12-26 22:10:19 +05:30
},
2020-03-13 15:44:24 +05:30
[types.SET_ENVIRONMENTS_FILTER](state, searchTerm) {
state.environmentsSearchTerm = searchTerm;
},
2020-05-24 23:13:21 +05:30
[types.SET_EXPANDED_PANEL](state, { group, panel }) {
state.expandedPanel.group = group;
state.expandedPanel.panel = panel;
},
2020-07-28 23:09:34 +05:30
[types.UPDATE_VARIABLE_VALUE](state, { name, value }) {
2021-03-08 18:12:59 +05:30
const variable = state.variables.find((v) => v.name === name);
2020-07-28 23:09:34 +05:30
if (variable) {
Object.assign(variable, {
value,
});
}
2020-05-24 23:13:21 +05:30
},
2020-07-28 23:09:34 +05:30
[types.UPDATE_VARIABLE_METRIC_LABEL_VALUES](state, { variable, label, data = [] }) {
const values = optionsFromSeriesData({ label, data });
// Add new options with assign to ensure Vue reactivity
Object.assign(variable.options, { values });
2020-05-24 23:13:21 +05:30
},
2020-10-24 23:57:45 +05:30
[types.REQUEST_PANEL_PREVIEW](state, panelPreviewYml) {
state.panelPreviewIsLoading = true;
state.panelPreviewYml = panelPreviewYml;
state.panelPreviewGraphData = null;
state.panelPreviewError = null;
},
[types.RECEIVE_PANEL_PREVIEW_SUCCESS](state, payload) {
state.panelPreviewIsLoading = false;
state.panelPreviewGraphData = mapPanelToViewModel(payload);
state.panelPreviewError = null;
},
[types.RECEIVE_PANEL_PREVIEW_FAILURE](state, error) {
state.panelPreviewIsLoading = false;
state.panelPreviewGraphData = null;
state.panelPreviewError = error;
},
[types.REQUEST_PANEL_PREVIEW_METRIC_RESULT](state, { index }) {
const metric = state.panelPreviewGraphData.metrics[index];
metric.loading = true;
if (!metric.result) {
metric.state = metricStates.LOADING;
}
},
[types.RECEIVE_PANEL_PREVIEW_METRIC_RESULT_SUCCESS](state, { index, data }) {
const metric = state.panelPreviewGraphData.metrics[index];
const metricState = metricStateFromData(data);
metric.loading = false;
metric.state = metricState.state;
metric.result = metricState.result;
},
[types.RECEIVE_PANEL_PREVIEW_METRIC_RESULT_FAILURE](state, { index, error }) {
const metric = state.panelPreviewGraphData.metrics[index];
metric.loading = false;
metric.state = emptyStateFromError(error);
metric.result = null;
},
[types.SET_PANEL_PREVIEW_TIME_RANGE](state, timeRange) {
state.panelPreviewTimeRange = timeRange;
},
[types.SET_PANEL_PREVIEW_IS_SHOWN](state, isPreviewShown) {
state.panelPreviewIsShown = isPreviewShown;
},
2019-09-04 21:01:54 +05:30
};