All files / modules/75-ce/components/NodeRecommendation TuneNodeRecommendationCard.tsx

97.87% Statements 46/47
100% Branches 16/16
94.12% Functions 16/17
97.78% Lines 44/45

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              2x 2x 2x 2x 2x 2x 2x 2x 2x             2x       20x   20x                                                                   2x                         20x 20x   20x                                                                                               2x 18x   18x                               2x                                   1x                           2x 18x   18x                                                                   2x 18x   18x                           1x                         1x                   2x 18x   18x                     1x                 1x               1x                         2x 18x   18x                                                                                                     2x 18x   18x   18x                           1x 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 from 'react'
import { Container, Layout, Text, TextInput, Button, Icon, ButtonVariation } from '@wings-software/uicore'
import { Color, FontVariation } from '@harness/design-system'
import { Slider } from '@blueprintjs/core'
import { isEqual } from 'lodash-es'
import { useStrings } from 'framework/strings'
import { addBufferToState, addBufferToValue } from '@ce/utils/recommendationUtils'
import { ACTIONS, Action, IState } from './constants'
import css from './NodeRecommendation.module.scss'
 
interface TuneRecommendationCardHeaderProps {
  cardVisible: boolean
  toggleCardVisible: () => void
}
 
export const TuneRecommendationCardHeader: React.FC<TuneRecommendationCardHeaderProps> = ({
  cardVisible,
  toggleCardVisible
}) => {
  const { getString } = useStrings()
 
  return (
    <Container padding="medium" flex className={css.spaceBetween}>
      <Text
        className={css.pointer}
        font={{ variation: FontVariation.H6 }}
        color={cardVisible ? Color.GREY_800 : Color.PRIMARY_7}
        onClick={toggleCardVisible}
      >
        {getString('ce.nodeRecommendation.setInstancePreferences')}
      </Text>
      <Icon
        name={cardVisible ? 'caret-up' : 'caret-down'}
        onClick={toggleCardVisible}
        color={Color.PRIMARY_7}
        className={css.pointer}
      />
    </Container>
  )
}
 
interface TuneRecommendationCardProps {
  loading: boolean
  cardVisible: boolean
  toggleCardVisible: () => void
  state: IState
  dispatch: React.Dispatch<Action>
  buffer: number
  setBuffer: React.Dispatch<React.SetStateAction<number>>
  showInstanceFamiliesModal: () => void
  initialState: IState
  updatedState: IState
  updateRecommendationDetails: () => void
}
 
export const TuneRecommendationCard: React.FC<TuneRecommendationCardProps> = props => {
  const {
    loading,
    cardVisible,
    toggleCardVisible,
    state,
    dispatch,
    buffer,
    setBuffer,
    showInstanceFamiliesModal,
    initialState,
    updateRecommendationDetails,
    updatedState
  } = props
  const { getString } = useStrings()
 
  return (
    <>
      <TuneRecommendationCardHeader cardVisible={cardVisible} toggleCardVisible={toggleCardVisible} />
      {cardVisible ? (
        <Container className={css.preferences} padding="medium" background={Color.PRIMARY_1}>
          <Layout.Vertical spacing="medium">
            <Container>
              <Text
                font={{ variation: FontVariation.SMALL_SEMI }}
                tooltipProps={{ dataTooltipId: 'prefResourceNeeds' }}
              >
                {getString('ce.nodeRecommendation.prefResourceNeeds')}
              </Text>
              <Layout.Horizontal flex className={css.spaceBetween} spacing="medium" margin={{ top: 'small' }}>
                <Layout.Horizontal spacing="medium">
                  <Resources state={state} dispatch={dispatch} />
                  <Container flex={{ justifyContent: 'center' }}>
                    <Container style={{ borderWidth: 1, borderStyle: 'solid', borderLeft: 0, width: 14, height: 60 }} />
                  </Container>
                  <Container flex={{ justifyContent: 'center' }} margin={{ right: 'small' }}>
                    <Icon name="plus" size={12} color={Color.GREY_700} />
                  </Container>
                </Layout.Horizontal>
                <Buffer state={state} buffer={buffer} setBuffer={setBuffer} />
              </Layout.Horizontal>
              <Layout.Horizontal padding={{ top: 'medium' }} spacing="medium">
                <LargestResources state={state} dispatch={dispatch} />
                <Nodes state={state} dispatch={dispatch} />
              </Layout.Horizontal>
            </Container>
            <InstanceFamilies showInstanceFamiliesModal={showInstanceFamiliesModal} state={state} />
            <ApplyPreferencesButtonGroup
              dispatch={dispatch}
              setBuffer={setBuffer}
              state={state}
              initialState={initialState}
              updatedState={updatedState}
              updateRecommendationDetails={updateRecommendationDetails}
              buffer={buffer}
              loading={loading}
            />
          </Layout.Vertical>
        </Container>
      ) : null}
    </>
  )
}
 
