All files / modules/75-ce/components/CloudCostInsightChart WorkloadDetailsChart.tsx

49.02% Statements 25/51
10% Branches 4/40
21.43% Functions 3/14
49.02% Lines 25/51

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              7x 7x 7x 7x 7x   7x 7x   7x 7x   7x               7x   7x   7x         7x                                                                             7x 4x       4x                                                 4x                                                                                     4x       4x                                                                                                                                       7x 2x 2x     4x                                       7x  
/*
 * 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 from 'react'
import Highcharts from 'highcharts/highcharts'
import moment from 'moment'
import { Layout } from '@wings-software/uicore'
import { QlceViewTimeGroupType } from 'services/ce/services'
import type { ChartConfigType } from './chartUtils'
import CEChart from '../CEChart/CEChart'
import css from './CloudCostInsightChart.module.scss'
 
export const DAYS_FOR_TICK_INTERVAL = 10
export const ONE_MONTH = 24 * 3600 * 1000 * 30
 
const chartOptions = {
  chart: {
    style: {
      fontFamily: 'var(--font-family)'
    }
  }
}
 
Highcharts.setOptions(chartOptions)
 
const HighchartPoint = Highcharts.Point as any
 
HighchartPoint.prototype.highlight = function (event: any) {
  // this.series.chart.tooltip.refresh(this) // Show the tooltip
  this.series.chart.xAxis[0].drawCrosshair(event, this) // Show the crosshair
}
 
const chartHover = (event: any, point: any, series: any) => {
  point.highlight && point.highlight(event)
 
  const yVal = point.y
 
  series.legendItem.element.innerHTML = `<tspan>${series.name}: ${isNaN(yVal) ? ' ' : yVal}<tspan>`
 
  const chartObj = series.chart
 
  const displayDate = moment.utc(point.x).format('MMM D, YYYY H:mm A')
 
  chartObj.lbl && chartObj.lbl.destroy()
 
  const label = chartObj.renderer
    .label(displayDate, null, 0)
    .css({
      color: '#212121',
      fontSize: '13px',
      fill: 'none'
    })
    .add()
  label.attr({
    x: +chartObj.plotWidth + +chartObj.plotLeft - label.width
  })
 
  chartObj.lbl = label
}
 
interface GetChartProps {
  chart: ChartConfigType[]
  idx: number
  onLoad: (chart: Highcharts.Chart) => void
  chartType: string
  aggregation: QlceViewTimeGroupType
  xAxisPointCount: number
  showLegends: boolean
  title: string
}
 
const GetChart: React.FC<GetChartProps> = ({ chart, idx, onLoad, chartType, aggregation, xAxisPointCount, title }) => {
  const CHART_LEGEND_PREFIX_V2: Record<string, string> = {
    CPU: 'vCPU',
    Memory: 'GB'
  }
  const xAxisOptions: Highcharts.XAxisOptions = {
    type: 'datetime',
    ordinal: true,
    tickInterval:
      aggregation === QlceViewTimeGroupType.Day && xAxisPointCount < DAYS_FOR_TICK_INTERVAL
        ? 24 * 3600 * 1000
        : undefined,
    labels: {
      formatter: function () {
        switch (aggregation) {
          case QlceViewTimeGroupType.Month:
            return moment(this.value).utc().format('MMM')
 
          default:
            return moment(this.value).utc().format('MMM DD')
        }
      }
    },
    crosshair: {
      width: 2,
      color: '#d5dae0',
      dashStyle: 'ShortDot'
    }
  }
 
  const plotOptions = {
    series: {
      connectNulls: true,
      animation: {
        duration: 500
      },
      point: {
        events: {
          mouseOver: function (e: any) {
            const eventDate = e.target.x
 
            Highcharts.charts.forEach(chartObj1 => {
              chartObj1 &&
                chartObj1.series.forEach(series => {
                  if (series.visible) {
                    series.points.forEach(point => {
                      if (point.x === eventDate) {
                        chartHover(e, point, series)
                      }
                    })
                  }
                })
            })
          }
        }
      }
    },
    line: {
      connectNulls: true,
      events: {
        mouseOut: function () {
          Highcharts.charts.forEach(chartObject => {
            chartObject?.xAxis[0]?.hideCrosshair()
            chartObject?.tooltip?.hide()
          })
        }
      },
      marker: {
        enabled: false
      }
    }
  }
 
  Iif (aggregation === QlceViewTimeGroupType.Month) {
    xAxisOptions.tickInterval = ONE_MONTH
  }
 
  return (
    <article key={idx}>
      <CEChart
        options={{
          series: chart as any,
          title: {
            text: title,
            align: 'left',
            margin: 24,
            style: {
              fontSize: '14px',
              fontWeight: 'normal'
            }
          },
          chart: {
            height: 260,
            type: chartType,
            events: {
              load() {
                onLoad(this)
              }
            }
          },
          legend: {
            align: 'left',
            enabled: true,
            margin: 20,
            itemDistance: 72,
            itemStyle: {
              color: '#627386',
              fontSize: '12px',
              fontWeight: 'normal'
            }
          },
          tooltip: {
            shared: true,
            enabled: true
          },
          plotOptions,
          yAxis: {
            allowDecimals: true,
            endOnTick: true,
            tickAmount: 5,
            title: {
              text: ''
            },
            labels: {
              formatter: function () {
                return `${this['value']} ${CHART_LEGEND_PREFIX_V2[title] || ''}`
              }
            }
          },
          xAxis: xAxisOptions
        }}
      />
    </article>
  )
}
 
interface CCMChartProps {
  data: ChartConfigType[][]
  onLoad: (chart: Highcharts.Chart) => void
  chartType: string
  aggregation: QlceViewTimeGroupType
  xAxisPointCount: number
  showLegends: boolean
}
 
const Chart: React.FC<CCMChartProps> = ({ data, onLoad, chartType, aggregation, xAxisPointCount, showLegends }) => {
  const chartTiles = ['CPU', 'Memory']
  return (
    <Layout.Horizontal className={css.horizontalCharts}>
      {data.map((chart, idx: number) => {
        return chart ? (
          <div className={css.horizontalChart} key={idx}>
            <GetChart
              title={chartTiles[idx]}
              key={idx}
              chartType={chartType}
              aggregation={aggregation}
              xAxisPointCount={xAxisPointCount}
              chart={chart}
              idx={idx}
              onLoad={onLoad}
              showLegends={showLegends}
            />
          </div>
        ) : null
      })}
    </Layout.Horizontal>
  )
}
 
export default Chart