All files / modules/70-pipeline/pages/execution/ExecutionPipelineView/ExecutionGraphView/ExecutionStageDetails ExecutionStageDetails.tsx

71.67% Statements 43/60
14.83% Branches 35/236
50% Functions 5/10
71.67% Lines 43/60

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              2x 2x 2x 2x           2x 2x 2x 2x 2x     2x 2x 2x 2x   2x   2x 2x 2x 2x   2x               2x                     19x 19x 19x 19x 19x       19x 19x         19x             19x     19x             19x   19x 9x                     19x   19x 9x                     19x 9x     19x                                             19x                                                                                   19x   19x         2x                                                                                      
/*
 * 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 { isEmpty, debounce, defaultTo } from 'lodash-es'
import { useParams } from 'react-router-dom'
import {
  ExecutionNode,
  NodeRunInfo,
  useGetBarrierInfo,
  useGetResourceConstraintsExecutionInfo
} from 'services/pipeline-ng'
import { PageSpinner } from '@common/components'
import { processExecutionData } from '@pipeline/utils/executionUtils'
import { useExecutionContext } from '@pipeline/context/ExecutionContext'
import { useExecutionLayoutContext } from '@pipeline/components/ExecutionLayout/ExecutionLayoutContext'
import ExecutionStageDiagram from '@pipeline/components/ExecutionStageDiagram/ExecutionStageDiagram'
import type { ExecutionPipeline } from '@pipeline/components/ExecutionStageDiagram/ExecutionPipelineModel'
import type { DynamicPopoverHandlerBinding } from '@common/components/DynamicPopover/DynamicPopover'
import { StepType } from '@pipeline/components/PipelineSteps/PipelineStepInterface'
import { isExecutionPaused, isExecutionRunning } from '@pipeline/utils/statusHelpers'
import { DynamicPopover } from '@common/exports'
import HoverCard from '@pipeline/components/HoverCard/HoverCard'
import type { ExecutionPathProps } from '@common/interfaces/RouteInterfaces'
import { StepMode as Modes } from '@pipeline/utils/stepUtils'
import type { ExecutionLayoutState } from '@pipeline/components/ExecutionLayout/ExecutionLayoutContext'
import ConditionalExecutionTooltipWrapper from '@pipeline/components/ConditionalExecutionToolTip/ConditionalExecutionTooltipWrapper'
import BarrierStepTooltip from './components/BarrierStepTooltip/BarrierStepTooltip'
import ResourceConstraintTooltip from './components/ResourceConstraints/ResourceConstraints'
import VerifyStepTooltip from './components/VerifyStepTooltip/VerifyStepTooltip'
import type { FailureInfo } from './components/VerifyStepTooltip/VerifyStepTooltip.types'
import css from './ExecutionStageDetails.module.scss'
 
export interface ExecutionStageDetailsProps {
  layout: ExecutionLayoutState
  onStepSelect(step?: string): void
  onStageSelect(step: string): void
}
 
export default function ExecutionStageDetails(props: ExecutionStageDetailsProps): React.ReactElement {
  const {
    pipelineExecutionDetail,
    pipelineStagesMap,
    loading,
    selectedStageId,
    selectedStepId,
    allNodeMap,
    setStepsGraphCanvasState,
    stepsGraphCanvasState,
    isDataLoadedForSelectedStage
  } = useExecutionContext()
  const { setStepDetailsVisibility } = useExecutionLayoutContext()
  const [barrierSetupId, setBarrierSetupId] = React.useState<string | undefined>()
  const [resourceUnit, setResourceUnit] = React.useState<string | undefined>()
  const [dynamicPopoverHandler, setDynamicPopoverHandler] = React.useState<
    DynamicPopoverHandlerBinding<unknown> | undefined
  >()
 
  const { executionIdentifier, accountId } = useParams<ExecutionPathProps>()
  const stage = pipelineStagesMap.get(selectedStageId)
  const {
    data: barrierInfo,
    loading: barrierInfoLoading,
    refetch: refetchBarrierInfo
  } = useGetBarrierInfo({
    lazy: true
  })
  const {
    data: resourceConstraintsData,
    loading: resourceConstraintsLoading,
    refetch: fetchResourceConstraints
  } = useGetResourceConstraintsExecutionInfo({
    lazy: true
  })
  const data: ExecutionPipeline<ExecutionNode> = {
    items: processExecutionData(pipelineExecutionDetail?.executionGraph),
    identifier: `${executionIdentifier}-${pipelineExecutionDetail?.executionGraph?.rootNodeId}`,
    status: stage?.status as any,
    allNodes: Object.keys(allNodeMap)
  }
 
  const refetchBarrierInfoRef = React.useCallback(refetchBarrierInfo, [])
  // load barrier info when barrier step is mouse over (when barrierSetupId is set)
  React.useEffect(() => {
    Iif (barrierSetupId) {
      refetchBarrierInfoRef({
        queryParams: {
          barrierSetupId: defaultTo(barrierSetupId, ''),
          planExecutionId: executionIdentifier
        }
      })
    }
  }, [barrierSetupId, refetchBarrierInfoRef])
 
  // TODO: consider removing debounce form next line
  const fetchResourceConstraintsRef = React.useCallback(debounce(fetchResourceConstraints, 1000), [])
  // load resource constrains when resource constrain step is mouse over (when resourceUnit is set)
  React.useEffect(() => {
    Iif (resourceUnit) {
      fetchResourceConstraintsRef({
        queryParams: {
          resourceUnit,
          accountId
        }
      })
    }
  }, [resourceUnit, fetchResourceConstraintsRef])
 
  // open details view when a step is selected
  React.useEffect(() => {
    setStepDetailsVisibility(!!selectedStepId)
  }, [selectedStepId, setStepDetailsVisibility])
 
  const onMouseEnter = (event: any): void => {
    const currentStage = event.stage || event.group
    const isFinished = currentStage?.data?.endTs
    const hasStarted = currentStage?.data?.startTs
    const status = currentStage?.data?.status
    dynamicPopoverHandler?.show(
      event.stageTarget,
      {
        event,
        data: currentStage
      },
      { useArrows: true, darkMode: false, fixedPosition: false }
    )
    if (!isFinished && hasStarted) {
      if (currentStage?.data?.stepType === StepType.Barrier && status !== 'Success') {
        setBarrierSetupId(currentStage?.data?.setupId)
      }
      if (currentStage?.data?.stepType === StepType.ResourceConstraint) {
        setResourceUnit(currentStage?.data?.stepParameters?.spec?.resourceUnit)
      }
    }
  }
 
  const renderPopover = ({
    data: stepInfo
  }: {
    data: {
      data: {
        stepType: string
        startTs: number
        status: string
        stepParameters: { identifier: string }
        failureInfo: FailureInfo
      }
      when: NodeRunInfo
    }
  }): JSX.Element => {
    return (
      <HoverCard data={stepInfo}>
        {stepInfo?.when && <ConditionalExecutionTooltipWrapper data={stepInfo.when} mode={Modes.STEP} />}
        {stepInfo?.data?.stepType === StepType.Barrier && stepInfo?.data?.status === 'Running' && (
          <BarrierStepTooltip
            loading={barrierInfoLoading}
            data={{ ...barrierInfo?.data, stepParameters: stepInfo?.data?.stepParameters }}
            startTs={stepInfo?.data?.startTs}
          />
        )}
        {stepInfo?.data?.stepType === StepType.ResourceConstraint && stepInfo?.data?.status === 'ResourceWaiting' && (
          <ResourceConstraintTooltip
            loading={resourceConstraintsLoading}
            data={{
              executionList: resourceConstraintsData?.data?.resourceConstraints,
              ...stepInfo,
              executionId: executionIdentifier
            }}
          />
        )}
        {stepInfo?.data?.stepType === StepType.Verify && stepInfo?.data?.status === 'Skipped' && (
          <VerifyStepTooltip failureInfo={stepInfo?.data?.failureInfo} />
        )}
      </HoverCard>
    )
  }
 
  // NOTE: check if we show stop node when stage has paused status
  const showEndNode = !(isExecutionRunning(stage?.status) || isExecutionPaused(stage?.status))
 
  return (
    <div className={css.main} data-layout={props.layout}>
      {!isEmpty(selectedStageId) && data.items?.length > 0 && (
        <ExecutionStageDiagram
          selectedIdentifier={selectedStepId}
          itemClickHandler={e => props.onStepSelect(e.stage.identifier)}
          data={data}
          showEndNode={showEndNode}
          disableCollapseButton={isExecutionRunning(stage?.status)}
          isWhiteBackground
          nodeStyle={{
            width: 64,
            height: 64
          }}
          loading={loading}
          gridStyle={{
            startX: 50,
            startY: 150
          }}
          graphConfiguration={{
            NODE_HAS_BORDER: false
          }}
          itemMouseEnter={onMouseEnter}
          itemMouseLeave={() => {
            dynamicPopoverHandler?.hide()
            setBarrierSetupId(undefined)
          }}
          mouseEnterStepGroupTitle={onMouseEnter}
          mouseLeaveStepGroupTitle={() => {
            dynamicPopoverHandler?.hide()
          }}
          canvasBtnsClass={css.canvasBtns}
          isStepView={true}
          setGraphCanvasState={state => setStepsGraphCanvasState?.(state)}
          graphCanvasState={stepsGraphCanvasState}
        />
      )}
      {loading && !isDataLoadedForSelectedStage && pipelineExecutionDetail && <PageSpinner fixed={false} />}
      <DynamicPopover
        className={css.popoverHeight}
        darkMode={true}
        render={renderPopover}
        bind={setDynamicPopoverHandler as any}
        closeOnMouseOut
      />
    </div>
  )
}