All files / modules/75-cd/components/ServiceDetails/ActiveServiceInstances ActiveServiceInstancePopover.tsx

96.43% Statements 27/28
60.53% Branches 46/76
100% Functions 5/5
96.3% Lines 26/27

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              11x 11x 11x 11x 11x             11x 11x 11x                                   11x 2x 1x       3x             7x                                                           11x 3x 2x 2x   2x               2x             2x 1x     1x   1x                               2x                                             1x                                     1x    
/*
 * 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 { useParams } from 'react-router-dom'
import { Card, Layout, Text } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import {
  GetActiveInstancesByServiceIdEnvIdAndBuildIdsQueryParams,
  InstanceDetailsDTO,
  NativeHelmInstanceInfoDTO,
  useGetActiveInstancesByServiceIdEnvIdAndBuildIds
} from 'services/cd-ng'
import type { ProjectPathProps, ServicePathProps } from '@common/interfaces/RouteInterfaces'
import { getReadableDateTime } from '@common/utils/dateUtils'
import { useStrings } from 'framework/strings'
import css from '@cd/components/ServiceDetails/ActiveServiceInstances/ActiveServiceInstances.module.scss'
 
export type InstanceData = Record<string, Record<string, InstanceDetailsDTO[]>>
 
export interface ActiveServiceInstancePopoverProps {
  buildId?: string
  envId?: string
  instanceNum?: number
}
 
interface SectionProps {
  header: string
  values: {
    label: string
    value: string
  }[]
}
 
const Section: React.FC<{ data: SectionProps[] }> = props => {
  const { data } = props
  return (
    <Card className={css.activeServiceInstancePopover}>
      <Layout.Vertical>
        {data.map(item => {
          return (
            <Layout.Vertical key={item.header} className={css.activeServiceInstancePopoverSection}>
              <Text font={{ weight: 'semi-bold', size: 'small' }} margin={{ bottom: 'small' }} color={Color.GREY_800}>
                {item.header}
              </Text>
              <Layout.Vertical>
                {item.values.map(itemValue =>
                  itemValue.value ? (
                    <Layout.Horizontal key={itemValue.label}>
                      <Text
                        font={{ weight: 'semi-bold', size: 'small' }}
                        color={Color.GREY_500}
                        margin={{ right: 'medium', bottom: 'xsmall' }}
                      >
                        {`${itemValue.label}:`}
                      </Text>
                      <Text
                        font={{ weight: 'semi-bold', size: 'small' }}
                        color={Color.GREY_800}
                        className={css.sectionValue}
                      >
                        {itemValue.value}
                      </Text>
                    </Layout.Horizontal>
                  ) : (
                    <></>
                  )
                )}
              </Layout.Vertical>
            </Layout.Vertical>
          )
        })}
      </Layout.Vertical>
    </Card>
  )
}
 
export const ActiveServiceInstancePopover: React.FC<ActiveServiceInstancePopoverProps> = props => {
  const { buildId = '', envId = '', instanceNum = 0 } = props
  const { accountId, orgIdentifier, projectIdentifier, serviceId } = useParams<ProjectPathProps & ServicePathProps>()
  const { getString } = useStrings()
 
  const queryParams: GetActiveInstancesByServiceIdEnvIdAndBuildIdsQueryParams = {
    accountIdentifier: accountId,
    orgIdentifier,
    projectIdentifier,
    serviceId,
    envId,
    buildIds: [buildId]
  }
  const { loading, data, error } = useGetActiveInstancesByServiceIdEnvIdAndBuildIds({
    queryParams,
    queryParamStringifyOptions: {
      arrayFormat: 'repeat'
    }
  })
 
  if (loading || error || !data?.data?.instancesByBuildIdList?.[0]?.instances?.length) {
    return <></>
  }
 
  const instanceData = data?.data?.instancesByBuildIdList?.[0]?.instances[instanceNum] || {}
 
  const sectionData: SectionProps[] = [
    {
      header: getString('cd.serviceDashboard.instanceDetails'),
      values: [
        {
          label: getString('cd.serviceDashboard.pod'),
          value: instanceData.podName || ''
        },
        {
          label: getString('cd.serviceDashboard.artifact'),
          value: instanceData.artifactName || ''
        }
      ]
    },
    {
      header: getString('infrastructureText'),
      values: Object.keys(instanceData.infrastructureDetails || {}).map(infrastructureDetailsKey => ({
        label: infrastructureDetailsKey,
        value: instanceData.infrastructureDetails?.[infrastructureDetailsKey]
      }))
    },
    {
      header: getString('deploymentText'),
      values: [
        {
          label: getString('cd.serviceDashboard.deployedAt'),
          value: getReadableDateTime(instanceData.deployedAt, 'MMM DD, YYYY hh:mm a')
        },
        {
          label: getString('cd.serviceDashboard.deployedBy'),
          value: instanceData.deployedByName
        },
        {
          label: getString('common.pipeline'),
          value: instanceData.pipelineExecutionName
        }
      ]
    }
  ]
  Iif ((instanceData.instanceInfoDTO as NativeHelmInstanceInfoDTO)?.helmChartInfo) {
    sectionData.push({
      header: getString('cd.serviceDashboard.helmChartDetails'),
      values: [
        {
          label: getString('cd.serviceDashboard.helmChartName'),
          value: (instanceData.instanceInfoDTO as NativeHelmInstanceInfoDTO).helmChartInfo?.name as string
        },
        {
          label: getString('cd.serviceDashboard.helmRopoUrl'),
          value: (instanceData.instanceInfoDTO as NativeHelmInstanceInfoDTO).helmChartInfo?.repoUrl as string
        },
        {
          label: getString('helmVersion'),
          value: (instanceData.instanceInfoDTO as NativeHelmInstanceInfoDTO).helmChartInfo?.version as string
        }
      ]
    })
  }
  return <Section data={sectionData}></Section>
}