All files / modules/85-cv/pages/health-source/common/SelectHealthSourceServices/components/RiskProfile RiskProfile.tsx

100% Statements 31/31
93.75% Branches 30/32
100% Functions 6/6
100% Lines 29/29

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              31x 31x 31x 31x 31x   31x 31x 31x 31x 31x                   31x 24x 23x 23x 23x 23x 23x 23x 1x 1x       23x 101x       23x 23x 1x           4x                   22x 17x                   23x                                                  
/*
 * 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, { useEffect, useMemo } from 'react'
import { Classes } from '@blueprintjs/core'
import { Container, FormInput, SelectOption, Text } from '@wings-software/uicore'
import { useToaster } from '@common/exports'
import { useStrings } from 'framework/strings'
import type { useGetMetricPacks, useGetLabelNames } from 'services/cv'
import { ServiceInstanceLabel } from '@cv/pages/health-source/common/ServiceInstanceLabel/ServiceInstanceLabel'
import { getErrorMessage } from '@cv/utils/CommonUtils'
import { getRiskCategoryOptions } from './RiskProfile.utils'
import { FieldNames } from './RiskProfile.constant'
import css from './RiskProfile.module.scss'
 
interface RiskProfileProps {
  metricPackResponse: ReturnType<typeof useGetMetricPacks>
  labelNamesResponse?: ReturnType<typeof useGetLabelNames>
  continuousVerificationEnabled?: boolean
  serviceInstance?: string
  riskCategory?: string
}
 
export function RiskProfile(props: RiskProfileProps): JSX.Element {
  const { metricPackResponse, labelNamesResponse, continuousVerificationEnabled, serviceInstance, riskCategory } = props
  const { error, loading, data } = metricPackResponse
  const { getString } = useStrings()
  const { showError, clear } = useToaster()
  const metricPackOptions = useMemo(() => getRiskCategoryOptions(data?.resource), [data])
  useEffect(() => {
    if (error) {
      clear()
      showError(getErrorMessage(error), 7000)
    }
  })
 
  const transformedLabelNames: SelectOption[] = useMemo(
    () => labelNamesResponse?.data?.data?.map(label => ({ label, value: label })) || [],
    [labelNamesResponse]
  )
 
  let metricPackContent: React.ReactNode = <Container />
  if (loading) {
    metricPackContent = (
      <Container>
        <Text tooltipProps={{ dataTooltipId: 'riskProfileBaselineDeviation' }} className={css.groupLabel}>
          {getString('cv.monitoringSources.baselineDeviation')}
        </Text>
        {[1, 2, 3, 4].map(val => (
          <Container
            key={val}
            width={150}
            height={15}
            style={{ marginBottom: 'var(--spacing-small)' }}
            className={Classes.SKELETON}
          />
        ))}
      </Container>
    )
  } else if (metricPackOptions?.length) {
    metricPackContent = (
      <FormInput.RadioGroup
        label={getString('cv.monitoringSources.riskCategoryLabel')}
        name={FieldNames.RISK_CATEGORY}
        items={metricPackOptions}
        key={riskCategory}
      />
    )
  }
 
  return (
    <Container className={css.main}>
      {metricPackContent}
      <Container className={css.checkBoxGroup}>
        <Text className={css.groupLabel}>{getString('cv.monitoringSources.baselineDeviation')}</Text>
        <FormInput.CheckBox
          label={getString('cv.monitoringSources.higherCounts')}
          name={FieldNames.HIGHER_BASELINE_DEVIATION}
        />
        <FormInput.CheckBox
          label={getString('cv.monitoringSources.lowerCounts')}
          name={FieldNames.LOWER_BASELINE_DEVIATION}
        />
      </Container>
      {continuousVerificationEnabled ? (
        <FormInput.Select
          name={FieldNames.SERVICE_INSTANCE}
          label={<ServiceInstanceLabel />}
          items={transformedLabelNames}
          value={serviceInstance ? { label: serviceInstance, value: serviceInstance } : undefined}
        />
      ) : null}
    </Container>
  )
}