All files / modules/75-ce/components/OverviewPage OverviewPageLayout.tsx

84.52% Statements 71/84
65.57% Branches 40/61
70% Functions 14/20
82.89% Lines 63/76

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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392              3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x   3x   3x   3x 3x 3x                                                                                                                                                 3x 8x 2x 2x     6x     3x 2x 1x 1x   1x                                     3x                     8x 7x 7x   7x                                               3x 10x 8x   5x                 8x               3x 2x 2x 2x                         3x 1x 1x 1x                             3x 6x 6x               3x                                                                                                         3x 9x 8x   8x 6x                                                   8x 6x 6x                                               8x     3x               3x 2x     3x 1x                                                          
/*
 * 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, { ReactNode, useMemo } from 'react'
import { Link, useParams } from 'react-router-dom'
import cx from 'classnames'
import { Container, Icon, Layout, Text, Utils } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import routes from '@common/RouteDefinitions'
import { CE_COLOR_CONST, getRadialChartOptions } from '@ce/components/CEChart/CEChartOptions'
import CEChart from '@ce/components/CEChart/CEChart'
import formatCost from '@ce/utils/formatCost'
import { CCM_CHART_TYPES } from '@ce/constants'
import { useStrings } from 'framework/strings'
import CostTrend from '@ce/common/CostTrend'
 
import css from './OverviewPage.module.scss'
 
export const LEGEND_LIMIT = 5
 
export enum ListType {
  KEY_ONLY,
  KEY_VALUE
}
 
export interface Stats {
  label: string
  value: number
  trend: number
  legendColor: string
  count?: number
  linkId?: string | null
}
 
interface LayoutProps {
  title: string
  chartData: Stats[]
  totalCost: Stats
  showTrendInChart?: boolean
  seeAll?: ReactNode
}
 
interface VerticalLayoutProps extends LayoutProps {
  footer?: React.ReactNode
}
 
interface HorizontalLayoutProps extends LayoutProps {
  sideBar?: React.ReactNode
  showGist?: boolean
  chartSize?: number
  headingSize?: any
}
 
interface CardProps {
  classNames?: string
}
 
interface CostDistributionRadialChartProps {
  data: Stats[]
  colors?: string[]
  classNames?: string
  chartSize?: { height: number; width: number }
  gist: React.ReactNode
}
 
interface GistProps {
  showTrend?: boolean
  label?: string
  totalCostFontSize: string
  totalCost: Stats
}
 
interface LegendProps {
  color: string
  label: React.ReactNode
}
 
interface EfficiencyScoreProps {
  title?: string
  score: number
  trend: number
}
 
interface ListProps {
  data: Stats[]
  type?: ListType
  classNames?: string
  showCost?: boolean
}
 
interface ChartTypeProps {
  chartType: CCM_CHART_TYPES
  setChartType: React.Dispatch<React.SetStateAction<CCM_CHART_TYPES>>
}
 
const getNumberOfDigits = (num = 0) => {
  if (num % 1 != 0) {
    const parts = num.toString().split('.')
    return parts[0].length + parts[1].length
  }
 
  return num.toString().length
}
 
export const VerticalLayout = (props: VerticalLayoutProps) => {
  const { title, chartData, totalCost, footer, showTrendInChart, seeAll } = props
  const len = getNumberOfDigits(+(totalCost?.value || 0).toFixed(2))
  const totalCostFontSize = len >= 7 ? '20px' : '24px'
 
  return (
    <div className={css.verticalLayout}>
      <Layout.Horizontal style={{ justifyContent: 'space-between', alignItems: 'center' }}>
        <Text color="grey800" font={{ weight: 'semi-bold', size: 'medium' }}>
          {title}
        </Text>
        {seeAll}
      </Layout.Horizontal>
      <div className={css.centerChart}>
        <CostDistributionRadialChart
          data={chartData}
          gist={<Gist totalCostFontSize={totalCostFontSize} totalCost={totalCost} showTrend={showTrendInChart} />}
        />
      </div>
      {footer}
    </div>
  )
}
 
export const HorizontalLayout = (props: HorizontalLayoutProps) => {
  const {
    title,
    chartData,
    totalCost,
    sideBar,
    showTrendInChart,
    seeAll,
    showGist = true,
    chartSize = 200,
    headingSize = 'medium'
  } = props
  const len = getNumberOfDigits(+(totalCost.value || 0).toFixed(2))
  const totalCostFontSize = len >= 7 ? '15px' : '18px'
 
  return (
    <div className={css.horizontalLayout}>
      <Layout.Horizontal style={{ justifyContent: 'space-between', alignItems: 'center' }}>
        <Text color="grey800" font={{ weight: 'semi-bold', size: headingSize }}>
          {title}
        </Text>
        {seeAll}
      </Layout.Horizontal>
      <div className={css.cols}>
        <CostDistributionRadialChart
          data={chartData}
          chartSize={{ height: chartSize, width: chartSize }}
          gist={
            showGist ? (
              <Gist totalCostFontSize={totalCostFontSize} totalCost={totalCost} showTrend={showTrendInChart} />
            ) : null
          }
        />
        {sideBar}
      </div>
    </div>
  )
}
 
const CostDistributionRadialChart = (props: CostDistributionRadialChartProps) => {
  const { data, colors = CE_COLOR_CONST, chartSize, gist } = props
  const options = useMemo(
    () =>
      getRadialChartOptions(data as any, colors, {
        chart: chartSize,
        plotOptions: {
          pie: { color: Utils.getRealCSSColor(Color.GREY_100), fillColor: Utils.getRealCSSColor(Color.GREY_100) }
        }
      }),
    [data, colors]
  )
 
  return (
    <div className={css.chartContainer}>
      <CEChart options={options} />
      <div className={css.gist}>{gist}</div>
    </div>
  )
}
 
const Gist = (props: GistProps) => {
  const { getString } = useStrings()
  const { label = getString('ce.overview.totalCost'), showTrend = true, totalCostFontSize, totalCost } = props
  return (
    <Layout.Vertical spacing="small" style={{ alignItems: 'center', justifyContent: 'center', height: '100%' }}>
      <Text color="grey600" font="small">
        {label}
      </Text>
      <Text color="grey800" font={{ weight: 'bold' }} style={{ fontSize: totalCostFontSize }}>
        {formatCost(totalCost?.value)}
      </Text>
      {showTrend && <CostTrend value={totalCost?.trend} defaultIcon="caret-down" />}
    </Layout.Vertical>
  )
}
 
export const EfficiencyScore = (props: EfficiencyScoreProps) => {
  const { getString } = useStrings()
  const { title = getString('ce.overview.cardtitles.efficiencyScore'), score, trend } = props
  return (
    <div className={css.efficienyScore}>
      <Layout.Vertical>
        <Text tooltipProps={{ dataTooltipId: 'overviewCostEfficiencyScore' }}>{title}</Text>
        <Layout.Horizontal spacing="medium" style={{ alignItems: 'center' }}>
          <Text color="grey800" font={{ weight: 'bold' }} style={{ fontSize: 24 }}>
            {score || '-'}
          </Text>
          <CostTrend value={trend} flipColors defaultIcon="caret-up" />
        </Layout.Horizontal>
      </Layout.Vertical>
    </div>
  )
}
 
const Legend = (props: LegendProps) => {
  const { color, label } = props
  return (
    <Layout.Horizontal spacing="small" style={{ alignItems: 'center' }}>
      <div className={css.legendColor} style={{ background: color }}></div>
      {label}
    </Layout.Horizontal>
  )
}
 
export const FlexList = (props: ListProps) => {
  const { accountId } = useParams<{ accountId: string }>()
  const { data = [], type = ListType.KEY_ONLY, classNames } = props
 
  const renderLegend = (item: Stats) => {
    return (
      <Legend
        color={item.legendColor as string}
        label={
          item.linkId ? (
            <Link
              to={routes.toPerspectiveDetails({
                accountId: accountId,
                perspectiveId: item.linkId,
                perspectiveName: item.linkId
              })}
            >
              <Text inline color="primary7" font="small" lineClamp={1} style={{ width: 100 }}>
                {item.label}
              </Text>
            </Link>
          ) : (
            <Text color="grey800" font="small" lineClamp={1} style={{ width: 100 }}>
              {item.label}
            </Text>
          )
        }
      />
    )
  }
 
  const renderItem = (item: Stats, idx: number) => {
    if (type === ListType.KEY_VALUE) {
      return (
        <Layout.Horizontal key={idx} spacing="large" style={{ alignItems: 'center' }}>
          {renderLegend(item)}
          <Text color="grey600" font="small" lineClamp={1} style={{ width: 90 }}>
            {formatCost(item.value)}
          </Text>
        </Layout.Horizontal>
      )
    }
 
    return (
      <Container padding={{ top: 'small' }} key={idx}>
        {renderLegend(item)}
      </Container>
    )
  }
 
  return <div className={cx(css.flexList, classNames)}>{data.map(renderItem)}</div>
}
 
export const TableList = (props: ListProps) => {
  const { accountId } = useParams<{ accountId: string }>()
  const { data = [], type = ListType.KEY_ONLY, classNames, showCost = true } = props
 
  const renderLegend = (item: Stats) => {
    return (
      <Legend
        color={item.legendColor as string}
        label={
          item.linkId ? (
            <Link
              to={routes.toPerspectiveDetails({
                accountId: accountId,
                perspectiveId: item.linkId,
                perspectiveName: item.linkId
              })}
            >
              <Text inline color="primary7" font="small" lineClamp={1} style={{ maxWidth: 100 }}>
                {item.label}
              </Text>
            </Link>
          ) : (
            <Text color="grey800" font="small" lineClamp={1} style={{ maxWidth: 100 }}>
              {item.label}
            </Text>
          )
        }
      />
    )
  }
 
  const renderItem = (item: Stats, idx: number) => {
    Eif (type === ListType.KEY_VALUE) {
      return (
        <tr key={idx}>
          <td>{renderLegend(item)}</td>
          <td className={css.radialChartTable}>
            <Text color="grey600" font="small" lineClamp={1} style={{ maxWidth: 90 }}>
              {showCost ? formatCost(item.value) : item.count}
            </Text>
            {item.count && showCost ? (
              <Text color="grey600" font="small" lineClamp={1} style={{ maxWidth: 90 }}>
                {`(${item.count})`}
              </Text>
            ) : null}
          </td>
        </tr>
      )
    }
 
    return (
      <tr key={idx}>
        <td>{renderLegend(item)}</td>
      </tr>
    )
  }
 
  return <table className={cx(css.tableList, classNames)}>{data.map(renderItem)}</table>
}
 
export const Loader = (props: { className?: string }) => {
  return (
    <Container className={cx(css.chartLoadingContainer, props.className)}>
      <Icon name="spinner" color="blue500" size={30} />
    </Container>
  )
}
 
export const Card: React.FC<CardProps> = props => {
  return <div className={cx(css.card, props.classNames)}>{props.children}</div>
}
 
export const ChartTypes: React.FC<ChartTypeProps> = ({ chartType, setChartType }) => {
  return (
    <Layout.Horizontal
      spacing="small"
      style={{
        alignItems: 'center',
        justifyContent: 'flex-end'
      }}
    >
      <Icon
        style={{ cursor: 'pointer ' }}
        name="timeline-area-chart"
        onClick={() => {
          setChartType(CCM_CHART_TYPES.AREA)
        }}
        size={20}
        color={chartType === CCM_CHART_TYPES.AREA ? 'primary7' : 'grey400'}
      />
      <Icon
        style={{ cursor: 'pointer ' }}
        name="timeline-bar-chart"
        onClick={() => {
          setChartType(CCM_CHART_TYPES.COLUMN)
        }}
        size={18}
        color={chartType === CCM_CHART_TYPES.COLUMN ? 'primary7' : 'grey400'}
      />
    </Layout.Horizontal>
  )
}