Welcome to mirror list, hosted at ThFree Co, Russian Federation.

monitor_helper.js « helpers « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 94a0d38f05f148dcf48f93a9d574797181e14297 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
 * @param {String} queryLabel - Default query label for chart
 * @param {Object} metricAttributes - Default metric attribute values (e.g. method, instance)
 * @returns {String} The formatted query label
 * @example
 * singleAttributeLabel('app', {__name__: "up", app: "prometheus"}) -> "app: prometheus"
 */
const singleAttributeLabel = (queryLabel, metricAttributes) => {
  if (!queryLabel) return '';
  const relevantAttribute = queryLabel.toLowerCase().replace(' ', '_');
  const value = metricAttributes[relevantAttribute];
  if (!value) return '';
  return `${queryLabel}: ${value}`;
};

/**
 * @param {String} queryLabel - Default query label for chart
 * @param {Object} metricAttributes - Default metric attribute values (e.g. method, instance)
 * @returns {String} The formatted query label
 * @example
 * templatedLabel('__name__', {__name__: "up", app: "prometheus"}) -> "__name__"
 */
const templatedLabel = (queryLabel, metricAttributes) => {
  if (!queryLabel) return '';
  // eslint-disable-next-line array-callback-return
  Object.entries(metricAttributes).map(([templateVar, label]) => {
    const regex = new RegExp(`{{\\s*${templateVar}\\s*}}`, 'g');
    // eslint-disable-next-line no-param-reassign
    queryLabel = queryLabel.replace(regex, label);
  });

  return queryLabel;
};

/**
 * @param {Object} metricAttributes - Default metric attribute values (e.g. method, instance)
 * @returns {String} The formatted query label
 * @example
 * multiMetricLabel('', {__name__: "up", app: "prometheus"}) -> "__name__: up, app: prometheus"
 */
const multiMetricLabel = metricAttributes => {
  return Object.entries(metricAttributes)
    .map(([templateVar, label]) => `${templateVar}: ${label}`)
    .join(', ');
};

/**
 * @param {String} queryLabel - Default query label for chart
 * @param {Object} metricAttributes - Default metric attribute values (e.g. method, instance)
 * @returns {String} The formatted query label
 */
const getSeriesLabel = (queryLabel, metricAttributes) => {
  return (
    singleAttributeLabel(queryLabel, metricAttributes) ||
    templatedLabel(queryLabel, metricAttributes) ||
    multiMetricLabel(metricAttributes) ||
    queryLabel
  );
};

/**
 * @param {Array} queryResults - Array of Result objects
 * @param {Object} defaultConfig - Default chart config values (e.g. lineStyle, name)
 * @returns {Array} The formatted values
 */
// eslint-disable-next-line import/prefer-default-export
export const makeDataSeries = (queryResults, defaultConfig) =>
  queryResults
    .map(result => {
      // NaN values may disrupt avg., max. & min. calculations in the legend, filter them out
      const data = result.values.filter(([, value]) => !Number.isNaN(value));
      if (!data.length) {
        return null;
      }
      const series = { data };
      return {
        ...defaultConfig,
        ...series,
        name: getSeriesLabel(defaultConfig.name, result.metric),
      };
    })
    .filter(series => series !== null);