All files / modules/70-pipeline/pages/full-page-log-view FullPageLogView.tsx

97.96% Statements 48/49
93.75% Branches 15/16
100% Functions 7/7
97.96% Lines 48/49

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              1x 1x 1x 1x   1x   1x 1x   1x 1x 1x 1x 1x   1x   1x             1x   13x 13x 13x 13x 13x 13x                   13x   13x                             13x 13x   13x 4x     4x 3x 3x               2x         3x   3x 5x                               5x   3x 3x 3x             4x 3x         13x 7x               6x 3x               3x 2x             1x     3x                    
/*
 * 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 from 'react'
import { useParams } from 'react-router-dom'
import { defaultTo, get } from 'lodash-es'
import { Spinner } from '@blueprintjs/core'
 
import { String as LocaleString, useStrings } from 'framework/strings'
import type { PipelineLogsPathProps } from '@common/interfaces/RouteInterfaces'
import { useGetExecutionDetail, useGetExecutionNode } from 'services/pipeline-ng'
import { logBlobPromise, useGetToken } from 'services/logs'
 
import { useDeepCompareEffect } from '@common/hooks'
import { createSections } from '@pipeline/components/LogsContent/LogsState/createSections'
import { ActionType, LogLineData } from '@pipeline/components/LogsContent/LogsState/types'
import { getDefaultReducerState } from '@pipeline/components/LogsContent/LogsState/utils'
import { processLogsData } from '@pipeline/components/LogsContent/LogsState/updateSectionData'
 
import LogsSection from './LogsSection'
 
import css from './FullPageLogView.module.scss'
 
export interface LogsData {
  name: string
  data: LogLineData[]
}
 
export default function FullPageLogView(): React.ReactElement {
  const { stageIdentifier, stepIndentifier, accountId, orgIdentifier, projectIdentifier, executionIdentifier } =
    useParams<PipelineLogsPathProps>()
  const { getString } = useStrings()
  const [logsData, setLogsData] = React.useState<LogsData[]>([])
  const [logsDataLoading, setLogsDataLoading] = React.useState(false)
  const { data: tokenData, loading: tokenLoading } = useGetToken({ queryParams: { accountID: accountId } })
  const { data: executionData, loading: executionLoading } = useGetExecutionDetail({
    planExecutionId: executionIdentifier,
    queryParams: {
      orgIdentifier,
      projectIdentifier,
      accountIdentifier: accountId,
      stageNodeId: stageIdentifier
    }
  })
 
  const node = get(executionData, ['data', 'executionGraph', 'nodeMap', stepIndentifier])
  // this is for retry node
  const { data: retryNodeData, loading: retryNodeLoading } = useGetExecutionNode({
    queryParams: {
      accountIdentifier: accountId,
      orgIdentifier,
      projectIdentifier,
      nodeExecutionId: stepIndentifier
    },
    /**
     * Do not fetch data:
     * 1. execution data call is in progress
     * 2. we have node data already
     */
    lazy: executionLoading || !!node
  })
 
  const loading = tokenLoading || executionLoading || retryNodeLoading || logsDataLoading
  const finalNode = defaultTo(retryNodeData?.data, node)
 
  useDeepCompareEffect(() => {
    const abortController = new AbortController()
 
    /* istanbul ignore else */
    if (finalNode) {
      try {
        const sections = createSections(
          getDefaultReducerState({ selectedStage: stageIdentifier, selectedStep: stepIndentifier }),
          {
            type: ActionType.CreateSections,
            payload: {
              node: finalNode,
              selectedStage: stageIdentifier,
              selectedStep: stepIndentifier,
              getSectionName: (index: number): string => getString('pipeline.logs.sectionName', { index })
            }
          }
        )
 
        setLogsDataLoading(true)
 
        const promises = sections.logKeys.map(async (key, i) => {
          const data = (await logBlobPromise(
            {
              queryParams: {
                accountID: accountId,
                'X-Harness-Token': '',
                key
              },
              requestOptions: {
                headers: {
                  'X-Harness-Token': tokenData as unknown as string
                }
              }
            },
            abortController.signal
          )) as unknown as string
 
          return { name: get(sections, ['units', i]), data: processLogsData(defaultTo(data, '')) }
        })
        Promise.all(promises).then(data => {
          setLogsData(data)
          setLogsDataLoading(false)
        })
      } catch (_e) {
        setLogsDataLoading(false)
      }
    }
 
    return () => {
      abortController.abort()
    }
  }, [finalNode])
 
  /* istanbul ignore else */
  if (loading) {
    return (
      <div className={css.main}>
        <Spinner />
      </div>
    )
  }
 
  /* istanbul ignore else */
  if (!finalNode || logsData.length === 0) {
    return (
      <div className={css.main}>
        <LocaleString stringID="common.logs.noLogsText" />
      </div>
    )
  }
 
  /* istanbul ignore else */
  if (logsData.length === 1) {
    return (
      <div className={css.main} data-testid="single-section">
        <LogsSection data={logsData[0].data} />
      </div>
    )
  }
 
  return (
    <div className={css.main} data-testid="multi-section">
      {logsData.map((section, i) => {
        return (
          <details key={i} open>
            <summary>{section.name}</summary>
            <LogsSection data={section.data} />
          </details>
        )
      })}
    </div>
  )
}