All files / modules/85-cv/hooks/useErrorBudgetRestHook/views ErrorBudgetResetHistory.tsx

100% Statements 38/38
94.44% Branches 17/18
100% Functions 7/7
100% Lines 38/38

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              11x 11x 11x   11x 11x 11x 11x   11x     11x 2x   2x             11x 2x 2x   2x             11x 2x 2x   2x             11x 2x   2x             11x 2x   2x             11x 4x 4x   4x                 4x 1x                       3x 1x   1x         2x 1x                           1x                                                                                       11x  
/*
 * 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 moment from 'moment'
import type { CellProps, Renderer } from 'react-table'
import { Container, Icon, PageError, TableV2, Text } from '@harness/uicore'
import { FontVariation, Color } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import { useGetErrorBudgetResetHistory, SLOErrorBudgetResetDTO } from 'services/cv'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { getErrorMessage } from '@cv/utils/CommonUtils'
import type { ErrorBudgetResetHistoryProps } from '../useErrorBudgetRestHook.types'
 
const RenderTimeAndDate: Renderer<CellProps<SLOErrorBudgetResetDTO>> = ({ row }) => {
  const { createdAt } = row.original
 
  return (
    <Text color={Color.BLACK} font={{ variation: FontVariation.SMALL }}>
      {moment(createdAt).format('L LT')}
    </Text>
  )
}
 
const RenderErrorBudget: Renderer<CellProps<SLOErrorBudgetResetDTO>> = ({ row }) => {
  const { getString } = useStrings()
  const { errorBudgetAtReset } = row.original
 
  return (
    <Text color={Color.BLACK} font={{ variation: FontVariation.SMALL }}>
      {errorBudgetAtReset} {getString('cv.minutes')}
    </Text>
  )
}
 
const RenderRemainingErrorBudget: Renderer<CellProps<SLOErrorBudgetResetDTO>> = ({ row }) => {
  const { getString } = useStrings()
  const { remainingErrorBudgetAtReset } = row.original
 
  return (
    <Text color={Color.BLACK} font={{ variation: FontVariation.SMALL }}>
      {remainingErrorBudgetAtReset} {getString('cv.minutes')}
    </Text>
  )
}
 
const RenderIncreasedErrorBudget: Renderer<CellProps<SLOErrorBudgetResetDTO>> = ({ row }) => {
  const { errorBudgetIncrementPercentage } = row.original
 
  return (
    <Text color={Color.BLACK} font={{ variation: FontVariation.SMALL }}>
      {errorBudgetIncrementPercentage} %
    </Text>
  )
}
 
const RenderReason: Renderer<CellProps<SLOErrorBudgetResetDTO>> = ({ row }) => {
  const { reason } = row.original
 
  return (
    <Text color={Color.BLACK} font={{ variation: FontVariation.SMALL }} lineClamp={1}>
      {reason}
    </Text>
  )
}
 
const ErrorBudgetResetHistory: React.FC<ErrorBudgetResetHistoryProps> = ({ serviceLevelObjectiveIdentifier }) => {
  const { getString } = useStrings()
  const { accountId, orgIdentifier, projectIdentifier } = useParams<ProjectPathProps>()
 
  const { data, loading, error, refetch } = useGetErrorBudgetResetHistory({
    identifier: serviceLevelObjectiveIdentifier,
    queryParams: {
      accountId,
      orgIdentifier,
      projectIdentifier
    }
  })
 
  if (loading) {
    return (
      <Container
        height={200}
        flex={{ justifyContent: 'center' }}
        border={{ top: true, bottom: true }}
        style={{ overflow: 'auto' }}
      >
        <Icon name="steps-spinner" color={Color.GREY_400} size={30} />
      </Container>
    )
  }
 
  if (error) {
    return (
      <Container height={200} border={{ top: true, bottom: true }} style={{ overflow: 'auto' }}>
        <PageError width={400} message={getErrorMessage(error)} onClick={() => refetch()} />
      </Container>
    )
  }
 
  if (!data?.resource?.length) {
    return (
      <Container
        height={200}
        flex={{ justifyContent: 'center' }}
        border={{ top: true, bottom: true }}
        style={{ overflow: 'auto' }}
      >
        <Text font={{ variation: FontVariation.BODY }} color={Color.GREY_600}>
          {getString('cv.noPreviousErrorBudgetResetHistoryAvailable')}
        </Text>
      </Container>
    )
  }
 
  return (
    <Container height={200} border={{ top: true, bottom: true }} style={{ overflow: 'auto' }}>
      <TableV2
        minimal
        sortable
        columns={[
          {
            Header: getString('cv.dateAndTime'),
            id: 'createdAt',
            accessor: 'createdAt',
            Cell: RenderTimeAndDate,
            width: 200
          },
          {
            Header: getString('cv.errorBudget'),
            id: 'errorBudgetAtReset',
            accessor: 'errorBudgetAtReset',
            Cell: RenderErrorBudget
          },
          {
            Header: getString('cv.remainingErrorBudget'),
            id: 'remainingErrorBudgetAtReset',
            accessor: 'remainingErrorBudgetAtReset',
            Cell: RenderRemainingErrorBudget
          },
          {
            Header: getString('cv.increaseWithPercentSign'),
            id: 'errorBudgetIncrementPercentage',
            accessor: 'errorBudgetIncrementPercentage',
            Cell: RenderIncreasedErrorBudget
          },
          {
            Header: getString('reason'),
            id: 'reason',
            accessor: 'reason',
            Cell: RenderReason
          }
        ]}
        data={data?.resource}
      />
    </Container>
  )
}
 
export default ErrorBudgetResetHistory