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

86% Statements 86/100
80.95% Branches 68/84
82.35% Functions 14/17
85.11% Lines 80/94

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 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421              1x 1x                     1x 1x 1x 1x 1x 1x 1x   1x               1x 1x 1x 1x       1x 1x 1x 1x               1x 1x 1x 1x   1x 6x   1x 23x   23x   3x   2x   2x   2x   4x   1x   1x         1x         1x         1x         1x               2x   1x                 1x       1x                           1x               17x 17x 17x   17x 17x   17x         17x     17x   17x   17x                     17x 17x   5x     17x   17x         17x 17x   17x   17x 2x                                           2x       17x   17x                             17x       17x 1x                                         3x                                                                         17x                                                             1x                                                   1x                               1x   1x 18x   18x                                                           1x 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, { useCallback, useMemo, useReducer, useRef, useState } from 'react'
import {
  Container,
  Layout,
  Text,
  Card,
  Button,
  ButtonVariation,
  Icon,
  Tabs,
  getErrorInfoFromErrorObject
} from '@wings-software/uicore'
import { Color, FontVariation } from '@harness/design-system'
import { Dialog, Position, Toaster } from '@blueprintjs/core'
import { useModalHook } from '@harness/use-modal'
import { defaultTo, isEqual } from 'lodash-es'
import pDebounce from 'p-debounce'
import { useToaster } from '@common/exports'
import useDidMountEffect from '@ce/common/useDidMountEffect'
import type { NodepoolTimeRangeValue } from '@ce/types'
import { getTimePeriodString, GET_NODEPOOL_DATE_RANGE } from '@ce/utils/momentUtils'
import type {
  NodeRecommendationDto,
  RecommendationItemDto,
  RecommendationOverviewStats,
  RecommendNodePoolClusterRequest,
  TotalResourceUsage
} from 'services/ce/services'
import { useStrings } from 'framework/strings'
import Recommender from '@ce/components/NodeRecommendation/Recommender'
import formatCost from '@ce/utils/formatCost'
import {
  RecommendationDetailsSavingsCard,
  RecommendationDetailsSpendCard
} from '@ce/components/RecommendationDetailsSummaryCards/RecommendationDetailsSummaryCards'
import { TuneRecommendationCard } from '@ce/components/NodeRecommendation/TuneNodeRecommendationCard'
import { RecommendationResponse, RecommendClusterRequest, useRecommendCluster } from 'services/ce/recommenderService'
import { useGetSeries } from 'services/ce/publicPricingService'
import {
  addBufferToState,
  calculateSavingsPercentage,
  convertStateToRecommendClusterPayload,
  getInstanceFamiliesFromState,
  getProviderIcon,
  isResourceConsistent
} from '@ce/utils/recommendationUtils'
import { InstanceFamiliesModalTab } from '../InstanceFamiliesModalTab/InstanceFamiliesModalTab'
import ResourceUtilizationCharts from './ResourceUtilizationCharts'
import { ACTIONS, Action, IState } from './constants'
import css from './NodeRecommendation.module.scss'
 
export const insertOrRemoveIntoArray = (array: string[], val: string): string[] =>
  array.indexOf(val) > -1 ? array.filter(ele => ele !== val) : [...array, val]
 
export const reducer = (state: IState, action: Action) => {
  const { type, data } = action
 
  switch (type) {
    case ACTIONS.SUM_CPUS:
      return { ...state, sumCpu: data }
    case ACTIONS.SUM_MEM:
      return { ...state, sumMem: data }
    case ACTIONS.MAX_CPUS:
      return { ...state, maxCpu: data }
    case ACTIONS.MAX_MEM:
      return { ...state, maxMemory: data }
    case ACTIONS.MIN_NODES:
      return { ...state, minNodes: data }
    case ACTIONS.MAX_NODES:
      return { ...state, maxNodes: data }
    case ACTIONS.INCLUDE_TYPES:
      return {
        ...state,
        includeTypes: insertOrRemoveIntoArray(state.includeTypes, data)
      }
    case ACTIONS.INCLUDE_SERIES:
      return {
        ...state,
        includeSeries: insertOrRemoveIntoArray(state.includeSeries, data)
      }
    case ACTIONS.EXCLUDE_TYPES:
      return {
        ...state,
        excludeTypes: insertOrRemoveIntoArray(state.excludeTypes, data)
      }
    case ACTIONS.EXCLUDE_SERIES:
      return {
        ...state,
        excludeSeries: insertOrRemoveIntoArray(state.excludeSeries, data)
      }
    case ACTIONS.CLEAR_INSTACE_FAMILY:
      return {
        ...state,
        includeTypes: data.includeTypes,
        includeSeries: data.includeSeries,
        excludeTypes: data.excludeTypes,
        excludeSeries: data.excludeSeries
      }
    case ACTIONS.RESET_TO_DEFAULT:
      return data
    case ACTIONS.UPDATE_TIME_RANGE:
      return {
        ...state,
        sumCpu: data.sumCpu,
        sumMem: data.sumMem,
        minNodes: data.minNodes,
        maxCpu: data.maxCpu,
        maxMemory: data.maxMemory
      }
    default:
      return state
  }
}
 
