All files / modules/75-ce/components/COGatewayList COGatewayCumulativeAnalytics.tsx

100% Statements 30/30
73.24% Branches 52/71
100% Functions 7/7
100% Lines 28/28

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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250                2x 2x 2x 2x 2x 2x 2x   2x 2x 2x             60x                 1x 1x 30x 1x   1x 30x     1x 30x     1x                                                                                                                                                                     14x 12x   2x   2x 7x 7x   7x                                                                                                                                                                                                                         2x  
/*
 * 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 { ProgressBar } from '@blueprintjs/core'
import React from 'react'
import { isEmpty as _isEmpty, defaultTo as _defaultTo } from 'lodash-es'
import { Container, HarnessDocTooltip, Heading, Icon, Layout, Text } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import Highcharts from 'highcharts'
import HighchartsReact from 'highcharts-react-official'
import { useStrings } from 'framework/strings'
import type { CumulativeSavings } from 'services/lw'
import EmptyView from '@ce/images/empty-state.svg'
import { geGaugeChartOptionsWithoutLabel, getDay } from './Utils'
import css from './COGatewayCumulativeAnalytics.module.scss'
 
interface COGatewayCumulativeAnalyticsProps {
  data: CumulativeSavings | undefined
  loadingData: boolean
}
 
const toFixedDecimalNumber = (num: number, decimalPlaces = 2) => Number(num.toFixed(decimalPlaces))
 
function getStackedAreaChartOptions(
  title: string,
  categories: string[],
  yAxisText: string,
  savingsData: number[],
  spendData: number[]
): Highcharts.Options {
  let step = 1
  Eif (categories && categories.length) {
    categories = categories.map(x => getDay(x, 'YYYY-MM-DDTHH:mm:ssZ'))
    step = Math.ceil(categories.length * 0.25)
  }
  savingsData = _defaultTo(
    savingsData.map(n => toFixedDecimalNumber(n)),
    []
  )
  spendData = _defaultTo(
    spendData.map(n => toFixedDecimalNumber(n)),
    []
  )
  return {
    chart: {
      type: 'spline',
      height: 180,
      spacing: [5, 20, 5, 5]
    },
    colors: ['rgba(71, 213, 223)', 'rgba(124, 77, 211,0.05)'],
    title: {
      text: title
    },
    xAxis: {
      categories: categories,
      labels: {
        step: step
      },
      units: [['day', [1]]],
      startOnTick: true,
      tickmarkPlacement: 'on'
    },
    yAxis: {
      // min: 0,
      title: {
        text: yAxisText
      },
      labels: {
        format: '${value}'
      }
    },
    credits: {
      enabled: false
    },
    tooltip: {
      pointFormat: '{series.name}: ${point.y}<br/>'
    },
    plotOptions: {
      area: {
        stacking: 'normal',
        pointPlacement: 'on'
      }
    },
    series: [
      {
        name: 'Savings',
        type: 'area',
        data: savingsData,
        showInLegend: false,
        color: {
          linearGradient: {
            x1: 0,
            x2: 1,
            y1: 0,
            y2: 1
          },
          stops: [
            [0, 'rgba(71, 213, 223, 0.7)'],
            [1, 'rgba(71, 213, 223, 0)']
          ]
        },
        pointPlacement: 'on'
      },
      {
        name: 'Spend',
        type: 'area',
        data: spendData,
        showInLegend: false,
        color: {
          linearGradient: {
            x1: 0,
            x2: 1,
            y1: 0,
            y2: 1
          },
          stops: [
            [0, 'rgba(124, 77, 211, 0.7)'],
            [1, 'rgba(124, 77, 211, 0) 55.59%)']
          ]
        },
        pointPlacement: 'on'
      }
    ]
  }
}
function getSavingsPercentage(totalSavings: number, totalPotentialCost: number): number {
  if (totalPotentialCost == 0) {
    return 0
  }
  return Math.round((totalSavings / totalPotentialCost) * 100)
}
const COGatewayCumulativeAnalytics: React.FC<COGatewayCumulativeAnalyticsProps> = ({ data, loadingData }) => {
  const { getString } = useStrings()
  const hasData = !_isEmpty(data)
 
  return (
    <Container padding="small">
      <div>
        <Text className={css.summaryHeading} data-tooltip-id="summaryOfRulesHeader">
          SUMMARY OF RULES
          <HarnessDocTooltip tooltipId="summaryOfRulesHeader" useStandAlone={true} />
        </Text>
        <Layout.Horizontal
          spacing="xxlarge"
          background={Color.WHITE}
          className={css.analyticsContainer}
          // style={{ margin: '0px var(--spacing-medium) !important' }}
        >
          <Layout.Vertical
            spacing="large"
            style={{ textAlign: 'center', flex: 3, marginRight: 'var(--spacing-xxlarge)' }}
          >
            <Text className={css.analyticsColHeader}>TOTAL SPEND VS SAVINGS</Text>
            {data?.days && data?.days.length ? (
              <HighchartsReact
                highchart={Highcharts}
                options={getStackedAreaChartOptions(
                  '',
                  data?.days as string[],
                  '',
                  data?.savings as number[],
                  data?.actual_cost as number[]
                )}
              />
            ) : loadingData ? (
              <Icon name="spinner" size={24} color="blue500" style={{ alignSelf: 'center' }} />
            ) : (
              <Text style={{ marginTop: 'var(--spacing-xxlarge)', fontSize: 'var(--font-size-medium)' }}>
                {getString('ce.co.noData')}
              </Text>
            )}
          </Layout.Vertical>
          <Layout.Vertical style={{ flex: 1 }}>
            <Layout.Vertical spacing="xsmall">
              <Text className={css.analyticsColHeader}>SAVINGS PERCENTAGE</Text>
              <Heading level={1}>
                {hasData ? getSavingsPercentage(data?.total_savings as number, data?.total_potential as number) : 0}%
              </Heading>
              <Layout.Horizontal>
                <HighchartsReact
                  highchart={Highcharts}
                  options={
                    hasData
                      ? geGaugeChartOptionsWithoutLabel(
                          getSavingsPercentage(data?.total_savings as number, data?.total_potential as number)
                        )
                      : geGaugeChartOptionsWithoutLabel(0)
                  }
                />
              </Layout.Horizontal>
            </Layout.Vertical>
            <Text className={css.analyticsColHeader}>ACTIVE RULES</Text>
            <Layout.Horizontal spacing="small">
              <Heading level={1}>{_defaultTo(data?.total_active_services, 0)}</Heading>
              <Text style={{ alignSelf: 'center' }}>Rules</Text>
            </Layout.Horizontal>
          </Layout.Vertical>
          <Layout.Vertical spacing="small" style={{ flex: 1.2 }}>
            <Layout.Vertical spacing="medium" padding="small">
              <Container padding="small" style={{ borderRadius: '4px', backgroundColor: 'rgba(71, 213, 223,0.05)' }}>
                <Layout.Vertical spacing="small">
                  <Text className={css.analyticsColHeader} color={Color.TEAL_800}>
                    TOTAL SAVINGS TILL DATE
                  </Text>
                  {loadingData ? (
                    <Icon name="spinner" size={24} color="blue500" />
                  ) : (
                    <>
                      {hasData ? (
                        <Heading level={1} color={Color.TEAL_800}>
                          ${(Math.round(data?.total_savings as number) * 100) / 100}
                        </Heading>
                      ) : (
                        <div>
                          <img src={EmptyView} />
                          <Text>{getString('ce.noSavingsDataMessage')}</Text>
                        </div>
                      )}
                    </>
                  )}
                </Layout.Vertical>
              </Container>
              <Container padding="small" style={{ borderRadius: '4px', backgroundColor: 'rgba(124, 77, 211,0.05)' }}>
                <Layout.Vertical spacing="small">
                  <Text className={css.analyticsColHeader} color={Color.PURPLE_700}>
                    TOTAL SPEND TILL DATE
                  </Text>
                  {loadingData ? (
                    <Icon name="spinner" size={24} color="blue500" />
                  ) : (
                    <Heading level={1} color={Color.PURPLE_700}>
                      ${(Math.round(data?.total_cost as number) * 100) / 100}
                    </Heading>
                  )}
                </Layout.Vertical>
              </Container>
            </Layout.Vertical>
          </Layout.Vertical>
        </Layout.Horizontal>
      </div>
    </Container>
  )
}
 
export default COGatewayCumulativeAnalytics