2020-05-24 23:13:21 +05:30
|
|
|
import { pickBy, mapKeys } from 'lodash';
|
2020-07-28 23:09:34 +05:30
|
|
|
import { parseBoolean } from '~/lib/utils/common_utils';
|
2020-03-13 15:44:24 +05:30
|
|
|
import {
|
|
|
|
timeRangeParamNames,
|
|
|
|
timeRangeFromParams,
|
|
|
|
timeRangeToParams,
|
|
|
|
} from '~/lib/utils/datetime_range';
|
2021-03-11 19:13:27 +05:30
|
|
|
import {
|
|
|
|
queryToObject,
|
|
|
|
mergeUrlParams,
|
|
|
|
removeParams,
|
|
|
|
updateHistory,
|
|
|
|
} from '~/lib/utils/url_utility';
|
2020-05-24 23:13:21 +05:30
|
|
|
import { VARIABLE_PREFIX } from './constants';
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
/**
|
|
|
|
* Extracts the initial state and props from HTML dataset
|
|
|
|
* and places them in separate objects to setup bundle.
|
|
|
|
* @param {*} dataset
|
|
|
|
*/
|
|
|
|
export const stateAndPropsFromDataset = (dataset = {}) => {
|
|
|
|
const {
|
|
|
|
currentDashboard,
|
|
|
|
deploymentsEndpoint,
|
|
|
|
dashboardEndpoint,
|
|
|
|
dashboardsEndpoint,
|
2020-10-24 23:57:45 +05:30
|
|
|
panelPreviewEndpoint,
|
2020-07-28 23:09:34 +05:30
|
|
|
dashboardTimezone,
|
|
|
|
canAccessOperationsSettings,
|
|
|
|
operationsSettingsPath,
|
|
|
|
projectPath,
|
|
|
|
logsPath,
|
2020-10-24 23:57:45 +05:30
|
|
|
externalDashboardUrl,
|
2020-07-28 23:09:34 +05:30
|
|
|
currentEnvironmentName,
|
|
|
|
customDashboardBasePath,
|
2020-10-24 23:57:45 +05:30
|
|
|
addDashboardDocumentationPath,
|
2020-07-28 23:09:34 +05:30
|
|
|
...dataProps
|
|
|
|
} = dataset;
|
|
|
|
|
|
|
|
// HTML attributes are always strings, parse other types.
|
|
|
|
dataProps.hasMetrics = parseBoolean(dataProps.hasMetrics);
|
|
|
|
dataProps.customMetricsAvailable = parseBoolean(dataProps.customMetricsAvailable);
|
|
|
|
dataProps.prometheusAlertsAvailable = parseBoolean(dataProps.prometheusAlertsAvailable);
|
2021-06-08 01:23:25 +05:30
|
|
|
dataProps.hasManagedPrometheus = parseBoolean(dataProps.hasManagedPrometheus);
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
return {
|
|
|
|
initState: {
|
|
|
|
currentDashboard,
|
|
|
|
deploymentsEndpoint,
|
|
|
|
dashboardEndpoint,
|
|
|
|
dashboardsEndpoint,
|
2020-10-24 23:57:45 +05:30
|
|
|
panelPreviewEndpoint,
|
2020-07-28 23:09:34 +05:30
|
|
|
dashboardTimezone,
|
|
|
|
canAccessOperationsSettings,
|
|
|
|
operationsSettingsPath,
|
|
|
|
projectPath,
|
|
|
|
logsPath,
|
2020-10-24 23:57:45 +05:30
|
|
|
externalDashboardUrl,
|
2020-07-28 23:09:34 +05:30
|
|
|
currentEnvironmentName,
|
|
|
|
customDashboardBasePath,
|
2020-10-24 23:57:45 +05:30
|
|
|
addDashboardDocumentationPath,
|
2020-07-28 23:09:34 +05:30
|
|
|
},
|
|
|
|
dataProps,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
/**
|
|
|
|
* List of non time range url parameters
|
|
|
|
* This will be removed once we add support for free text variables
|
|
|
|
* via the dashboard yaml files in https://gitlab.com/gitlab-org/gitlab/-/issues/215689
|
|
|
|
*/
|
|
|
|
export const dashboardParams = ['dashboard', 'group', 'title', 'y_label', 'embedded'];
|
2019-10-12 21:52:04 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
/**
|
|
|
|
* This method is used to validate if the graph data format for a chart component
|
2020-04-08 14:13:33 +05:30
|
|
|
* that needs a time series as a response from a prometheus query (queryRange) is
|
2019-09-30 21:07:59 +05:30
|
|
|
* of a valid format or not.
|
|
|
|
* @param {Object} graphData the graph data response from a prometheus request
|
|
|
|
* @returns {boolean} whether the graphData format is correct
|
|
|
|
*/
|
|
|
|
export const graphDataValidatorForValues = (isValues, graphData) => {
|
|
|
|
const responseValueKeyName = isValues ? 'value' : 'values';
|
|
|
|
return (
|
2020-01-01 13:55:28 +05:30
|
|
|
Array.isArray(graphData.metrics) &&
|
2021-03-08 18:12:59 +05:30
|
|
|
graphData.metrics.filter((query) => {
|
2019-09-30 21:07:59 +05:30
|
|
|
if (Array.isArray(query.result)) {
|
|
|
|
return (
|
2021-03-08 18:12:59 +05:30
|
|
|
query.result.filter((res) => Array.isArray(res[responseValueKeyName])).length ===
|
2019-09-30 21:07:59 +05:30
|
|
|
query.result.length
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return false;
|
2021-03-08 18:12:59 +05:30
|
|
|
}).length === graphData.metrics.filter((query) => query.result).length
|
2019-09-30 21:07:59 +05:30
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
/**
|
|
|
|
* Checks that element that triggered event is located on cluster health check dashboard
|
|
|
|
* @param {HTMLElement} element to check against
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
const isClusterHealthBoard = () => (document.body.dataset.page || '').includes(':clusters:show');
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
/* eslint-disable @gitlab/require-i18n-strings */
|
2019-12-21 20:55:43 +05:30
|
|
|
/**
|
|
|
|
* Tracks snowplow event when user generates link to metric chart
|
|
|
|
* @param {String} chart link that will be sent as a property for the event
|
|
|
|
* @return {Object} config object for event tracking
|
|
|
|
*/
|
2021-03-08 18:12:59 +05:30
|
|
|
export const generateLinkToChartOptions = (chartLink) => {
|
2019-12-21 20:55:43 +05:30
|
|
|
const isCLusterHealthBoard = isClusterHealthBoard();
|
|
|
|
|
|
|
|
const category = isCLusterHealthBoard
|
|
|
|
? 'Cluster Monitoring'
|
|
|
|
: 'Incident Management::Embedded metrics';
|
|
|
|
const action = isCLusterHealthBoard
|
|
|
|
? 'generate_link_to_cluster_metric_chart'
|
|
|
|
: 'generate_link_to_metrics_chart';
|
|
|
|
|
|
|
|
return { category, action, label: 'Chart link', property: chartLink };
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tracks snowplow event when user downloads CSV of cluster metric
|
|
|
|
* @param {String} chart title that will be sent as a property for the event
|
2020-01-01 13:55:28 +05:30
|
|
|
* @return {Object} config object for event tracking
|
2019-12-21 20:55:43 +05:30
|
|
|
*/
|
2021-03-08 18:12:59 +05:30
|
|
|
export const downloadCSVOptions = (title) => {
|
2019-12-21 20:55:43 +05:30
|
|
|
const isCLusterHealthBoard = isClusterHealthBoard();
|
|
|
|
|
|
|
|
const category = isCLusterHealthBoard
|
|
|
|
? 'Cluster Monitoring'
|
|
|
|
: 'Incident Management::Embedded metrics';
|
|
|
|
const action = isCLusterHealthBoard
|
|
|
|
? 'download_csv_of_cluster_metric_chart'
|
|
|
|
: 'download_csv_of_metrics_dashboard_chart';
|
|
|
|
|
|
|
|
return { category, action, label: 'Chart title', property: title };
|
|
|
|
};
|
2020-05-24 23:13:21 +05:30
|
|
|
/* eslint-enable @gitlab/require-i18n-strings */
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
/**
|
2020-01-01 13:55:28 +05:30
|
|
|
* Generate options for snowplow to track adding a new metric via the dashboard
|
|
|
|
* custom metric modal
|
|
|
|
* @return {Object} config object for event tracking
|
|
|
|
*/
|
|
|
|
export const getAddMetricTrackingOptions = () => ({
|
|
|
|
category: document.body.dataset.page,
|
|
|
|
action: 'click_button',
|
|
|
|
label: 'add_new_metric',
|
|
|
|
property: 'modal',
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function validates the graph data contains exactly 3 metrics plus
|
2019-12-26 22:10:19 +05:30
|
|
|
* value validations from graphDataValidatorForValues.
|
|
|
|
* @param {Object} isValues
|
|
|
|
* @param {Object} graphData the graph data response from a prometheus request
|
|
|
|
* @returns {boolean} true if the data is valid
|
|
|
|
*/
|
2021-03-08 18:12:59 +05:30
|
|
|
export const graphDataValidatorForAnomalyValues = (graphData) => {
|
2019-12-26 22:10:19 +05:30
|
|
|
const anomalySeriesCount = 3; // metric, upper, lower
|
|
|
|
return (
|
2020-01-01 13:55:28 +05:30
|
|
|
graphData.metrics &&
|
|
|
|
graphData.metrics.length === anomalySeriesCount &&
|
2019-12-26 22:10:19 +05:30
|
|
|
graphDataValidatorForValues(false, graphData)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
/**
|
|
|
|
* Returns a time range from the current URL params
|
|
|
|
*
|
|
|
|
* @returns {Object|null} The time range defined by the
|
|
|
|
* current URL, reading from search query or `window.location.search`.
|
|
|
|
* Returns `null` if no parameters form a time range.
|
|
|
|
*/
|
|
|
|
export const timeRangeFromUrl = (search = window.location.search) => {
|
|
|
|
const params = queryToObject(search);
|
|
|
|
return timeRangeFromParams(params);
|
|
|
|
};
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
/**
|
|
|
|
* Variable labels are used as names for the dropdowns and also
|
|
|
|
* as URL params. Prefixing the name reduces the risk of
|
|
|
|
* collision with other URL params
|
|
|
|
*
|
|
|
|
* @param {String} label label for the template variable
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2021-03-08 18:12:59 +05:30
|
|
|
export const addPrefixToLabel = (label) => `${VARIABLE_PREFIX}${label}`;
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Before the templating variables are passed to the backend the
|
|
|
|
* prefix needs to be removed.
|
|
|
|
*
|
|
|
|
* This method removes the prefix at the beginning of the string.
|
|
|
|
*
|
|
|
|
* @param {String} label label to remove prefix from
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2021-03-08 18:12:59 +05:30
|
|
|
export const removePrefixFromLabel = (label) =>
|
2020-05-24 23:13:21 +05:30
|
|
|
(label || '').replace(new RegExp(`^${VARIABLE_PREFIX}`), '');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert parsed template variables to an object
|
2020-06-23 00:09:42 +05:30
|
|
|
* with just keys and values. Prepare the variables
|
2020-05-24 23:13:21 +05:30
|
|
|
* to be added to the URL. Keys of the object will
|
|
|
|
* have a prefix so that these params can be
|
|
|
|
* differentiated from other URL params.
|
|
|
|
*
|
|
|
|
* @param {Object} variables
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2021-03-08 18:12:59 +05:30
|
|
|
export const convertVariablesForURL = (variables) =>
|
2020-07-28 23:09:34 +05:30
|
|
|
variables.reduce((acc, { name, value }) => {
|
|
|
|
if (value !== null) {
|
|
|
|
acc[addPrefixToLabel(name)] = value;
|
|
|
|
}
|
2020-05-24 23:13:21 +05:30
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* User-defined variables from the URL are extracted. The variables
|
|
|
|
* begin with a constant prefix so that it doesn't collide with
|
|
|
|
* other URL params.
|
|
|
|
*
|
2020-07-01 16:08:20 +05:30
|
|
|
* @param {String} search URL
|
2020-05-24 23:13:21 +05:30
|
|
|
* @returns {Object} The custom variables defined by the user in the URL
|
|
|
|
*/
|
2020-07-01 16:08:20 +05:30
|
|
|
export const templatingVariablesFromUrl = (search = window.location.search) => {
|
2020-05-24 23:13:21 +05:30
|
|
|
const params = queryToObject(search);
|
|
|
|
// pick the params with variable prefix
|
|
|
|
const paramsWithVars = pickBy(params, (val, key) => key.startsWith(VARIABLE_PREFIX));
|
|
|
|
// remove the prefix before storing in the Vuex store
|
|
|
|
return mapKeys(paramsWithVars, (val, key) => removePrefixFromLabel(key));
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2020-06-23 00:09:42 +05:30
|
|
|
* Update the URL with variables. This usually get triggered when
|
2020-05-24 23:13:21 +05:30
|
|
|
* the user interacts with the dynamic input elements in the monitoring
|
|
|
|
* dashboard header.
|
|
|
|
*
|
2020-06-23 00:09:42 +05:30
|
|
|
* @param {Object} variables user defined variables
|
2020-05-24 23:13:21 +05:30
|
|
|
*/
|
2021-03-08 18:12:59 +05:30
|
|
|
export const setCustomVariablesFromUrl = (variables) => {
|
2020-05-24 23:13:21 +05:30
|
|
|
// prep the variables to append to URL
|
2020-06-23 00:09:42 +05:30
|
|
|
const parsedVariables = convertVariablesForURL(variables);
|
2020-05-24 23:13:21 +05:30
|
|
|
// update the URL
|
|
|
|
updateHistory({
|
|
|
|
url: mergeUrlParams(parsedVariables, window.location.href),
|
|
|
|
title: document.title,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
/**
|
|
|
|
* Returns a URL with no time range based on the current URL.
|
|
|
|
*
|
|
|
|
* @param {String} New URL
|
|
|
|
*/
|
|
|
|
export const removeTimeRangeParams = (url = window.location.href) =>
|
|
|
|
removeParams(timeRangeParamNames, url);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a URL for the a different time range based on the
|
|
|
|
* current URL and a time range.
|
|
|
|
*
|
|
|
|
* @param {String} New URL
|
|
|
|
*/
|
|
|
|
export const timeRangeToUrl = (timeRange, url = window.location.href) => {
|
|
|
|
const toUrl = removeTimeRangeParams(url);
|
|
|
|
const params = timeRangeToParams(timeRange);
|
|
|
|
return mergeUrlParams(params, toUrl);
|
|
|
|
};
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
/**
|
|
|
|
* Locates a panel (and its corresponding group) given a (URL) search query. Returns
|
|
|
|
* it as payload for the store to set the right expandaded panel.
|
|
|
|
*
|
|
|
|
* Params used to locate a panel are:
|
|
|
|
* - group: Group identifier
|
|
|
|
* - title: Panel title
|
|
|
|
* - y_label: Panel y_label
|
|
|
|
*
|
|
|
|
* @param {Object} dashboard - Dashboard reference from the Vuex store
|
|
|
|
* @param {String} search - URL location search query
|
|
|
|
* @returns {Object} payload - Payload for expanded panel to be displayed
|
|
|
|
* @returns {String} payload.group - Group where panel is located
|
|
|
|
* @returns {Object} payload.panel - Dashboard panel (graphData) reference
|
|
|
|
* @throws Will throw an error if Panel cannot be located.
|
|
|
|
*/
|
|
|
|
export const expandedPanelPayloadFromUrl = (dashboard, search = window.location.search) => {
|
|
|
|
const params = queryToObject(search);
|
|
|
|
|
|
|
|
// Search for the panel if any of the search params is identified
|
|
|
|
if (params.group || params.title || params.y_label) {
|
|
|
|
const panelGroup = dashboard.panelGroups.find(({ group }) => params.group === group);
|
|
|
|
const panel = panelGroup.panels.find(
|
|
|
|
// eslint-disable-next-line babel/camelcase
|
|
|
|
({ y_label, title }) => y_label === params.y_label && title === params.title,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!panel) {
|
|
|
|
// eslint-disable-next-line @gitlab/require-i18n-strings
|
|
|
|
throw new Error('Panel could no found by URL parameters.');
|
|
|
|
}
|
|
|
|
return { group: panelGroup.group, panel };
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert panel information to a URL for the user to
|
|
|
|
* bookmark or share highlighting a specific panel.
|
|
|
|
*
|
|
|
|
* If no group/panel is set, the dashboard URL is returned.
|
|
|
|
*
|
|
|
|
* @param {?String} dashboard - Dashboard path, used as identifier for a dashboard
|
2020-06-23 00:09:42 +05:30
|
|
|
* @param {?Object} variables - Custom variables that came from the URL
|
2020-05-24 23:13:21 +05:30
|
|
|
* @param {?String} group - Group Identifier
|
|
|
|
* @param {?Object} panel - Panel object from the dashboard
|
|
|
|
* @param {?String} url - Base URL including current search params
|
|
|
|
* @returns Dashboard URL which expands a panel (chart)
|
|
|
|
*/
|
|
|
|
export const panelToUrl = (
|
|
|
|
dashboard = null,
|
2020-06-23 00:09:42 +05:30
|
|
|
variables,
|
2020-05-24 23:13:21 +05:30
|
|
|
group,
|
|
|
|
panel,
|
|
|
|
url = window.location.href,
|
|
|
|
) => {
|
|
|
|
const params = {
|
|
|
|
dashboard,
|
2020-06-23 00:09:42 +05:30
|
|
|
...variables,
|
2020-05-24 23:13:21 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
if (group && panel) {
|
|
|
|
params.group = group;
|
|
|
|
params.title = panel.title;
|
|
|
|
params.y_label = panel.y_label;
|
|
|
|
} else {
|
|
|
|
// Remove existing parameters if any
|
|
|
|
params.group = null;
|
|
|
|
params.title = null;
|
|
|
|
params.y_label = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mergeUrlParams(params, url);
|
|
|
|
};
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
/**
|
|
|
|
* Get the metric value from first data point.
|
|
|
|
* Currently only used for bar charts
|
|
|
|
*
|
|
|
|
* @param {Array} values data points
|
|
|
|
* @returns {Number}
|
|
|
|
*/
|
2021-03-08 18:12:59 +05:30
|
|
|
const metricValueMapper = (values) => values[0]?.[1];
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the metric name from metric object
|
|
|
|
* Currently only used for bar charts
|
|
|
|
* e.g. { handler: '/query' }
|
|
|
|
* { method: 'get' }
|
|
|
|
*
|
|
|
|
* @param {Object} metric metric object
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2021-03-08 18:12:59 +05:30
|
|
|
const metricNameMapper = (metric) => Object.values(metric)?.[0];
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse metric object to extract metric value and name in
|
|
|
|
* [<metric-value>, <metric-name>] format.
|
|
|
|
* Currently only used for bar charts
|
|
|
|
*
|
|
|
|
* @param {Object} param0 metric object
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
|
|
|
const resultMapper = ({ metric, values = [] }) => [
|
|
|
|
metricValueMapper(values),
|
|
|
|
metricNameMapper(metric),
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bar charts graph data parser to massage data from
|
|
|
|
* backend to a format acceptable by bar charts component
|
|
|
|
* in GitLab UI
|
|
|
|
*
|
|
|
|
* e.g.
|
|
|
|
* {
|
|
|
|
* SLO: [
|
|
|
|
* [98, 'api'],
|
|
|
|
* [99, 'web'],
|
|
|
|
* [99, 'database']
|
|
|
|
* ]
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* @param {Array} data series information
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
export const barChartsDataParser = (data = []) =>
|
|
|
|
data?.reduce(
|
|
|
|
(acc, { result = [], label }) => ({
|
|
|
|
...acc,
|
|
|
|
[label]: result.map(resultMapper),
|
|
|
|
}),
|
|
|
|
{},
|
|
|
|
);
|