summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/monitoring/utils.js
blob: 478e2b3d06ccd23018db820553065ab4933cf729 (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
import { timeWindows } from './constants';

/**
 * method that converts a predetermined time window to minutes
 * defaults to 8 hours as the default option
 * @param {String} timeWindow - The time window to convert to minutes
 * @returns {number} The time window in minutes
 */
const getTimeDifferenceSeconds = timeWindow => {
  switch (timeWindow) {
    case timeWindows.thirtyMinutes:
      return 60 * 30;
    case timeWindows.threeHours:
      return 60 * 60 * 3;
    case timeWindows.oneDay:
      return 60 * 60 * 24 * 1;
    case timeWindows.threeDays:
      return 60 * 60 * 24 * 3;
    case timeWindows.oneWeek:
      return 60 * 60 * 24 * 7 * 1;
    default:
      return 60 * 60 * 8;
  }
};

export const getTimeDiff = selectedTimeWindow => {
  const end = Date.now() / 1000; // convert milliseconds to seconds
  const start = end - getTimeDifferenceSeconds(selectedTimeWindow);

  return { start, end };
};

/**
 * This method is used to validate if the graph data format for a chart component
 * that needs a time series as a response from a prometheus query (query_range) is
 * 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 (
    Array.isArray(graphData.queries) &&
    graphData.queries.filter(query => {
      if (Array.isArray(query.result)) {
        return (
          query.result.filter(res => Array.isArray(res[responseValueKeyName])).length ===
          query.result.length
        );
      }
      return false;
    }).length === graphData.queries.length
  );
};

export default {};