All files / modules/70-pipeline/pages/execution/ExecutionPipelineView/ExecutionLogView/StepsTree StepsTree.tsx

100% Statements 57/57
79.2% Branches 99/125
100% Functions 12/12
100% Lines 56/56

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 228 229 230 231 232 233 234 235 236 237 238 239 240              3x 3x 3x 3x     3x         3x 3x 3x               3x     79x     3x 63x                               3x 32x 32x 32x               3x 1x     2x     32x     103x 79x 79x   79x 1x       2x 2x                                 1x   1x               1x                         78x                     3x                                   24x 15x   15x                                                         9x   9x 18x   9x 18x     9x 9x   9x 3x 3x 6x 3x 3x     3x 6x       3x 3x     3x 3x 3x         9x                                        
/*
 * 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 from 'react'
import { Icon, Text, IconName } from '@wings-software/uicore'
import cx from 'classnames'
import { get, mapKeys, omit, defaultTo } from 'lodash-es'
 
import type { ExecutionNode, InterruptEffectDTO } from 'services/pipeline-ng'
import { String, useStrings } from 'framework/strings'
import type {
  ExecutionPipelineItem,
  ExecutionPipelineNode
} from '@pipeline/components/ExecutionStageDiagram/ExecutionPipelineModel'
import { Duration } from '@common/components'
import { ExecutionStatusIconMap } from '@pipeline/utils/executionUtils'
import {
  isExecutionRunning,
  isExecutionSuccess,
  isExecutionNotStarted,
  isExecutionQueued,
  ExecutionStatusEnum
} from '@pipeline/utils/statusHelpers'
 
import css from './StepsTree.module.scss'
 
function getRetryInterrupts(step: ExecutionPipelineNode<ExecutionNode>): InterruptEffectDTO[] {
  return defaultTo(step?.item?.data?.interruptHistories, []).filter(row => row.interruptType === 'RETRY')
}
 
const IconMap: Record<string, IconName> = {
  ...mapKeys(ExecutionStatusIconMap, (_value, key) => key.toLowerCase()),
  running: 'spinner',
  asyncwaiting: 'spinner',
  timedwaiting: 'spinner',
  taskwaiting: 'spinner'
}
 
export interface StepsTreeProps {
  nodes: Array<ExecutionPipelineNode<ExecutionNode>>
  selectedStepId?: string
  onStepSelect(stepId: string, retryId?: string): void
  isRoot?: boolean
  retryStep?: string
  allNodeMap: Record<string, ExecutionNode>
}
 
export function StepsTree(props: StepsTreeProps): React.ReactElement {
  const { nodes, selectedStepId, onStepSelect, isRoot, retryStep, allNodeMap } = props
  const { getString } = useStrings()
  const commonProps: Omit<StepsTreeProps, 'nodes' | 'isRoot'> = {
    selectedStepId,
    onStepSelect,
    retryStep,
    allNodeMap
  }
 
  function handleStepSelect(identifier: string, status?: string, retryId?: string): void {
    if (isExecutionNotStarted(status) || isExecutionQueued(status)) {
      return
    }
 
    onStepSelect(identifier, retryId)
  }
 
  return (
    <ul className={css.root}>
      {nodes.map((step, i) => {
        if (step.item) {
          const statusLower = step.item.status.toLowerCase()
          const retryInterrupts = getRetryInterrupts(step)
 
          if (retryInterrupts.length > 0) {
            const retryNodes: Array<ExecutionPipelineNode<ExecutionNode>> = defaultTo(
              step.item.data?.interruptHistories,
              []
            )
              .filter(node => node?.interruptConfig?.retryInterruptConfig)
              .map((node, k): { item: ExecutionPipelineItem<ExecutionNode> } => ({
                item: {
                  ...(step.item as ExecutionPipelineItem<ExecutionNode>),
                  name: getString('pipeline.execution.retryStepCount', { num: k + 1 }),
                  retryId: defaultTo(node.interruptConfig.retryInterruptConfig?.retryId, ''),
                  status: ExecutionStatusEnum.Failed,
                  // override data in order to stop infinite loop
                  data: defaultTo(
                    omit(
                      get(allNodeMap, defaultTo(node.interruptConfig.retryInterruptConfig?.retryId, '')),
                      'interruptHistories'
                    ),
                    {}
                  )
                }
              }))
 
            const num = retryNodes.length + 1
 
            retryNodes.push({
              item: {
                ...(step.item as ExecutionPipelineItem<ExecutionNode>),
                name: getString('pipeline.execution.retryStepCount', { num }),
                data: omit(step.item.data, 'interruptHistories') // override data in order to stop infinite loop
              }
            })
 
            return (
              <li key={step.item.identifier} className={css.item} data-type="retry-item">
                <div className={css.step} data-status={statusLower}>
                  <Icon className={css.icon} name={IconMap[statusLower]} />
                  <Text lineClamp={1} className={css.name}>
                    {step.item.name}
                  </Text>
                </div>
                <StepsTree nodes={retryNodes} {...commonProps} />
              </li>
            )
          }
 
          return (
            <li
              className={cx(css.item, {
                [css.active]: step.item?.retryId === retryStep && selectedStepId === step.item.identifier
              })}
              key={defaultTo(step.item.retryId, step.item.identifier)}
              data-type="item"
            >
              <div
                className={css.step}
                data-status={statusLower}
                onClick={() => handleStepSelect(step.item?.identifier as string, step.item?.status, step.item?.retryId)}
              >
                <Icon className={css.icon} name={IconMap[statusLower]} />
                <Text lineClamp={1} className={css.name}>
                  {step.item.name}
                </Text>
                <Duration
                  className={css.duration}
                  startTime={step.item.data?.startTs}
                  endTime={step.item.data?.endTs}
                  durationText={' '}
                  icon={null}
                />
              </div>
            </li>
          )
        }
 
        if (step.group) {
          const statusLower = step.group.status.toLowerCase()
 
          return (
            <li className={css.item} key={step.group.identifier} data-type="group">
              <div className={css.step} data-status={statusLower}>
                <Icon className={css.icon} name={IconMap[statusLower]} />
                <div className={css.nameWrapper}>
                  {isRoot ? null : <div className={css.groupIcon} />}
                  {step.group.name ? (
                    <Text lineClamp={1} className={css.name}>
                      {step.group.name}
                    </Text>
                  ) : (
                    <String className={css.name} stringID="stepGroup" />
                  )}
                </div>
 
                <Duration
                  className={css.duration}
                  startTime={step.group.data?.startTs}
                  endTime={step.group.data?.endTs}
                  durationText={' '}
                  icon={null}
                />
              </div>
              <StepsTree nodes={step.group.items} {...commonProps} />
            </li>
          )
        }
 
        /* istanbul ignore else */
        if (step.parallel) {
          // here assumption is that parallel steps cannot have nested parallel steps
          const isRunning = step.parallel.some(pStep =>
            isExecutionRunning(defaultTo(pStep.item?.status, pStep.group?.status))
          )
          const isSuccess = step.parallel.every(pStep =>
            isExecutionSuccess(defaultTo(pStep.item?.status, pStep.group?.status))
          )
 
          let icon: IconName = IconMap.notstarted
          let statusLower = ''
 
          if (isRunning) {
            icon = IconMap.running
            statusLower = 'running'
          } else if (isSuccess) {
            icon = IconMap.success
            statusLower = 'success'
          } else {
            // find first non success state
            const nonSuccessStep = step.parallel.find(
              pStep => !isExecutionSuccess(defaultTo(pStep.item?.status, pStep.group?.status))
            )
 
            /* istanbul ignore else */
            if (nonSuccessStep) {
              const status = defaultTo(nonSuccessStep.item?.status, nonSuccessStep.group?.status)?.toLowerCase()
 
              /* istanbul ignore else */
              if (status) {
                statusLower = status
                icon = IconMap[status]
              }
            }
          }
 
          return (
            <li className={css.item} key={i} data-type="parallel">
              <div className={css.step} data-status={statusLower}>
                <Icon className={css.icon} name={icon} />
                <div className={css.nameWrapper}>
                  {isRoot ? null : <div className={css.parallelIcon} />}
                  <String className={css.name} stringID="parallelSteps" />
                </div>
              </div>
              <StepsTree nodes={step.parallel} {...commonProps} />
            </li>
          )
        }
 
        /* istanbul ignore next */
        return null
      })}
    </ul>
  )
}