All files / modules/85-cv/pages/health-source/common/utils HealthSource.utils.ts

100% Statements 52/52
82.64% Branches 119/144
100% Functions 10/10
100% Lines 46/46

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              36x   36x             36x 4x 4x 4x 4x     36x         3x 3x 3x 2x       3x     36x             41x 41x 2x   39x 34x 34x   34x 1x           41x     36x               41x 41x     41x     6x 3x   3x     41x 1x       41x     36x                           5x 5x 5x   5x 2x   5x 1x     5x   5x           5x                           36x     3x                                      
/*
 * 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 } from 'lodash-es'
import type { StringKeys } from 'framework/strings'
import { HealthSourceFieldNames } from '@cv/pages/health-source/common/utils/HealthSource.constants'
import type { MetricPackDTO, RiskProfile } from 'services/cv'
import type {
  BaseHealthSourceMetricDefinition,
  BaseHealthSourceMetricInfo
} from '@cv/pages/health-source/common/utils/HealthSource.types'
 
export const convertMetricPackToMetricData = (value?: MetricPackDTO[]): { [key: string]: boolean } => {
  const dataObject: { [key: string]: boolean } = {}
  const metricList: MetricPackDTO[] = value || []
  metricList.forEach((i: MetricPackDTO) => (dataObject[i.identifier as string] = true))
  return dataObject
}
 
export const validateMetricPackData = (
  metricData: { [key: string]: boolean },
  getString: (key: StringKeys) => string,
  errors: any
) => {
  const errorsToReturn = cloneDeep(errors)
  const metricValueList = Object.values(metricData).filter(val => val)
  if (!metricValueList.length) {
    errorsToReturn[HealthSourceFieldNames.METRIC_DATA] = getString(
      'cv.monitoringSources.appD.validations.selectMetricPack'
    )
  }
  return errorsToReturn
}
 
export const validateAssignComponent = (
  isAssignComponentValid: boolean,
  errors: any,
  getString: (key: StringKeys) => string,
  values: any,
  isRiskCategoryValid: boolean
): ((key: string | boolean | string[]) => string) => {
  const assignErrors = cloneDeep(errors)
  if (!isAssignComponentValid) {
    assignErrors['sli'] = getString('cv.monitoringSources.gco.mapMetricsToServicesPage.validation.baseline')
  } else {
    if (values.continuousVerification || values.healthScore) {
      Eif (!values.lowerBaselineDeviation && !values.higherBaselineDeviation) {
        assignErrors['lowerBaselineDeviation'] = getString('cv.monitoringSources.prometheus.validation.deviation')
      }
      if (!isRiskCategoryValid) {
        assignErrors['riskCategory'] = getString(
          'cv.monitoringSources.gco.mapMetricsToServicesPage.validation.riskCategory'
        )
      }
    }
  }
  return assignErrors
}
 
export function validateIdentifier<T extends BaseHealthSourceMetricInfo>(
  values: BaseHealthSourceMetricInfo,
  createdMetrics: string[],
  selectedMetricIndex: number,
  errors: any,
  getString: (key: StringKeys) => string,
  mappedMetrics?: Map<string, T>
): (key: string) => string {
  const errorsWithIdentifier = cloneDeep(errors)
  const identifiers = createdMetrics.map(metricName => mappedMetrics?.get(metricName)?.identifier)
 
  const duplicateIdentifier =
    identifiers.length < 2
      ? []
      : identifiers?.filter((identifier, index) => {
          if (index === selectedMetricIndex) {
            return false
          }
          return identifier === values.identifier
        })
 
  if (values.identifier && duplicateIdentifier.length) {
    errorsWithIdentifier[HealthSourceFieldNames.IDENTIFIER] = getString(
      'cv.monitoringSources.prometheus.validation.metricIdentifierUnique'
    )
  }
  return errorsWithIdentifier
}
 
export function mapCommonMetricInfoToCommonMetricDefinition(
  baseMetricInfo: BaseHealthSourceMetricInfo
): BaseHealthSourceMetricDefinition {
  const {
    identifier = '',
    metricName = '',
    groupName,
    riskCategory,
    lowerBaselineDeviation,
    higherBaselineDeviation,
    sli,
    continuousVerification,
    healthScore,
    isManualQuery
  } = baseMetricInfo
  const [category, metricType] = riskCategory?.split('/') || []
  const thresholdTypes: RiskProfile['thresholdTypes'] = []
 
  if (lowerBaselineDeviation) {
    thresholdTypes.push('ACT_WHEN_LOWER')
  }
  if (higherBaselineDeviation) {
    thresholdTypes.push('ACT_WHEN_HIGHER')
  }
 
  const ifOnlySliIsSelected = Boolean(sli) && !(Boolean(healthScore) || Boolean(continuousVerification))
 
  const riskProfile: any = {
    category: category,
    metricType: metricType,
    thresholdTypes
  }
 
  return {
    identifier: identifier,
    metricName,
    groupName: groupName?.value as string,
    sli: { enabled: Boolean(sli) },
    analysis: {
      riskProfile: ifOnlySliIsSelected ? {} : riskProfile,
      liveMonitoring: { enabled: Boolean(healthScore) },
      deploymentVerification: { enabled: Boolean(continuousVerification) }
    },
    isManualQuery: isManualQuery
  }
}
 
export function mapCommonMetricDefinitionToCommonMetricInfo(
  metricDefinition: BaseHealthSourceMetricDefinition
): BaseHealthSourceMetricInfo {
  return {
    identifier: metricDefinition.identifier,
    metricName: metricDefinition.metricName,
    riskCategory:
      metricDefinition.analysis?.riskProfile?.category && metricDefinition.analysis?.riskProfile?.metricType
        ? `${metricDefinition.analysis?.riskProfile?.category}/${metricDefinition.analysis?.riskProfile?.metricType}`
        : '',
    lowerBaselineDeviation: metricDefinition.analysis?.riskProfile?.thresholdTypes?.includes('ACT_WHEN_LOWER') || false,
    higherBaselineDeviation:
      metricDefinition.analysis?.riskProfile?.thresholdTypes?.includes('ACT_WHEN_HIGHER') || false,
    groupName: metricDefinition.groupName
      ? { label: metricDefinition.groupName, value: metricDefinition.groupName }
      : undefined,
    continuousVerification: metricDefinition.analysis?.deploymentVerification?.enabled,
    healthScore: metricDefinition.analysis?.liveMonitoring?.enabled,
    sli: metricDefinition.sli?.enabled,
    isManualQuery: metricDefinition.isManualQuery
  }
}