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

84% Statements 42/50
62.5% Branches 15/24
88.89% Functions 8/9
80.95% Lines 34/42

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              3x   3x 3x       29x   3x 9x     9x 8x   1x     3x 18x     18x 15x   3x       3x 2x     2x     2x       3x 16x     3x 16x     3x         3x           3x 28x 28x 7x 21x     21x     3x 28x 28x     28x    
/*
 * 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 { QualityOfService } from '@ce/types'
 
const BYTES_IN_A_GB = 1000000000
const BYTES_IN_A_MB = 1000000
 
type fnNumberToString = (val: number) => string
 
const log10: (n: number) => number = n => Math.log(n) / Math.log(10)
 
export const getCPUValueInReadableForm: (val: number) => string = (val: number) => {
  Iif (!val) {
    return '0'
  }
  if (log10(val) < 0) {
    return `${Math.ceil(val * 1000)}m`
  }
  return val % 1 === 0 ? `${val}` : `${(val + 0.1).toFixed(1)}`
}
 
export const getMemValueInReadableForm: (val: number) => string = (val: number) => {
  Iif (!val) {
    return '0'
  }
  if (log10(val) >= 9) {
    return val % 1 === 0 ? `${val / BYTES_IN_A_GB}Gi` : `${(val / BYTES_IN_A_GB + 0.1).toFixed(1)}Gi`
  } else {
    return `${Math.ceil(val / BYTES_IN_A_MB)}Mi`
  }
}
 
export const getMemValueInReadableFormForChart: fnNumberToString = val => {
  Iif (!val) {
    return '0'
  }
  Iif (log10(val) >= 9) {
    return `${(val / BYTES_IN_A_GB).toFixed(2)}Gi`
  } else {
    return `${(val / BYTES_IN_A_MB).toFixed(2)}Mi`
  }
}
 
export const getCPUValueInmCPUs: fnNumberToString = val => {
  return `${val * 1000}`
}
 
export const getMemValueInGB: fnNumberToString = val => {
  return `${val / BYTES_IN_A_GB}Gi`
}
 
export const getRecommendationYaml: (
  cpuReqVal: string | number,
  memReqVal: string | number,
  memLimitVal: string | number,
  qualityOfService: QualityOfService
) => string = (cpuReqVal, memReqVal, memLimitVal, qualityOfService) => {
  const cpuLimitVal = qualityOfService === QualityOfService.GUARANTEED ? `\n  cpu: ${cpuReqVal}` : null
 
  return `limits:\n  memory: ${memLimitVal}${cpuLimitVal}\nrequests:\n  memory: ${memReqVal}\n  cpu: ${cpuReqVal}\n`
}
 
export const getMemoryValueInGBFromExpression: (val: string | number) => number = val => {
  const stringVal = `${val}`
  if (stringVal.includes('Gi')) {
    return Number(stringVal.split('Gi')[0])
  } else Iif (stringVal.includes('Mi')) {
    return Number(stringVal.split('Mi')[0]) / 1000
  }
  return Number(val) / BYTES_IN_A_GB
}
 
export const getCPUValueInCPUFromExpression: (val: string | number) => number = val => {
  const stringVal = `${val}`
  Iif (stringVal.includes('m')) {
    return Number(stringVal.split('m')[0]) / 1000
  }
  return Number(val)
}