All files / modules/75-cd/pages/dashboard DeploymentExecutionsChart.tsx

71.7% Statements 38/53
28.33% Branches 51/180
70% Functions 7/10
69.39% Lines 34/49

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              11x 11x 11x 11x 11x   11x   11x 11x   11x 11x 11x 11x                   11x                                             11x 1x 1x   1x                   1x 1x 1x 1x 1x 1x 1x 3x 3x 3x   1x                               1x   1x 3x   2x 3x 1x                                                                                                        
/*
 * 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 } from 'react'
import { Container, Text } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { useParams } from 'react-router-dom'
import { defaultTo } from 'lodash-es'
import type { TooltipFormatterContextObject } from 'highcharts'
import { useStrings } from 'framework/strings'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { useGetDeploymentExecution } from 'services/cd-ng'
import NoDeployments from '@pipeline/components/Dashboards/images/NoDeployments.svg'
 
import { useErrorHandler } from '@pipeline/components/Dashboards/shared'
import { OverviewChartsWithToggle } from '@common/components/OverviewChartsWithToggle/OverviewChartsWithToggle'
import { renderTooltipContent } from '@pipeline/components/LandingDashboardDeploymentsWidget/LandingDashboardDeploymentsWidget'
import styles from './CDDashboardPage.module.scss'
 
interface PointStats {
  deployments?: {
    failure?: number
    success?: number
    total?: number
  }
  time?: number
}
const getTooltip = (currPoint: TooltipFormatterContextObject): string => {
  const custom = currPoint?.series?.userOptions?.custom
  const point: PointStats = custom?.[currPoint.key]
  const time =
    point && point?.time
      ? new Date(point?.time).toLocaleDateString('en-US', { day: 'numeric', month: 'long' })
      : currPoint.x
  let failureRate: string | number = 'Infinity'
  if (point?.deployments?.failure && point.deployments?.total) {
    failureRate = ((point.deployments.failure / point.deployments.total) * 100).toFixed(1) + '%'
  }
  if (point?.deployments?.failure === 0) {
    failureRate = '0'
  }
  return renderTooltipContent({
    time,
    failureRate,
    count: point?.deployments?.total,
    successCount: point?.deployments?.success,
    failureCount: point?.deployments?.failure
  })
}
 
export default function DeploymentExecutionsChart(props: any) {
  const { projectIdentifier, orgIdentifier, accountId } = useParams<ProjectPathProps>()
  const { range, title } = props
 
  const { data, error } = useGetDeploymentExecution({
    queryParams: {
      accountIdentifier: accountId,
      projectIdentifier,
      orgIdentifier,
      startTime: range?.range[0]?.getTime() || 0,
      endTime: range?.range[1]?.getTime() || 0
    }
  })
 
  useErrorHandler(error)
  const chartData = useMemo(() => {
    Eif (data?.data?.executionDeploymentList?.length) {
      const successData: number[] = []
      const failureData: number[] = []
      const custom: any = []
      data.data.executionDeploymentList.forEach(val => {
        successData.push(defaultTo(val.deployments?.success, 0))
        failureData.push(defaultTo(val.deployments?.failure, 0))
        custom.push(val)
      })
      return [
        {
          name: 'Failed',
          data: failureData,
          color: '#EE5F54',
          custom
        },
        {
          name: 'Success',
          data: successData,
          color: '#5FB34E',
          custom
        }
      ]
    }
  }, [data])
  const { getString } = useStrings()
 
  const failedData = chartData?.find(item => item.name === 'Failed') as any
  const allFailedCount = failedData?.data?.every((item: any) => item === 0)
 
  const chartSuccessData = chartData?.find(item => item.name === 'Success') as any
  const allSuccessCount = chartSuccessData?.data?.every((item: any) => item === 0)
  return (
    <>
      <Text className={styles.healthCardTitle}>{title}</Text>
 
      {allFailedCount && allSuccessCount ? (
        <Container className={styles.emptyView}>
          <Container className={styles.emptyViewCard}>
            <img src={NoDeployments} />
            <Text>{getString('common.noDeployments')}</Text>
          </Container>
        </Container>
      ) : (
        <div className={styles.chartContainer}>
          <OverviewChartsWithToggle
            data={defaultTo(chartData, [])}
            customChartOptions={{
              tooltip: {
                useHTML: true,
                formatter: function () {
                  return getTooltip(this)
                },
                backgroundColor: Color.BLACK,
                outside: true,
                borderColor: 'black'
              },
              xAxis: {
                title: {
                  text: 'Date'
                },
                labels: {
                  formatter: function (this) {
                    let time = new Date().toLocaleDateString('en-US', { day: 'numeric', month: 'short' })
                    if (data?.data?.executionDeploymentList?.length) {
                      const val = data?.data?.executionDeploymentList?.[this.pos]?.time
                      time = val ? new Date(val).toLocaleDateString('en-US', { day: 'numeric', month: 'short' }) : time
                    }
                    return time
                  }
                }
              },
              yAxis: {
                title: {
                  text: '# of Deployments'
                }
              }
            }}
          />
        </div>
      )}
    </>
  )
}