All files / modules/85-cv/pages/monitored-service/components/ServiceHealth ServiceHealth.utils.ts

95.19% Statements 99/104
83.15% Branches 74/89
100% Functions 14/14
94.38% Lines 84/89

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                24x     24x                         24x 49x                 24x 11x 11x 11x 11x   11x 517x 517x     11x     24x 5x 1x 1x 48x 48x 48x 48x     1x     24x 115x 115x   8x 8x   97x 97x   2x 2x   2x 2x   2x 2x   4x   115x     24x 20x 20x   2x 2x   14x 14x   1x 1x   1x 1x   1x 1x   1x   20x     24x   9x   9x 9x               9x     24x         16x 6x 6x 6x     24x         6x 48x 1x       24x 90x 90x           24x 49x 49x           24x         7x   7x 7x 7x     24x         29x 29x 1x   29x     24x 34x    
/*
 * 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 { SelectOption } from '@wings-software/uicore'
import { minBy } from 'lodash-es'
import type { UseStringsReturn } from 'framework/strings'
import type { RiskData } from 'services/cv'
import {
  DAYS,
  daysTimeFormat,
  DEFAULT_MIN_SLIDER_WIDTH,
  HOURS,
  hoursTimeFormat,
  LEFT_TEXTFIELD_WIDTH,
  MAX_BARS_TO_SHOW,
  MIN_BARS_TO_SHOW,
  NUMBER_OF_DATA_POINTS,
  TimePeriodEnum
} from './ServiceHealth.constants'
 
export const getTimePeriods = (getString: UseStringsReturn['getString']): SelectOption[] => {
  return [
    { value: TimePeriodEnum.FOUR_HOURS, label: getString('cv.monitoredServices.serviceHealth.last4Hrs') },
    { value: TimePeriodEnum.TWENTY_FOUR_HOURS, label: getString('cv.monitoredServices.serviceHealth.last24Hrs') },
    { value: TimePeriodEnum.THREE_DAYS, label: getString('cv.monitoredServices.serviceHealth.last3Days') },
    { value: TimePeriodEnum.SEVEN_DAYS, label: getString('cv.monitoredServices.serviceHealth.last7Days') },
    { value: TimePeriodEnum.THIRTY_DAYS, label: getString('common.duration.month') }
  ]
}
 
export const getTimestampsForPeriodWithoutRiskData = (selectedTimePeriod: string): number[] => {
  const timestamps = []
  const intervalInHrs = getTimeInHrs(selectedTimePeriod) / NUMBER_OF_DATA_POINTS
  let endTime = Date.now()
  timestamps.push(endTime)
 
  for (let i = 1; i < NUMBER_OF_DATA_POINTS; i++) {
    endTime -= intervalInHrs * 60 * 60000
    timestamps.push(endTime)
  }
 
  return timestamps.reverse()
}
 
export const getTimestampsForPeriod = (riskData?: RiskData[]): number[] => {
  if (!riskData?.length) return []
  const timestamps: number[] = []
  for (const risk of riskData) {
    const { timeRangeParams } = risk || {}
    Eif (timeRangeParams?.startTime && timeRangeParams.endTime) {
      timestamps.push(timeRangeParams.startTime * 1000)
      timestamps.push(timeRangeParams.endTime * 1000)
    }
  }
  return timestamps
}
 
export const getTimeInHrs = (selectedTimePeriod: string): number => {
  let timeInHrs = 24
  switch (selectedTimePeriod) {
    case TimePeriodEnum.FOUR_HOURS:
      timeInHrs = 4
      break
    case TimePeriodEnum.TWENTY_FOUR_HOURS:
      timeInHrs = 24
      break
    case TimePeriodEnum.THREE_DAYS:
      timeInHrs = 24 * 3
      break
    case TimePeriodEnum.SEVEN_DAYS:
      timeInHrs = 24 * 7
      break
    case TimePeriodEnum.THIRTY_DAYS:
      timeInHrs = 24 * 30
      break
    default:
      timeInHrs = 24
  }
  return timeInHrs
}
 
export const getTimeFormat = (selectedTimePeriod: string): string => {
  let timeFormat = HOURS
  switch (selectedTimePeriod) {
    case TimePeriodEnum.FOUR_HOURS:
      timeFormat = HOURS
      break
    case TimePeriodEnum.TWENTY_FOUR_HOURS:
      timeFormat = HOURS
      break
    case TimePeriodEnum.THREE_DAYS:
      timeFormat = DAYS
      break
    case TimePeriodEnum.SEVEN_DAYS:
      timeFormat = DAYS
      break
    case TimePeriodEnum.THIRTY_DAYS:
      timeFormat = DAYS
      break
    default:
      timeFormat = HOURS
  }
  return timeFormat
}
 
export const getTimeFormatMoment = (format?: string): string => {
  let timeFormat
  switch (format) {
    case HOURS:
      timeFormat = hoursTimeFormat
      break
    case DAYS:
      timeFormat = daysTimeFormat
      break
    default:
      timeFormat = hoursTimeFormat
  }
 
  return timeFormat
}
 
export function calculateStartAndEndTimes(
  startXPercentage: number,
  endXPercentage: number,
  timestamps?: number[]
): [number, number] | undefined {
  if (!timestamps?.length) return
  const startTime = Math.floor(startXPercentage * (timestamps[timestamps.length - 1] - timestamps[0]) + timestamps[0])
  const endTime = Math.floor(endXPercentage * (timestamps[timestamps.length - 1] - timestamps[0]) + timestamps[0])
  return [startTime, endTime]
}
 
export function calculateLowestHealthScoreBar(
  startTime?: number,
  endTime?: number,
  healthScoreData?: RiskData[]
): RiskData | undefined {
  if (startTime && endTime && healthScoreData && healthScoreData.length) {
    const dataPointsLyingInTheRange = healthScoreData.filter((el: RiskData) => isInTheRange(el, startTime, endTime))
    return minBy(dataPointsLyingInTheRange, 'healthScore')
  }
}
 
export const isChangesInTheRange = (el: any, startTime: number, endTime: number): boolean => {
  Eif (el?.startTime && el?.endTime) {
    return startTime <= el.startTime && el.startTime <= endTime
  } else {
    return false
  }
}
 
export const isInTheRange = (el: RiskData, startTime: number, endTime: number): boolean => {
  Eif (el?.timeRangeParams?.startTime && el?.timeRangeParams?.endTime) {
    return startTime <= el.timeRangeParams.startTime * 1000 && el.timeRangeParams.startTime * 1000 <= endTime
  } else {
    return false
  }
}
 
export const getSliderDimensions = (
  containerWidth: number,
  selectedTimePeriod: string
): { minWidth: number; maxWidth: number } => {
  // This is temporary change , will be removed once BE fix is done.
  const maxBarsToShow = limitMaxSliderWidth(selectedTimePeriod) ? MIN_BARS_TO_SHOW : MAX_BARS_TO_SHOW
 
  const minWidth = (containerWidth - LEFT_TEXTFIELD_WIDTH) / (NUMBER_OF_DATA_POINTS / MIN_BARS_TO_SHOW)
  const maxWidth = (containerWidth - LEFT_TEXTFIELD_WIDTH) / (NUMBER_OF_DATA_POINTS / maxBarsToShow)
  return { minWidth, maxWidth }
}
 
export const getDimensionsAsPerContainerWidth = (
  defaultMaxSliderWidth: number,
  selectedTimePeriod: SelectOption,
  containerWidth?: number
): { minWidth: number; maxWidth: number } => {
  let dimensions = { minWidth: DEFAULT_MIN_SLIDER_WIDTH, maxWidth: defaultMaxSliderWidth }
  if (containerWidth) {
    dimensions = { ...dimensions, ...getSliderDimensions(containerWidth, selectedTimePeriod?.value as string) }
  }
  return dimensions
}
 
export const limitMaxSliderWidth = (selectedTimePeriod: string): boolean => {
  return selectedTimePeriod === TimePeriodEnum.SEVEN_DAYS || selectedTimePeriod === TimePeriodEnum.THIRTY_DAYS
}