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

95.45% Statements 21/22
83.33% Branches 5/6
100% Functions 3/3
95.45% Lines 21/22

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              2x 2x 2x 2x                               2x       2x           5x 5x 5x 5x   5x 5x   5x 10x 4x   6x     6x     5x           5x           5x             5x            
/*
 * 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.
 */
 
export enum AlertStatus {
  Danger,
  Warning,
  Info
}
 
interface CostStatusInfo {
  cost: number
  ratio: number
  percentage: number
  status: AlertStatus
}
 
export interface getAllBudgetCostInfoReturnType {
  actualCostStatus: CostStatusInfo
  forecastedCostStatus: CostStatusInfo
  budgetAmountStatus: CostStatusInfo
}
 
export const getAllBudgetCostInfo: (
  forecastedCost: number,
  actualCost: number,
  budgetAmount: number
) => getAllBudgetCostInfoReturnType = (forecastedCost, actualCost, budgetAmount) => {
  /**
   * Find out the max cost of all and it will be considered as 100%
   * and the rest of the cost percentages will be calculated based on that
   */
 
  const baseCost = Math.max(actualCost, forecastedCost, budgetAmount)
  const actualCostRatio = actualCost / baseCost
  const forecastCostRatio = forecastedCost / baseCost
  const budgetAmountRatio = budgetAmount / baseCost
 
  const actualCostPerWithBudget = (actualCost / budgetAmount) * 100
  const forecastCostPerWithBudget = (forecastedCost / budgetAmount) * 100
 
  const getAlertStatus: (percentage: number) => AlertStatus = percentage => {
    if (percentage > 100) {
      return AlertStatus.Danger
    }
    Iif (percentage > 90) {
      return AlertStatus.Warning
    }
    return AlertStatus.Info
  }
 
  const actualCostStatus: CostStatusInfo = {
    cost: actualCost,
    ratio: actualCostRatio,
    percentage: actualCostPerWithBudget,
    status: getAlertStatus(actualCostPerWithBudget)
  }
  const forecastedCostStatus: CostStatusInfo = {
    cost: forecastedCost,
    ratio: forecastCostRatio,
    percentage: forecastCostPerWithBudget,
    status: getAlertStatus(forecastCostPerWithBudget)
  }
  const budgetAmountStatus: CostStatusInfo = {
    cost: budgetAmount,
    ratio: budgetAmountRatio,
    percentage: 100,
    status: AlertStatus.Info
  }
 
  return {
    actualCostStatus,
    forecastedCostStatus,
    budgetAmountStatus
  }
}