All files / modules/85-cv/pages/monitored-service MonitoredServicePage.tsx

85.71% Statements 42/49
80% Branches 44/55
42.86% Functions 3/7
87.5% Lines 42/48

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              9x 9x 9x 9x 9x   9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x   9x 3x 3x 3x 3x                 3x                 3x   3x 1x                 1x       2x       2x                                     2x                                 2x     3x                 2x                     2x                                                                                             9x 5x 4x   4x 3x     1x                     9x  
/*
 * 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 { useHistory, useParams } from 'react-router-dom'
import { Container, Tabs, PageError, Page, FlexExpander, Views } from '@wings-software/uicore'
import { useQueryParams } from '@common/hooks'
import { useStrings } from 'framework/strings'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { useGetMonitoredService } from 'services/cv'
import routes from '@common/RouteDefinitions'
import { getCVMonitoringServicesSearchParam, getErrorMessage } from '@cv/utils/CommonUtils'
import DetailsBreadcrumb from '@cv/pages/monitored-service/views/DetailsBreadcrumb'
import DetailsHeaderTitle from '@cv/pages/monitored-service/views/DetailsHeaderTitle'
import DetailsToolbar from '@cv/pages/monitored-service/views/DetailsToolbar'
import Configurations from './components/Configurations/Configurations'
import { MonitoredServiceEnum } from './MonitoredServicePage.constants'
import ServiceHealth from './components/ServiceHealth/ServiceHealth'
import HealthScoreCard from './components/ServiceHealth/components/HealthScoreCard/HealthScoreCard'
import CVSLOsListingPage from '../slos/CVSLOsListingPage'
import { isProjectChangedOnMonitoredService } from './MonitoredServicePage.utils'
import css from './MonitoredServicePage.module.scss'
 
const ServiceHealthAndConfiguration: React.FC = () => {
  const history = useHistory()
  const { getString } = useStrings()
  const { tab = MonitoredServiceEnum.SLOs, view } = useQueryParams<{ tab?: MonitoredServiceEnum; view?: Views.GRID }>()
  const { orgIdentifier, projectIdentifier, accountId, identifier } = useParams<
    ProjectPathProps & { identifier: string }
  >()
 
  const {
    data: monitoredServiceData,
    refetch,
    loading,
    error
  } = useGetMonitoredService({
    identifier,
    queryParams: {
      accountId,
      orgIdentifier,
      projectIdentifier
    }
  })
 
  const { monitoredService, lastModifiedAt } = monitoredServiceData?.data ?? {}
 
  if (error) {
    Iif (isProjectChangedOnMonitoredService(error, identifier)) {
      history.push(
        routes.toCVMonitoringServices({
          projectIdentifier,
          orgIdentifier,
          accountId
        })
      )
    } else {
      return <PageError message={getErrorMessage(error)} onClick={() => refetch()} />
    }
  }
 
  Iif (!loading && !monitoredService) {
    return <Page.NoDataCard message={getString('noData')} />
  }
 
  const onTabChange = (nextTab: MonitoredServiceEnum): void => {
    if (nextTab !== tab) {
      history.push({
        pathname: routes.toCVAddMonitoringServicesEdit({
          accountId,
          orgIdentifier,
          projectIdentifier,
          identifier,
          module: 'cv'
        }),
        search: getCVMonitoringServicesSearchParam({
          view,
          tab: nextTab
        })
      })
    }
  }
 
  const panelServiceHealth = (
    <Page.Body
      loading={loading}
      noData={{
        when: () => !monitoredService
      }}
      className={css.pageBody}
    >
      <ServiceHealth
        hasChangeSource={!!monitoredService?.sources?.changeSources?.length}
        monitoredServiceIdentifier={monitoredService?.identifier ?? ''}
        serviceIdentifier={monitoredService?.serviceRef as string}
        environmentIdentifier={monitoredService?.environmentRef as string}
      />
    </Page.Body>
  )
 
  const panelSLO = (
    <Page.Body
      loading={loading}
      noData={{
        when: () => !monitoredService
      }}
      className={css.pageBody}
    >
      <CVSLOsListingPage monitoredService={monitoredService} />
    </Page.Body>
  )
 
  const panelConfigurations = (
    <Page.Body
      loading={loading}
      noData={{
        when: () => !monitoredService
      }}
      className={css.pageBody}
    >
      <Configurations />
    </Page.Body>
  )
 
  return (
    <>
      <Page.Header
        size="large"
        breadcrumbs={<DetailsBreadcrumb />}
        title={<DetailsHeaderTitle loading={loading} monitoredService={monitoredService} />}
        toolbar={
          <DetailsToolbar loading={loading} monitoredService={monitoredService} lastModifiedAt={lastModifiedAt} />
        }
        className={css.header}
      />
      <Container className={css.monitoredServiceTabs}>
        <Tabs
          id="monitoredServiceTabs"
          selectedTabId={tab}
          onChange={onTabChange}
          tabList={[
            {
              id: MonitoredServiceEnum.SLOs,
              title: getString('cv.slos.title'),
              panel: panelSLO
            },
            {
              id: MonitoredServiceEnum.ServiceHealth,
              title: getString('cv.monitoredServices.monitoredServiceTabs.serviceHealth'),
              panel: panelServiceHealth
            },
            {
              id: MonitoredServiceEnum.Configurations,
              title: getString('cv.monitoredServices.monitoredServiceTabs.configurations'),
              panel: panelConfigurations
            }
          ]}
        >
          <FlexExpander />
          {tab === MonitoredServiceEnum.ServiceHealth && (
            <HealthScoreCard
              monitoredServiceIdentifier={monitoredService?.identifier}
              monitoredServiceLoading={loading}
            />
          )}
        </Tabs>
      </Container>
    </>
  )
}
 
const CVMonitoredServiceDetails: React.FC = () => {
  const { getString } = useStrings()
  const { identifier } = useParams<{ identifier?: string }>()
 
  if (identifier) {
    return <ServiceHealthAndConfiguration />
  }
 
  return (
    <>
      <Page.Header
        breadcrumbs={<DetailsBreadcrumb />}
        title={getString('cv.monitoredServices.addNewMonitoredServices')}
      />
      <Configurations />
    </>
  )
}
 
export default CVMonitoredServiceDetails