All files / modules/85-cv/pages/health-source/connectors/DatadogLogsHealthSource DatadogLogsHealthSource.utils.ts

70.45% Statements 31/44
61.61% Branches 69/112
66.67% Functions 6/9
70.45% Lines 31/44

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                              24x 24x   24x   24x       9x             24x           1x     1x         1x 1x       1x 1x   1x     24x 3x 3x     3x                     3x                 3x 6x 6x                       3x     24x                                                               24x           16x                           16x 32x 16x   16x     16x         16x    
/*
 * 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 {
  DatadogLogsHealthSpec,
  DatadogLogsInfo,
  DatadogLogsSetupSource,
  SelectedAndMappedMetrics,
  UpdateSelectedMetricsMap
} from '@cv/pages/health-source/connectors/DatadogLogsHealthSource/DatadogLogsHealthSource.type'
import type { UpdatedHealthSource } from '@cv/pages/health-source/HealthSourceDrawer/HealthSourceDrawerContent.types'
import { HealthSourceTypes } from '@cv/pages/health-source/types'
import { DatadogProduct } from '@cv/pages/health-source/connectors/DatadogMetricsHealthSource/DatadogMetricsHealthSource.utils'
import type { UseStringsReturn } from 'framework/strings'
import { MapDatadogLogsFieldNames } from '@cv/pages/health-source/connectors/DatadogLogsHealthSource/components/DatadogLogsMapToService.constants'
 
export function initializeSelectedMetricsMap(
  defaultSelectedMetricName: string,
  logsDefinitions?: Map<string, DatadogLogsInfo>
): SelectedAndMappedMetrics {
  return {
    selectedMetric: Array.from(logsDefinitions?.keys() || [])?.[0] || defaultSelectedMetricName,
    mappedMetrics:
      logsDefinitions || new Map([[defaultSelectedMetricName, { metricName: defaultSelectedMetricName, query: '' }]])
  }
}
 
export function updateSelectedMetricsMap({
  updatedMetric,
  oldMetric,
  mappedMetrics,
  formikProps
}: UpdateSelectedMetricsMap): SelectedAndMappedMetrics {
  const updatedMap = new Map(mappedMetrics)
 
  // in the case where user updates metric name, update the key for current value
  Iif (oldMetric !== formikProps.values?.metricName) {
    updatedMap.delete(oldMetric)
  }
 
  // if newly created metric create form object
  Eif (!updatedMap.has(updatedMetric)) {
    updatedMap.set(updatedMetric, { metricName: updatedMetric, query: '' })
  }
 
  // update map with current form data
  Eif (formikProps.values?.metricName) {
    updatedMap.set(formikProps.values.metricName, formikProps.values as DatadogLogsInfo)
  }
  return { selectedMetric: updatedMetric, mappedMetrics: updatedMap }
}
 
export function transformDatadogHealthSourceToDatadogLogsSetupSource(sourceData: any): DatadogLogsSetupSource {
  const existingHealthSource: UpdatedHealthSource = sourceData?.healthSourceList?.find(
    (source: UpdatedHealthSource) => source.name === sourceData.healthSourceName
  )
 
  Iif (!existingHealthSource) {
    return {
      isEdit: false,
      healthSourceIdentifier: sourceData.healthSourceIdentifier,
      logsDefinitions: new Map<string, DatadogLogsInfo>(),
      healthSourceName: sourceData.healthSourceName,
      connectorRef: sourceData.connectorRef,
      product: { label: DatadogProduct.CLOUD_LOGS, value: DatadogProduct.CLOUD_LOGS }
    }
  }
 
  const setupSource: DatadogLogsSetupSource = {
    isEdit: sourceData.isEdit,
    logsDefinitions: new Map(),
    healthSourceIdentifier: sourceData.healthSourceIdentifier,
    healthSourceName: sourceData.healthSourceName,
    product: sourceData.product,
    connectorRef: sourceData.connectorRef
  }
 
  for (const logQueryDefinition of (existingHealthSource?.spec as DatadogLogsHealthSpec)?.queries || []) {
    Eif (logQueryDefinition?.name) {
      setupSource.logsDefinitions.set(logQueryDefinition.name, {
        metricName: logQueryDefinition.name,
        query: logQueryDefinition.query || '',
        serviceInstanceIdentifierTag: logQueryDefinition.serviceInstanceIdentifier,
        indexes:
          logQueryDefinition.indexes?.map(logIndex => {
            return { value: logIndex, label: logIndex }
          }) || []
      })
    }
  }
 
  return setupSource
}
 
export function transformDatadogLogsSetupSourceToHealthSource(
  setupSource: DatadogLogsSetupSource
): UpdatedHealthSource {
  const dsConfig: UpdatedHealthSource = {
    type: HealthSourceTypes.DatadogLog as UpdatedHealthSource['type'],
    identifier: setupSource.healthSourceIdentifier,
    name: setupSource.healthSourceName,
    spec: {
      connectorRef: setupSource?.connectorRef,
      feature: DatadogProduct.CLOUD_LOGS,
      queries: []
    }
  }
 
  for (const entry of setupSource.logsDefinitions.entries()) {
    const { metricName, query, serviceInstanceIdentifierTag, indexes }: DatadogLogsInfo = entry[1]
 
    if (!metricName || !query) {
      continue
    }
 
    const logsHealthSpec = dsConfig.spec as DatadogLogsHealthSpec
    logsHealthSpec.queries.push({
      query,
      name: metricName,
      serviceInstanceIdentifier: serviceInstanceIdentifierTag,
      indexes: indexes?.map(logIndexOption => logIndexOption.value as string) || []
    })
  }
  return dsConfig
}
 
export function validateMappings(
  getString: UseStringsReturn['getString'],
  createdMetrics: string[],
  selectedMetricIndex: number,
  values?: DatadogLogsInfo
): { [fieldName: string]: string } {
  const requiredFieldErrors = {
    ...(!values?.metricName && {
      [MapDatadogLogsFieldNames.METRIC_NAME]: getString('cv.monitoringSources.queryNameValidation')
    }),
    ...(!values?.query && {
      [MapDatadogLogsFieldNames.QUERY]: getString('cv.monitoringSources.gco.manualInputQueryModal.validation.query')
    }),
    ...(!values?.serviceInstanceIdentifierTag && {
      [MapDatadogLogsFieldNames.SERVICE_INSTANCE_IDENTIFIER_TAG]: getString(
        'cv.monitoringSources.gcoLogs.validation.serviceInstance'
      )
    })
  }
 
  const duplicateNames = createdMetrics?.filter((name, index) => {
    if (index === selectedMetricIndex) {
      return false
    }
    return name === values?.metricName
  })
 
  Iif (values?.metricName && duplicateNames.length) {
    requiredFieldErrors[MapDatadogLogsFieldNames.METRIC_NAME] = getString(
      'cv.monitoringSources.gcoLogs.validation.queryNameUnique'
    )
  }
  return requiredFieldErrors
}