All files / modules/85-cv/pages/slos/components/CVCreateSLO CVCreateSLO.constants.ts

100% Statements 32/32
100% Branches 4/4
100% Functions 14/14
100% Lines 27/27

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 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168                    13x   13x                         13x   13x                           13x                                     13x         13x     1x               13x 1x           13x 26x           13x 28x             13x 18x 18x   18x     296x                 296x       296x                 148x                 296x                 296x       296x       296x       296x                    
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import type { IOptionProps } from '@blueprintjs/core'
import type { SelectOption } from '@wings-software/uicore'
import type { RadioButtonProps } from '@wings-software/uicore/dist/components/RadioButton/RadioButton'
import * as Yup from 'yup'
import type { UseStringsReturn } from 'framework/strings'
import {
  SLOForm,
  PeriodTypes,
  SLITypes,
  SLIMetricTypes,
  Comparators,
  SLOFormFields,
  CreateSLOTabs,
  SLIMissingDataTypes,
  PeriodLengthTypes,
  SLIEventTypes
} from '@cv/pages/slos/components/CVCreateSLO/CVCreateSLO.types'
 
export const TabsOrder = [CreateSLOTabs.NAME, CreateSLOTabs.SLI, CreateSLOTabs.SLO_TARGET_BUDGET_POLICY]
 
export const initialValuesSLO: SLOForm = {
  [SLOFormFields.NAME]: '',
  [SLOFormFields.IDENTIFIER]: '',
  [SLOFormFields.USER_JOURNEY_REF]: '',
  [SLOFormFields.MONITORED_SERVICE_REF]: '',
  [SLOFormFields.HEALTH_SOURCE_REF]: '',
  [SLOFormFields.SLI_TYPE]: SLITypes.LATENCY,
  [SLOFormFields.SLI_METRIC_TYPE]: SLIMetricTypes.RATIO,
  [SLOFormFields.VALID_REQUEST_METRIC]: '',
  [SLOFormFields.SLI_MISSING_DATA_TYPE]: SLIMissingDataTypes.GOOD,
  [SLOFormFields.PERIOD_TYPE]: PeriodTypes.ROLLING,
  [SLOFormFields.SLO_TARGET_PERCENTAGE]: 99
}
 
export const comparatorOptions: SelectOption[] = [
  {
    label: Comparators.LESS,
    value: Comparators.LESS
  },
  {
    label: Comparators.GREATER,
    value: Comparators.GREATER
  },
  {
    label: Comparators.LESS_EQUAL,
    value: Comparators.LESS_EQUAL
  },
  {
    label: Comparators.GREATER_EQUAL,
    value: Comparators.GREATER_EQUAL
  }
]
 
export const defaultOption: SelectOption = {
  label: '',
  value: ''
}
 
export const getSLITypeOptions = (
  getString: UseStringsReturn['getString']
): Pick<RadioButtonProps, 'value' | 'label'>[] => {
  return [
    { label: getString('cv.slos.slis.type.availability'), value: SLITypes.AVAILABILITY },
    { label: getString('cv.slos.slis.type.latency'), value: SLITypes.LATENCY }
  ]
}
 
// PickMetric
 
export const getSLIMetricOptions = (getString: UseStringsReturn['getString']): IOptionProps[] => {
  return [
    { label: getString('cv.slos.slis.metricOptions.thresholdBased'), value: SLIMetricTypes.THRESHOLD },
    { label: getString('cv.slos.slis.metricOptions.ratioBased'), value: SLIMetricTypes.RATIO }
  ]
}
 
export const getEventTypeOptions = (getString: UseStringsReturn['getString']): SelectOption[] => {
  return [
    { label: getString('cv.good'), value: SLIEventTypes.GOOD },
    { label: getString('cv.bad'), value: SLIEventTypes.BAD }
  ]
}
 
export const getMissingDataTypeOptions = (getString: UseStringsReturn['getString']): SelectOption[] => {
  return [
    { label: getString('cv.good'), value: SLIMissingDataTypes.GOOD },
    { label: getString('cv.bad'), value: SLIMissingDataTypes.BAD },
    { label: getString('cv.ignore'), value: SLIMissingDataTypes.IGNORE }
  ]
}
 