export const UpdatePreferenceToaster = Toaster.create({
  className: css.toaster,
  position: Position.BOTTOM_RIGHT
})
 
interface NodeRecommendationDetailsProps {
  recommendationStats: RecommendationOverviewStats
  recommendationDetails: NodeRecommendationDto
  timeRange: NodepoolTimeRangeValue
  recommendationName: string
  nodeRecommendationRequestData: RecommendNodePoolClusterRequest
  nodePoolData: RecommendationItemDto
}
 
const NodeRecommendationDetails: React.FC<NodeRecommendationDetailsProps> = ({
  recommendationDetails,
  recommendationStats,
  timeRange,
  recommendationName,
  nodeRecommendationRequestData,
  nodePoolData
}) => {
  const { getString } = useStrings()
  const { showError } = useToaster()
  const timeRangeFilter = GET_NODEPOOL_DATE_RANGE[timeRange.value]
 
  const [buffer, setBuffer] = useState(0)
  const [tuneRecomVisible, setTuneRecomVisible] = useState(true)
 
  const { includeTypes, includeSeries, excludeTypes, excludeSeries } = defaultTo(
    recommendationDetails.resourceRequirement,
    {}
  ) as RecommendClusterRequest
 
  const { sumCpu, sumMem, minNodes } = (nodeRecommendationRequestData?.recommendClusterRequest ||
    {}) as RecommendClusterRequest
 
  const { maxcpu, maxmemory } = (nodeRecommendationRequestData?.totalResourceUsage || {}) as TotalResourceUsage
 
  const { provider, region, service } = (recommendationDetails.recommended || {}) as RecommendationResponse
 
  const initialState = {
    maxCpu: +defaultTo(maxcpu, 0).toFixed(2),
    maxMemory: +defaultTo(maxmemory, 0).toFixed(2),
    sumCpu: +defaultTo(sumCpu, 0).toFixed(2),
    sumMem: +defaultTo(sumMem, 0).toFixed(2),
    minNodes: +defaultTo(minNodes, 0).toFixed(2),
    includeTypes: includeTypes || [],
    includeSeries: includeSeries || [],
    excludeTypes: excludeTypes || [],
    excludeSeries: excludeSeries || []
  }
  const [recomDetails, setRecomDetails] = useState(recommendationDetails)
  const [state, dispatch] = useReducer(
    reducer,
    useMemo(() => initialState as IState, [])
  )
 
  const [updatedState, setUpdatedState] = useState(initialState)
 
  const pathParams = {
    provider: defaultTo(provider, ''),
    region: defaultTo(region, ''),
    service: defaultTo(service, '')
  }
  const { mutate: fetchNewRecommendation, loading } = useRecommendCluster(pathParams)
  const { data: seriesData, loading: seriesDataLoading } = useGetSeries(pathParams)
 
  const debouncedFetchNewRecomm = useCallback(pDebounce(fetchNewRecommendation, 500), [])
 
  const updateRecommendationDetails = async () => {
    Iif (isResourceConsistent(state.sumCpu, state.sumMem, state.maxCpu, state.maxMemory, buffer)) {
      try {
        const payload = convertStateToRecommendClusterPayload(
          state,
          defaultTo(recommendationDetails.resourceRequirement, {}) as RecommendClusterRequest,
          buffer
        )
        const response = await debouncedFetchNewRecomm(payload)
        const newState = {
          ...recomDetails,
          recommended: { ...recomDetails.recommended, ...response }
        } as NodeRecommendationDto
 
        if (!isEqual(recomDetails, newState)) {
          setRecomDetails(newState)
        }
        setUpdatedState(addBufferToState(state, buffer))
        UpdatePreferenceToaster.show({ message: getString('ce.nodeRecommendation.updatePreferences'), icon: 'tick' })
      } catch (e: any) {
        showError(getErrorInfoFromErrorObject(e))
      }
    } else {
      showError(getString('ce.nodeRecommendation.inconsistentResourceReq'))
    }
  }
 
  const currentTimeRange = useRef<NodepoolTimeRangeValue>(timeRange)
 
  useDidMountEffect(() => {
    dispatch({
      type: ACTIONS.UPDATE_TIME_RANGE,
      data: {
        maxCpu: +defaultTo(maxcpu, 0).toFixed(2),
        maxMemory: +defaultTo(maxmemory, 0).toFixed(2),
        sumCpu: +defaultTo(sumCpu, 0).toFixed(2),
        sumMem: +defaultTo(sumMem, 0).toFixed(2),
        minNodes: +defaultTo(minNodes, 0).toFixed(2)
      }
    })
 
    currentTimeRange.current = timeRange
  }, [timeRange])
 
  useDidMountEffect(() => {
    updateRecommendationDetails()
  }, [currentTimeRange.current])
 
  const [showModal, hideModal] = useModalHook(() => {
    return (
      <Dialog
        isOpen={true}
        enforceFocus={false}
        onClose={hideModal}
        style={{ width: 1175 }}
        className={css.modalContainer}
      >
        <Layout.Vertical spacing="medium" padding="xxlarge" height="100%">
          <Text
            font={{ variation: FontVariation.H4 }}
            icon={getProviderIcon(defaultTo(recommendationDetails.recommended?.provider, ''))}
          >
            {`${recommendationName}: Preferred Instance Families`}
          </Text>
          <Text font={{ variation: FontVariation.SMALL }}>
            {getString('ce.nodeRecommendation.instaceFamiliesModalDesc')}
          </Text>
          <Container height="100%">
            <Tabs
              id={'horizontalTabs'}
              tabList={Object.keys(defaultTo(seriesData?.categoryDetails, {})).map(key => ({
                id: key,
                title: key,
                panel: (
                  <InstanceFamiliesModalTab
                    data={seriesData?.categoryDetails ? seriesData?.categoryDetails[key] : {}}
                    state={state}
                    dispatch={dispatch}
                  />
                )
              }))}
            />
          </Container>
          <InstaceFamiliesModalButtonGroup
            onSave={hideModal}
            onCancel={() => {
              hideModal()
              dispatch({
                type: ACTIONS.CLEAR_INSTACE_FAMILY,
                data: {
                  includeTypes: updatedState.includeTypes,
                  includeSeries: updatedState.includeSeries,
                  excludeTypes: updatedState.excludeTypes,
                  excludeSeries: updatedState.excludeSeries
                }
              })
            }}
            saveButtonDisabled={isEqual(
              getInstanceFamiliesFromState(state),
              getInstanceFamiliesFromState(updatedState)
            )}
          />
        </Layout.Vertical>
      </Dialog>
    )
  }, [seriesDataLoading, state, updatedState])
 
  return (
    <>
      <Layout.Vertical>
        <Card style={{ paddingLeft: 0, paddingRight: 0 }}>
          <Layout.Horizontal padding={{ left: 'large' }}>
            <Container width={400}>
              <RecommendationDetailsSpendCard
                withRecommendationAmount={formatCost(
                  recommendationStats?.totalMonthlyCost - recommendationStats?.totalMonthlySaving
                )}
                withoutRecommendationAmount={formatCost(recommendationStats?.totalMonthlyCost)}
                title={`${getString('ce.recommendation.listPage.monthlyPotentialCostText')}`}
                spentBy={getTimePeriodString(timeRangeFilter[1], 'MMM DD')}
              />
            </Container>
            <Container>
              <RecommendationDetailsSavingsCard
                amount={formatCost(recommendationStats?.totalMonthlySaving)}
                title={getString('ce.recommendation.listPage.monthlySavingsText')}
                amountSubTitle={calculateSavingsPercentage(
                  recommendationStats?.totalMonthlySaving,
                  recommendationStats?.totalMonthlyCost
                )}
                subTitle={`${getTimePeriodString(timeRangeFilter[0], 'MMM DD')} - ${getTimePeriodString(
                  timeRangeFilter[1],
                  'MMM DD'
                )}`}
              />
            </Container>
          </Layout.Horizontal>
          <Recommender stats={recommendationStats} details={recomDetails} loading={loading} />
          <TuneRecommendationHelpText toggleCardVisible={() => setTuneRecomVisible(prevState => !prevState)} />
        </Card>
      </Layout.Vertical>
      <Layout.Vertical spacing="large" padding="xlarge">
        <Text font={{ variation: FontVariation.H5 }}>{getString('ce.recommendation.detailsPage.nodepoolDetails')}</Text>
        <Container margin={{ bottom: 'large' }}>
          <Text font={{ variation: FontVariation.SMALL_SEMI }} color={Color.GREY_500}>
            {getString('common.cluster')}
          </Text>
          <Text font={{ variation: FontVariation.SMALL_SEMI }} color={Color.GREY_800}>
            {nodePoolData.clusterName}
          </Text>
        </Container>
        <ResourceUtilizationCharts
          sumCpu={+defaultTo(sumCpu, 0).toFixed(2)}
          sumMem={+defaultTo(sumMem, 0).toFixed(2)}
          minNodes={+defaultTo(minNodes, 0).toFixed(2)}
          timeRange={timeRange}
        />
        <Text font={{ variation: FontVariation.H5 }} padding={{ top: 'xsmall' }}>
          {getString('ce.recommendation.detailsPage.tuneRecommendations')}
        </Text>
        <Card className={css.tuneRecommendationCard}>
          <TuneRecommendationCard
            cardVisible={tuneRecomVisible}
            loading={loading}
            toggleCardVisible={() => setTuneRecomVisible(prevState => !prevState)}
            state={state}
            dispatch={dispatch}
            buffer={buffer}
            setBuffer={setBuffer}
            showInstanceFamiliesModal={showModal}
            initialState={initialState}
            updatedState={updatedState}
            updateRecommendationDetails={updateRecommendationDetails}
          />
        </Card>
      </Layout.Vertical>
    </>
  )
}
 
