All files / modules/85-cv/components/ColumnChart ColumnChart.tsx

100% Statements 38/38
85.56% Branches 77/90
100% Functions 5/5
100% Lines 36/36

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              18x 18x 18x 18x 18x 18x 18x   18x 18x 18x 18x 18x   18x                       29x 29x 29x 29x 29x   29x 20x 6x 6x 2x   4x     6x 2x                     29x 1x     28x 3x     144x                           25x 19x                                             6x                   200x 200x                                                  
/*
 * 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, { useLayoutEffect, useRef, useState } from 'react'
import { Classes, PopoverInteractionKind, PopoverPosition } from '@blueprintjs/core'
import cx from 'classnames'
import { Text, Container, Popover, PageError, NoDataCard } from '@wings-software/uicore'
import { useStrings } from 'framework/strings'
import { getErrorMessage } from '@cv/utils/CommonUtils'
import noDataImage from '@cv/assets/noData.svg'
import type { ColumnChartProps } from './ColumnChart.types'
import { calculatePositionForTimestamp, getColumnPositions, getLoadingColumnPositions } from './ColumnChart.utils'
import { COLUMN_WIDTH, COLUMN_HEIGHT, TOTAL_COLUMNS, LOADING_COLUMN_HEIGHTS } from './ColumnChart.constants'
import ColumnChartPopoverContent from './components/ColumnChartPopoverContent/ColumnChartPopoverContent'
import ColumnChartEventMarker from './components/ColummnChartEventMarker/ColumnChartEventMarker'
import css from './ColumnChart.module.scss'
 
export default function ColumnChart(props: ColumnChartProps): JSX.Element {
  const {
    data,
    leftOffset = 0,
    columnWidth = COLUMN_WIDTH,
    isLoading,
    error,
    refetchOnError,
    columnHeight = COLUMN_HEIGHT,
    timestampMarker,
    hasTimelineIntegration,
    duration
  } = props
  const containerRef = useRef<HTMLDivElement>(null)
  const [cellPositions, setCellPositions] = useState<number[]>(Array(TOTAL_COLUMNS).fill(null))
  const [markerPosition, setMarkerPosition] = useState<number | undefined>()
  const { getString } = useStrings()
 
  useLayoutEffect(() => {
    if (!containerRef?.current) return
    const containerWidth = (containerRef.current.parentElement?.getBoundingClientRect().width || 0) - leftOffset
    if (isLoading) {
      setCellPositions(getLoadingColumnPositions(containerWidth))
    } else {
      setCellPositions(getColumnPositions(containerWidth, data))
    }
 
    if (timestampMarker && data?.[data.length - 1]?.timeRange?.endTime && data[0]?.timeRange?.startTime) {
      setMarkerPosition(
        calculatePositionForTimestamp({
          containerWidth,
          startTime: timestampMarker.timestamp,
          endOfTimestamps: data[data.length - 1].timeRange.endTime,
          startOfTimestamps: data[0].timeRange.startTime
        })
      )
    }
  }, [containerRef?.current, data, isLoading])
 
  if (error) {
    return <PageError message={getErrorMessage(error)} onClick={refetchOnError} />
  }
 
  if (isLoading) {
    return (
      <div ref={containerRef} className={css.main}>
        {cellPositions.map((val, index) => (
          <div
            key={index}
            style={{
              left: val,
              height: Math.floor((LOADING_COLUMN_HEIGHTS[index] / 100) * columnHeight),
              width: columnWidth
            }}
            className={cx(css.column, Classes.SKELETON)}
          />
        ))}
      </div>
    )
  }
 
  if (!data?.length || data.every(el => el?.height === 0)) {
    return (
      <NoDataCard
        message={
          <>
            <Text font={{ size: 'small' }}>
              {getString('cv.monitoredServices.serviceHealth.noDataAvailableForHealthScore', {
                duration: duration?.label?.toLowerCase()
              })}
            </Text>
            {hasTimelineIntegration && (
              <Text font={{ size: 'small' }}>
                {getString('cv.monitoredServices.serviceHealth.pleaseSelectAnotherTimeWindow')}
              </Text>
            )}
          </>
        }
        image={noDataImage}
        imageClassName={css.noDataImage}
        containerClassName={css.noData}
      />
    )
  }
 
  return (
    <div ref={containerRef} className={css.main}>
      {markerPosition && (
        <ColumnChartEventMarker
          columnHeight={columnHeight}
          leftOffset={markerPosition}
          markerColor={timestampMarker?.color || ''}
        />
      )}
      {cellPositions.map((position, index) => {
        const cell = data?.[index] || {}
        return (
          <div
            key={index}
            className={css.column}
            style={{
              backgroundColor: cell.color,
              left: position || 0,
              height: Math.floor(((cell.height || 0) / 100) * columnHeight),
              width: columnWidth
            }}
          >
            <Popover
              content={<ColumnChartPopoverContent cell={cell} />}
              position={PopoverPosition.TOP}
              popoverClassName={css.chartPopover}
              interactionKind={PopoverInteractionKind.HOVER}
            >
              <Container height={columnHeight} width={columnWidth} />
            </Popover>
          </div>
        )
      })}
    </div>
  )
}