All files / modules/70-pipeline/components/Dashboards shared.tsx

65.52% Statements 57/87
46.84% Branches 37/79
66.67% Functions 8/12
67.86% Lines 57/84

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              19x 19x 19x   19x       19x 26x 1x   25x 25x 25x       19x 2x 2x   2x     2x 2x 2x 2x 2x       2x 1x 1x   2x 2x 2x   2x   2x   1x     2x       2x                                                 19x 5x 3x 3x   3x 3x             19x 15x 15x 15x 12x 12x               19x 8x 8x 6x 6x 6x         6x   8x     19x               19x                     19x 12x                           1x                               1x                       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, { useEffect, useState } from 'react'
import { useToaster } from '@wings-software/uicore'
import moment from 'moment'
import type { GetDataError } from 'restful-react'
import useRBACError, { RBACError } from '@rbac/utils/useRBACError/useRBACError'
import type { ExecutionStatus } from '@pipeline/utils/statusHelpers'
import type { Failure, Error } from 'services/cd-ng'
 
export function roundNumber(value?: number, precision = 2) {
  if (typeof value !== 'number') {
    return value
  }
  Eif (Number.isInteger(precision) && precision >= 0) {
    const factor = 10 ** precision
    return Math.round(value * factor) / factor
  }
}
 
export function formatDuration(value?: number | string) {
  Eif (typeof value === 'string') {
    value = Number.parseInt(value)
  }
  Iif (typeof value !== 'number') {
    return
  }
  let signPrefix = undefined
  let h = 0
  let m = 0
  let s = 0
  Iif (value < 0) {
    signPrefix = '-'
    value = Math.abs(value)
  }
  if (value >= 3600) {
    h = Math.floor(value / 3600)
    value = value % 3600
  }
  Eif (value >= 60) {
    m = Math.floor(value / 60)
    value = value % 60
  }
  s = value
 
  if (h > 0) {
    // truncate seconds for large intervals
    s = 0
  }
 
  const labelStyle = {
    fontSize: '0.7em',
    marginRight: '0.2em'
  }
  return (
    <>
      {signPrefix}
      {h > 0 && (
        <span>
          {h}
          <span style={labelStyle}>h</span>
        </span>
      )}
      {m > 0 && (
        <span style={{ fontSize: h > 0 ? '0.8em' : '1em' }}>
          {m}
          <span style={labelStyle}>m</span>
        </span>
      )}
      {s > 0 && (
        <span style={{ fontSize: m > 0 ? '0.8em' : '1em' }}>
          {s}
          <span style={labelStyle}>s</span>
        </span>
      )}
    </>
  )
}
 
export function diffStartAndEndTime(startTime?: number, endTime?: number): string | undefined {
  if (startTime && startTime > -1 && endTime && endTime > -1) {
    const diffMins = moment(endTime).diff(startTime, 'minutes')
    Iif (diffMins === 0) {
      return `${moment(endTime).diff(startTime, 'seconds')}s`
    } else Eif (diffMins < 180) {
      return `${diffMins}m`
    } else {
      return `${moment(endTime).diff(startTime, 'hours')}h`
    }
  }
}
 
export function useErrorHandler(error: GetDataError<Failure | Error> | null, timeout?: number, key?: string) {
  const toaster = useToaster()
  const { getRBACErrorMessage } = useRBACError()
  useEffect(() => {
    const errorMsg = getRBACErrorMessage(error as RBACError)
    Iif (errorMsg) {
      if (!(toaster as any)?.state?.toasts?.find((t: any) => t.message === errorMsg)) {
        toaster.showError(errorMsg, timeout, key)
      }
    }
  }, [error])
}
 
export function useRefetchCall(refetch: () => Promise<any>, loading: boolean, pollInterval = 10000) {
  const [fetching, setFetching] = useState(false)
  useEffect(() => {
    let timeoutId = 0
    Eif (!loading) {
      timeoutId = window.setTimeout(() => {
        setFetching(true)
        refetch().finally(() => setFetching(false))
      }, pollInterval)
    }
    return () => clearTimeout(timeoutId)
  }, [loading])
  return fetching
}
 
export const FailedStatus: Partial<Record<ExecutionStatus, ExecutionStatus>> = {
  Failed: 'Failed',
  Aborted: 'Aborted',
  Expired: 'Expired',
  IgnoreFailed: 'IgnoreFailed',
  Errored: 'Errored'
}
 
export const ActiveStatus: Partial<Record<ExecutionStatus, ExecutionStatus>> = {
  Running: 'Running',
  AsyncWaiting: 'AsyncWaiting',
  TaskWaiting: 'TaskWaiting',
  TimedWaiting: 'TimedWaiting',
  Paused: 'Paused',
  InterventionWaiting: 'InterventionWaiting',
  ApprovalWaiting: 'ApprovalWaiting',
  ResourceWaiting: 'ResourceWaiting'
}
 
export function mapToExecutionStatus(status?: string): ExecutionStatus | undefined {
  switch (status) {
    case 'INTERVENTIONWAITING':
      return 'InterventionWaiting'
    case 'APPROVALWAITING':
      return 'ApprovalWaiting'
    case 'RESOURCEWAITING':
      return 'ResourceWaiting'
    case 'NOTSTARTED':
      return 'NotStarted'
    case 'QUEUED':
      return 'Queued'
    case 'SKIPPED':
      return 'Skipped'
    case 'RUNNING':
      return 'Running'
    case 'ASYNCWAITING':
      return 'AsyncWaiting'
    case 'TASKWAITING':
      return 'TaskWaiting'
    case 'TIMEDWAITING':
      return 'TimedWaiting'
    case 'PAUSED':
      return 'Paused'
    case 'PAUSING':
      return 'Pausing'
    case 'DISCONTINUING':
      return 'Discontinuing'
    case 'SUSPENDED':
      return 'Suspended'
    case 'FAILED':
      return 'Failed'
    case 'ABORTED':
      return 'Aborted'
    case 'EXPIRED':
      return 'Expired'
    case 'IGNOREFAILED':
      return 'IgnoreFailed'
    case 'ERRORED':
      return 'Errored'
    case 'APPROVALREJECTED':
      return 'ApprovalRejected'
    case 'SUCCESS':
      return 'Success'
  }
}