All files / modules/85-cv/pages/health-source/connectors/NewRelic NewRelicHealthSourceContainer.util.ts

86.21% Statements 25/29
34.05% Branches 95/279
100% Functions 5/5
85.19% Lines 23/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              25x 25x     25x     25x 2x             2x 1x                               1x   1x 1x   1x     1x       1x                                                 2x                   2x           2x         25x 4x 2x             4x   4x                         4x                                                       4x    
/*
 * 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 { isEmpty } from 'lodash-es'
import { v4 as uuid } from 'uuid'
import type { NewRelicHealthSourceSpec, NewRelicMetricDefinition, RiskProfile } from 'services/cv'
import type { UpdatedHealthSource } from '../../HealthSourceDrawer/HealthSourceDrawerContent.types'
import { HealthSourceTypes } from '../../types'
import type { NewRelicData } from './NewRelicHealthSource.types'
 
export const createNewRelicPayload = (formData: any): UpdatedHealthSource | null => {
  const specPayload = {
    applicationName: formData?.newRelicApplication?.label,
    applicationId: formData?.newRelicApplication?.value,
    metricData: formData?.metricData,
    newRelicMetricDefinitions: [] as NewRelicMetricDefinition[]
  }
 
  if (formData.showCustomMetric) {
    for (const entry of formData.mappedServicesAndEnvs.entries()) {
      const {
        metricName,
        metricIdentifier,
        groupName,
        query,
        metricValue,
        timestamp,
        timestampFormat,
        serviceInstanceIdentifier,
        sli,
        continuousVerification,
        healthScore,
        riskCategory,
        lowerBaselineDeviation,
        higherBaselineDeviation
      } = entry[1]
 
      const [category, metricType] = riskCategory?.split('/') || []
      const thresholdTypes: RiskProfile['thresholdTypes'] = []
 
      Iif (lowerBaselineDeviation) {
        thresholdTypes.push('ACT_WHEN_LOWER')
      }
      Iif (higherBaselineDeviation) {
        thresholdTypes.push('ACT_WHEN_HIGHER')
      }
 
      specPayload?.newRelicMetricDefinitions?.push({
        identifier: metricIdentifier || uuid(),
        metricName,
        groupName: groupName?.value as string,
        nrql: query,
        responseMapping: {
          metricValueJsonPath: metricValue,
          serviceInstanceJsonPath: serviceInstanceIdentifier,
          timestampFormat: timestampFormat,
          timestampJsonPath: timestamp
        },
        sli: { enabled: Boolean(sli) },
        analysis: {
          riskProfile: {
            category,
            metricType,
            thresholdTypes
          },
          liveMonitoring: { enabled: Boolean(healthScore) },
          deploymentVerification: { enabled: Boolean(continuousVerification) }
        }
      })
    }
  }
 
  return {
    name: formData.name || (formData.healthSourceName as string),
    identifier: formData.identifier || (formData.healthSourceIdentifier as string),
    type: formData.type,
    spec: {
      ...specPayload,
      feature: formData.product?.value as string,
      connectorRef: (formData?.connectorRef?.connector?.identifier as string) || (formData.connectorRef as string),
      metricPacks: Object.entries(formData?.metricData)
        .map(item => {
          return item[1]
            ? {
                identifier: item[0]
              }
            : {}
        })
        .filter(item => !isEmpty(item))
    }
  }
}
 
export const createNewRelicData = (sourceData: any): NewRelicData => {
  const payload: UpdatedHealthSource = sourceData?.healthSourceList?.find(
    (source: UpdatedHealthSource) => source.identifier === sourceData.healthSourceIdentifier
  )
 
  const {
    applicationName = '',
    applicationId = '',
    metricPacks = []
  } = (payload?.spec as NewRelicHealthSourceSpec) || {}
 
  const newRelicData = {
    name: sourceData?.healthSourceName,
    identifier: sourceData?.healthSourceIdentifier,
    connectorRef: sourceData?.connectorRef,
    isEdit: sourceData?.isEdit,
    product: sourceData?.product,
    type: HealthSourceTypes.NewRelic,
    applicationName,
    applicationId,
    metricPacks,
    mappedServicesAndEnvs: new Map()
  }
 
  for (const metricDefinition of (payload?.spec as NewRelicHealthSourceSpec)?.newRelicMetricDefinitions || []) {
    if (metricDefinition?.metricName) {
      newRelicData.mappedServicesAndEnvs.set(metricDefinition.metricName, {
        metricName: metricDefinition.metricName,
        metricIdentifier: metricDefinition.identifier,
        groupName: { label: metricDefinition.groupName || '', value: metricDefinition.groupName || '' },
 
        query: metricDefinition?.nrql,
 
        metricValue: metricDefinition?.responseMapping?.metricValueJsonPath,
        timestampFormat: metricDefinition?.responseMapping?.timestampFormat,
        timestamp: metricDefinition?.responseMapping?.timestampJsonPath,
 
        sli: metricDefinition?.sli?.enabled,
        continuousVerification: metricDefinition?.analysis?.deploymentVerification?.enabled,
        healthScore: metricDefinition?.analysis?.liveMonitoring?.enabled,
        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
      })
    }
  }
 
  return newRelicData
}