All files / modules/85-cv/pages/health-source/connectors/PrometheusHealthSource/components/PrometheusRiskProfile PrometheusRiskProfile.tsx

96.77% Statements 30/31
85.71% Branches 24/28
83.33% Functions 5/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              1x 1x 1x 1x 1x   1x 1x 1x 1x 1x             1x 3x 3x 3x 3x 3x 3x 3x 1x 1x       3x 3x       3x 3x 1x       4x                   2x 1x                 3x                                            
/*
 * 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 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 '../../../GCOMetricsHealthSource/GCOMetricsHealthSource.utils'
import { PrometheusMonitoringSourceFieldNames } from '../../PrometheusHealthSource.constants'
import css from './PrometheusRiskProfile.module.scss'
 
interface PrometheusRiskProfileProps {
  metricPackResponse: ReturnType<typeof useGetMetricPacks>
  labelNamesResponse: ReturnType<typeof useGetLabelNames>
}
 
export function PrometheusRiskProfile(props: PrometheusRiskProfileProps): JSX.Element {
  const { metricPackResponse, labelNamesResponse } = 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 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={PrometheusMonitoringSourceFieldNames.RISK_CATEGORY}
        items={metricPackOptions}
      />
    )
  }
 
  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={PrometheusMonitoringSourceFieldNames.HIGHER_BASELINE_DEVIATION}
        />
        <FormInput.CheckBox
          label={getString('cv.monitoringSources.lowerCounts')}
          name={PrometheusMonitoringSourceFieldNames.LOWER_BASELINE_DEVIATION}
        />
      </Container>
      <FormInput.Select
        name={PrometheusMonitoringSourceFieldNames.SERVICE_INSTANCE}
        label={<ServiceInstanceLabel />}
        items={transformedLabelNames}
      />
    </Container>
  )
}