export const getSLOFormValidationSchema = (getString: UseStringsReturn['getString']): any => {
  const REQUIRED = getString('cv.required')
  const METRIC_IS_REQUIRED = getString('cv.metricIsRequired')
 
  return Yup.object().shape({
    [SLOFormFields.NAME]: Yup.string().trim().required(getString('cv.slos.validations.nameValidation')),
    [SLOFormFields.IDENTIFIER]: Yup.string().when([SLOFormFields.NAME], {
      is: name => name,
      then: Yup.string().trim().required(getString('validation.identifierRequired'))
    }),
    [SLOFormFields.USER_JOURNEY_REF]: Yup.string().required(getString('cv.slos.validations.userJourneyRequired')),
    [SLOFormFields.MONITORED_SERVICE_REF]: Yup.string().required(
      getString('connectors.cdng.validations.monitoringServiceRequired')
    ),
    [SLOFormFields.HEALTH_SOURCE_REF]: Yup.string().required(getString('cv.slos.validations.healthSourceRequired')),
    [SLOFormFields.EVENT_TYPE]: Yup.string().when(SLOFormFields.SLI_METRIC_TYPE, {
      is: SLIMetricType => SLIMetricType === SLIMetricTypes.RATIO,
      then: Yup.string().nullable().required(REQUIRED)
    }),
    [SLOFormFields.GOOD_REQUEST_METRIC]: Yup.string().when(SLOFormFields.SLI_METRIC_TYPE, {
      is: SLIMetricType => SLIMetricType === SLIMetricTypes.RATIO,
      then: Yup.string().nullable().required(METRIC_IS_REQUIRED)
    }),
    [SLOFormFields.VALID_REQUEST_METRIC]: Yup.string()
      .required(METRIC_IS_REQUIRED)
      .test(
        'bothMetricsShouldBeDifferent',
        getString('cv.metricForGoodAndValidRequestsShouldBeDifferent'),
        function (validRequestMetric) {
          return validRequestMetric && this.parent.SLIMetricType === SLIMetricTypes.RATIO
            ? validRequestMetric !== this.parent.goodRequestMetric
            : true
        }
      ),
    [SLOFormFields.OBJECTIVE_VALUE]: Yup.number()
      .typeError(REQUIRED)
      .min(0, getString('cv.minValueN', { n: 0 }))
      .when([SLOFormFields.SLI_METRIC_TYPE], {
        is: SLIMetricType => SLIMetricType === SLIMetricTypes.RATIO,
        then: Yup.number()
          .typeError(REQUIRED)
          .max(100, getString('cv.maxValue', { n: 100 }))
      })
      .required(REQUIRED),
    [SLOFormFields.OBJECTIVE_COMPARATOR]: Yup.string().required(REQUIRED),
    [SLOFormFields.SLI_MISSING_DATA_TYPE]: Yup.string().required(getString('cv.sliMissingDataTypeIsRequired')),
    [SLOFormFields.PERIOD_LENGTH]: Yup.string().when([SLOFormFields.PERIOD_TYPE], {
      is: periodType => periodType === PeriodTypes.ROLLING,
      then: Yup.string().nullable().required(getString('cv.periodLengthIsRequired'))
    }),
    [SLOFormFields.PERIOD_LENGTH_TYPE]: Yup.string().when([SLOFormFields.PERIOD_TYPE], {
      is: periodType => periodType === PeriodTypes.CALENDAR,
      then: Yup.string().nullable().required(getString('cv.periodLengthIsRequired'))
    }),
    [SLOFormFields.DAY_OF_WEEK]: Yup.string().when([SLOFormFields.PERIOD_LENGTH_TYPE], {
      is: periodLengthType => periodLengthType === PeriodLengthTypes.WEEKLY,
      then: Yup.string().nullable().required(getString('cv.windowsEndIsRequired'))
    }),
    [SLOFormFields.DAY_OF_MONTH]: Yup.string().when([SLOFormFields.PERIOD_LENGTH_TYPE], {
      is: periodLengthType => periodLengthType === PeriodLengthTypes.MONTHLY,
      then: Yup.string().nullable().required(getString('cv.windowsEndIsRequired'))
    }),
    [SLOFormFields.SLO_TARGET_PERCENTAGE]: Yup.number()
      .typeError(REQUIRED)
      .min(0, getString('cv.minValueN', { n: 0 }))
      .max(100, getString('cv.maxValue', { n: 100 }))
      .required(REQUIRED)
  })
}