All files / modules/85-cv/components/ChangeTimeline ChangeTimeline.tsx

97.3% Statements 36/37
91.11% Branches 82/90
100% Functions 5/5
97.3% Lines 36/37

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              12x 12x 12x 12x     12x   12x 12x   12x           12x   12x 47x 46x                             46x               46x                   46x             46x 20x 3x           17x     46x 40x 40x 36x                                                             46x 40x 40x 4x                                               46x                       46x 46x   46x 20x               20x           46x 1x     45x                                                                                    
/*
 * 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 React, { useEffect, useMemo } from 'react'
import { useParams } from 'react-router-dom'
import { useStrings } from 'framework/strings'
import { getErrorMessage } from '@cv/utils/CommonUtils'
import type { TimePeriodEnum } from '@cv/pages/monitored-service/components/ServiceHealth/ServiceHealth.constants'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { useChangeEventTimeline, useGetMonitoredServiceChangeTimeline } from 'services/cv'
import type { ChangeTimelineProps } from './ChangeTimeline.types'
import { Timeline } from './components/Timeline/Timeline'
import { ChangeSourceTypes } from './ChangeTimeline.constants'
 
import {
  createChangeInfoCardData,
  createNoDataMessage,
  createTimelineSeriesData,
  getStartAndEndTime
} from './ChangeTimeline.utils'
import ChangeTimelineError from './components/ChangeTimelineError/ChangeTimelineError'
 
export default function ChangeTimeline(props: ChangeTimelineProps): JSX.Element {
  const { getString } = useStrings()
  const { accountId, orgIdentifier, projectIdentifier } = useParams<ProjectPathProps>()
  const {
    useMonitoredServiceChangeTimeline,
    monitoredServiceIdentifier,
    serviceIdentifier,
    environmentIdentifier,
    startTime,
    endTime,
    selectedTimePeriod,
    selectedTimeRange,
    onSliderMoved,
    changeCategories,
    changeSourceTypes,
    hideTimeline,
    duration
  } = props
 
  const {
    data: monitoredServiceChangeTimelineData,
    refetch: monitoredServiceChangeTimelineRefetch,
    loading: monitoredServiceChangeTimelineLoading,
    error: monitoredServiceChangeTimelineError,
    cancel: monitoredServiceChangeTimelineCancel
  } = useGetMonitoredServiceChangeTimeline({
    lazy: true
  })
 
  const {
    data: changeEventTimelineData,
    refetch: changeEventTimelineRefetch,
    loading: changeEventTimelineLoading,
    error: changeEventTimelineError,
    cancel: changeEventTimelineCancel
  } = useChangeEventTimeline({
    lazy: true,
    accountIdentifier: accountId,
    projectIdentifier,
    orgIdentifier
  })
 
  const { startTimeRoundedOffToNearest30min, endTimeRoundedOffToNearest30min } = useMemo(() => {
    if (selectedTimeRange) {
      return {
        startTimeRoundedOffToNearest30min: selectedTimeRange.startTime,
        endTimeRoundedOffToNearest30min: selectedTimeRange.endTime
      }
    }
 
    return getStartAndEndTime((selectedTimePeriod?.value as string) || '')
  }, [selectedTimePeriod?.value, selectedTimeRange])
 
  useEffect(() => {
    changeEventTimelineCancel()
    /* istanbul ignore else */ if (!useMonitoredServiceChangeTimeline) {
      changeEventTimelineRefetch({
        queryParams: {
          ...(monitoredServiceIdentifier ? { monitoredServiceIdentifiers: [monitoredServiceIdentifier] } : {}),
          ...(serviceIdentifier
            ? { serviceIdentifiers: Array.isArray(serviceIdentifier) ? serviceIdentifier : [serviceIdentifier] }
            : {}),
          ...(environmentIdentifier
            ? { envIdentifiers: Array.isArray(environmentIdentifier) ? environmentIdentifier : [environmentIdentifier] }
            : {}),
          changeCategories: changeCategories || [],
          changeSourceTypes: changeSourceTypes || [],
          startTime: startTimeRoundedOffToNearest30min,
          endTime: endTimeRoundedOffToNearest30min
        },
        queryParamStringifyOptions: {
          arrayFormat: 'repeat'
        }
      })
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [
    startTimeRoundedOffToNearest30min,
    endTimeRoundedOffToNearest30min,
    changeCategories,
    changeSourceTypes,
    serviceIdentifier,
    environmentIdentifier,
    useMonitoredServiceChangeTimeline,
    monitoredServiceIdentifier
  ])
 
  useEffect(() => {
    monitoredServiceChangeTimelineCancel()
    /* istanbul ignore else */ if (useMonitoredServiceChangeTimeline) {
      monitoredServiceChangeTimelineRefetch({
        queryParams: {
          accountId,
          orgIdentifier,
          projectIdentifier,
          monitoredServiceIdentifier,
          changeSourceTypes: changeSourceTypes || [],
          duration: duration?.value as TimePeriodEnum,
          endTime: Date.now()
        }
      })
    }
 
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [
    accountId,
    orgIdentifier,
    projectIdentifier,
    monitoredServiceIdentifier,
    useMonitoredServiceChangeTimeline,
    changeSourceTypes,
    duration
  ])
 
  const { data, error, loading } = useMonitoredServiceChangeTimeline
    ? {
        data: monitoredServiceChangeTimelineData,
        error: monitoredServiceChangeTimelineError,
        loading: monitoredServiceChangeTimelineLoading
      }
    : {
        data: changeEventTimelineData,
        error: changeEventTimelineError,
        loading: changeEventTimelineLoading
      }
 
  const { categoryTimeline } = data?.resource || {}
  const { Deployment, Infrastructure, Alert } = categoryTimeline || {}
 
  useEffect(() => {
    const changeInfoCardData = createChangeInfoCardData(
      startTime,
      endTime,
      Deployment,
      Infrastructure,
      Alert,
      getString
    )
    Iif (changeInfoCardData.length) {
      onSliderMoved?.(changeInfoCardData)
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [startTime, endTime, Deployment, Infrastructure, Alert])
 
  if (error) {
    return <ChangeTimelineError error={getErrorMessage(error) || ''} />
  }
 
  return (
    <Timeline
      isLoading={loading}
      rowOffset={90}
      timelineRows={[
        {
          labelName: getString('deploymentsText'),
          data: createTimelineSeriesData(ChangeSourceTypes.Deployments, getString, categoryTimeline?.Deployment),
          noDataMessage: createNoDataMessage(
            categoryTimeline?.Deployment,
            ChangeSourceTypes.Deployments,
            selectedTimePeriod?.label,
            getString
          )
        },
        {
          labelName: getString('infrastructureText'),
          data: createTimelineSeriesData(ChangeSourceTypes.Infrastructure, getString, categoryTimeline?.Infrastructure),
          noDataMessage: createNoDataMessage(
            categoryTimeline?.Infrastructure,
            ChangeSourceTypes.Infrastructure,
            selectedTimePeriod?.label,
            getString
          )
        },
        {
          labelName: getString('cv.changeSource.tooltip.incidents'),
          data: createTimelineSeriesData(ChangeSourceTypes.Incidents, getString, categoryTimeline?.Alert),
          noDataMessage: createNoDataMessage(
            categoryTimeline?.Alert,
            ChangeSourceTypes.Incidents,
            selectedTimePeriod?.label,
            getString
          )
        }
      ]}
      timestamps={[startTimeRoundedOffToNearest30min, endTimeRoundedOffToNearest30min]}
      labelWidth={90}
      hideTimeline={hideTimeline}
    />
  )
}