All files / app/assets/javascripts/monitoring validators.js

88.89% Statements 8/9
91.67% Branches 11/12
100% Functions 5/5
88.89% Lines 8/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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    1x 1x 1x                                     7x 7x 7x                                                 4x   4x      
import { isSafeURL } from '~/lib/utils/url_utility';
 
const isRunbookUrlValid = (runbookUrl) => {
  Eif (!runbookUrl) {
    return true;
  }
  return isSafeURL(runbookUrl);
};
 
// Prop validator for alert information, expecting an object like the example below.
//
// {
//   '/root/autodevops-deploy/prometheus/alerts/16.json?environment_id=37': {
//     alert_path: "/root/autodevops-deploy/prometheus/alerts/16.json?environment_id=37",
//     metricId: '1',
//     operator: ">",
//     query: "rate(http_requests_total[5m])[30m:1m]",
//     threshold: 0.002,
//     title: "Core Usage (Total)",
//     runbookUrl: "https://www.gitlab.com/my-project/-/wikis/runbook"
//   }
// }
export function alertsValidator(value) {
  return Object.keys(value).every((key) => {
    const alert = value[key];
    return (
      alert.alert_path &&
      key === alert.alert_path &&
      alert.metricId &&
      typeof alert.metricId === 'string' &&
      alert.operator &&
      typeof alert.threshold === 'number' &&
      isRunbookUrlValid(alert.runbookUrl)
    );
  });
}
 
// Prop validator for query information, expecting an array like the example below.
//
// [
//   {
//     metricId: '16',
//     label: 'Total Cores'
//   },
//   {
//     metricId: '17',
//     label: 'Sub-total Cores'
//   }
// ]
export function queriesValidator(value) {
  return value.every(
    (query) =>
      query.metricId && typeof query.metricId === 'string' && typeof query.label === 'string',
  );
}