All files / modules/75-ce/components/OverviewPage OverviewCostByProviders.tsx

82.5% Statements 33/40
54.17% Branches 13/24
55.56% Functions 5/9
82.5% Lines 33/40

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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192              1x 1x 1x   1x 1x 1x           1x 1x 1x 1x 1x 1x             1x 1x 1x 1x 1x   1x               1x 1x 1x 1x 1x 1x     1x       1x                               1x           1x               1x 1x 1x                                                                 1x                                                                                                                                       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, { useMemo, useState } from 'react'
import moment from 'moment'
import { Container, Layout, Text } from '@wings-software/uicore'
import type { TimeRange } from '@ce/pages/overview/OverviewPage'
import { CCM_CHART_TYPES } from '@ce/constants'
import { getTimeFilters } from '@ce/utils/perspectiveUtils'
import {
  QlceViewAggregateOperation,
  QlceViewGroupByInput,
  TimeSeriesDataPoints,
  useFetchOverviewTimeSeriesQuery
} from 'services/ce/services'
import { useStrings } from 'framework/strings'
import { getGMTEndDateTime, getGMTStartDateTime } from '@ce/utils/momentUtils'
import { ChartConfigType, transformTimeSeriesData } from '../CloudCostInsightChart/chartUtils'
import CEChart from '../CEChart/CEChart'
import { ChartTypes, Loader } from './OverviewPageLayout'
import css from './OverviewPage.module.scss'
 
interface CostByProvidersProps {
  timeRange: TimeRange
  clusterDataPresent: boolean
}
 
const OverviewCostByProviders = (props: CostByProvidersProps) => {
  const { getString } = useStrings()
  const [chartObj, setChartObj] = useState<Highcharts.Chart | null>(null)
  const { timeRange, clusterDataPresent } = props
  const [chartType, setChartType] = useState(CCM_CHART_TYPES.COLUMN)
 
  const [result] = useFetchOverviewTimeSeriesQuery({
    variables: {
      aggregateFunction: [{ operationType: QlceViewAggregateOperation.Sum, columnName: 'cost' }],
      filters: [...getTimeFilters(getGMTStartDateTime(timeRange.from), getGMTEndDateTime(timeRange.to))],
      groupBy: [{ timeTruncGroupBy: { resolution: 'DAY' } } as QlceViewGroupByInput]
    }
  })
 
  const { data, fetching } = result
  const dataPoints = data?.overviewTimeSeriesStats?.data
  const chartListData = useMemo(() => {
    const points = (dataPoints as TimeSeriesDataPoints[]) || null
    const transformedPoints = points && transformTimeSeriesData(points, [])
    return [transformedPoints]
  }, [dataPoints])
 
  Iif (fetching) {
    return <Loader />
  }
 
  return (
    <div className={css.costByProviders}>
      <Layout.Vertical spacing="medium">
        <Text
          color="grey800"
          font={{ weight: 'semi-bold', size: 'medium' }}
          tooltipProps={{ dataTooltipId: 'overviewCostByProvider' }}
        >
          {getString('ce.overview.cardtitles.costByProviders')}
        </Text>
        <Layout.Horizontal style={{ justifyContent: 'space-between' }} spacing="medium">
          <Legends chartRef={chartObj as unknown as Highcharts.Chart} />
          <ChartTypes chartType={chartType} setChartType={setChartType} />
        </Layout.Horizontal>
        <Container>
          {chartListData.map((chart, idx: number) => {
            const options = getColumnChartConfig({
              data: chart,
              chartType,
              chartHeight: !clusterDataPresent ? 380 : 400,
              setChartObj
            })
            return chart ? <CEChart key={idx} options={options as any} /> : null
          })}
        </Container>
      </Layout.Vertical>
    </div>
  )
}
 
const Legends = ({ chartRef }: { chartRef: Highcharts.Chart }) => {
  Eif (!chartRef) {
    return null
  }
 
  return (
    <Container className={css.legendContainer}>
      {chartRef.series?.map(chart => {
        const chartColor: string = (chart as any).color
        return (
          <Layout.Horizontal key={chart.userOptions.name} spacing="small" style={{ alignItems: 'center' }}>
            <div
              className={css.colorBoxContainer}
              style={{
                backgroundColor: chartColor
              }}
            />
            <Text font="small" lineClamp={1}>
              {chart.userOptions.name}
            </Text>
          </Layout.Horizontal>
        )
      })}
    </Container>
  )
}
 
interface ColumnChartConfig {
  data: ChartConfigType[]
  chartType: CCM_CHART_TYPES
  chartHeight: number
  setChartObj: (value: React.SetStateAction<Highcharts.Chart | null>) => void
}
 
function getColumnChartConfig({ data, chartType, chartHeight, setChartObj }: ColumnChartConfig) {
  return {
    chart: {
      type: chartType,
      height: chartHeight,
      events: {
        load() {
          setChartObj(this as any)
        }
      }
    },
    xAxis: {
      gridLineColor: '#fff',
      type: 'datetime',
      ordinal: true,
      min: null,
      lineWidth: 0,
      // tickInterval: 24 * 3600 * 1000,
      labels: {
        formatter: function () {
          return moment(this.value).utc().format('MMM DD')
        }
      }
    } as Highcharts.XAxisOptions,
    yAxis: {
      gridLineColor: '#fff',
      type: 'logarithmic',
      tickInterval: 1,
      title: {
        text: ''
      },
      labels: {
        formatter: function () {
          return `$${this.value}`
        }
      }
    } as Highcharts.YAxisOptions,
    tooltip: {
      headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
      pointFormat:
        '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
        '<td style="padding:0"><b>${point.y:.1f}</b></td></tr>',
      footerFormat: '</table>',
      useHTML: true
    },
    plotOptions: {
      column: {
        pointPadding: 0.2,
        borderWidth: 0,
        borderRadius: 2
      },
      legend: {
        enabled: true,
        align: 'top',
        verticalAlign: 'middle',
        layout: 'vertical'
      },
      area: {
        fillOpacity: 0.1,
        marker: {
          enabled: false
        }
      }
    },
    series: data,
    colors: ['#0278d5', '#3dc7f6']
  }
}
 
export default OverviewCostByProviders