All files / modules/85-cv/utils CommonUtils.ts

86.87% Statements 86/99
68.06% Branches 98/144
90.91% Functions 20/22
85.87% Lines 79/92

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              127x 127x 127x             127x 127x 127x 127x 127x     127x 127x 127x 127x 127x 127x 127x     127x 127x             127x         1311x   1311x   257x   41x   73x   97x   2x   841x       127x       124x   32x   18x   18x   18x       38x       127x     332x   223x       19x   9x   40x   41x               127x             127x 496x 23x           473x     127x         30x     30x                       30x                     127x 63x     68x 60x         127x 44x     48x 10x         127x 414x     127x 5x     127x 359x   90x   176x     63x     30x       127x 361x   90x   176x     65x     30x       127x 206x     25x     90x     29x     62x      
/*
 * Copyright 2022 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 { get } from 'lodash-es'
import { Utils, Views, SelectOption, MultiSelectOption } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import type { UseStringsReturn } from 'framework/strings'
import type { ResponseListEnvironmentResponse, EnvironmentResponse } from 'services/cd-ng'
import type { StringsMap } from 'stringTypes'
import type { MonitoredServiceEnum } from '@cv/pages/monitored-service/MonitoredServicePage.constants'
import type { SloHealthIndicatorDTO } from 'services/cv'
 
export enum EVENT_TYPE {
  KNOWN = 'KNOWN',
  UNKNOWN = 'UNKNOWN',
  FREQUENCY = 'UNEXPECTED_FREQUENCY',
  BASELINE = 'BASELINE'
}
 
export enum RiskValues {
  NO_DATA = 'NO_DATA',
  NO_ANALYSIS = 'NO_ANALYSIS',
  HEALTHY = 'HEALTHY',
  OBSERVE = 'OBSERVE',
  NEED_ATTENTION = 'NEED_ATTENTION',
  UNHEALTHY = 'UNHEALTHY'
}
 
export enum SLOErrorBudget {
  EXHAUSTED = 'EXHAUSTED'
}
 
// Need to remove once removed from BE.
type OldRiskTypes = 'LOW' | 'MEDIUM' | 'HIGH'
type RiskTypes = keyof typeof RiskValues | OldRiskTypes
 
export const getRiskColorValue = (
  riskStatus?: RiskTypes | SloHealthIndicatorDTO['errorBudgetRisk'],
  realCSSColor = true,
  dark = true
): string => {
  const COLOR_NO_DATA = dark ? Color.GREY_400 : Color.GREY_100
 
  switch (riskStatus) {
    case RiskValues.HEALTHY:
      return realCSSColor ? Utils.getRealCSSColor(Color.GREEN_500) : Color.GREEN_500
    case RiskValues.OBSERVE:
      return realCSSColor ? Utils.getRealCSSColor(Color.YELLOW_800) : Color.YELLOW_800
    case RiskValues.NEED_ATTENTION:
      return realCSSColor ? Utils.getRealCSSColor(Color.ORANGE_600) : Color.ORANGE_600
    case RiskValues.UNHEALTHY:
      return realCSSColor ? Utils.getRealCSSColor(Color.RED_600) : Color.RED_600
    case SLOErrorBudget.EXHAUSTED:
      return realCSSColor ? Utils.getRealCSSColor(Color.RED_800) : Color.RED_800
    default:
      return realCSSColor ? Utils.getRealCSSColor(COLOR_NO_DATA) : COLOR_NO_DATA
  }
}
 
export function getSecondaryRiskColorValue(
  riskStatus?: RiskTypes | SloHealthIndicatorDTO['errorBudgetRisk'],
  realCSSColor = true
): string {
  switch (riskStatus) {
    case RiskValues.HEALTHY:
      return realCSSColor ? Utils.getRealCSSColor(Color.GREEN_50) : Color.GREEN_50
    case RiskValues.OBSERVE:
      return realCSSColor ? Utils.getRealCSSColor(Color.YELLOW_50) : Color.YELLOW_50
    case RiskValues.NEED_ATTENTION:
      return realCSSColor ? Utils.getRealCSSColor(Color.ORANGE_50) : Color.ORANGE_50
    case RiskValues.UNHEALTHY:
      return realCSSColor ? Utils.getRealCSSColor(Color.RED_50) : Color.RED_50
    case SLOErrorBudget.EXHAUSTED:
      return realCSSColor ? Utils.getRealCSSColor(Color.RED_100) : Color.RED_100
    default:
      return realCSSColor ? Utils.getRealCSSColor(Color.GREY_50) : Color.GREY_50
  }
}
 
export const getRiskLabelStringId = (
  riskStatus?: RiskTypes | SloHealthIndicatorDTO['errorBudgetRisk']
): keyof StringsMap => {
  switch (riskStatus) {
    case RiskValues.NO_DATA:
      return 'noData'
    case RiskValues.NO_ANALYSIS:
      return 'cv.noAnalysis'
    case RiskValues.HEALTHY:
      return 'cv.monitoredServices.serviceHealth.serviceDependencies.states.healthy'
    case RiskValues.OBSERVE:
      return 'cv.monitoredServices.serviceHealth.serviceDependencies.states.observe'
    case RiskValues.NEED_ATTENTION:
      return 'cv.monitoredServices.serviceHealth.serviceDependencies.states.needsAttention'
    case RiskValues.UNHEALTHY:
      return 'cv.monitoredServices.serviceHealth.serviceDependencies.states.unhealthy'
    case SLOErrorBudget.EXHAUSTED:
      return 'cv.monitoredServices.serviceHealth.serviceDependencies.states.exhausted'
    default:
      return 'na'
  }
}
 
export function roundNumber(value: number, precision = 2) {
  if (Number.isInteger(precision) && precision >= 0) {
    const factor = 10 ** precision
    return Math.round(value * factor) / factor
  }
}
 
export function getErrorMessage(errorObj?: any): string | undefined {
  if (get(errorObj, 'data')) {
    return (
      get(errorObj, 'data.detailedMessage') ||
      get(errorObj, 'data.message') ||
      JSON.stringify(get(errorObj, 'data'), null, '\t')
    )
  }
  return get(errorObj, 'message')
}
 
export const getEnvironmentOptions = (
  environmentList: ResponseListEnvironmentResponse | null,
  loading: boolean,
  getString: UseStringsReturn['getString']
): SelectOption[] => {
  Iif (loading) {
    return [{ label: getString('loading'), value: 'loading' }]
  }
  Iif (environmentList?.data?.length) {
    const allOption: SelectOption = { label: getString('all'), value: getString('all') }
    const environmentSelectOption: SelectOption[] =
      environmentList?.data?.map((environmentData: EnvironmentResponse) => {
        const { name = '', identifier = '' } = environmentData?.environment || {}
        return {
          label: name,
          value: identifier
        }
      }) || []
    return [allOption, ...environmentSelectOption]
  }
  return []
}
 
interface GetCVMonitoringServicesSearchParamProps {
  view?: Views
  tab?: MonitoredServiceEnum
  redirectToSLO?: boolean
  sloIdentifier?: string
  monitoredServiceIdentifier?: string
}
 
export const getCVMonitoringServicesSearchParam = (props: GetCVMonitoringServicesSearchParamProps): string => {
  return (
    '?' +
    Object.entries(props)
      .filter(param => param[1] !== undefined)
      .map(param => `${param[0]}=${param[1]}`)
      .join('&')
  )
}
 
export function getSearchString(params: { [key: string]: unknown }): string {
  return (
    '?' +
    Object.entries(params)
      .filter(param => param[1] !== undefined)
      .map(param => `${param[0]}=${param[1]}`)
      .join('&')
  )
}
 
export const prepareFilterInfo = (data?: MultiSelectOption[]): Array<string | number> => {
  return data ? data.map((d: MultiSelectOption) => d.value as string) : []
}
 
export const isNumeric = (val: string): boolean => {
  return /^-?\d+$/.test(val)
}
 
export function getEventTypeColor(eventType?: string, realCSSColor = true): string {
  switch (eventType) {
    case EVENT_TYPE.UNKNOWN:
      return realCSSColor ? Utils.getRealCSSColor(Color.RED_800) : Color.RED_800
    case EVENT_TYPE.KNOWN:
      return realCSSColor ? Utils.getRealCSSColor(Color.PRIMARY_7) : Color.PRIMARY_7
    case EVENT_TYPE.FREQUENCY:
    case 'UNEXPECTED':
      return realCSSColor ? Utils.getRealCSSColor(Color.YELLOW_800) : Color.YELLOW_800
    case EVENT_TYPE.BASELINE:
    default:
      return realCSSColor ? Utils.getRealCSSColor(Color.GREY_700) : Color.GREY_700
  }
}
 
export function getEventTypeLightColor(eventType?: string, realCSSColor = true): string {
  switch (eventType) {
    case EVENT_TYPE.UNKNOWN:
      return realCSSColor ? Utils.getRealCSSColor(Color.RED_50) : Color.RED_50
    case EVENT_TYPE.KNOWN:
      return realCSSColor ? Utils.getRealCSSColor(Color.PRIMARY_2) : Color.PRIMARY_2
    case EVENT_TYPE.FREQUENCY:
    case 'UNEXPECTED':
      return realCSSColor ? Utils.getRealCSSColor(Color.YELLOW_200) : Color.YELLOW_200
    case EVENT_TYPE.BASELINE:
    default:
      return realCSSColor ? Utils.getRealCSSColor(Color.GREY_200) : Color.GREY_200
  }
}
 
export function getEventTypeChartColor(eventType?: string, realCSSColor = true): string {
  switch (eventType) {
    case EVENT_TYPE.UNKNOWN:
    case 'UNKNOWN_EVENT':
      return realCSSColor ? Utils.getRealCSSColor(Color.RED_400) : Color.RED_400
    case EVENT_TYPE.KNOWN:
    case 'KNOWN_EVENT':
      return realCSSColor ? Utils.getRealCSSColor(Color.PRIMARY_4) : Color.PRIMARY_4
    case EVENT_TYPE.FREQUENCY:
    case 'UNEXPECTED_FREQUENCY':
      return realCSSColor ? Utils.getRealCSSColor(Color.YELLOW_700) : Color.YELLOW_700
    case EVENT_TYPE.BASELINE:
    default:
      return realCSSColor ? Utils.getRealCSSColor(Color.GREY_300) : Color.GREY_300
  }
}