All files / modules/75-cd/components/Services/MostActiveServicesWidget MostActiveServicesWidget.tsx

98.75% Statements 79/80
86.79% Branches 92/106
96.15% Functions 25/26
98.7% Lines 76/77

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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302              3x 3x 3x 3x 3x 3x 3x 3x 3x   3x 3x 3x 3x                                         3x 3x 3x     3x     8x             3x 8x                       3x 10x   10x   9x               8x 8x 8x 2x   8x 8x             9x 9x 9x 8x 8x           10x 8x       10x 8x                   10x 10x 10x 10x   10x   10x 9x                   10x   10x 9x       10x   9x   27x         1x                     10x 9x 8x 8x   8x       8x                                       10x   9x               10x   9x 18x   1x                             10x 10x                               10x 5x 5x 2x           3x 1x           2x                                 5x     5x                                              
/*
 * 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, { useCallback, useContext, useMemo, useState } from 'react'
import { useParams } from 'react-router-dom'
import cx from 'classnames'
import { Card, Container, LabelPosition, Layout, Text, WeightedStack, PageError } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { useStrings, UseStringsReturn } from 'framework/strings'
import { Ticker, TickerVerticalAlignment } from '@common/components/Ticker/Ticker'
import { DeploymentsTimeRangeContext, getFixed, INVALID_CHANGE_RATE } from '@cd/components/Services/common'
import { DashboardWorkloadDeployment, GetWorkloadsQueryParams, useGetWorkloads } from 'services/cd-ng'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { FAIL_COLORS, SUCCESS_COLORS } from '@dashboards/constants'
import { PageSpinner } from '@common/components'
import MostActiveServicesEmptyState from '@cd/icons/MostActiveServicesEmptyState.svg'
import css from '@cd/components/Services/MostActiveServicesWidget/MostActiveServicesWidget.module.scss'
 
interface MostActiveServicesWidgetData {
  label: string
  value: number
  color: string
  change: number
}
 
export interface MostActiveServicesWidget {
  environmentTypes?: Record<string, GetWorkloadsQueryParams['environmentType']>
  types?: {
    [key: string]: {
      label: string
      colors?: string[]
    }
  }
  title?: string
  parseByType?: (data: DashboardWorkloadDeployment | []) => MostActiveServicesWidgetData[]
}
 
enum DEFAULT_TYPES_ENUM {
  DEPLOYMENTS = 'DEFAULT_TYPES_ENUM.DEPLOYMENTS',
  ERRORS = 'DEFAULT_TYPES_ENUM.ERRORS'
}
 
const getDefaultEnvironments = (
  getString: UseStringsReturn['getString']
): { [key: string]: GetWorkloadsQueryParams['environmentType'] } => {
  return {
    [getString('all')]: undefined,
    [getString('cd.serviceDashboard.prod')]: 'Production',
    [getString('cd.serviceDashboard.nonProd')]: 'PreProduction'
  }
}
 
const getDefaultTypes = (getString: UseStringsReturn['getString']) => {
  return {
    [DEFAULT_TYPES_ENUM.DEPLOYMENTS]: {
      label: getString('deploymentsText'),
      colors: SUCCESS_COLORS
    },
    [DEFAULT_TYPES_ENUM.ERRORS]: {
      label: getString('errors'),
      colors: FAIL_COLORS
    }
  }
}
 
export const MostActiveServicesWidget: React.FC<MostActiveServicesWidget> = props => {
  const { getString } = useStrings()
 
  const defaultParseByType = useCallback(
    (data: DashboardWorkloadDeployment | [], selectedType: string): MostActiveServicesWidgetData[] => {
      const items = ((data as DashboardWorkloadDeployment)?.workloadDeploymentInfoList || []).map(
        workloadDeploymentInfo => {
          const {
            totalDeployments = 0,
            failureRate = 0,
            percentSuccess = 0,
            failureRateChangeRate,
            rateSuccess
          } = workloadDeploymentInfo
          let value = (workloadDeploymentInfo as any)?.totalSuccess || (totalDeployments * percentSuccess) / 100
          if (selectedType === DEFAULT_TYPES_ENUM.ERRORS) {
            value = (workloadDeploymentInfo as any)?.totalFailure || (totalDeployments * failureRate) / 100
          }
          const change = selectedType === DEFAULT_TYPES_ENUM.DEPLOYMENTS ? rateSuccess : failureRateChangeRate
          return {
            label: workloadDeploymentInfo.serviceName || '',
            value: getFixed(value),
            change: change || 0
          }
        }
      )
      items.sort((itemA, itemB) => (itemA.value > itemB.value ? -1 : 1))
      const colorList = types[selectedType].colors || [Color.BLACK]
      return items
        .map((item, index) => ({ ...item, color: colorList[Math.min(index, colorList.length - 1)] }))
        .filter(item => item.value)
      // eslint-disable-next-line react-hooks/exhaustive-deps
    },
    []
  )
 
  const DEFAULT_ENVIRONMENT_TYPES = useMemo(
    () => getDefaultEnvironments(getString),
    // eslint-disable-next-line react-hooks/exhaustive-deps
    []
  )
  const DEFAULT_TYPES = useMemo(
    () => getDefaultTypes(getString),
    // eslint-disable-next-line react-hooks/exhaustive-deps
    []
  )
 
  const {
    environmentTypes = DEFAULT_ENVIRONMENT_TYPES,
    types = DEFAULT_TYPES,
    title,
    parseByType = defaultParseByType
  } = props
  const [selectedEnvironmentType, setSelectedEnvironmentType] = useState(Object.keys(environmentTypes)[0])
  const [selectedType, setSelectedType] = useState(Object.keys(types)[0])
  const { accountId, orgIdentifier, projectIdentifier } = useParams<ProjectPathProps>()
 
  const { timeRange } = useContext(DeploymentsTimeRangeContext)
 
  const queryParams: GetWorkloadsQueryParams = useMemo(() => {
    return {
      accountIdentifier: accountId,
      orgIdentifier,
      projectIdentifier,
      startTime: timeRange?.range[0]?.getTime() || 0,
      endTime: timeRange?.range[1]?.getTime() || 0,
      environmentType: environmentTypes[selectedEnvironmentType]
    }
  }, [accountId, orgIdentifier, projectIdentifier, timeRange, environmentTypes, selectedEnvironmentType])
 
  const { loading, error, data: workloadsData, refetch } = useGetWorkloads({ queryParams })
 
  const data = useMemo(
    () => parseByType(workloadsData?.data || [], selectedType),
    [workloadsData?.data, selectedType, parseByType]
  )
 
  const EnvironmentTypeComponent = useMemo(
    () => (
      <Layout.Horizontal>
        {Object.keys(environmentTypes).map(environmentTypeKey => (
          <Text
            key={environmentTypeKey}
            font={{ size: 'small', weight: 'semi-bold' }}
            intent={environmentTypeKey === selectedEnvironmentType ? 'primary' : 'none'}
            className={css.environmentType}
            onClick={() => setSelectedEnvironmentType(environmentTypeKey)}
            data-test="mostActiveServicesWidgetEnvironmentType"
          >
            {environmentTypeKey}
          </Text>
        ))}
      </Layout.Horizontal>
    ),
    [environmentTypes, selectedEnvironmentType]
  )
 
  const Tickers = useMemo(() => {
    return data.map((service, index) => {
      const { change } = service
      const isBoostMode = change === INVALID_CHANGE_RATE
      const [color, tickerValueStyle] =
        (selectedType === DEFAULT_TYPES_ENUM.DEPLOYMENTS && !isBoostMode && change < 0) ||
        (selectedType === DEFAULT_TYPES_ENUM.ERRORS && (isBoostMode || change > 0))
          ? [Color.RED_500, css.tickerValueRed]
          : [Color.GREEN_600, css.tickerValueGreen]
      return (
        <div className={css.tickerContainer} key={index}>
          {change !== undefined ? (
            <Ticker
              value={isBoostMode ? '' : `${getFixed(Math.abs(change))}%`}
              color={color}
              tickerValueStyles={cx(css.tickerValueStyles, tickerValueStyle)}
              verticalAlign={TickerVerticalAlignment.CENTER}
              decreaseMode={!isBoostMode && change < 0}
              boost={isBoostMode}
              size={isBoostMode ? 10 : 6}
            />
          ) : (
            <></>
          )}
        </div>
      )
    })
  }, [data])
 
  const weightedStackData = useMemo(
    () =>
      data.map(service => ({
        label: service.label,
        value: service.value,
        color: service.color
      })),
    [data]
  )
 
  const TypeComponent = useMemo(
    () =>
      Object.keys(types).map(typeKey => (
        <div
          key={typeKey}
          onClick={() => setSelectedType(typeKey)}
          className={cx(css.typeContainer, { [css.typeSelected]: typeKey === selectedType })}
          data-test={`mostActiveServicesWidgetType${typeKey === selectedType ? 'Selected' : ''}`}
        >
          <Text
            font={{ size: 'xsmall', weight: 'semi-bold' }}
            color={typeKey === selectedType ? Color.WHITE : Color.BLACK}
          >
            {types[typeKey].label}
          </Text>
        </div>
      )),
    [types, selectedType]
  )
 
  const MostActiveServicesWidgetContainer: React.FC = ({ children }) => {
    return (
      <Card className={css.card}>
        <Layout.Vertical height={'100%'}>
          {title && (
            <Text font={{ weight: 'bold' }} color={Color.GREY_600}>
              {title}
            </Text>
          )}
          <Container margin={{ bottom: 'xlarge' }}>{EnvironmentTypeComponent}</Container>
          <Layout.Horizontal margin={{ bottom: 'large' }}>{TypeComponent}</Layout.Horizontal>
          {children}
        </Layout.Vertical>
      </Card>
    )
  }
 
  if (loading || error || !data || !data.length) {
    const component = (() => {
      if (loading) {
        return (
          <Container data-test="mostActiveServicesWidgetLoader">
            <PageSpinner />
          </Container>
        )
      }
      if (error) {
        return (
          <Container data-test="mostActiveServicesWidgetError" height={'100%'}>
            <PageError onClick={() => refetch()} width={230} />
          </Container>
        )
      }
      return (
        <Layout.Vertical
          flex={{ align: 'center-center' }}
          data-test="mostActiveServicesWidgetEmpty"
          className={css.mostActiveServicesWidgetEmpty}
        >
          <Container margin={{ bottom: 'medium' }}>
            <img width="50" height="50" src={MostActiveServicesEmptyState} style={{ alignSelf: 'center' }} />
          </Container>
          <Text color={Color.GREY_400}>
            {getString('cd.serviceDashboard.noActiveServices', {
              timeRange: timeRange?.label
            })}
          </Text>
        </Layout.Vertical>
      )
    })()
    return <MostActiveServicesWidgetContainer>{component}</MostActiveServicesWidgetContainer>
  }
 
  return (
    <MostActiveServicesWidgetContainer>
      <Layout.Horizontal
        flex={{ distribution: 'space-between', alignItems: 'flex-start' }}
        width="100%"
        height={150}
        className={css.stackTickerContainer}
        data-test="mostActiveServicesWidgetContent"
      >
        <Layout.Vertical className={css.weightedStackContainer} width="60%">
          <WeightedStack
            data={weightedStackData}
            labelPosition={LabelPosition.BOTTOM}
            stackStyles={css.stack}
            progressBarStyles={css.progressBar}
            labelStyles={css.label}
          />
        </Layout.Vertical>
        <Layout.Vertical width="30%">{Tickers}</Layout.Vertical>
      </Layout.Horizontal>
    </MostActiveServicesWidgetContainer>
  )
}