All files / modules/85-cv/hooks/useLogContentHook/views/ExecutionLog ExecutionLog.tsx

98.15% Statements 53/54
88% Branches 44/50
91.67% Functions 11/12
98.11% Lines 52/53

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              17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x   17x   17x                                           45x 45x 45x   45x 45x 45x 45x   45x   45x   45x 24x 20x         45x 24x               45x   4x 1x 1x 1x           45x 3x 3x 3x     45x 1x 1x 1x     45x 2x 2x 2x       45x                                 16x                                                                                                         2x                                 2x                                         17x  
/*
 * 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, { Reducer, useCallback } from 'react'
import cx from 'classnames'
import { GroupedVirtuosoHandle, Virtuoso, VirtuosoHandle } from 'react-virtuoso'
import { Button, ButtonSize, ButtonVariation, Color, Container, Layout, SelectOption, Text } from '@harness/uicore'
import { useStrings, String as StrTemplate } from 'framework/strings'
import { MultiLogLine } from '@pipeline/components/LogsContent/components/MultiLogLine/MultiLogLine'
import LogContentHeader from '../LogContentHeader/LogContentHeader'
import LogContentToolbar from '../LogContentToolbar/LogContentToolbar'
import ExecutionLogSearch from './ExecutionLogSearch'
import { reducer, useActionCreator } from './ExecutionLogState'
import { defaultReducerState } from './ExecutionLog.constants'
import { isPositiveNumber } from '../../useLogContentHook.utils'
import { ExecutionAndAPICallLogProps, LogTypes } from '../../useLogContentHook.types'
import { convertLogDataToLogLineData } from './ExecutionLog.utils'
import type { State, Action, ActionType } from './ExecutionLog.types'
import css from './ExecutionLog.module.scss'
 
const ExecutionLog: React.FC<ExecutionAndAPICallLogProps> = ({
  isFullScreen,
  setIsFullScreen,
  verifyStepExecutionId,
  monitoredServiceIdentifier,
  serviceName,
  envName,
  resource,
  loading,
  errorMessage,
  refetchLogs,
  healthSource,
  setHealthSource,
  timeRange,
  setTimeRange,
  errorLogsOnly,
  setErrorLogsOnly,
  pageNumber,
  setPageNumber,
  handleDownloadLogs,
  showTimelineSlider
}) => {
  const { getString } = useStrings()
  const [state, dispatch] = React.useReducer<Reducer<State, Action<ActionType>>>(reducer, defaultReducerState)
  const actions = useActionCreator(dispatch)
 
  const [isAtBottom, setIsAtBottom] = React.useState(false)
  const virtuosoRef = React.useRef<null | GroupedVirtuosoHandle | VirtuosoHandle>(null)
  const { currentIndex, linesWithResults } = state.searchData
  const length = state.data.length
 
  const { content, totalPages = 0 } = resource ?? {}
 
  const isMonitoredService = Boolean(monitoredServiceIdentifier)
 
  React.useEffect(() => {
    /* istanbul ignore else */ if (content?.length) {
      actions.setExecutionLogs(content)
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [content])
 
  React.useEffect(() => {
    const index = linesWithResults[currentIndex]
 
    /* istanbul ignore next */
    if (virtuosoRef.current && isPositiveNumber(index)) {
      virtuosoRef.current.scrollToIndex(index)
    }
  }, [currentIndex, linesWithResults])
 
  const handleHealthSource = useCallback(
    (_healthSource: SelectOption): void => {
      if (_healthSource.value !== healthSource?.value) {
        actions.resetExecutionLogs()
        setPageNumber(0)
        setHealthSource?.(_healthSource)
      }
    },
    [healthSource, actions, setPageNumber, setHealthSource]
  )
 
  const handleTimeRange = (_timeRange: SelectOption): void => {
    actions.resetExecutionLogs()
    setPageNumber(0)
    setTimeRange?.(_timeRange)
  }
 
  const handleDisplayOnlyErrors = (_errorLogsOnly: boolean): void => {
    actions.resetExecutionLogs()
    setPageNumber(0)
    setErrorLogsOnly(_errorLogsOnly)
  }
 
  const handleClick = (): void => {
    const handle = virtuosoRef.current
    /* istanbul ignore else */ if (handle) {
      handle.scrollToIndex(isAtBottom ? 0 : length)
    }
  }
 
  return (
    <div>
      <LogContentHeader
        logType={LogTypes.ExecutionLog}
        verifyStepExecutionId={verifyStepExecutionId}
        monitoredServiceIdentifier={monitoredServiceIdentifier}
        serviceName={serviceName}
        envName={envName}
        healthSource={healthSource}
        handleHealthSource={handleHealthSource}
        timeRange={timeRange}
        handleTimeRange={handleTimeRange}
        errorLogsOnly={errorLogsOnly}
        handleDisplayOnlyErrors={handleDisplayOnlyErrors}
      />
      <LogContentToolbar
        logType={LogTypes.ExecutionLog}
        data={state.data.map(log => log.text)}
        searchInput={<ExecutionLogSearch state={state} actions={actions} />}
        isFullScreen={isFullScreen}
        setIsFullScreen={setIsFullScreen}
        isVerifyStep={Boolean(verifyStepExecutionId)}
        timeRange={timeRange}
        isMonitoredService={isMonitoredService}
        handleDownloadLogs={handleDownloadLogs}
      />
      <div className={cx(css.main, { [css.fullScreen]: isFullScreen })}>
        <pre className={css.container}>
          {length > 0 && (
            <Virtuoso
              height="100%"
              overscan={50}
              ref={virtuosoRef}
              atBottomThreshold={Math.ceil(length / 3)}
              atBottomStateChange={setIsAtBottom}
              totalCount={length}
              followOutput={/* istanbul ignore next */ _ => 'smooth'}
              itemContent={index => (
                <MultiLogLine
                  {...convertLogDataToLogLineData(state.data[index])}
                  limit={length}
                  lineNumber={index}
                  searchText={state.searchData.text}
                  currentSearchIndex={currentIndex}
                />
              )}
            />
          )}
          {!length && !loading && !errorMessage && (
            <StrTemplate
              tagName="div"
              className={css.noLogs}
              stringID={
                isMonitoredService && !showTimelineSlider
                  ? 'cv.monitoredServices.noAvailableLogData'
                  : 'common.logs.noLogsText'
              }
            />
          )}
        </pre>
 
        <div className={css.footer}>
          <Container flex>
            <Container width="80%">
              {loading && <StrTemplate tagName="div" className={css.text} stringID="loading" />}
              {!loading && errorMessage && (
                <Layout.Horizontal spacing="small">
                  <Text lineClamp={1} color={Color.RED_500} className={css.text}>
                    {errorMessage}
                  </Text>
                  <a className={css.buttonLink} onClick={() => refetchLogs()}>
                    {getString('retry')}
                  </a>
                </Layout.Horizontal>
              )}
            </Container>
            {length > 0 && (
              <div>
                {pageNumber !== totalPages - 1 && (
                  <Button
                    text={getString('pipeline.verification.loadMore')}
                    icon="down"
                    className={css.button}
                    size={ButtonSize.SMALL}
                    margin={{ right: 'small' }}
                    variation={ButtonVariation.PRIMARY}
                    disabled={!!errorMessage}
                    onClick={() => setPageNumber(_prevPageNumber => _prevPageNumber + 1)}
                  />
                )}
                <Button
                  size={ButtonSize.SMALL}
                  variation={ButtonVariation.PRIMARY}
                  text={getString(isAtBottom ? 'cv.top' : 'cv.bottom')}
                  icon={isAtBottom ? 'arrow-up' : 'arrow-down'}
                  iconProps={{ size: 10 }}
                  onClick={handleClick}
                  className={css.button}
                />
              </div>
            )}
          </Container>
        </div>
      </div>
    </div>
  )
}
 
export default ExecutionLog