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

91.38% Statements 53/58
79.22% Branches 61/77
80% Functions 12/15
91.07% Lines 51/56

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              13x 13x 13x 13x   13x 13x 13x 13x 13x 13x 13x 13x 13x 13x                                     13x       13x                   4x 4x 4x   4x 4x           4x 4x     4x                                 4x                                                                       13x         8x 8x 8x                                                                                                         8x 1x 1x 1x       13x 4x 4x 4x 4x 3x 2x 2x 2x 3x 6x 6x 6x         4x 3x       3x             13x 4x                                                                                                                
/*
 * 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, useState, useEffect, useRef } from 'react'
import { Container, Text, Icon, Button } from '@wings-software/uicore'
import { useModalHook } from '@harness/use-modal'
import { Color } from '@harness/design-system'
import type { FontProps } from '@harness/design-system'
import HighchartsReact from 'highcharts-react-official'
import Highcharts from 'highcharts'
import classnames from 'classnames'
import moment from 'moment'
import merge from 'lodash-es/merge'
import { Popover, Menu, MenuItem, Dialog } from '@blueprintjs/core'
import { TimelineBar } from '@cv/components/TimelineView/TimelineBar'
import { useStrings } from 'framework/strings'
import { getIconBySourceType } from '@cv/pages/health-source/HealthSourceTable/HealthSourceTable.utils'
import styles from './TimeseriesRow.module.scss'
 
export interface SeriesConfig {
  name?: string
  series: Highcharts.SeriesLineOptions[]
  chartOptions?: Highcharts.Options
}
 
export interface TimeseriesRowProps {
  transactionName: React.ReactNode
  metricName?: React.ReactNode
  seriesData?: Array<SeriesConfig>
  chartOptions?: Highcharts.Options
  withContextMenu?: boolean
  className?: string
  setChartDivRef?: (element: HTMLDivElement | null) => void
  dataSourceType?: string
}
 
const FONT_SIZE_SMALL: FontProps = {
  size: 'small'
}
 
export default function TimeseriesRow({
  transactionName,
  metricName,
  seriesData,
  className,
  chartOptions,
  withContextMenu = true,
  setChartDivRef,
  dataSourceType
}: TimeseriesRowProps): JSX.Element {
  const { getString } = useStrings()
  const showDetails = useTimeseriesDetailsModal(transactionName, metricName, dataSourceType as string)
  const chartingRowRef = useRef<HTMLDivElement>(null)
 
  const rows = useMemo(() => {
    return seriesData?.map(data => ({
      name: data.name,
      series: data.series,
      options: merge(chartsConfig(data.series), chartOptions, data.chartOptions)
    }))
  }, [seriesData, chartOptions])
  useEffect(() => {
    setChartDivRef?.(chartingRowRef?.current)
  }, [chartingRowRef])
 
  return (
    <Container className={classnames(styles.timeseriesRow, className)}>
      <Container className={styles.labels}>
        <div className={styles.metricLablesContainer}>
          <Text color={Color.BLACK} font={FONT_SIZE_SMALL} lineClamp={1}>
            {transactionName}
          </Text>
          <Text font={FONT_SIZE_SMALL} lineClamp={1}>
            {metricName}
          </Text>
        </div>
        <Container className={styles.icons}>
          {dataSourceType ? <Icon name={getIconBySourceType(dataSourceType)} size={14} /> : null}
        </Container>
      </Container>
      <Container className={styles.charts}>
        {rows?.map((data, index) => (
          <React.Fragment key={index}>
            {data.name && <Text>{data.name}</Text>}
            <Container className={styles.chartRow}>
              <div className={styles.chartContainer} ref={chartingRowRef}>
                <HighchartsReact highcharts={Highcharts} options={data.options} />
              </div>
              {withContextMenu && (
                <Container padding={{ right: 'xsmall' }}>
                  <Popover
                    content={
                      <Menu>
                        <MenuItem
                          icon="fullscreen"
                          text={getString('viewDetails')}
                          onClick={() =>
                            showDetails({
                              name: data.name,
                              series: data.series
                            })
                          }
                        />
                      </Menu>
                    }
                  >
                    <Icon name="main-more" className={styles.verticalMoreIcon} color={Color.GREY_350} />
                  </Popover>
                </Container>
              )}
            </Container>
          </React.Fragment>
        ))}
      </Container>
    </Container>
  )
}
 
export function useTimeseriesDetailsModal(
  transactionName: React.ReactNode,
  metricName: React.ReactNode,
  dataSourceType: string
) {
  const [range, setRange] = useState<{ startDate: number; endDate: number } | undefined>()
  const [seriesData, setSeriesData] = useState<SeriesConfig>()
  const [openModal, hideModal] = useModalHook(
    () => (
      <Dialog
        isOpen
        usePortal
        autoFocus
        canEscapeKeyClose
        canOutsideClickClose
        enforceFocus={false}
        onClose={hideModal}
        style={{ width: '80vw', borderLeft: 0, paddingBottom: 0, position: 'relative', overflow: 'hidden' }}
      >
        <Container className={styles.detailsModal} padding="small" margin="xxxlarge">
          <TimeseriesRow
            transactionName={transactionName}
            metricName={metricName}
            seriesData={seriesData && [seriesData]}
            dataSourceType={dataSourceType}
            chartOptions={{
              chart: {
                height: 200,
                marginLeft: 50
              },
              xAxis: {
                gridLineWidth: 0
              },
              yAxis: {
                gridLineWidth: 1,
                labels: {
                  enabled: true,
                  style: {
                    fontSize: 'var(--font-size-xsmall)',
                    color: 'var(--grey-300)'
                  }
                }
              },
              plotOptions: {
                series: {
                  marker: {
                    symbol: 'circle'
                  }
                }
              }
            }}
            withContextMenu={false}
          />
          {range && <TimelineBar className={styles.timelineBar} {...range} />}
          <Button minimal icon="cross" iconProps={{ size: 18 }} onClick={hideModal} className={styles.crossButton} />
        </Container>
      </Dialog>
    ),
    [seriesData]
  )
  return (data: SeriesConfig, options?: Highcharts.Options) => {
    setRange(extractTimeRange(data, options))
    setSeriesData(data)
    return openModal()
  }
}
 
export function extractTimeRange(data: SeriesConfig, options?: Highcharts.Options) {
  let start: number = (options?.xAxis as Highcharts.XAxisOptions)?.min ?? 0
  let end: number = (options?.xAxis as Highcharts.XAxisOptions)?.max ?? 0
  if (!start && !end) {
    const seriesWithData = data.series.filter(serie => serie.data?.length)
    if (seriesWithData.length) {
      start = Infinity
      end = -Infinity
      seriesWithData.forEach(serie => {
        serie?.data?.forEach((item: any) => {
          const timestamp = Array.isArray(item) ? item[0] : item.x
          start = Math.min(start, timestamp)
          end = Math.max(end, timestamp)
        })
      })
    }
  }
  if (start && end) {
    Iif (start === end) {
      start -= 3600000
      end += 3600000
    }
    return {
      startDate: start,
      endDate: end
    }
  }
}
 
export function chartsConfig(series: Highcharts.SeriesLineOptions[]): Highcharts.Options {
  return {
    chart: {
      backgroundColor: 'transparent',
      height: 40,
      type: 'line',
      spacing: [5, 0, 5, 0]
    },
    credits: undefined,
    title: {
      text: ''
    },
    legend: {
      enabled: false
    },
    xAxis: {
      labels: { enabled: false },
      lineWidth: 0,
      tickLength: 0,
      gridLineWidth: 0,
      title: {
        text: ''
      }
    },
    yAxis: {
      labels: { enabled: false },
      lineWidth: 0,
      tickLength: 0,
      gridLineWidth: 0,
      title: {
        text: ''
      }
    },
    plotOptions: {
      series: {
        stickyTracking: false,
        lineWidth: 1,
        turboThreshold: 50000
      },
      line: {
        marker: {
          enabled: false
        }
      }
    },
    tooltip: {
      formatter: function tooltipFormatter(this: any): string {
        return `<section class="serviceeGuardTimeSeriesTooltip"><p>${moment(this.x).format(
          'M/D/YYYY h:mm:ss a'
        )}</p><br/><p>Value: ${Math.round(this.y * 100) / 100}</p></section>`
      },
      outside: true
    },
    subtitle: undefined,
    series
  }
}