All files / modules/85-cv/pages/health-source/connectors/AppDynamics/Components/AppDCustomMetricForm AppDCustomMetricForm.tsx

92.45% Statements 49/53
71.32% Branches 92/129
66.67% Functions 6/9
92.45% Lines 49/53

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              24x 24x 24x 24x 24x   24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x   24x 24x   24x 10x 9x 9x 9x   9x               9x   9x 1x         9x 2x 1x                                       9x 9x     9x 9x 9x     9x 9x 9x         9x 1x 1x       9x 1x       9x                                                                                                                                                                                                                                                            
/*
 * 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 React, { useState, useMemo, useEffect, useContext } from 'react'
import { Container, Accordion, SelectOption, FormInput, Text, Radio, useToaster } from '@wings-software/uicore'
import { FontVariation, Color } from '@harness/design-system'
import cx from 'classnames'
import { useParams } from 'react-router-dom'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { SetupSourceTabsContext } from '@cv/components/CVSetupSourcesView/SetupSourceTabs/SetupSourceTabs'
import SelectHealthSourceServices from '@cv/pages/health-source/common/SelectHealthSourceServices/SelectHealthSourceServices'
import { GroupName } from '@cv/pages/health-source/common/GroupName/GroupName'
import { getErrorMessage } from '@cv/utils/CommonUtils'
import { SetupSourceCardHeader } from '@cv/components/CVSetupSourcesView/SetupSourceCardHeader/SetupSourceCardHeader'
import { initializeGroupNames } from '@cv/pages/health-source/common/GroupName/GroupName.utils'
import { NameId } from '@common/components/NameIdDescriptionTags/NameIdDescriptionTags'
import { useStrings } from 'framework/strings'
import { useGetMetricPacks, useGetServiceInstanceMetricPath, AppDMetricDefinitions } from 'services/cv'
import { AppDynamicsMonitoringSourceFieldNames } from '../../AppDHealthSource.constants'
import { PATHTYPE } from './AppDCustomMetricForm.constants'
import { getBasePathValue, getMetricPathValue, setServiceIntance } from './AppDCustomMetricForm.utils'
import BasePath from '../BasePath/BasePath'
import { BasePathInitValue } from '../BasePath/BasePath.constants'
import MetricChart from '../MetricChart/MetricChart'
import MetricPath from '../MetricPath/MetricPath'
import type { AppDCustomMetricFormInterface } from './AppDCustomMetricForm.types'
import css from '../../AppDHealthSource.module.scss'
import basePathStyle from '../BasePath/BasePath.module.scss'
 
export default function AppDCustomMetricForm(props: AppDCustomMetricFormInterface) {
  const { formikValues, formikSetField, mappedMetrics, selectedMetric, connectorIdentifier } = props
  const { getString } = useStrings()
  const { showError } = useToaster()
  const { accountId, orgIdentifier, projectIdentifier } = useParams<ProjectPathProps>()
 
  const metricPackResponse = useGetMetricPacks({
    queryParams: { projectIdentifier, orgIdentifier, accountId, dataSourceType: 'APP_DYNAMICS' }
  })
 
  const {
    data: serviceInsanceData,
    refetch: refetchServiceInsance,
    error: serviceInstanceError
  } = useGetServiceInstanceMetricPath({ lazy: true })
 
  useEffect(() => {
    Iif (serviceInstanceError) {
      showError(getErrorMessage(serviceInstanceError))
    }
  }, [serviceInstanceError])
 
  useEffect(() => {
    if (formikValues?.continuousVerification) {
      refetchServiceInsance({
        queryParams: {
          accountId,
          orgIdentifier,
          projectIdentifier,
          connectorIdentifier,
          appName: formikValues.appdApplication,
          baseFolder: getBasePathValue(formikValues?.basePath),
          tier: formikValues.appDTier,
          metricPath: getMetricPathValue(formikValues?.metricPath)
        }
      })
    }
  }, [
    formikValues?.continuousVerification,
    formikValues.appdApplication,
    formikValues?.basePath,
    formikValues?.metricPath
  ])
 
  useEffect(() => {
    setServiceIntance({ serviceInsanceData, formikValues, formikSetField })
  }, [serviceInsanceData, formikValues?.continuousVerification])
 
  const [appdGroupName, setAppdGroupName] = useState<SelectOption[]>(initializeGroupNames(mappedMetrics, getString))
  const basePathValue = getBasePathValue(formikValues?.basePath)
  const metricPathValue = getMetricPathValue(formikValues?.metricPath)
  const {
    sourceData: { existingMetricDetails }
  } = useContext(SetupSourceTabsContext)
  const metricDefinitions = existingMetricDetails?.spec?.metricDefinitions
  const currentSelectedMetricDetail = metricDefinitions?.find(
    (metricDefinition: AppDMetricDefinitions) =>
      metricDefinition.metricName === mappedMetrics.get(selectedMetric || '')?.metricName
  )
 
  useEffect(() => {
    Eif (formikValues.pathType === PATHTYPE.DropdownPath && formikValues.metricName === selectedMetric) {
      formikSetField(PATHTYPE.FullPath, '')
    }
  }, [formikValues.pathType, selectedMetric])
 
  const completeMetricPath = useMemo(
    () => `${basePathValue}|${formikValues.appDTier}|${metricPathValue}`.split('|').join(' | '),
    [basePathValue, formikValues.appDTier, metricPathValue]
  )
 
  return (
    <Container className={css.main}>
      <SetupSourceCardHeader
        mainHeading={getString('cv.monitoringSources.prometheus.querySpecificationsAndMappings')}
        subHeading={getString('cv.monitoringSources.prometheus.customizeQuery')}
      />
      <Container className={css.content}>
        <Accordion activeId="metricToService" className={css.accordian} allowMultiOpen>
          <Accordion.Panel
            id="metricToService"
            summary={getString('cv.monitoringSources.mapMetricsToServices')}
            details={
              <>
                <NameId
                  nameLabel={getString('cv.monitoringSources.metricNameLabel')}
                  identifierProps={{
                    inputName: AppDynamicsMonitoringSourceFieldNames.METRIC_NAME,
                    idName: AppDynamicsMonitoringSourceFieldNames.METRIC_IDENTIFIER,
                    isIdentifierEditable: Boolean(!currentSelectedMetricDetail?.identifier)
                  }}
                />
                <GroupName
                  groupNames={appdGroupName}
                  onChange={formikSetField}
                  item={formikValues?.groupName}
                  setGroupNames={setAppdGroupName}
                />
                <Text padding={{ bottom: 'medium' }} font={{ variation: FontVariation.H6 }}>
                  {getString('cv.monitoringSources.appD.appdPathTitle')}
                </Text>
                <Radio
                  padding={{ bottom: 'medium', left: 'xlarge' }}
                  label={getString('cv.healthSource.connectors.AppDynamics.metricPathType.text')}
                  checked={formikValues?.pathType === PATHTYPE.FullPath}
                  onChange={() => formikSetField('pathType', PATHTYPE.FullPath)}
                />
                <FormInput.Text
                  className={css.fullPath}
                  name={PATHTYPE.FullPath}
                  tooltipProps={{ dataTooltipId: 'appDynamicsCompletePath' }}
                  disabled={formikValues?.pathType !== PATHTYPE.FullPath}
                />
                <Radio
                  padding={{ bottom: 'medium', left: 'xlarge' }}
                  label={getString('cv.healthSource.connectors.AppDynamics.metricPathType.dropdown')}
                  checked={formikValues?.pathType === PATHTYPE.DropdownPath}
                  onChange={() => formikSetField('pathType', PATHTYPE.DropdownPath)}
                />
                <Container
                  padding={{ left: 'large' }}
                  className={cx({ [css.disabled]: formikValues?.pathType !== PATHTYPE.DropdownPath })}
                >
                  <Text padding={{ bottom: 'medium' }} tooltipProps={{ dataTooltipId: 'appDynamicsBasePath' }}>
                    {getString('cv.monitoringSources.appD.appdPathDetail')}
                  </Text>
                  <BasePath
                    basePathValue={formikValues?.basePath || BasePathInitValue}
                    onChange={formikSetField}
                    appName={formikValues.appdApplication}
                    connectorIdentifier={connectorIdentifier}
                  />
                  {basePathValue && formikValues.appDTier && (
                    <MetricPath
                      onChange={formikSetField}
                      metricPathValue={formikValues?.metricPath}
                      connectorIdentifier={connectorIdentifier}
                      baseFolder={basePathValue}
                      appName={formikValues.appdApplication}
                      tier={formikValues.appDTier}
                    />
                  )}
                  <Container className={basePathStyle.basePathContainer}>
                    <Text
                      font={{ variation: FontVariation.SMALL_BOLD }}
                      color={Color.GREY_400}
                      className={basePathStyle.basePathLabel}
                    >
                      {getString('cv.healthSource.connectors.AppDynamics.selectedPathLabel')}
                    </Text>
                    <Text className={basePathStyle.basePathValue} font={{ variation: FontVariation.SMALL_SEMI }}>
                      {completeMetricPath}
                    </Text>
                  </Container>
                </Container>
              </>
            }
          />
          <Accordion.Panel
            id="metricChart"
            summary={getString('cv.monitoringSources.prometheus.chartAndRecords')}
            details={
              <>
                <MetricChart
                  connectorIdentifier={connectorIdentifier}
                  appName={formikValues.appdApplication}
                  tier={formikValues.appDTier}
                  baseFolder={getBasePathValue(formikValues?.basePath)}
                  metricPath={getMetricPathValue(formikValues?.metricPath)}
                  fullPath={formikValues?.pathType === PATHTYPE.FullPath ? formikValues.fullPath : undefined}
                />
              </>
            }
          />
          <Accordion.Panel
            id="riskProfile"
            summary={getString('cv.monitoringSources.assign')}
            details={
              <>
                <SelectHealthSourceServices
                  values={{
                    sli: !!formikValues?.sli,
                    healthScore: !!formikValues?.healthScore,
                    continuousVerification: !!formikValues?.continuousVerification,
                    riskCategory: formikValues?.riskCategory
                  }}
                  metricPackResponse={metricPackResponse}
                  hideServiceIdentifier
                />
              </>
            }
          />
        </Accordion>
      </Container>
    </Container>
  )
}