All files / modules/75-ce/components/CloudCostInsightChart CloudCostInsightChart.tsx

88.89% Statements 24/27
83.33% Branches 10/12
75% Functions 3/4
88.89% Lines 24/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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175              7x   7x 7x 7x                 7x   7x 7x 7x 7x 7x                                                                                                                                             7x                       11x   7x   7x               7x             7x 1x             6x 2x                           4x                               7x   7x   7x 1x    
/*
 * 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, { forwardRef, useMemo } from 'react'
import type Highcharts from 'highcharts/highcharts'
import { isEqual, pick } from 'lodash-es'
import cx from 'classnames'
import { Container, Icon } from '@wings-software/uicore'
import type {
  PerspectiveTimeSeriesData,
  TimeSeriesDataPoints,
  QlceViewTimeGroupType,
  Maybe
} from 'services/ce/services'
 
import type { CCM_CHART_TYPES } from '@ce/constants'
import { CCM_PAGE_TYPE } from '@ce/types'
import type { PerspectiveAnomalyData } from 'services/ce'
import Chart from './Chart'
import WorkloadDetailsChart from './WorkloadDetailsChart'
import { ResourceType, chartConfigMapping, chartOptionsMapping } from './ChartConfigs'
import { transformTimeSeriesData, ChartConfigType } from './chartUtils'
import css from './CloudCostInsightChart.module.scss'
 
interface CloudCostInsightChartProps {
  fetching: boolean
  errors?: any[] | null
  data: Maybe<PerspectiveTimeSeriesData>
  columnSequence: string[]
  chartType: CCM_CHART_TYPES
  aggregation: QlceViewTimeGroupType
  xAxisPointCount: number
  setFilterUsingChartClick?: (value: string) => void
  showLegends?: boolean
  pageType?: CCM_PAGE_TYPE
  anomaliesCountData?: PerspectiveAnomalyData[]
}
 
/* istanbul ignore next */
function getChartList({
  data,
  pageType,
  columnSequence
}: {
  data: Maybe<PerspectiveTimeSeriesData>
  pageType?: CCM_PAGE_TYPE
  columnSequence: string[]
}): ChartConfigType[][] {
  if (!data) {
    return []
  }
  if (pageType === CCM_PAGE_TYPE.Workload || pageType === CCM_PAGE_TYPE.Node) {
    const cpuChart: any[] = []
    const memoryChart: any[] = []
 
    Object.keys(data).forEach(chartKey => {
      const key = chartKey as keyof PerspectiveTimeSeriesData
      const chartData = data[key] as Array<TimeSeriesDataPoints>
      if (!Array.isArray(chartData)) return
      const transformedSeries = transformTimeSeriesData(chartData)
 
      const { chartType, resource } = chartConfigMapping[chartKey] || {}
 
      const chartSeries = transformedSeries.map(chart => {
        const chartName = chart.name || 'DEFAULT'
 
        return Object.assign({}, chart, chartOptionsMapping[chartType][chartName], {
          visible: true,
          showInLegend: true,
          chartSeries: chartKey
        })
      })
 
      if (resource === ResourceType.CPU) {
        cpuChart.push(...chartSeries)
      }
      if (resource === ResourceType.MEMORY) {
        memoryChart.push(...chartSeries)
      }
    })
 
    const series: any[] = []
 
    cpuChart.length && series.push(cpuChart)
    memoryChart.length && series.push(memoryChart)
    return series
  }
 
  const defBillingData = (data?.stats as TimeSeriesDataPoints[]) || null
  const defBillingChartData = defBillingData && transformTimeSeriesData(defBillingData, columnSequence)
  return [defBillingChartData]
}
 
const CloudCostInsightChart = forwardRef((props: CloudCostInsightChartProps, ref: React.Ref<Highcharts.Chart>) => {
  const {
    data,
    chartType,
    aggregation,
    xAxisPointCount,
    setFilterUsingChartClick,
    showLegends,
    pageType,
    fetching,
    columnSequence,
    anomaliesCountData
  } = props
 
  const chartListData = useMemo(
    () =>
      getChartList({
        data,
        pageType,
        columnSequence
      }),
    [data, columnSequence]
  )
 
  const setChartRef: (chart: Highcharts.Chart) => void = chart => {
    const chartRef = ref as { current: Highcharts.Chart }
    if (chartRef) {
      chartRef.current = chart
    }
  }
 
  if (fetching) {
    return (
      <Container className={cx(css.chartMainContainer, css.loadingContainer)} data-testid="loader">
        <Icon name="spinner" size={28} color="blue500" />
      </Container>
    )
  }
 
  if (pageType === CCM_PAGE_TYPE.Workload || pageType === CCM_PAGE_TYPE.Node) {
    return (
      <Container className={css.chartMainContainer}>
        <WorkloadDetailsChart
          data={chartListData}
          aggregation={aggregation}
          chartType={chartType}
          onLoad={setChartRef}
          xAxisPointCount={xAxisPointCount}
          showLegends={showLegends || false}
        />
      </Container>
    )
  }
 
  return (
    <div>
      <Chart
        data={chartListData}
        aggregation={aggregation}
        chartType={chartType}
        onLoad={setChartRef}
        xAxisPointCount={xAxisPointCount}
        setFilterUsingChartClick={setFilterUsingChartClick}
        showLegends={showLegends || false}
        anomaliesCountData={anomaliesCountData}
      />
    </div>
  )
})
 
CloudCostInsightChart.displayName = 'CloudCostInsightChart'
 
const propsToMonitor = ['data', 'fetching', 'errors', 'columnSequence', 'chartType', 'anomaliesCountData']
 
export default React.memo(CloudCostInsightChart, (prevProps, nextProps) => {
  return isEqual(pick(prevProps, propsToMonitor), pick(nextProps, propsToMonitor))
})