All files / modules/75-ce/components/RecommendationDiffViewer RecommendationDiffViewer.tsx

100% Statements 12/12
87.5% Branches 7/8
100% Functions 2/2
100% Lines 12/12

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              2x 2x 2x   2x 2x   2x                       2x                   28x                                                                       28x                                                                       2x         7x                                                               2x  
/*
 * 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 { Container, Layout, Text } from '@wings-software/uicore'
import { Color, FontVariation } from '@harness/design-system'
import type { BorderProps } from '@harness/design-system'
import cx from 'classnames'
import { QualityOfService, ResourceDetails, ResourceObject } from '@ce/types'
 
import css from './RecommendationDiffViewer.module.scss'
 
interface DiffBlockProps {
  text: string
  color: string
  textColor: string
  resources: ResourceDetails
  border?: BorderProps
  qualityOfService?: QualityOfService
  dataTestId?: string
}
 
const DiffBlock: React.FC<DiffBlockProps> = ({
  text,
  color,
  resources,
  textColor,
  border,
  qualityOfService,
  dataTestId
}) => {
  const innerElement = (
    <>
      <Text font={{ mono: true, variation: FontVariation.BODY }} color={Color.GREY_600}>
        {text}:
      </Text>
      <Layout.Horizontal padding={{ left: 'medium', top: 'xsmall' }}>
        <Text font={{ mono: true, variation: FontVariation.BODY }} color={Color.GREY_600}>
          memory:
        </Text>
        <Text
          font={{ mono: true, variation: FontVariation.BODY }}
          color={textColor}
          padding={{ left: 'small' }}
          data-testid={`${dataTestId}-memVal`}
        >
          {resources.memory}
        </Text>
      </Layout.Horizontal>
      {qualityOfService !== QualityOfService.BURSTABLE ? (
        <Layout.Horizontal padding={{ left: 'medium', top: 'xsmall' }}>
          <Text font={{ mono: true, variation: FontVariation.BODY }} color={Color.GREY_600}>
            cpu:
          </Text>
          {resources.cpu ? (
            <Text
              font={{ mono: true, variation: FontVariation.BODY }}
              color={textColor}
              padding={{ left: 'xsmall' }}
              data-testid={`${dataTestId}-cpuVal`}
            >
              {resources.cpu}
            </Text>
          ) : null}
        </Layout.Horizontal>
      ) : null}
    </>
  )
  return border ? (
    <Container
      background={color}
      padding={{
        left: 'xxlarge',
        top: 'small',
        bottom: 'small'
      }}
      className={cx(
        { [css.borderLeft]: border.left },
        { [css.borderRight]: border.right },
        { [css.borderBottom]: border.bottom }
      )}
    >
      {innerElement}
    </Container>
  ) : (
    <Container
      background={color}
      padding={{
        left: 'xxlarge',
        top: 'small',
        bottom: 'small'
      }}
    >
      {innerElement}
    </Container>
  )
}
 
interface RecommendationDiffViewerProps {
  recommendedResources: ResourceObject
  currentResources: ResourceObject
  qualityOfService: QualityOfService
}
 
const RecommendationDiffViewer: React.FC<RecommendationDiffViewerProps> = ({
  recommendedResources,
  currentResources,
  qualityOfService
}) => {
  return (
    <Container className={css.diffContainer}>
      <DiffBlock resources={currentResources.limits} text="limits" color={Color.GREEN_100} textColor={Color.RED_700} />
      <DiffBlock
        resources={
          qualityOfService === QualityOfService.GUARANTEED ? recommendedResources.requests : recommendedResources.limits
        }
        text="limits"
        color={Color.GREEN_100}
        border={{ left: true, right: true }}
        textColor={Color.GREEN_700}
        qualityOfService={qualityOfService}
        dataTestId="limitsId"
      />
      <DiffBlock
        resources={currentResources.requests}
        text="request"
        color={Color.PRIMARY_1}
        textColor={Color.RED_700}
      />
      <DiffBlock
        resources={recommendedResources.requests}
        text="request"
        color={Color.PRIMARY_1}
        border={{ left: true, right: true, bottom: true }}
        textColor={Color.GREEN_700}
        dataTestId="requestId"
      />
    </Container>
  )
}
 
export default RecommendationDiffViewer