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

93.33% Statements 28/30
87.1% Branches 27/31
100% Functions 6/6
93.33% Lines 28/30

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              2x 2x 2x               2x 2x 2x 2x 2x             2x 22x                                   2x 2x   2x                                                                       2x 2x 2x       3x   14x 14x 14x 14x                                   2x               2x   2x               2x               2x         2x                                               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, Icon, FlexExpander } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import type {
  Maybe,
  PerspectiveTrendStats,
  ClusterData,
  InstanceDetails,
  K8sRecommendationFilterDtoInput
} from 'services/ce/services'
import { useStrings } from 'framework/strings'
import { CCM_PAGE_TYPE } from '@ce/types'
import { OVERVIEW_FIELD_MAPPER } from './constants'
import RecommendationSummaryCard from '../PerspectiveSummary/RecommendationSummaryCard'
import css from './WorkloadSummary.module.scss'
 
interface KeyValuePairRendererProps {
  keyVal?: React.ReactNode
  value?: React.ReactNode
}
 
const KeyValuePairRenderer: (props: KeyValuePairRendererProps) => JSX.Element = ({ keyVal, value }) => {
  return (
    <Layout.Horizontal className={css.keyValueTable}>
      <Text color={Color.GREY_400} lineClamp={1} className={css.key} width={132}>
        {keyVal}
      </Text>
      <Text color={Color.PRIMARY_9} lineClamp={1} className={css.value} width={160}>
        {value}
      </Text>
    </Layout.Horizontal>
  )
}
 
const CostDetails: ({
  summaryData,
  pageType
}: {
  summaryData: PerspectiveTrendStats
  pageType: CCM_PAGE_TYPE
}) => JSX.Element = ({ summaryData, pageType }) => {
  const { cost, idleCost, utilizedCost, unallocatedCost, systemCost } = summaryData
 
  return (
    <Container>
      {cost && <KeyValuePairRenderer keyVal={cost.statsLabel} value={cost.statsValue} />}
      {idleCost && (
        <KeyValuePairRenderer
          keyVal={idleCost.statsLabel}
          value={`${idleCost.statsValue} (${idleCost.statsDescription})`}
        />
      )}
      {utilizedCost && (
        <KeyValuePairRenderer
          keyVal={utilizedCost.statsLabel}
          value={`${utilizedCost.statsValue} (${utilizedCost.statsDescription})`}
        />
      )}
      {pageType === CCM_PAGE_TYPE.Node ? (
        <>
          {unallocatedCost && (
            <KeyValuePairRenderer
              keyVal={unallocatedCost.statsLabel}
              value={`${unallocatedCost.statsValue} (${unallocatedCost.statsDescription})`}
            />
          )}
          {systemCost && (
            <KeyValuePairRenderer
              keyVal={systemCost.statsLabel}
              value={`${systemCost.statsValue} (${systemCost.statsDescription})`}
            />
          )}
        </>
      ) : null}
    </Container>
  )
}
 
const NodeDetails: ({ infoData, pageType }: { infoData: Record<string, any>; pageType: CCM_PAGE_TYPE }) => JSX.Element =
  ({ infoData, pageType }) => {
    const fieldTables = OVERVIEW_FIELD_MAPPER[pageType]
    return (
      <Layout.Horizontal>
        {fieldTables?.length
          ? fieldTables.map((table, idx) => (
              <div key={`overview-field-${idx}`}>
                {table.map(({ name, key, formatter }) => {
                  const accessor = key as string
                  const value = (infoData && infoData[accessor]) || ''
                  const formattedVal = formatter ? formatter(value) : value
                  return <KeyValuePairRenderer key={accessor} keyVal={name} value={formattedVal} />
                })}
              </div>
            ))
          : null}
      </Layout.Horizontal>
    )
  }
 
interface WorkloadSummaryProps {
  fetching: boolean
  summaryData: Maybe<PerspectiveTrendStats> | undefined
  infoData: ClusterData | InstanceDetails
  pageType: CCM_PAGE_TYPE
  showRecommendations?: boolean
  recommendationFilters?: K8sRecommendationFilterDtoInput
}
 
const WorkloadSummary: (props: WorkloadSummaryProps) => JSX.Element = ({
  fetching,
  summaryData,
  infoData,
  pageType,
  showRecommendations,
  recommendationFilters
}) => {
  const { getString } = useStrings()
 
  Iif (fetching) {
    return (
      <Container className={css.loadingContainer}>
        <Icon name="spinner" size={26} color="blue500" />
      </Container>
    )
  }
 
  Iif (!summaryData) {
    return (
      <Container>
        <Icon name="error" size={26} />
      </Container>
    )
  }
 
  const detailsText = {
    [CCM_PAGE_TYPE.Workload]: getString('ce.perspectives.workloadDetails.workloadDetailsText'),
    [CCM_PAGE_TYPE.Node]: getString('ce.perspectives.nodeDetails.nodeDetailsText')
  }
 
  return (
    <Layout.Horizontal className={css.summaryDetailsContainer}>
      <Container className={css.container}>
        <Text className={css.headingText}>{detailsText[pageType]}</Text>
        <NodeDetails infoData={infoData} pageType={pageType} />
      </Container>
      <Container className={css.container}>
        <Text className={css.headingText}>{getString('ce.perspectives.workloadDetails.costDetailsText')}</Text>
        <CostDetails pageType={pageType} summaryData={summaryData} />
      </Container>
      <FlexExpander />
      {showRecommendations && recommendationFilters ? (
        <Container
          margin={{
            right: 'large'
          }}
        >
          <RecommendationSummaryCard pageType={pageType} filters={recommendationFilters} />
        </Container>
      ) : null}
    </Layout.Horizontal>
  )
}
 
export default WorkloadSummary