All files / modules/85-cv/pages/health-source/connectors/Dynatrace DynatraceHealthSource.tsx

96.3% Statements 26/27
33.33% Branches 2/6
85.71% Functions 6/7
96.3% Lines 26/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 153 154 155 156 157 158 159 160              24x 24x 24x 24x         24x 24x 24x 24x 24x           24x 24x   24x 24x   24x 6x 6x 6x 6x                     6x           6x 4x       6x           26x                     5x                     13x                                                                                                                 2x                                  
/*
 * 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, { useMemo, useState } from 'react'
import { Button, Formik, FormikForm } from '@wings-software/uicore'
import { PopoverInteractionKind } from '@blueprintjs/core'
import { noop } from 'lodash-es'
import type {
  DynatraceFormDataInterface,
  DynatraceHealthSourceProps
} from '@cv/pages/health-source/connectors/Dynatrace/DynatraceHealthSource.types'
import CardWithOuterTitle from '@cv/pages/health-source/common/CardWithOuterTitle/CardWithOuterTitle'
import DrawerFooter from '@cv/pages/health-source/common/DrawerFooter/DrawerFooter'
import useGroupedSideNaveHook from '@cv/hooks/GroupedSideNaveHook/useGroupedSideNaveHook'
import { useStrings } from 'framework/strings'
import {
  defaultDynatraceCustomMetric,
  mapDynatraceDataToDynatraceForm,
  onSubmitDynatraceData,
  validateMapping
} from '@cv/pages/health-source/connectors/Dynatrace/DynatraceHealthSource.utils'
import DynatraceCustomMetrics from '@cv/pages/health-source/connectors/Dynatrace/components/DynatraceCustomMetrics/DynatraceCustomMetrics'
import DynatraceMetricPacksToService from './components/DynatraceMetricPacksToService/DynatraceMetricPacksToService'
 
import CustomMetric from '../../common/CustomMetric/CustomMetric'
import css from '@cv/pages/health-source/connectors/Dynatrace/DynatraceHealthSource.module.scss'
 
export default function DynatraceHealthSource(props: DynatraceHealthSourceProps): JSX.Element {
  const { dynatraceFormData: initialPayload, connectorIdentifier, onPrevious, onSubmit } = props
  const { getString } = useStrings()
  const [dynatraceMetricData, setDynatraceMetricData] = useState<DynatraceFormDataInterface>(initialPayload)
  const [showCustomMetric, setShowCustomMetric] = useState<boolean>(!!dynatraceMetricData.customMetrics.size)
 
  const {
    createdMetrics,
    mappedMetrics,
    selectedMetric,
    groupedCreatedMetrics,
    groupedCreatedMetricsList,
    setMappedMetrics,
    setCreatedMetrics,
    setGroupedCreatedMetrics
  } = useGroupedSideNaveHook({
    defaultCustomMetricName: getString('cv.healthSource.connectors.Dynatrace.defaultMetricName'),
    initCustomMetricData: defaultDynatraceCustomMetric(getString),
    mappedServicesAndEnvs: dynatraceMetricData.customMetrics
  })
 
  const dynatraceMetricFormData = useMemo(
    () => mapDynatraceDataToDynatraceForm(dynatraceMetricData, mappedMetrics, selectedMetric, showCustomMetric),
    [dynatraceMetricData, mappedMetrics, selectedMetric, showCustomMetric]
  )
 
  return (
    <Formik<DynatraceFormDataInterface>
      onSubmit={noop}
      enableReinitialize
      formName={'dynatraceHealthSourceForm'}
      isInitialValid={(args: any) =>
        Object.keys(
          validateMapping(
            args.initialValues,
            groupedCreatedMetricsList,
            groupedCreatedMetricsList.indexOf(selectedMetric),
            getString,
            mappedMetrics
          )
        ).length === 0
      }
      validate={values => {
        return validateMapping(
          values,
          groupedCreatedMetricsList,
          groupedCreatedMetricsList.indexOf(selectedMetric),
          getString,
          mappedMetrics
        )
      }}
      initialValues={dynatraceMetricFormData}
    >
      {formik => {
        return (
          <FormikForm className={css.formFullheight}>
            <DynatraceMetricPacksToService
              connectorIdentifier={connectorIdentifier}
              dynatraceMetricData={dynatraceMetricData}
              setDynatraceMetricData={setDynatraceMetricData}
              metricValues={formik.values}
            />
            {showCustomMetric ? (
              <CustomMetric
                isValidInput={formik.isValid}
                setMappedMetrics={setMappedMetrics}
                selectedMetric={selectedMetric}
                formikValues={formik.values}
                mappedMetrics={mappedMetrics}
                createdMetrics={createdMetrics}
                groupedCreatedMetrics={groupedCreatedMetrics}
                setCreatedMetrics={setCreatedMetrics}
                setGroupedCreatedMetrics={setGroupedCreatedMetrics}
                defaultMetricName={getString('cv.healthSource.connectors.Dynatrace.defaultMetricName')}
                tooptipMessage={getString('cv.monitoringSources.gcoLogs.addQueryTooltip')}
                addFieldLabel={getString('cv.monitoringSources.addMetric')}
                initCustomForm={defaultDynatraceCustomMetric(getString)}
              >
                <DynatraceCustomMetrics
                  metricValues={formik.values}
                  formikSetField={formik.setFieldValue}
                  mappedMetrics={mappedMetrics}
                  selectedMetric={selectedMetric}
                  connectorIdentifier={connectorIdentifier}
                  selectedServiceId={(formik.values.selectedService.value as string) || ''}
                />
              </CustomMetric>
            ) : (
              formik.values.selectedService.value && (
                <CardWithOuterTitle
                  title={getString('cv.healthSource.connectors.customMetrics')}
                  dataTooltipId={'customMetricsTitle'}
                >
                  <Button
                    icon="plus"
                    minimal
                    margin={{ left: 'medium' }}
                    intent="primary"
                    tooltip={getString('cv.healthSource.connectors.customMetricsTooltip')}
                    tooltipProps={{ interactionKind: PopoverInteractionKind.HOVER_TARGET_ONLY }}
                    onClick={() => setShowCustomMetric(true)}
                  >
                    {getString('cv.monitoringSources.addMetric')}
                  </Button>
                </CardWithOuterTitle>
              )
            )}
            <DrawerFooter
              isSubmit
              onPrevious={onPrevious}
              onNext={() =>
                onSubmitDynatraceData(
                  formik,
                  mappedMetrics,
                  selectedMetric,
                  groupedCreatedMetricsList.indexOf(selectedMetric),
                  createdMetrics,
                  getString,
                  onSubmit
                )
              }
            />
          </FormikForm>
        )
      }}
    </Formik>
  )
}