const Resources: React.FC<{ dispatch: React.Dispatch<Action>; state: IState }> = ({ dispatch, state }) => {
  const { getString } = useStrings()
 
  return (
    <Container>
      <Layout.Vertical spacing="small">
        <Container flex className={css.spaceBetween}>
          <Text
            padding={{ right: 'small' }}
            inline
            color={Color.GREY_500}
            font={{ variation: FontVariation.SMALL_SEMI }}
          >
            {getString('ce.nodeRecommendation.cpus')}
          </Text>
          <TextInput
            value={`${state.sumCpu}`}
            wrapperClassName={css.input}
            onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
              dispatch({ type: ACTIONS.SUM_CPUS, data: e.target.value })
            }
            data-testid="sumCpu-input"
          />
        </Container>
        <Container flex className={css.spaceBetween}>
          <Text
            padding={{ right: 'small' }}
            inline
            color={Color.GREY_500}
            font={{ variation: FontVariation.SMALL_SEMI }}
          >
            {getString('ce.nodeRecommendation.mem')}
          </Text>
          <TextInput
            value={`${state.sumMem}`}
            wrapperClassName={css.input}
            onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
              dispatch({ type: ACTIONS.SUM_MEM, data: e.target.value })
            }
            data-testid="sumMem-input"
          />
        </Container>
      </Layout.Vertical>
    </Container>
  )
}
 
const Buffer: React.FC<{
  state: IState
  buffer: number
  setBuffer: React.Dispatch<React.SetStateAction<number>>
}> = ({ state, buffer, setBuffer }) => {
  const { getString } = useStrings()
 
  return (
    <Container width="60%">
      <Layout.Vertical spacing="xxsmall">
        <Layout.Horizontal style={{ justifyContent: 'space-between' }}>
          <Text
            font={{ variation: FontVariation.SMALL_SEMI }}
            rightIcon="info"
            rightIconProps={{ size: 12, color: Color.GREY_800 }}
            tooltipProps={{ dataTooltipId: 'buffer' }}
          >
            {getString('ce.nodeRecommendation.buffer')}
          </Text>
          <Text font={{ variation: FontVariation.SMALL_SEMI }} color={Color.GREY_400}>{`${buffer}%`}</Text>
        </Layout.Horizontal>
        <Slider
          min={0}
          max={100}
          stepSize={1}
          labelRenderer={false}
          value={buffer}
          onChange={val => setBuffer(val)}
          className={css.bufferSlider}
        />
        <Text font={{ variation: FontVariation.SMALL }} margin={{ top: 'xsmall' }}>
          {getString('ce.nodeRecommendation.cpuAndMemeValWithBuffer', {
            cpu: addBufferToValue(state.sumCpu, buffer),
            mem: addBufferToValue(state.sumMem, buffer)
          })}
        </Text>
      </Layout.Vertical>
    </Container>
  )
}
 
const LargestResources: React.FC<{ dispatch: React.Dispatch<Action>; state: IState }> = ({ dispatch, state }) => {
  const { getString } = useStrings()
 
  return (
    <Container>
      <Layout.Vertical spacing="small">
        <Text font={{ variation: FontVariation.SMALL_SEMI }} tooltipProps={{ dataTooltipId: 'largestWorkloadReq' }}>
          {getString('ce.nodeRecommendation.largestWorkloadReq')}
        </Text>
        <Container flex className={css.spaceBetween} width="75%">
          <Text inline color={Color.GREY_500} font={{ variation: FontVariation.SMALL_SEMI }}>
            {getString('ce.nodeRecommendation.cpus')}
          </Text>
          <TextInput
            value={`${state.maxCpu}`}
            wrapperClassName={css.input}
            onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
              dispatch({ type: ACTIONS.MAX_CPUS, data: e.target.value })
            }
            data-testid="maxCpu-input"
          />
        </Container>
        <Container flex className={css.spaceBetween} width="75%">
          <Text inline color={Color.GREY_500} font={{ variation: FontVariation.SMALL_SEMI }}>
            {getString('ce.nodeRecommendation.mem')}
          </Text>
          <TextInput
            value={`${state.maxMemory}`}
            wrapperClassName={css.input}
            onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
              dispatch({ type: ACTIONS.MAX_MEM, data: e.target.value })
            }
            data-testid="maxMemory-input"
          />
        </Container>
      </Layout.Vertical>
    </Container>
  )
}
 