export default NodeRecommendationDetails
 
export const TuneRecommendationHelpText: React.FC<{ toggleCardVisible: () => void }> = ({ toggleCardVisible }) => {
  const { getString } = useStrings()
 
  return (
    <Container margin="medium" className={css.tuneRecomInfoContainer} padding="medium" background={Color.BLUE_50}>
      <Layout.Horizontal spacing="xsmall">
        <Icon name="info-messaging" />
        <Container>
          <Text inline color={Color.GREY_700} font={{ variation: FontVariation.SMALL }}>
            {getString('ce.nodeRecommendation.tuneRecommendationsInfo1')}
          </Text>
          <Text
            inline
            color={Color.PRIMARY_7}
            font={{ variation: FontVariation.SMALL }}
            onClick={toggleCardVisible}
            className={css.pointer}
          >
            {getString('ce.recommendation.detailsPage.tuneRecommendations').toLowerCase()}
          </Text>
          <Text inline color={Color.GREY_700} font={{ variation: FontVariation.SMALL }}>
            {getString('ce.nodeRecommendation.tuneRecommendationsInfo2')}
          </Text>
        </Container>
      </Layout.Horizontal>
    </Container>
  )
}
 
const InstaceFamiliesModalButtonGroup: React.FC<{
  onSave: () => void
  onCancel: () => void
  saveButtonDisabled: boolean
}> = ({ onCancel, onSave, saveButtonDisabled }) => {
  const { getString } = useStrings()
 
  return (
    <Layout.Horizontal spacing="medium">
      <Button variation={ButtonVariation.PRIMARY} onClick={onSave} disabled={saveButtonDisabled}>
        {getString('ce.nodeRecommendation.savePreferences')}
      </Button>
      <Button variation={ButtonVariation.TERTIARY} onClick={onCancel}>
        {getString('cancel')}
      </Button>
    </Layout.Horizontal>
  )
}