2020-04-02 21:07:51 +00:00
|
|
|
import { NOT_IN_DB_PREFIX } from '../constants';
|
2020-06-18 00:08:35 +00:00
|
|
|
import {
|
|
|
|
addPrefixToCustomVariableParams,
|
|
|
|
addDashboardMetaDataToLink,
|
|
|
|
normalizeCustomDashboardPath,
|
|
|
|
} from './utils';
|
2020-04-02 21:07:51 +00:00
|
|
|
|
2020-12-23 18:10:19 +00:00
|
|
|
const metricsIdsInPanel = (panel) =>
|
|
|
|
panel.metrics
|
|
|
|
.filter((metric) => metric.metricId && metric.result)
|
|
|
|
.map((metric) => metric.metricId);
|
2019-12-11 09:08:12 +00:00
|
|
|
|
2020-05-12 15:10:33 +00:00
|
|
|
/**
|
|
|
|
* Returns a reference to the currently selected dashboard
|
|
|
|
* from the list of dashboards.
|
|
|
|
*
|
|
|
|
* @param {Object} state
|
|
|
|
*/
|
2020-06-18 00:08:35 +00:00
|
|
|
export const selectedDashboard = (state, getters) => {
|
2020-05-12 15:10:33 +00:00
|
|
|
const { allDashboards } = state;
|
|
|
|
return (
|
2020-12-23 18:10:19 +00:00
|
|
|
allDashboards.find((d) => d.path === getters.fullDashboardPath) ||
|
|
|
|
allDashboards.find((d) => d.default) ||
|
2020-05-15 12:08:28 +00:00
|
|
|
null
|
2020-05-12 15:10:33 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-12-17 15:08:15 +00:00
|
|
|
/**
|
|
|
|
* Get all state for metric in the dashboard or a group. The
|
|
|
|
* states are not repeated so the dashboard or group can show
|
|
|
|
* a global state.
|
|
|
|
*
|
|
|
|
* @param {Object} state
|
|
|
|
* @returns {Function} A function that returns an array of
|
|
|
|
* states in all the metric in the dashboard or group.
|
|
|
|
*/
|
2020-12-23 18:10:19 +00:00
|
|
|
export const getMetricStates = (state) => (groupKey) => {
|
2020-02-27 00:09:19 +00:00
|
|
|
let groups = state.dashboard.panelGroups;
|
2019-12-17 15:08:15 +00:00
|
|
|
if (groupKey) {
|
2020-12-23 18:10:19 +00:00
|
|
|
groups = groups.filter((group) => group.key === groupKey);
|
2019-12-17 15:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const metricStates = groups.reduce((acc, group) => {
|
2020-12-23 18:10:19 +00:00
|
|
|
group.panels.forEach((panel) => {
|
|
|
|
panel.metrics.forEach((metric) => {
|
2019-12-17 15:08:15 +00:00
|
|
|
if (metric.state) {
|
|
|
|
acc.push(metric.state);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
// Deduplicate and sort array
|
|
|
|
return Array.from(new Set(metricStates)).sort();
|
|
|
|
};
|
|
|
|
|
2019-12-11 09:08:12 +00:00
|
|
|
/**
|
|
|
|
* Getter to obtain the list of metric ids that have data
|
|
|
|
*
|
|
|
|
* Useful to understand which parts of the dashboard should
|
|
|
|
* be displayed. It is a Vuex Method-Style Access getter.
|
|
|
|
*
|
|
|
|
* @param {Object} state
|
|
|
|
* @returns {Function} A function that returns an array of
|
|
|
|
* metrics in the dashboard that contain results, optionally
|
|
|
|
* filtered by group key.
|
|
|
|
*/
|
2020-12-23 18:10:19 +00:00
|
|
|
export const metricsWithData = (state) => (groupKey) => {
|
2020-02-27 00:09:19 +00:00
|
|
|
let groups = state.dashboard.panelGroups;
|
2019-12-11 09:08:12 +00:00
|
|
|
if (groupKey) {
|
2020-12-23 18:10:19 +00:00
|
|
|
groups = groups.filter((group) => group.key === groupKey);
|
2019-12-11 09:08:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const res = [];
|
2020-12-23 18:10:19 +00:00
|
|
|
groups.forEach((group) => {
|
|
|
|
group.panels.forEach((panel) => {
|
2019-12-11 09:08:12 +00:00
|
|
|
res.push(...metricsIdsInPanel(panel));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
2020-04-02 21:07:51 +00:00
|
|
|
/**
|
|
|
|
* Metrics loaded from project-defined dashboards do not have a metric_id.
|
|
|
|
* This getter checks which metrics are stored in the db (have a metric id)
|
|
|
|
* This is hopefully a temporary solution until BE processes metrics before passing to FE
|
|
|
|
*
|
|
|
|
* Related:
|
|
|
|
* https://gitlab.com/gitlab-org/gitlab/-/issues/28241
|
|
|
|
* https://gitlab.com/gitlab-org/gitlab/-/merge_requests/27447
|
|
|
|
*/
|
2020-12-23 18:10:19 +00:00
|
|
|
export const metricsSavedToDb = (state) => {
|
2020-04-02 21:07:51 +00:00
|
|
|
const metricIds = [];
|
|
|
|
state.dashboard.panelGroups.forEach(({ panels }) => {
|
|
|
|
panels.forEach(({ metrics }) => {
|
|
|
|
const metricIdsInDb = metrics
|
|
|
|
.filter(({ metricId }) => !metricId.startsWith(NOT_IN_DB_PREFIX))
|
|
|
|
.map(({ metricId }) => metricId);
|
|
|
|
|
|
|
|
metricIds.push(...metricIdsInDb);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return metricIds;
|
|
|
|
};
|
|
|
|
|
2020-01-22 09:08:39 +00:00
|
|
|
/**
|
|
|
|
* Filter environments by names.
|
|
|
|
*
|
|
|
|
* This is used in the environments dropdown with searchable input.
|
|
|
|
*
|
|
|
|
* @param {Object} state
|
|
|
|
* @returns {Array} List of environments
|
|
|
|
*/
|
2020-12-23 18:10:19 +00:00
|
|
|
export const filteredEnvironments = (state) =>
|
|
|
|
state.environments.filter((env) =>
|
2020-01-22 09:08:39 +00:00
|
|
|
env.name.toLowerCase().includes((state.environmentsSearchTerm || '').trim().toLowerCase()),
|
|
|
|
);
|
|
|
|
|
2020-06-09 18:08:28 +00:00
|
|
|
/**
|
|
|
|
* User-defined links from the yml file can have other
|
|
|
|
* dashboard-related metadata baked into it. This method
|
|
|
|
* returns modified links which will get rendered in the
|
|
|
|
* metrics dashboard
|
|
|
|
*
|
|
|
|
* @param {Object} state
|
|
|
|
* @returns {Array} modified array of links
|
|
|
|
*/
|
2020-12-23 18:10:19 +00:00
|
|
|
export const linksWithMetadata = (state) => {
|
2020-06-09 18:08:28 +00:00
|
|
|
const metadata = {
|
|
|
|
timeRange: state.timeRange,
|
|
|
|
};
|
|
|
|
return state.links?.map(addDashboardMetaDataToLink(metadata));
|
|
|
|
};
|
|
|
|
|
2020-05-11 15:09:37 +00:00
|
|
|
/**
|
2020-07-03 09:08:53 +00:00
|
|
|
* Maps a variables array to an object for replacement in
|
|
|
|
* prometheus queries.
|
2020-05-13 15:08:23 +00:00
|
|
|
*
|
2020-05-27 18:08:14 +00:00
|
|
|
* This method outputs an object in the below format
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* variables[key1]=value1,
|
|
|
|
* variables[key2]=value2,
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* This is done so that the backend can identify the custom
|
|
|
|
* user-defined variables coming through the URL and differentiate
|
|
|
|
* from other variables used for Prometheus API endpoint.
|
|
|
|
*
|
2020-07-03 09:08:53 +00:00
|
|
|
* @param {Object} state - State containing variables provided by the user
|
|
|
|
* @returns {Array} The custom variables object to be send to the API
|
2020-05-27 18:08:14 +00:00
|
|
|
* in the format of {variables[key1]=value1, variables[key2]=value2}
|
2020-05-11 15:09:37 +00:00
|
|
|
*/
|
|
|
|
|
2020-12-23 18:10:19 +00:00
|
|
|
export const getCustomVariablesParams = (state) =>
|
2020-07-03 09:08:53 +00:00
|
|
|
state.variables.reduce((acc, variable) => {
|
|
|
|
const { name, value } = variable;
|
|
|
|
if (value !== null) {
|
|
|
|
acc[addPrefixToCustomVariableParams(name)] = value;
|
|
|
|
}
|
2020-05-27 18:08:14 +00:00
|
|
|
return acc;
|
|
|
|
}, {});
|
2020-05-11 15:09:37 +00:00
|
|
|
|
2020-06-18 00:08:35 +00:00
|
|
|
/**
|
|
|
|
* For a given custom dashboard file name, this method
|
|
|
|
* returns the full file path.
|
|
|
|
*
|
|
|
|
* @param {Object} state
|
|
|
|
* @returns {String} full dashboard path
|
|
|
|
*/
|
2020-12-23 18:10:19 +00:00
|
|
|
export const fullDashboardPath = (state) =>
|
2020-06-18 00:08:35 +00:00
|
|
|
normalizeCustomDashboardPath(state.currentDashboard, state.customDashboardBasePath);
|