const Nodes: React.FC<{ dispatch: React.Dispatch<Action>; state: IState }> = ({ dispatch, state }) => {
  const { getString } = useStrings()
 
  return (
    <Container padding={{ left: 'large' }}>
      <Layout.Vertical spacing="small">
        <Text font={{ variation: FontVariation.SMALL_SEMI }} tooltipProps={{ dataTooltipId: 'prefMinNodeCount' }}>
          {getString('ce.nodeRecommendation.prefMinNodeCount')}
        </Text>
        <Layout.Horizontal spacing="small" className={css.minNodeContainer}>
          <Button
            icon="minus"
            variation={ButtonVariation.SECONDARY}
            onClick={() => {
              dispatch({ type: ACTIONS.MIN_NODES, data: state.minNodes - 1 })
            }}
            disabled={state.minNodes <= 0}
            data-testid="increment-minnode-btn"
          />
          <TextInput
            value={`${state.minNodes}`}
            wrapperClassName={css.minNodeInput}
            onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
              dispatch({ type: ACTIONS.MIN_NODES, data: +e.target.value })
            }
            data-testid="minnodes-input"
          />
          <Button
            icon="plus"
            variation={ButtonVariation.SECONDARY}
            onClick={() => {
              dispatch({ type: ACTIONS.MIN_NODES, data: state.minNodes + 1 })
            }}
            data-testid="decrement-minnode-btn"
          />
        </Layout.Horizontal>
      </Layout.Vertical>
    </Container>
  )
}
 
const InstanceFamilies: React.FC<{
  state: IState
  showInstanceFamiliesModal: () => void
}> = ({ state, showInstanceFamiliesModal }) => {
  const { getString } = useStrings()
 
  return (
    <Container>
      <Container margin={{ bottom: 'small' }}>
        <Text
          inline
          font={{ variation: FontVariation.SMALL_SEMI }}
          // tooltipProps={{ dataTooltipId: 'preferredInstanceFamilies' }}
        >
          {getString('ce.nodeRecommendation.preferredInstanceFamilies')}
        </Text>
        {state.includeSeries.length || state.includeTypes.length ? (
          <Button
            inline
            variation={ButtonVariation.LINK}
            icon="edit"
            onClick={showInstanceFamiliesModal}
            iconProps={{ size: 12 }}
          >
            {getString('edit')}
          </Button>
        ) : null}
      </Container>
      {state.includeSeries.length || state.includeTypes.length ? (
        <TextInput
          value={[...state.includeSeries, ...state.includeTypes].join(', ')}
          contentEditable={false}
          className={css.instaceFamilyInput}
          readOnly
        />
      ) : null}
      <Button
        icon="plus"
        variation={ButtonVariation.LINK}
        margin={{ bottom: 'small' }}
        onClick={showInstanceFamiliesModal}
      >
        {getString('ce.nodeRecommendation.addPreferredInstanceFamilies')}
      </Button>
    </Container>
  )
}
 
const ApplyPreferencesButtonGroup: React.FC<{
  updateRecommendationDetails: () => void
  state: IState
  initialState: IState
  updatedState: IState
  dispatch: React.Dispatch<Action>
  setBuffer: React.Dispatch<React.SetStateAction<number>>
  buffer: number
  loading: boolean
}> = ({ updateRecommendationDetails, state, initialState, updatedState, dispatch, setBuffer, buffer, loading }) => {
  const { getString } = useStrings()
 
  const stateWithBuffer = addBufferToState(state, buffer)
 
  return (
    <Layout.Horizontal spacing="small">
      <Button
        variation={ButtonVariation.PRIMARY}
        onClick={updateRecommendationDetails}
        disabled={isEqual(stateWithBuffer, updatedState)}
        loading={loading}
      >
        {getString('ce.nodeRecommendation.applyPreferences')}
      </Button>
      {!isEqual(stateWithBuffer, initialState) ? (
        <Button
          variation={ButtonVariation.SECONDARY}
          onClick={() => {
            setBuffer(0)
            dispatch({
              type: ACTIONS.RESET_TO_DEFAULT,
              data: initialState
            })
          }}
        >
          {getString('ce.recommendation.detailsPage.resetRecommendationText')}
        </Button>
      ) : null}
    </Layout.Horizontal>
  )
}