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

94.23% Statements 49/52
81.82% Branches 36/44
87.5% Functions 7/8
94% Lines 47/50

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              12x 12x 12x                     12x 12x 12x   12x 12x 12x 12x 12x 12x         12x 12x 12x 12x   12x             12x 12x   12x                               12x               12x 1x     11x 1x                     10x     12x             12x 12x   12x 12x                                             12x             12x               12x 1x     11x 2x                 9x   9x                           12x         12x   12x 12x   12x   12x               3x                                                                   12x  
/*
 * 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 React, { useMemo, useState } from 'react'
import { useParams } from 'react-router-dom'
import {
  Container,
  Icon,
  Pagination,
  Select,
  Heading,
  NoDataCard,
  Layout,
  PageError,
  Card
} from '@wings-software/uicore'
import { Color, FontVariation } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import { useGetAllErrorTrackingClusterData, useGetAllErrorTrackingData } from 'services/cv'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { getErrorMessage } from '@cv/utils/CommonUtils'
import { HealthSourceDropDown } from '@cv/components/HealthSourceDropDown/HealthSourceDropDown'
import noDataImage from '@cv/assets/noData.svg'
import { LogAnalysisRow } from '../LogsAnalysis/components/LogAnalysisRow/LogAnalysisRow'
import { getClusterTypes, getErrorTrackingAnalysisTableData } from './ErrorTrackingAnalysis.utils'
import {
  ErrorTrackingAnalysisContentProps,
  ErrorTrackingAnalysisProps,
  ErrorTrackingEvents
} from './ErrorTrackingAnalysis.types'
import { PAGE_SIZE } from './ErrorTrackingAnalysis.constants'
import ClusterChart from '../LogsAnalysis/components/ClusterChart/ClusterChart'
import { VerificationType } from '../HealthSourceDropDown/HealthSourceDropDown.constants'
import css from './ErrorTrackingAnalysis.module.scss'
 
const ClusterChartContainer: React.FC<ErrorTrackingAnalysisContentProps> = ({
  monitoredServiceIdentifier,
  startTime,
  endTime,
  logEvent,
  healthSource
}) => {
  const { getString } = useStrings()
  const { orgIdentifier, projectIdentifier, accountId } = useParams<ProjectPathProps>()
 
  const { data, loading, error, refetch } = useGetAllErrorTrackingClusterData({
    queryParams: {
      accountId,
      orgIdentifier,
      projectIdentifier,
      monitoredServiceIdentifier,
      startTime,
      endTime,
      ...(logEvent ? { clusterTypes: [logEvent] } : {}),
      healthSources: healthSource ? [healthSource] : undefined
    },
    queryParamStringifyOptions: {
      arrayFormat: 'repeat'
    }
  })
 
  Iif (loading) {
    return (
      <Container flex={{ justifyContent: 'center' }} margin={{ top: 'xxxlarge' }}>
        <Icon name="steps-spinner" color={Color.GREY_400} size={30} />
      </Container>
    )
  }
 
  if (error) {
    return <PageError message={getErrorMessage(error)} onClick={() => refetch()} />
  }
 
  if (!data?.resource?.length) {
    return (
      <NoDataCard
        image={noDataImage}
        imageClassName={css.logClusterNoDataImage}
        className={css.noData}
        containerClassName={css.noDataContainer}
        message={getString('cv.monitoredServices.noAvailableData')}
      />
    )
  }
 
  return <ClusterChart data={data.resource} />
}
 
const ErrorTrackingAnalysisContent: React.FC<ErrorTrackingAnalysisContentProps> = ({
  monitoredServiceIdentifier,
  startTime,
  endTime,
  logEvent,
  healthSource
}) => {
  const { getString } = useStrings()
  const { orgIdentifier, projectIdentifier, accountId } = useParams<ProjectPathProps>()
 
  const queryParams = useMemo(() => {
    return {
      page: 0,
      size: PAGE_SIZE,
      accountId,
      orgIdentifier,
      projectIdentifier,
      monitoredServiceIdentifier,
      startTime,
      endTime,
      ...(logEvent ? { clusterTypes: [logEvent] } : {}),
      healthSources: healthSource ? [healthSource] : undefined
    }
  }, [
    accountId,
    endTime,
    healthSource,
    logEvent,
    orgIdentifier,
    projectIdentifier,
    monitoredServiceIdentifier,
    startTime
  ])
 
  const { data, refetch, loading, error } = useGetAllErrorTrackingData({
    queryParams,
    queryParamStringifyOptions: {
      arrayFormat: 'repeat'
    }
  })
 
  Iif (loading) {
    return (
      <Container flex={{ justifyContent: 'center' }} className={css.loadingContainer}>
        <Icon name="steps-spinner" color={Color.GREY_400} size={30} />
      </Container>
    )
  }
 
  if (error) {
    return <PageError message={getErrorMessage(error)} onClick={() => refetch()} />
  }
 
  if (!data?.resource?.content?.length) {
    return (
      <NoDataCard
        message={getString('cv.monitoredServices.noAvailableData')}
        image={noDataImage}
        containerClassName={css.logsAnalysisNoData}
      />
    )
  }
 
  const { pageSize = 0, totalPages = 0, totalItems = 0, pageIndex = 0 } = data.resource
 
  return (
    <>
      <LogAnalysisRow data={getErrorTrackingAnalysisTableData(data.resource.content)} isErrorTracking />
      <Pagination
        pageSize={pageSize}
        pageCount={totalPages}
        itemCount={totalItems}
        pageIndex={pageIndex}
        gotoPage={index => refetch({ queryParams: { ...queryParams, page: index } })}
      />
    </>
  )
}
 
const ErrorTrackingAnalysis: React.FC<ErrorTrackingAnalysisProps> = ({
  monitoredServiceIdentifier,
  startTime,
  endTime
}) => {
  const { getString } = useStrings()
 
  const [logEvent, setLogEvent] = useState<ErrorTrackingEvents>(ErrorTrackingEvents.UNKNOWN)
  const [healthSource, setHealthSource] = useState<string>()
 
  const clusterTypes = getClusterTypes(getString)
 
  return (
    <div className={css.container}>
      <Layout.Horizontal spacing="medium" margin={{ bottom: 'medium' }}>
        <Select
          items={clusterTypes}
          defaultSelectedItem={clusterTypes[2]}
          className={css.logsAnalysisFilters}
          inputProps={{ placeholder: getString('pipeline.verification.logs.filterByClusterType') }}
          onChange={item => setLogEvent(item.value as ErrorTrackingEvents)}
        />
        <HealthSourceDropDown
          onChange={setHealthSource}
          className={css.logsAnalysisFilters}
          monitoredServiceIdentifier={monitoredServiceIdentifier}
          verificationType={VerificationType.LOG}
        />
      </Layout.Horizontal>
 
      <Card className={css.clusterChart}>
        <Heading level={2} font={{ variation: FontVariation.CARD_TITLE }}>
          {getString('pipeline.verification.logs.logCluster')}
        </Heading>
        <ClusterChartContainer
          monitoredServiceIdentifier={monitoredServiceIdentifier}
          startTime={startTime}
          endTime={endTime}
          logEvent={logEvent}
          healthSource={healthSource}
        />
      </Card>
 
      <ErrorTrackingAnalysisContent
        monitoredServiceIdentifier={monitoredServiceIdentifier}
        startTime={startTime}
        endTime={endTime}
        logEvent={logEvent}
        healthSource={healthSource}
      />
    </div>
  )
}
 
export default ErrorTrackingAnalysis