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

490 lines
13 KiB
JavaScript
Raw Normal View History

2020-04-08 14:13:33 +05:30
import { slugify } from '~/lib/utils/text_utility';
2020-03-13 15:44:24 +05:30
import createGqClient, { fetchPolicies } from '~/lib/graphql';
2020-04-08 14:13:33 +05:30
import { SUPPORTED_FORMATS } from '~/lib/utils/unit_format';
2020-03-13 15:44:24 +05:30
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
2020-07-01 16:08:20 +05:30
import { mergeURLVariables, parseTemplatingVariables } from './variable_mapping';
2020-06-23 00:09:42 +05:30
import { DATETIME_RANGE_TYPES } from '~/lib/utils/constants';
import { timeRangeToParams, getRangeType } from '~/lib/utils/datetime_range';
import { isSafeURL, mergeUrlParams } from '~/lib/utils/url_utility';
2020-07-28 23:09:34 +05:30
import { NOT_IN_DB_PREFIX, linkTypes, OUT_OF_THE_BOX_DASHBOARDS_PATH_PREFIX } from '../constants';
2020-03-13 15:44:24 +05:30
export const gqClient = createGqClient(
{},
{
fetchPolicy: fetchPolicies.NO_CACHE,
},
);
2017-09-10 17:25:29 +05:30
2020-04-08 14:13:33 +05:30
/**
* Metrics loaded from project-defined dashboards do not have a metric_id.
* This method creates a unique ID combining metric_id and id, if either is present.
* This is hopefully a temporary solution until BE processes metrics before passing to FE
2020-04-22 19:07:51 +05:30
*
* Related:
* https://gitlab.com/gitlab-org/gitlab/-/issues/28241
* https://gitlab.com/gitlab-org/gitlab/-/merge_requests/27447
*
2020-04-08 14:13:33 +05:30
* @param {Object} metric - metric
2020-04-22 19:07:51 +05:30
* @param {Number} metric.metric_id - Database metric id
* @param {String} metric.id - User-defined identifier
2020-04-08 14:13:33 +05:30
* @returns {Object} - normalized metric with a uniqueID
*/
// eslint-disable-next-line babel/camelcase
2020-04-22 19:07:51 +05:30
export const uniqMetricsId = ({ metric_id, id }) => `${metric_id || NOT_IN_DB_PREFIX}_${id}`;
2019-12-21 20:55:43 +05:30
2020-03-13 15:44:24 +05:30
/**
* Project path has a leading slash that doesn't work well
* with project full path resolver here
* https://gitlab.com/gitlab-org/gitlab/blob/5cad4bd721ab91305af4505b2abc92b36a56ad6b/app/graphql/resolvers/full_path_resolver.rb#L10
*
* @param {String} str String with leading slash
* @returns {String}
*/
export const removeLeadingSlash = str => (str || '').replace(/^\/+/, '');
/**
* GraphQL environments API returns only id and name.
* For the environments dropdown we need metrics_path.
* This method parses the results and add neccessart attrs
*
* @param {Array} response Environments API result
* @param {String} projectPath Current project path
* @returns {Array}
*/
export const parseEnvironmentsResponse = (response = [], projectPath) =>
(response || []).map(env => {
const id = getIdFromGraphQLId(env.id);
return {
...env,
id,
metrics_path: `${projectPath}/environments/${id}/metrics`,
};
});
2020-04-22 19:07:51 +05:30
/**
* Annotation API returns time in UTC. This method
* converts time to local time.
*
* startingAt always exists but endingAt does not.
* If endingAt does not exist, a threshold line is
* drawn.
*
* If endingAt exists, a threshold range is drawn.
* But this is not supported as of %12.10
*
* @param {Array} response annotations response
* @returns {Array} parsed responses
*/
export const parseAnnotationsResponse = response => {
if (!response) {
return [];
}
return response.map(annotation => ({
...annotation,
startingAt: new Date(annotation.startingAt),
endingAt: annotation.endingAt ? new Date(annotation.endingAt) : null,
}));
};
2019-12-21 20:55:43 +05:30
/**
2020-04-08 14:13:33 +05:30
* Maps metrics to its view model
*
* This function difers from other in that is maps all
* non-define properties as-is to the object. This is not
* advisable as it could lead to unexpected side-effects.
*
* Related issue:
* https://gitlab.com/gitlab-org/gitlab/issues/207198
*
* @param {Array} metrics - Array of prometheus metrics
* @returns {Object}
2019-12-21 20:55:43 +05:30
*/
2020-04-22 19:07:51 +05:30
const mapToMetricsViewModel = metrics =>
2020-04-08 14:13:33 +05:30
metrics.map(({ label, id, metric_id, query_range, prometheus_endpoint_path, ...metric }) => ({
2020-04-22 19:07:51 +05:30
label,
2020-04-08 14:13:33 +05:30
queryRange: query_range,
prometheusEndpointPath: prometheus_endpoint_path,
metricId: uniqMetricsId({ metric_id, id }),
2020-04-22 19:07:51 +05:30
// metric data
loading: false,
result: null,
state: null,
2020-04-08 14:13:33 +05:30
...metric,
}));
2020-01-01 13:55:28 +05:30
2020-04-08 14:13:33 +05:30
/**
2020-04-22 19:07:51 +05:30
* Maps X-axis view model
2020-04-08 14:13:33 +05:30
*
2020-04-22 19:07:51 +05:30
* @param {Object} axis
*/
const mapXAxisToViewModel = ({ name = '' }) => ({ name });
/**
* Maps Y-axis view model
*
* Defaults to a 2 digit precision and `engineering` format. It only allows
2020-04-08 14:13:33 +05:30
* formats in the SUPPORTED_FORMATS array.
*
* @param {Object} axis
*/
2020-04-22 19:07:51 +05:30
const mapYAxisToViewModel = ({
name = '',
format = SUPPORTED_FORMATS.engineering,
precision = 2,
}) => {
2020-04-08 14:13:33 +05:30
return {
name,
2020-04-22 19:07:51 +05:30
format: SUPPORTED_FORMATS[format] || SUPPORTED_FORMATS.engineering,
2020-04-08 14:13:33 +05:30
precision,
};
};
2020-06-23 00:09:42 +05:30
/**
* Maps a link to its view model, expects an url and
* (optionally) a title.
*
* Unsafe URLs are ignored.
*
* @param {Object} Link
* @returns {Object} Link object with a `title`, `url` and `type`
*
*/
const mapLinksToViewModel = ({ url = null, title = '', type } = {}) => {
return {
title: title || String(url),
type,
url: url && isSafeURL(url) ? String(url) : '#',
};
};
2020-04-08 14:13:33 +05:30
/**
* Maps a metrics panel to its view model
*
* @param {Object} panel - Metrics panel
* @returns {Object}
*/
2020-07-28 23:09:34 +05:30
export const mapPanelToViewModel = ({
2020-05-24 23:13:21 +05:30
id = null,
2020-04-22 19:07:51 +05:30
title = '',
type,
x_axis = {},
x_label,
y_label,
y_axis = {},
2020-07-28 23:09:34 +05:30
field,
2020-04-22 19:07:51 +05:30
metrics = [],
2020-06-23 00:09:42 +05:30
links = [],
2020-04-22 19:07:51 +05:30
max_value,
}) => {
// Both `x_axis.name` and `x_label` are supported for now
// https://gitlab.com/gitlab-org/gitlab/issues/210521
const xAxis = mapXAxisToViewModel({ name: x_label, ...x_axis }); // eslint-disable-line babel/camelcase
2020-04-08 14:13:33 +05:30
// Both `y_axis.name` and `y_label` are supported for now
// https://gitlab.com/gitlab-org/gitlab/issues/208385
2020-04-22 19:07:51 +05:30
const yAxis = mapYAxisToViewModel({ name: y_label, ...y_axis }); // eslint-disable-line babel/camelcase
2020-04-08 14:13:33 +05:30
return {
2020-05-24 23:13:21 +05:30
id,
2020-04-08 14:13:33 +05:30
title,
type,
2020-04-22 19:07:51 +05:30
xLabel: xAxis.name,
2020-04-08 14:13:33 +05:30
y_label: yAxis.name, // Changing y_label to yLabel is pending https://gitlab.com/gitlab-org/gitlab/issues/207198
yAxis,
2020-04-22 19:07:51 +05:30
xAxis,
2020-07-28 23:09:34 +05:30
field,
2020-04-22 19:07:51 +05:30
maxValue: max_value,
2020-06-23 00:09:42 +05:30
links: links.map(mapLinksToViewModel),
metrics: mapToMetricsViewModel(metrics),
2020-04-08 14:13:33 +05:30
};
};
/**
* Maps a metrics panel group to its view model
*
* @param {Object} panelGroup - Panel Group
* @returns {Object}
*/
const mapToPanelGroupViewModel = ({ group = '', panels = [] }, i) => {
return {
key: `${slugify(group || 'default')}-${i}`,
group,
2020-04-22 19:07:51 +05:30
panels: panels.map(mapPanelToViewModel),
2020-04-08 14:13:33 +05:30
};
};
2020-06-23 00:09:42 +05:30
/**
* Convert dashboard time range to Grafana
* dashboards time range.
*
* @param {Object} timeRange
* @returns {Object}
*/
export const convertToGrafanaTimeRange = timeRange => {
const timeRangeType = getRangeType(timeRange);
if (timeRangeType === DATETIME_RANGE_TYPES.fixed) {
return {
from: new Date(timeRange.start).getTime(),
to: new Date(timeRange.end).getTime(),
};
} else if (timeRangeType === DATETIME_RANGE_TYPES.rolling) {
const { seconds } = timeRange.duration;
return {
from: `now-${seconds}s`,
to: 'now',
};
}
// fallback to returning the time range as is
return timeRange;
};
/**
* Convert dashboard time ranges to other supported
* link formats.
*
* @param {Object} timeRange metrics dashboard time range
* @param {String} type type of link
* @returns {String}
*/
export const convertTimeRanges = (timeRange, type) => {
if (type === linkTypes.GRAFANA) {
return convertToGrafanaTimeRange(timeRange);
}
return timeRangeToParams(timeRange);
};
/**
* Adds dashboard-related metadata to the user-defined links.
*
* As of %13.1, metadata only includes timeRange but in the
* future more info will be added to the links.
*
* @param {Object} metadata
* @returns {Function}
*/
export const addDashboardMetaDataToLink = metadata => link => {
let modifiedLink = { ...link };
if (metadata.timeRange) {
modifiedLink = {
...modifiedLink,
url: mergeUrlParams(convertTimeRanges(metadata.timeRange, link.type), link.url),
};
}
return modifiedLink;
};
2020-04-08 14:13:33 +05:30
/**
* Maps a dashboard json object to its view model
*
* @param {Object} dashboard - Dashboard object
* @param {String} dashboard.dashboard - Dashboard name object
* @param {Array} dashboard.panel_groups - Panel groups array
* @returns {Object}
*/
2020-06-23 00:09:42 +05:30
export const mapToDashboardViewModel = ({
dashboard = '',
templating = {},
links = [],
panel_groups = [],
}) => {
2020-04-08 14:13:33 +05:30
return {
dashboard,
2020-07-28 23:09:34 +05:30
variables: mergeURLVariables(parseTemplatingVariables(templating.variables)),
2020-06-23 00:09:42 +05:30
links: links.map(mapLinksToViewModel),
2020-04-08 14:13:33 +05:30
panelGroups: panel_groups.map(mapToPanelGroupViewModel),
};
};
2019-12-21 20:55:43 +05:30
2020-07-28 23:09:34 +05:30
// Prometheus Results Parsing
const dateTimeFromUnixTime = unixTime => new Date(unixTime * 1000).toISOString();
const mapScalarValue = ([unixTime, value]) => [dateTimeFromUnixTime(unixTime), Number(value)];
// Note: `string` value type is unused as of prometheus 2.19.
const mapStringValue = ([unixTime, value]) => [dateTimeFromUnixTime(unixTime), value];
/**
* Processes a scalar result.
*
* The corresponding result property has the following format:
*
* [ <unix_time>, "<scalar_value>" ]
*
* @param {array} result
* @returns {array}
*/
const normalizeScalarResult = result => [
{
metric: {},
value: mapScalarValue(result),
values: [mapScalarValue(result)],
},
];
/**
* Processes a string result.
*
* The corresponding result property has the following format:
*
* [ <unix_time>, "<string_value>" ]
*
* Note: This value type is unused as of prometheus 2.19.
*
* @param {array} result
* @returns {array}
*/
const normalizeStringResult = result => [
{
metric: {},
value: mapStringValue(result),
values: [mapStringValue(result)],
},
];
/**
* Proccesses an instant vector.
*
* Instant vectors are returned as result type `vector`.
*
* The corresponding result property has the following format:
*
* [
* {
* "metric": { "<label_name>": "<label_value>", ... },
* "value": [ <unix_time>, "<sample_value>" ],
* "values": [ [ <unix_time>, "<sample_value>" ] ]
* },
* ...
* ]
*
* `metric` - Key-value pairs object representing metric measured
* `value` - The vector result
* `values` - An array with a single value representing the result
*
* This method also adds the matrix version of the vector
* by introducing a `values` array with a single element. This
* allows charts to default to `values` if needed.
*
* @param {array} result
* @returns {array}
*/
const normalizeVectorResult = result =>
result.map(({ metric, value }) => {
const scalar = mapScalarValue(value);
// Add a single element to `values`, to support matrix
// style charts.
return { metric, value: scalar, values: [scalar] };
});
2020-06-23 00:09:42 +05:30
/**
2020-07-28 23:09:34 +05:30
* Range vectors are returned as result type matrix.
*
* The corresponding result property has the following format:
2020-06-23 00:09:42 +05:30
*
* {
* "metric": { "<label_name>": "<label_value>", ... },
2020-07-28 23:09:34 +05:30
* "value": [ <unix_time>, "<sample_value>" ],
2020-06-23 00:09:42 +05:30
* "values": [ [ <unix_time>, "<sample_value>" ], ... ]
* },
*
2020-07-28 23:09:34 +05:30
* `metric` - Key-value pairs object representing metric measured
* `value` - The last (more recent) result
* `values` - A range of results for the metric
*
2020-06-23 00:09:42 +05:30
* See https://prometheus.io/docs/prometheus/latest/querying/api/#range-vectors
*
2020-07-28 23:09:34 +05:30
* @param {array} result
* @returns {object} Normalized result.
2020-06-23 00:09:42 +05:30
*/
2020-07-28 23:09:34 +05:30
const normalizeResultMatrix = result =>
result.map(({ metric, values }) => {
const mappedValues = values.map(mapScalarValue);
return {
metric,
value: mappedValues[mappedValues.length - 1],
values: mappedValues,
2019-09-30 21:07:59 +05:30
};
2020-07-28 23:09:34 +05:30
});
2019-09-30 21:07:59 +05:30
2020-07-28 23:09:34 +05:30
/**
* Parse response data from a Prometheus Query that comes
* in the format:
*
* {
* "resultType": "matrix" | "vector" | "scalar" | "string",
* "result": <value>
* }
*
* @see https://prometheus.io/docs/prometheus/latest/querying/api/#expression-query-result-formats
*
* @param {object} data - Data containing results and result type.
* @returns {object} - A result array of metric results:
* [
* {
* metric: { ... },
* value: ['2015-07-01T20:10:51.781Z', '1'],
* values: [['2015-07-01T20:10:51.781Z', '1'] , ... ],
* },
* ...
* ]
*
*/
export const normalizeQueryResponseData = data => {
const { resultType, result } = data;
if (resultType === 'vector') {
return normalizeVectorResult(result);
} else if (resultType === 'scalar') {
return normalizeScalarResult(result);
} else if (resultType === 'string') {
return normalizeStringResult(result);
}
return normalizeResultMatrix(result);
2019-09-30 21:07:59 +05:30
};
2020-05-30 21:06:31 +05:30
/**
* Custom variables defined in the dashboard yml file are
* eventually passed over the wire to the backend Prometheus
* API proxy.
*
* This method adds a prefix to the URL param keys so that
* the backend can differential these variables from the other
* variables.
*
* This is currently only used by getters/getCustomVariablesParams
*
2020-07-28 23:09:34 +05:30
* @param {String} name Variable key that needs to be prefixed
2020-05-30 21:06:31 +05:30
* @returns {String}
*/
2020-07-28 23:09:34 +05:30
export const addPrefixToCustomVariableParams = name => `variables[${name}]`;
/**
* Normalize custom dashboard paths. This method helps support
* metrics dashboard to work with custom dashboard file names instead
* of the entire path.
*
* If dashboard is empty, it is the default dashboard.
* If dashboard is set, it usually is a custom dashboard unless
* explicitly it is set to default dashboard path.
*
* @param {String} dashboard dashboard path
* @param {String} dashboardPrefix custom dashboard directory prefix
* @returns {String} normalized dashboard path
*/
export const normalizeCustomDashboardPath = (dashboard, dashboardPrefix = '') => {
const currDashboard = dashboard || '';
let dashboardPath = `${dashboardPrefix}/${currDashboard}`;
if (!currDashboard) {
dashboardPath = '';
} else if (
currDashboard.startsWith(dashboardPrefix) ||
currDashboard.startsWith(OUT_OF_THE_BOX_DASHBOARDS_PATH_PREFIX)
) {
dashboardPath = currDashboard;
}
return dashboardPath;
};