All files / modules/85-cv/pages/health-source/connectors/Dynatrace DynatraceHealthSource.utils.tsx

100% Statements 68/68
83.33% Branches 35/42
100% Functions 12/12
100% Lines 61/61

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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238              27x                       27x 27x       27x           27x   27x 2x             2x           2x         2x 1x 1x   2x             27x 2x 2x   2x 2x   2x 2x                         2x 1x 1x           2x     27x           6x 6x                   27x 11x 2x             27x             36x   36x 1x   36x 1x         36x 35x                 36x     27x               38x   38x 2x 1x       1x       36x 35x         38x                   27x                 5x                           5x 5x 3x 3x   2x 2x 2x   2x 2x     27x     19x                      
/*
 * Copyright 2022 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 { cloneDeep, isEmpty } from 'lodash-es'
import type { SelectOption } from '@wings-software/uicore'
import type { FormikProps } from 'formik'
import type { UpdatedHealthSource } from '@cv/pages/health-source/HealthSourceDrawer/HealthSourceDrawerContent.types'
import type { DynatraceHealthSourceSpec, DynatraceServiceDTO } from 'services/cv'
import type {
  DynatraceFormDataInterface,
  DynatraceMetricData,
  DynatraceMetricInfo,
  InitDynatraceCustomMetricInterface
} from '@cv/pages/health-source/connectors/Dynatrace/DynatraceHealthSource.types'
import type { StringKeys } from 'framework/strings'
import { DynatraceProductNames } from '@cv/pages/health-source/HealthSourceDrawer/component/defineHealthSource/DefineHealthSource.constant'
import {
  DynatraceHealthSourceFieldNames,
  QUERY_CONTAINS_SERVICE_VALIDATION_PARAM
} from '@cv/pages/health-source/connectors/Dynatrace/DynatraceHealthSource.constants'
import {
  convertMetricPackToMetricData,
  mapCommonMetricInfoToCommonMetricDefinition,
  mapCommonMetricDefinitionToCommonMetricInfo,
  validateMetricPackData
} from '@cv/pages/health-source/common/utils/HealthSource.utils'
import { validateCommonCustomMetricFields } from '@cv/pages/health-source/common/CustomMetric/CustomMetric.utils'
 
export const mapDynatraceMetricDataToHealthSource = (dynatraceMetricData: DynatraceMetricData): UpdatedHealthSource => {
  const specPayload: DynatraceHealthSourceSpec = {
    connectorRef: dynatraceMetricData.connectorRef,
    serviceId: dynatraceMetricData.selectedService.value as string,
    serviceName: dynatraceMetricData.selectedService.label as string,
    feature: DynatraceProductNames.APM,
    metricPacks: Object.entries(dynatraceMetricData.metricData)
      .map(item => {
        return item[1]
          ? {
              identifier: item[0]
            }
          : {}
      })
      .filter(item => !isEmpty(item)),
    metricDefinitions: [],
    serviceMethodIds: dynatraceMetricData.serviceMethods
  }
 
  for (const entry of dynatraceMetricData.customMetrics.entries()) {
    const { metricSelector } = entry[1]
    specPayload.metricDefinitions?.push({ ...mapCommonMetricInfoToCommonMetricDefinition(entry[1]), metricSelector })
  }
  return {
    type: 'Dynatrace',
    identifier: dynatraceMetricData.healthSourceIdentifier,
    name: dynatraceMetricData.healthSourceName,
    spec: specPayload
  }
}
export const mapHealthSourceToDynatraceMetricData = (sourceData: any): DynatraceMetricData => {
  const healthSource: UpdatedHealthSource = sourceData.healthSourceList.find(
    (source: UpdatedHealthSource) => source.identifier === sourceData.healthSourceIdentifier
  )
  const dynatraceHealthSourceSpec = (healthSource?.spec as DynatraceHealthSourceSpec) || {}
  const { serviceName = '', serviceId = '', serviceMethodIds, metricPacks } = dynatraceHealthSourceSpec
 
  const metricDefinitions = dynatraceHealthSourceSpec.metricDefinitions || []
  const dynatraceMetricData: DynatraceMetricData = {
    product: sourceData.product,
    healthSourceName: sourceData.healthSourceName,
    healthSourceIdentifier: sourceData.healthSourceIdentifier,
    connectorRef: sourceData.connectorRef,
    isEdit: sourceData.isEdit,
    selectedService: { label: serviceName, value: serviceId },
    metricPacks,
    metricData: convertMetricPackToMetricData(metricPacks),
    serviceMethods: serviceMethodIds,
    customMetrics: new Map()
  }
 
  for (const metricDefinition of metricDefinitions) {
    Eif (metricDefinition.metricName) {
      dynatraceMetricData.customMetrics.set(metricDefinition.metricName, {
        metricSelector: metricDefinition.metricSelector,
        ...mapCommonMetricDefinitionToCommonMetricInfo(metricDefinition)
      })
    }
  }
  return dynatraceMetricData
}
 
export const mapDynatraceDataToDynatraceForm = (
  dynatraceMetricData: DynatraceFormDataInterface,
  mappedMetrics: Map<string, DynatraceMetricInfo>,
  selectedMetric: string,
  showCustomMetric: boolean
): DynatraceFormDataInterface => {
  const metricIdentifier = selectedMetric.split(' ').join('_')
  return {
    ...dynatraceMetricData,
    ...mappedMetrics.get(selectedMetric),
    metricData: dynatraceMetricData.metricData,
    metricName: selectedMetric,
    showCustomMetric,
    identifier: metricIdentifier
  }
}
 
export function mapServiceListToOptions(services: DynatraceServiceDTO[]): SelectOption[] {
  return services.map(service => {
    return {
      label: service.displayName || '',
      value: service.entityId || ''
    }
  })
}
 
export const validateMapping = (
  dynatraceMetricData: DynatraceFormDataInterface,
  createdMetrics: string[],
  selectedMetricIndex: number,
  getString: (key: StringKeys) => string,
  mappedMetrics: Map<string, DynatraceMetricInfo>
): ((key: string) => string) => {
  let errors = {} as any
 
  if (!dynatraceMetricData.showCustomMetric) {
    errors = validateMetricPackData(dynatraceMetricData.metricData, getString, errors)
  }
  if (!dynatraceMetricData.selectedService.value || dynatraceMetricData.selectedService.value === 'loading') {
    errors[DynatraceHealthSourceFieldNames.DYNATRACE_SELECTED_SERVICE] = getString(
      'cv.healthSource.connectors.Dynatrace.validations.selectedService'
    )
  }
  // if custom metrics are present then validate custom metrics form
  if (dynatraceMetricData.showCustomMetric) {
    errors = validateDynatraceCustomMetricFields(
      dynatraceMetricData,
      createdMetrics,
      selectedMetricIndex,
      errors,
      getString,
      mappedMetrics
    )
  }
  return errors
}
 
export const validateDynatraceCustomMetricFields = (
  values: DynatraceMetricInfo,
  createdMetrics: string[],
  selectedMetricIndex: number,
  errors: any,
  getString: (key: StringKeys) => string,
  mappedMetrics?: Map<string, DynatraceMetricInfo>
): ((key: string) => string) => {
  const errorsToReturn = cloneDeep(errors)
 
  if (!values.metricSelector) {
    if (values.isManualQuery) {
      errorsToReturn[DynatraceHealthSourceFieldNames.METRIC_SELECTOR] = getString(
        'cv.monitoringSources.gco.manualInputQueryModal.validation.query'
      )
    } else {
      errorsToReturn[DynatraceHealthSourceFieldNames.ACTIVE_METRIC_SELECTOR] = getString(
        'cv.monitoringSources.metricValidation'
      )
    }
  } else if (!values.metricSelector.includes(QUERY_CONTAINS_SERVICE_VALIDATION_PARAM)) {
    errorsToReturn[DynatraceHealthSourceFieldNames.METRIC_SELECTOR] = `${getString(
      'cv.monitoringSources.datadog.validation.queryContains'
    )}${QUERY_CONTAINS_SERVICE_VALIDATION_PARAM}`
  }
 
  return validateCommonCustomMetricFields(
    values,
    createdMetrics,
    selectedMetricIndex,
    errorsToReturn,
    getString,
    mappedMetrics
  )
}
 
export const onSubmitDynatraceData = (
  formik: FormikProps<DynatraceFormDataInterface>,
  mappedMetrics: Map<string, DynatraceMetricInfo>,
  selectedMetric: string,
  selectedMetricIndex: number,
  createdMetrics: string[],
  getString: (key: StringKeys) => string,
  onSubmit: (healthSourcePayload: DynatraceMetricData) => void
): void => {
  formik.setTouched({
    ...formik.touched,
    [DynatraceHealthSourceFieldNames.DYNATRACE_SELECTED_SERVICE]: true,
    [DynatraceHealthSourceFieldNames.METRIC_DATA]: { Performance: true },
    [DynatraceHealthSourceFieldNames.METRIC_NAME]: true,
    [DynatraceHealthSourceFieldNames.IDENTIFIER]: true,
    [DynatraceHealthSourceFieldNames.METRIC_SELECTOR]: true,
    [DynatraceHealthSourceFieldNames.ACTIVE_METRIC_SELECTOR]: true,
    [DynatraceHealthSourceFieldNames.GROUP_NAME]: true,
    [DynatraceHealthSourceFieldNames.SLI]: true,
    [DynatraceHealthSourceFieldNames.CONTINUOUS_VERIFICATION]: true,
    [DynatraceHealthSourceFieldNames.LOWER_BASELINE_DEVIATION]: true,
    [DynatraceHealthSourceFieldNames.RISK_CATEGORY]: true
  })
  const errors = validateMapping(formik.values, createdMetrics, selectedMetricIndex, getString, mappedMetrics)
  if (Object.keys(errors).length > 0) {
    formik.validateForm()
    return
  }
  const updatedMetric = formik.values
  Eif (updatedMetric.metricName) {
    mappedMetrics.set(selectedMetric, updatedMetric)
  }
  const updatedValues = { ...formik.values, customMetrics: updatedMetric.showCustomMetric ? mappedMetrics : new Map() }
  onSubmit(updatedValues)
}
 
export const defaultDynatraceCustomMetric = (
  getString: (key: StringKeys) => string
): InitDynatraceCustomMetricInterface => {
  return {
    metricSelector: '',
    sli: false,
    healthScore: false,
    continuousVerification: false,
    identifier: getString('cv.healthSource.connectors.Dynatrace.defaultMetricName').split(' ').join('_'),
    metricName: getString('cv.healthSource.connectors.Dynatrace.defaultMetricName'),
    isNew: true,
    groupName: { label: '', value: '' }
  }
}