All files / modules/85-cv/pages/health-source/HealthSourceDrawer/component/defineHealthSource DefineHealthSource.utils.ts

89.66% Statements 52/58
82.95% Branches 73/88
90% Functions 9/10
90.2% Lines 46/51

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              24x     24x 24x   24x 24x 24x 24x 24x 24x               24x 23x                         24x 4x 4x         24x 232x   42x             32x                     32x                     2x             32x             32x                           32x             8x                                   20x       24x         7x           7x       24x 12x 8x   12x 12x           12x     24x 17x 9x   17x   17x     24x         7x 7x 2x     7x 5x     7x    
/*
 * 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 * as Yup from 'yup'
import type { UseStringsReturn } from 'framework/strings'
import type { HealthSource } from 'services/cv'
import { Connectors } from '@connectors/constants'
import { HealthSourceTypes } from '@cv/pages/health-source/types'
import type { SelectOption } from '@pipeline/components/PipelineSteps/Steps/StepsTypes'
import { GCOProduct } from '@cv/pages/health-source/connectors/GCOLogsMonitoringSource/GoogleCloudOperationsMonitoringSourceUtils'
import { PrometheusProductNames } from '@cv/pages/health-source/connectors/PrometheusHealthSource/PrometheusHealthSource.constants'
import { DatadogProduct } from '@cv/pages/health-source/connectors/DatadogMetricsHealthSource/DatadogMetricsHealthSource.utils'
import { ErrorTrackingProductNames } from '@cv/pages/health-source/connectors/ErrorTrackingHealthSource/ErrorTrackingHealthSource.utils'
import { CustomHealthProduct } from '@cv/pages/health-source/connectors/CustomHealthSource/CustomHealthSource.constants'
import {
  NewRelicProductNames,
  ConnectorRefFieldName,
  SplunkProduct,
  DynatraceProductNames
} from './DefineHealthSource.constant'
import type { DefineHealthSourceFormInterface } from './DefineHealthSource.types'
 
export const validate = (getString: UseStringsReturn['getString']) => {
  return Yup.object().shape({
    sourceType: Yup.string().trim().required(getString('cv.onboarding.selectProductScreen.validationText.source')),
    healthSourceName: Yup.string().trim().required(getString('cv.onboarding.selectProductScreen.validationText.name')),
    product: Yup.string()
      .trim()
      .required()
      .notOneOf(['Custom Connector'], getString('cv.onboarding.selectProductScreen.validationText.product')),
    [ConnectorRefFieldName]: Yup.string()
      .nullable()
      .required(getString('cv.onboarding.selectProductScreen.validationText.connectorRef'))
  })
}
 
export const validateDuplicateIdentifier = (values: DefineHealthSourceFormInterface): any => {
  const { healthSourceIdentifier, healthSourceList } = values
  Iif (healthSourceList?.some(item => item.identifier === healthSourceIdentifier)) {
    return { healthSourceName: 'identifier already exist' }
  }
}
 
export const getFeatureOption = (type: string, getString: UseStringsReturn['getString']): SelectOption[] => {
  switch (type) {
    case Connectors.APP_DYNAMICS:
      return [
        {
          value: 'Application Monitoring',
          label: getString('cv.monitoringSources.appD.product.applicationMonitoring')
        }
      ]
    case Connectors.GCP:
      return [
        {
          value: GCOProduct.CLOUD_METRICS,
          label: getString('cv.monitoringSources.gco.product.metrics')
        },
        {
          value: GCOProduct.CLOUD_LOGS,
          label: getString('cv.monitoringSources.gco.product.logs')
        }
      ]
    case Connectors.DATADOG:
      return [
        {
          value: DatadogProduct.CLOUD_METRICS,
          label: getString('cv.monitoringSources.gco.product.metrics')
        },
        {
          value: DatadogProduct.CLOUD_LOGS,
          label: getString('cv.monitoringSources.gco.product.logs')
        }
      ]
    case HealthSourceTypes.StackdriverLog:
      return [
        {
          value: GCOProduct.CLOUD_LOGS,
          label: getString('cv.monitoringSources.gco.product.logs')
        }
      ]
    case Connectors.PROMETHEUS:
      return [
        {
          label: PrometheusProductNames.APM,
          value: getString('connectors.prometheusLabel')
        }
      ]
    case Connectors.NEW_RELIC:
      return [
        {
          value: NewRelicProductNames.APM,
          label: getString('connectors.newRelic.products.fullStackObservability')
        }
      ]
    case Connectors.DYNATRACE:
      return [
        {
          value: DynatraceProductNames.APM,
          label: getString('connectors.newRelic.products.fullStackObservability')
        }
      ]
    case Connectors.SPLUNK:
      return [
        {
          value: SplunkProduct.SPLUNK_LOGS,
          label: getString('cv.monitoringSources.gco.product.logs')
        }
      ]
    case Connectors.CUSTOM_HEALTH:
      return [
        {
          label: getString('cv.customHealthSource.customHealthMetric'),
          value: CustomHealthProduct.METRICS
        },
        {
          label: getString('cv.customHealthSource.customHealthLog'),
          value: CustomHealthProduct.LOGS
        }
      ]
    case Connectors.ERROR_TRACKING:
      return [
        {
          value: ErrorTrackingProductNames.LOGS,
          label: getString('cv.monitoringSources.gco.product.logs')
        }
      ]
    default:
      return []
  }
}
 
export function getProductBasedOnType(
  getString: UseStringsReturn['getString'],
  type?: HealthSource['type'],
  currProduct?: SelectOption
): SelectOption | undefined {
  switch (type) {
    case 'CustomHealthLog':
      return getFeatureOption(Connectors.CUSTOM_HEALTH, getString)[1]
    case 'CustomHealthMetric':
      return getFeatureOption(Connectors.CUSTOM_HEALTH, getString)[0]
    default:
      return { ...currProduct } as SelectOption
  }
}
 
export const getInitialValues = (sourceData: any, getString: UseStringsReturn['getString']): any => {
  const currentHealthSource = sourceData?.healthSourceList?.find(
    (el: any) => el?.identifier === sourceData?.healthSourceIdentifier
  )
  const selectedFeature = currentHealthSource?.spec?.feature
  const initialValues = {
    ...sourceData,
    product: selectedFeature
      ? { label: selectedFeature, value: selectedFeature }
      : getProductBasedOnType(getString, currentHealthSource?.type, sourceData?.product)
  }
  return initialValues
}
 
export const getSelectedFeature = (sourceData: any): any => {
  const currentHealthSource = sourceData?.healthSourceList?.find(
    (el: any) => el?.identifier === sourceData?.healthSourceIdentifier
  )
  const selectedFeature = currentHealthSource?.spec?.feature
 
  return selectedFeature ? { label: selectedFeature, value: selectedFeature } : { ...sourceData?.product }
}
 
export const modifyCustomHealthFeatureBasedOnFF = (
  isCustomLogEnabled: boolean,
  isCustomMetricEnabled: boolean,
  customHealthOptions: SelectOption[]
): SelectOption[] => {
  const featureOptionForConnector = []
  if (isCustomMetricEnabled) {
    featureOptionForConnector.push(customHealthOptions[0])
  }
 
  if (isCustomLogEnabled) {
    featureOptionForConnector.push(customHealthOptions[1])
  }
 
  return featureOptionForConnector
}