All files / modules/85-cv/components/ChangeTimeline ChangeTimeline.utils.ts

92.94% Statements 79/85
80.77% Branches 63/78
100% Functions 14/14
92% Lines 69/75

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              24x 24x 24x     24x 24x 24x 24x 24x 24x 24x       24x 24x       24x 69x   23x   23x   23x           24x 15x   5x   5x   5x           24x 2x         1x   1x           24x 9x   3x   3x   3x           24x 45x 15x 30x 9x   21x     24x         69x   23x   23x   23x               24x               22x 2x 30x   30x 30x 2x                                   20x       24x 98x 98x     24x 98x 98x 98x 98x 98x   98x     24x         138x 138x 45x 45x 45x                       138x     24x           141x              
/*
 * 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 moment from 'moment'
import { sumBy, isNumber } from 'lodash-es'
import { Color } from '@harness/design-system'
import type { ChangeEventDTO, TimeRangeDetail } from 'services/cv'
import type { UseStringsReturn } from 'framework/strings'
import DeploymentWithTwoChanges from '@cv/assets/ChangeTimelineSymbol/Deployment/DeploymentWithTwoChange.svg'
import DeploymentWithNChanges from '@cv/assets/ChangeTimelineSymbol/Deployment/DeploymentWithNChange.svg'
import IncidentWithTwoChanges from '@cv/assets/ChangeTimelineSymbol/Incident/IncidentWithTwoChange.svg'
import IncidentWithNChanges from '@cv/assets/ChangeTimelineSymbol/Incident/IncidentWithNChange.svg'
import InfraWithTwoChanges from '@cv/assets/ChangeTimelineSymbol/Infra/InfraWithTwoChange.svg'
import InfraWithNChanges from '@cv/assets/ChangeTimelineSymbol/Infra/InfraWithNChange.svg'
import {
  getTimeInHrs,
  isChangesInTheRange
} from '@cv/pages/monitored-service/components/ServiceHealth/ServiceHealth.utils'
import { TimePeriodEnum } from '@cv/pages/monitored-service/components/ServiceHealth/ServiceHealth.constants'
import { ChangeSourceTypes } from './ChangeTimeline.constants'
import type { TimelineData } from './components/TimelineRow/TimelineRow.types'
import type { ChangesInfoCardData } from './ChangeTimeline.types'
 
export const getChangeSoureIconColor = (type = '', isChartSymbol = false): string => {
  switch (type) {
    case ChangeSourceTypes.Deployments:
      return isChartSymbol ? 'var(--green-400)' : Color.GREEN_400
    case ChangeSourceTypes.Infrastructure:
      return isChartSymbol ? 'var(--primary-4)' : Color.PRIMARY_4
    case ChangeSourceTypes.Incidents:
      return isChartSymbol ? 'var(--purple-400)' : Color.PURPLE_400
    default:
      return Color.GREY_200
  }
}
 
const getSymbolBytypeForTwoCluster = (type: string) => {
  switch (type) {
    case ChangeSourceTypes.Deployments:
      return DeploymentWithTwoChanges
    case ChangeSourceTypes.Infrastructure:
      return InfraWithTwoChanges
    case ChangeSourceTypes.Incidents:
      return IncidentWithTwoChanges
    default:
      return 'diamond'
  }
}
 
export const getColorForChangeEventType = (type: ChangeEventDTO['type']): string => {
  switch (type) {
    case 'HarnessCD':
    case 'HarnessCDNextGen':
      return getChangeSoureIconColor(ChangeSourceTypes.Deployments, true)
    case 'PagerDuty':
      return getChangeSoureIconColor(ChangeSourceTypes.Incidents, true)
    case 'K8sCluster':
      return getChangeSoureIconColor(ChangeSourceTypes.Infrastructure, true)
    default:
      return ''
  }
}
 
const getSymbolBytypeForGreaterThanTwoCluster = (type: string) => {
  switch (type) {
    case ChangeSourceTypes.Deployments:
      return DeploymentWithNChanges
    case ChangeSourceTypes.Infrastructure:
      return InfraWithNChanges
    case ChangeSourceTypes.Incidents:
      return IncidentWithNChanges
    default:
      return 'diamond'
  }
}
 
const getSymbolAndColorByChangeType = (count: number, type: ChangeSourceTypes): TimelineData['icon'] => {
  if (count === 2) {
    return { height: 16, width: 16, url: getSymbolBytypeForTwoCluster(type) }
  } else if (count > 2) {
    return { height: 18, width: 18, url: getSymbolBytypeForGreaterThanTwoCluster(type) }
  }
  return { height: 9, width: 9, fillColor: getChangeSoureIconColor(type, true), url: 'diamond' }
}
 
export const createTooltipLabel = (
  count: number,
  type: ChangeSourceTypes,
  getString: UseStringsReturn['getString']
): string => {
  switch (type) {
    case ChangeSourceTypes.Deployments:
      return `${count} ${count > 1 ? type : getString('deploymentText')}`
    case ChangeSourceTypes.Infrastructure:
      return `${count} ${getString('infrastructureText')} ${count > 1 ? getString('changes') : getString('change')}`
    case ChangeSourceTypes.Incidents:
      return `${count} ${
        count > 1 ? getString('cv.changeSource.tooltip.incidents') : getString('cv.changeSource.incident')
      }`
    default:
      return ''
  }
}
 
export const createChangeInfoCardData = (
  startTime: number | undefined,
  endTime: number | undefined,
  Deployment: TimeRangeDetail[],
  Infrastructure: TimeRangeDetail[],
  Alert: TimeRangeDetail[],
  getString: UseStringsReturn['getString']
): ChangesInfoCardData[] => {
  if (startTime && endTime) {
    const filterDeployment = Deployment?.filter((item: TimeRangeDetail) =>
      isChangesInTheRange(item, startTime, endTime)
    )
    const filterInfra = Infrastructure?.filter((item: TimeRangeDetail) => isChangesInTheRange(item, startTime, endTime))
    const filterIncident = Alert?.filter((item: TimeRangeDetail) => isChangesInTheRange(item, startTime, endTime))
    return [
      {
        key: ChangeSourceTypes.Deployments,
        count: sumBy(filterDeployment, 'count'),
        message: createTooltipLabel(sumBy(filterDeployment, 'count'), ChangeSourceTypes.Deployments, getString)
      },
      {
        key: ChangeSourceTypes.Incidents,
        count: sumBy(filterIncident, 'count'),
        message: createTooltipLabel(sumBy(filterIncident, 'count'), ChangeSourceTypes.Incidents, getString)
      },
      {
        key: ChangeSourceTypes.Infrastructure,
        count: sumBy(filterInfra, 'count'),
        message: createTooltipLabel(sumBy(filterInfra, 'count'), ChangeSourceTypes.Infrastructure, getString)
      }
    ]
  } else {
    return []
  }
}
 
export const nearestMinutes = (interval: number, someMoment: moment.Moment) => {
  const roundedMinutes = Math.ceil(someMoment.clone().minute() / interval) * interval
  return someMoment.clone().minute(roundedMinutes).second(0)
}
 
export const getStartAndEndTime = (duration: string) => {
  const now = moment()
  const diff = getTimeInHrs(duration || '') * 60 * 60 * 1000
  const diffInMin = duration === TimePeriodEnum.FOUR_HOURS ? 5 : 30
  const endTimeRoundedOffToNearest30min = nearestMinutes(diffInMin, now).valueOf()
  const startTimeRoundedOffToNearest30min = endTimeRoundedOffToNearest30min - diff
 
  return { startTimeRoundedOffToNearest30min, endTimeRoundedOffToNearest30min }
}
 
export const createTimelineSeriesData = (
  type: ChangeSourceTypes,
  getString: UseStringsReturn['getString'],
  timeRangeDetail?: TimeRangeDetail[]
): TimelineData[] => {
  const timelineData: TimelineData[] = []
  for (const timeRange of timeRangeDetail || []) {
    const { endTime, startTime, count } = timeRange || {}
    Eif (endTime && startTime && isNumber(count)) {
      timelineData.push({
        startTime,
        endTime,
        icon: getSymbolAndColorByChangeType(count, type),
        tooltip: {
          message: createTooltipLabel(count, type, getString),
          sideBorderColor: getChangeSoureIconColor(type, true)
        }
      })
    }
  }
 
  return timelineData
}
 
export const createNoDataMessage = (
  timeRangeDetail: TimeRangeDetail[] | undefined,
  type: ChangeSourceTypes,
  duration: string | undefined,
  getString: UseStringsReturn['getString']
): string => {
  return timeRangeDetail && timeRangeDetail.length === 0
    ? `${getString('cv.changeSource.noDataAvailableForChangeScore', {
        type,
        duration: duration?.toLowerCase() || ''
      })}.  ${getString('cv.monitoredServices.serviceHealth.pleaseSelectAnotherTimeWindow')}`
    : ''
}