All files / modules/75-ce/utils recommendationUtils.ts

89.19% Statements 33/37
85.71% Branches 12/14
87.5% Functions 7/8
85.19% Lines 23/27

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                      7x 2x           2x     7x 105x   7x 29x   7x             5x 5x     5x     5x         5x     7x             2x   2x 2x   2x 1x     2x     7x                                                             18x                         7x            
/*
 * 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 type { IconName } from '@harness/uicore'
import type { IState } from '@ce/components/NodeRecommendation/constants'
import type { RecommendClusterRequest } from 'services/ce/recommenderService'
 
export const getProviderIcon = (provider: string): IconName => {
  const iconMapping: Record<string, IconName> = {
    google: 'gcp',
    azure: 'service-azure',
    amazon: 'service-aws'
  }
 
  return iconMapping[provider] || 'app-kubernetes'
}
 
export const addBufferToValue = (value: number, bufferPercentage: number): number =>
  +(((100 + bufferPercentage) / 100) * value).toFixed(2)
 
export const calculateSavingsPercentage = (savings: number, totalCost: number): string =>
  `(${Math.floor((savings / totalCost) * 100)}%)`
 
export const isResourceConsistent = (
  sumCpu: number,
  sumMemory: number,
  maxCpu: number,
  maxMemory: number,
  buffer: number
): boolean => {
  const sumCpuWithBuffer = addBufferToValue(sumCpu, buffer)
  const sumMemWithBuffer = addBufferToValue(sumMemory, buffer)
 
  const isInconsistent =
    Math.round(sumCpuWithBuffer) < Math.round(maxCpu) || Math.round(sumMemWithBuffer) < Math.round(maxMemory)
 
  const anyZero =
    Math.round(sumCpuWithBuffer) === 0 ||
    Math.round(sumMemWithBuffer) === 0 ||
    Math.round(maxCpu) === 0 ||
    Math.round(maxMemory) === 0
 
  return !isInconsistent && !anyZero
}
 
export const calculateNodes = (
  sumCpu: number,
  sumMemory: number,
  maxCpu: number,
  maxMemory: number,
  minNodes: number
): { maximumNodes: number; minimumNodes: number } => {
  let minimumNodes = minNodes || 3
 
  let maximumNodes = Math.min(Math.floor(sumCpu / maxCpu), Math.floor(sumMemory / maxMemory))
  maximumNodes = Math.max(maximumNodes, 1)
 
  if (maximumNodes < minNodes) {
    minimumNodes = maximumNodes
  }
 
  return { maximumNodes, minimumNodes }
}
 
export const convertStateToRecommendClusterPayload = (
  state: IState,
  resourceRequirement: RecommendClusterRequest,
  buffer: number
): RecommendClusterRequest => {
  const sumCpuWithBuffer = addBufferToValue(state.sumCpu, buffer)
  const sumMemWithBuffer = addBufferToValue(state.sumMem, buffer)
 
  const { maximumNodes, minimumNodes } = calculateNodes(
    sumCpuWithBuffer,
    sumMemWithBuffer,
    state.maxCpu,
    +state.maxMemory,
    +state.minNodes
  )
 
  return {
    ...resourceRequirement,
    sumCpu: +sumCpuWithBuffer,
    sumMem: +sumMemWithBuffer,
    minCpu: +state.maxCpu,
    minMem: +state.maxMemory,
    maxNodes: maximumNodes,
    minNodes: minimumNodes,
    includeSeries: state.includeSeries,
    includeTypes: state.includeTypes,
    excludeSeries: state.excludeSeries,
    excludeTypes: state.excludeTypes
  }
}
 
export const addBufferToState = (state: IState, buffer: number): IState => ({
  ...state,
  sumCpu: addBufferToValue(state.sumCpu, buffer),
  sumMem: addBufferToValue(state.sumMem, buffer)
})
 
interface InstanceFamilies {
  includeSeries: string[]
  includeTypes: string[]
  excludeSeries: string[]
  excludeTypes: string[]
}
 
export const getInstanceFamiliesFromState = (state: IState): InstanceFamilies => ({
  includeSeries: state.includeSeries,
  includeTypes: state.includeTypes,
  excludeSeries: state.excludeSeries,
  excludeTypes: state.excludeTypes
})