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

97.14% Statements 34/35
65.85% Branches 27/41
100% Functions 8/8
96.97% Lines 32/33

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              1x 1x 1x 1x 1x             1x 1x 1x   1x 1x 1x             1x 2x 4x   4x                 1x 1x                           1x 1x 1x 1x 1x       1x 1x               1x 2x 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 } from 'react'
import { Container, Text } from '@wings-software/uicore'
import { Link, useParams } from 'react-router-dom'
import routes from '@common/RouteDefinitions'
import {
  Maybe,
  PerspectiveTrendStats,
  QlceViewAggregateOperation,
  StatsInfo,
  useFetchPerspectiveDetailsSummaryQuery
} from 'services/ce/services'
import { useStrings } from 'framework/strings'
import { getGMTEndDateTime, getGMTStartDateTime } from '@ce/utils/momentUtils'
import { getTimeFilters } from '@ce/utils/perspectiveUtils'
import type { TimeRange } from '@ce/pages/overview/OverviewPage'
import { CE_COLOR_CONST } from '../CEChart/CEChartOptions'
import { EfficiencyScore, LEGEND_LIMIT, ListType, Loader, Stats, TableList, VerticalLayout } from './OverviewPageLayout'
import css from './OverviewPage.module.scss'
 
interface ClusterCostBreakdownProps {
  timeRange: TimeRange
  defaultClusterPerspectiveId: string | null
}
 
const transformClusterCost = (data: Maybe<StatsInfo>[] = []): Stats[] => {
  return data
    .filter(d => d != null)
    .map((d, idx) => {
      return {
        label: d?.statsLabel as string,
        value: d?.value,
        trend: d?.statsTrend,
        legendColor: CE_COLOR_CONST[idx % CE_COLOR_CONST.length]
      }
    })
}
 
const useClusterCostBreakdown = (timeRange: TimeRange) => {
  const [summaryResult] = useFetchPerspectiveDetailsSummaryQuery({
    variables: {
      isClusterQuery: true,
      aggregateFunction: [
        { operationType: QlceViewAggregateOperation.Sum, columnName: 'billingamount' },
        { operationType: QlceViewAggregateOperation.Sum, columnName: 'actualidlecost' },
        { operationType: QlceViewAggregateOperation.Sum, columnName: 'unallocatedcost' },
        { operationType: QlceViewAggregateOperation.Max, columnName: 'startTime' },
        { operationType: QlceViewAggregateOperation.Min, columnName: 'startTime' }
      ],
      filters: [...getTimeFilters(getGMTStartDateTime(timeRange.from), getGMTEndDateTime(timeRange.to))]
    }
  })
 
  const { data: summaryData, fetching: summaryFetching } = summaryResult
  const cloudCost = useMemo(() => (summaryData?.perspectiveTrendStats || {}) as PerspectiveTrendStats, [summaryData])
  const totalCost = useMemo(() => transformClusterCost([cloudCost.cost]), [cloudCost])
  const chartData = useMemo(
    () => transformClusterCost([cloudCost.idleCost, cloudCost.utilizedCost, cloudCost.unallocatedCost]),
    [cloudCost]
  )
 
  const { statsValue = 0, statsTrend = 0 } = cloudCost.efficiencyScoreStats || {}
  return {
    fetching: summaryFetching,
    chartData,
    totalCost: totalCost[0],
    effciency: { statsValue, statsTrend }
  }
}
 
const OverviewClusterCostBreakdown = (props: ClusterCostBreakdownProps) => {
  const { getString } = useStrings()
  const { accountId } = useParams<{ accountId: string }>()
  const { timeRange, defaultClusterPerspectiveId } = props
  const {
    fetching,
    chartData,
    totalCost,
    effciency: { statsValue, statsTrend }
  } = useClusterCostBreakdown(timeRange)
 
  Iif (fetching) {
    return <Loader />
  }
 
  return (
    <div className={css.clusterCost}>
      <VerticalLayout
        title={getString('ce.overview.cardtitles.clusterBreakdown')}
        chartData={chartData}
        totalCost={totalCost}
        seeAll={
          defaultClusterPerspectiveId && (
            <Link
              to={routes.toPerspectiveDetails({
                accountId: accountId,
                perspectiveId: defaultClusterPerspectiveId,
                perspectiveName: defaultClusterPerspectiveId
              })}
            >
              <Text inline color="primary7">
                {getString('ce.overview.seeAll')}
              </Text>
            </Link>
          )
        }
        footer={
          <div>
            <Text tooltipProps={{ dataTooltipId: 'overviewCostBreakdown' }}>Cost breakdown</Text>
            <div className={css.breakdown}>
              <Container padding={{ top: 'medium' }}>
                <TableList data={chartData.slice(0, LEGEND_LIMIT)} type={ListType.KEY_VALUE} classNames={css.rowGap8} />
              </Container>
              <EfficiencyScore
                score={!isNaN(statsValue as number) && totalCost?.value > 0 ? Number(statsValue) : 0}
                trend={totalCost?.value > 0 ? statsTrend : 0}
              />
            </div>
          </div>
        }
      />
    </div>
  )
}
 
export default OverviewClusterCostBreakdown