All files / modules/85-cv/components/LogsAnalysis LogAnalysis.tsx

84.62% Statements 44/52
68.18% Branches 30/44
62.5% Functions 5/8
88% Lines 44/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              12x 12x 12x                     12x 12x 12x   12x 12x 12x 12x 12x 12x 12x 12x 12x 12x   12x             8x 8x   8x                               8x               8x       8x                       8x     12x             8x 8x   8x 8x                                             8x             8x               8x       8x 1x                 7x   7x                           12x 8x   8x 8x   8x   8x               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 { FontVariation, Color } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import { useGetAllLogsClusterData, useGetAllLogsData } 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 './components/LogAnalysisRow/LogAnalysisRow'
import { getClusterTypes, getLogAnalysisTableData } from './LogAnalysis.utils'
import { LogAnalysisContentProps, LogAnalysisProps, LogEvents } from './LogAnalysis.types'
import { PAGE_SIZE } from './LogAnalysis.constants'
import ClusterChart from './components/ClusterChart/ClusterChart'
import { VerificationType } from '../HealthSourceDropDown/HealthSourceDropDown.constants'
import css from './LogAnalysis.module.scss'
 
const ClusterChartContainer: React.FC<LogAnalysisContentProps> = ({
  monitoredServiceIdentifier,
  startTime,
  endTime,
  logEvent,
  healthSource
}) => {
  const { getString } = useStrings()
  const { orgIdentifier, projectIdentifier, accountId } = useParams<ProjectPathProps>()
 
  const { data, loading, error, refetch } = useGetAllLogsClusterData({
    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>
    )
  }
 
  Iif (error) {
    return <PageError message={getErrorMessage(error)} onClick={() => refetch()} />
  }
 
  Iif (!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 LogAnalysisContent: React.FC<LogAnalysisContentProps> = ({
  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 } = useGetAllLogsData({
    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>
    )
  }
 
  Iif (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={getLogAnalysisTableData(data.resource.content)} />
      <Pagination
        pageSize={pageSize}
        pageCount={totalPages}
        itemCount={totalItems}
        pageIndex={pageIndex}
        gotoPage={index => refetch({ queryParams: { ...queryParams, page: index } })}
      />
    </>
  )
}
 
const LogAnalysis: React.FC<LogAnalysisProps> = ({ monitoredServiceIdentifier, startTime, endTime }) => {
  const { getString } = useStrings()
 
  const [logEvent, setLogEvent] = useState<LogEvents>(LogEvents.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 LogEvents)}
        />
        <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>
 
      <LogAnalysisContent
        monitoredServiceIdentifier={monitoredServiceIdentifier}
        startTime={startTime}
        endTime={endTime}
        logEvent={logEvent}
        healthSource={healthSource}
      />
    </div>
  )
}
 
export default LogAnalysis