All files / modules/85-cv/pages/slos/components/SLOTargetChart SLOTargetChart.tsx

97.5% Statements 39/40
83.72% Branches 36/43
87.5% Functions 7/8
100% Lines 37/37

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              6x 6x   6x 6x 6x 6x 6x 6x 6x     6x           26x   26x             26x                 6x                       28x   28x 19x       28x 21x     28x 1x             27x 1x       1x         26x                   6x 37x 37x                       37x     37x             37x                   29x 27x 1x       28x     8x     6x  
/*
 * 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, useEffect } from 'react'
import { merge } from 'lodash-es'
import type Highcharts from 'highcharts'
import { Container, Icon, PageError, Text } from '@wings-software/uicore'
import { FontVariation, Color } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import { TimeSeriesAreaChart } from '@common/components'
import { SLIMetricTypes } from '@cv/pages/slos/components/CVCreateSLO/CVCreateSLO.types'
import { getDefaultChartOptions } from './SLOTargetChart.utils'
import { convertServiceLevelIndicatorToSLIFormData } from '../CVCreateSLO/CVCreateSLO.utils'
import type { SLOTargetChartProps, SLOTargetChartWithAPIGetSliGraphProps } from './SLOTargetChart.types'
 
export const SLOTargetChart: React.FC<SLOTargetChartProps> = ({
  topLabel,
  bottomLabel,
  dataPoints,
  customChartOptions
}) => {
  const finalChartOptions = useMemo(() => merge(getDefaultChartOptions(), customChartOptions), [customChartOptions])
 
  const seriesData: Omit<Highcharts.SeriesColumnOptions, 'type'>[] = [
    {
      data: dataPoints,
      showInLegend: false
    }
  ]
 
  return (
    <div>
      {topLabel}
      <TimeSeriesAreaChart customChartOptions={finalChartOptions} seriesData={seriesData} />
      {bottomLabel}
    </div>
  )
}
 
const SLOTargetChartWithAPIGetSliGraph: React.FC<SLOTargetChartWithAPIGetSliGraphProps> = ({
  topLabel,
  bottomLabel,
  customChartOptions,
  serviceLevelIndicator,
  monitoredServiceIdentifier,
  debounceFetchSliGraphData,
  sliGraphData,
  loading,
  error,
  retryOnError
}) => {
  const sliFormData = convertServiceLevelIndicatorToSLIFormData(serviceLevelIndicator)
 
  const dataPoints = useMemo(
    () => sliGraphData?.dataPoints?.map(point => [Number(point.timeStamp) || 0, Number(point.value) || 0]),
    [sliGraphData?.dataPoints]
  )
 
  useEffect(() => {
    debounceFetchSliGraphData?.(serviceLevelIndicator, monitoredServiceIdentifier)
  }, [...Object.values(sliFormData)])
 
  if (loading) {
    return (
      <Container flex={{ justifyContent: 'center' }} height="100%">
        <Icon name="steps-spinner" color={Color.GREY_400} size={30} />
      </Container>
    )
  }
 
  if (error) {
    return (
      <PageError
        width={400}
        message={error}
        onClick={() => retryOnError(serviceLevelIndicator, monitoredServiceIdentifier)}
      />
    )
  }
 
  return (
    <SLOTargetChart
      topLabel={topLabel}
      bottomLabel={bottomLabel}
      customChartOptions={customChartOptions}
      dataPoints={dataPoints}
    />
  )
}
 
const SLOTargetChartWrapper: React.FC<SLOTargetChartWithAPIGetSliGraphProps> = props => {
  const { getString } = useStrings()
  const { serviceLevelIndicator, monitoredServiceIdentifier } = props
 
  const {
    healthSourceRef,
    SLIType,
    SLIMetricType,
    validRequestMetric,
    objectiveValue = -1,
    objectiveComparator,
    SLIMissingDataType,
    eventType,
    goodRequestMetric
  } = convertServiceLevelIndicatorToSLIFormData(serviceLevelIndicator)
 
  const emptyState = (
    <Container flex={{ justifyContent: 'center' }} height="100%">
      <Text font={{ variation: FontVariation.BODY }} color={Color.GREY_600}>
        {getString('cv.pleaseFillTheRequiredFieldsToSeeTheSLIData')}
      </Text>
    </Container>
  )
 
  if (
    monitoredServiceIdentifier &&
    healthSourceRef &&
    SLIType &&
    SLIMetricType &&
    validRequestMetric &&
    objectiveValue >= 0 &&
    objectiveComparator &&
    SLIMissingDataType
  ) {
    if (SLIMetricType === SLIMetricTypes.RATIO) {
      if (!eventType || !goodRequestMetric || validRequestMetric === goodRequestMetric || objectiveValue > 100) {
        return emptyState
      }
    }
 
    return <SLOTargetChartWithAPIGetSliGraph {...props} />
  }
 
  return emptyState
}
 
export default SLOTargetChartWrapper