All files / modules/85-cv/pages/monitored-service/CVMonitoredService/components/MonitoredServiceListView MonitoredServiceListView.tsx

87.5% Statements 56/64
60.34% Branches 35/58
92.31% Functions 12/13
87.5% Lines 56/64

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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340              10x 10x   10x 10x 10x 10x 10x 10x   10x 10x 10x 10x 10x     10x 10x 10x               10x 10x 10x 10x   10x 60x     10x 60x 60x   60x   60x                                                                                 10x 60x 60x   60x 60x                                                                                               10x 60x   60x                   60x     10x 60x   60x                   60x     10x                       28x   28x   28x 28x 60x   60x                     60x             6x                 2x       2x                                             28x   28x         32x 4x                                                                                                                                                                 10x  
/*
 * 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, Link } from 'react-router-dom'
import type { CellProps, Renderer } from 'react-table'
import { Container, Text, Layout, TableV2, NoDataCard, Heading } from '@wings-software/uicore'
import { FontVariation, Color } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import routes from '@common/RouteDefinitions'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { usePermission } from '@rbac/hooks/usePermission'
import ToggleOnOff from '@cv/pages/monitored-service/CVMonitoredService/components/ToggleOnOff/ToggleOnOff'
import noServiceAvailableImage from '@cv/assets/noServiceAvailable.png'
import FilterCard from '@cv/components/FilterCard/FilterCard'
import ContextMenuActions from '@cv/components/ContextMenuActions/ContextMenuActions'
import type { MonitoredServiceListItemDTO } from 'services/cv'
import type { ExtendedMonitoredServiceDTO } from '@cv/pages/monitored-service/components/Configurations/Configurations.utils'
import { EnvironmentToolTipDisplay } from '@cv/components/HarnessServiceAndEnvironment/components/EnvironmentToolTipDisplay'
import IconGrid from '../IconGrid/IconGrid'
import {
  calculateChangePercentage,
  RenderHealthTrend,
  RenderHealthScore,
  ServiceDeleteContext,
  getMonitoredServiceFilterOptions
} from '../../CVMonitoredService.utils'
import type { MonitoredServiceListViewProps } from '../../CVMonitoredService.types'
import MonitoredServiceCategory from '../../../components/Configurations/components/Dependency/component/components/MonitoredServiceCategory/MonitoredServiceCategory'
import { getListTitle } from './MonitoredServiceListView.utils'
import SLOsIconGrid from '../SLOsIconGrid/SLOsIconGrid'
import css from '../../CVMonitoredService.module.scss'
 
const CategoryProps: Renderer<CellProps<MonitoredServiceListItemDTO>> = ({ row }) => (
  <MonitoredServiceCategory type={row.original.type} abbrText verticalAlign />
)
 
const RenderServiceName: Renderer<CellProps<MonitoredServiceListItemDTO>> = ({ row }) => {
  const { accountId, orgIdentifier, projectIdentifier } = useParams<ProjectPathProps>()
  const monitoredService = row.original
 
  const envRefList = (monitoredService as ExtendedMonitoredServiceDTO)?.environmentRefList
 
  return (
    <Layout.Vertical>
      <Link
        to={routes.toCVAddMonitoringServicesEdit({
          accountId,
          orgIdentifier,
          projectIdentifier,
          identifier: monitoredService.identifier,
          module: 'cv'
        })}
      >
        <Text
          color={Color.PRIMARY_7}
          className={css.monitoredServiceName}
          title={monitoredService.serviceName}
          font={{ align: 'left', size: 'normal', weight: 'semi-bold' }}
        >
          {monitoredService.serviceName}
        </Text>
      </Link>
      <Link
        to={routes.toCVAddMonitoringServicesEdit({
          accountId,
          projectIdentifier,
          orgIdentifier,
          identifier: monitoredService.identifier,
          module: 'cv'
        })}
      >
        <EnvironmentToolTipDisplay
          type={monitoredService?.type}
          color={Color.PRIMARY_7}
          font={{ align: 'left', size: 'xsmall' }}
          envRefList={envRefList}
          environmentRef={monitoredService?.environmentRef}
        />
      </Link>
    </Layout.Vertical>
  )
}
 
const RenderServiceChanges: Renderer<CellProps<MonitoredServiceListItemDTO>> = ({ row }) => {
  const { getString } = useStrings()
  const monitoredService = row.original
 
  Eif (!monitoredService.changeSummary?.categoryCountMap) {
    return <></>
  }
 
  const { Deployment, Infrastructure, Alert } = monitoredService.changeSummary.categoryCountMap
  const { color, percentage } = calculateChangePercentage(monitoredService.changeSummary)
  const styles = {
    font: { variation: FontVariation.H6 },
    color: Color.BLACK
  }
 
  return (
    <Layout.Horizontal spacing="medium" flex={{ alignItems: 'center', justifyContent: 'flex-start' }}>
      <Text
        tooltip={getString('deploymentText')}
        icon="nav-project"
        iconProps={{ size: 16, color: Color.GREY_700 }}
        {...styles}
      >
        {Deployment.count ?? 0}
      </Text>
      <Text
        tooltip={getString('infrastructureText')}
        icon="infrastructure"
        iconProps={{ size: 20, color: Color.GREY_700 }}
        {...styles}
      >
        {Infrastructure.count ?? 0}
      </Text>
      <Text
        tooltip={getString('cv.changeSource.tooltip.incidents')}
        icon="warning-outline"
        iconProps={{ size: 16 }}
        {...styles}
      >
        {Alert.count ?? 0}
      </Text>
      <Text
        icon="symbol-triangle-up"
        color={color}
        font={{ variation: FontVariation.TINY_SEMI }}
        iconProps={{ size: 10, color: color }}
      >
        {`${percentage}%`}
      </Text>
    </Layout.Horizontal>
  )
}
 
const RenderDependenciesHealth: Renderer<CellProps<MonitoredServiceListItemDTO>> = ({ row }) => {
  const monitoredService = row.original
 
  Iif (monitoredService.dependentHealthScore?.length) {
    return (
      <IconGrid
        iconProps={{ name: 'polygon', size: 14, padding: { right: 'xsmall' } }}
        items={monitoredService.dependentHealthScore}
        width={100}
      />
    )
  }
 
  return null
}
 
const RenderSLOErrorBudgetData: Renderer<CellProps<MonitoredServiceListItemDTO>> = ({ row }) => {
  const monitoredService = row.original
 
  Iif (monitoredService.sloHealthIndicators?.length) {
    return (
      <SLOsIconGrid
        iconProps={{ name: 'symbol-square', size: 14, padding: { right: 'xsmall' } }}
        items={monitoredService.sloHealthIndicators}
        width={100}
      />
    )
  }
 
  return <></>
}
 
const MonitoredServiceListView: React.FC<MonitoredServiceListViewProps> = ({
  serviceCountData,
  monitoredServiceListData,
  selectedFilter,
  onFilter,
  onEditService,
  onDeleteService,
  onToggleService,
  healthMonitoringFlagLoading,
  refetchServiceCountData,
  setPage
}) => {
  const { getString } = useStrings()
 
  const { projectIdentifier } = useParams<ProjectPathProps>()
 
  const { content, pageSize = 0, pageIndex = 0, totalPages = 0, totalItems = 0 } = monitoredServiceListData || {}
  const RenderStatusToggle: Renderer<CellProps<MonitoredServiceListItemDTO>> = ({ row }) => {
    const monitoredService = row.original
 
    const [canToggle] = usePermission(
      {
        resource: {
          resourceType: ResourceType.MONITOREDSERVICE,
          resourceIdentifier: projectIdentifier
        },
        permissions: [PermissionIdentifier.TOGGLE_MONITORED_SERVICE]
      },
      [projectIdentifier]
    )
 
    return (
      <Layout.Horizontal flex={{ alignItems: 'center' }}>
        <ToggleOnOff
          disabled={!canToggle}
          checked={Boolean(monitoredService.healthMonitoringEnabled)}
          loading={healthMonitoringFlagLoading}
          onChange={checked => {
            onToggleService(monitoredService.identifier as string, checked)
          }}
        />
        <ContextMenuActions
          titleText={getString('common.delete', { name: monitoredService.serviceName })}
          contentText={<ServiceDeleteContext serviceName={monitoredService.serviceName} />}
          confirmButtonText={getString('yes')}
          deleteLabel={getString('cv.monitoredServices.deleteService')}
          onDelete={() => {
            onDeleteService(monitoredService.identifier as string)
          }}
          editLabel={getString('cv.monitoredServices.editService')}
          onEdit={() => {
            onEditService(monitoredService.identifier as string)
          }}
          RbacPermissions={{
            edit: {
              permission: PermissionIdentifier.EDIT_MONITORED_SERVICE,
              resource: {
                resourceType: ResourceType.MONITOREDSERVICE,
                resourceIdentifier: projectIdentifier
              }
            },
            delete: {
              permission: PermissionIdentifier.DELETE_MONITORED_SERVICE,
              resource: {
                resourceType: ResourceType.MONITOREDSERVICE,
                resourceIdentifier: projectIdentifier
              }
            }
          }}
        />
      </Layout.Horizontal>
    )
  }
 
  const filterOptions = getMonitoredServiceFilterOptions(getString, serviceCountData)
 
  return (
    <Container padding={{ top: 'medium', left: 'xlarge', right: 'xlarge' }} height="inherit">
      <FilterCard
        data={filterOptions}
        cardClassName={css.filterCard}
        selected={filterOptions.find(card => card.type === selectedFilter)}
        onChange={item => onFilter(item.type)}
      />
      {content?.length ? (
        <>
          <Heading
            level={2}
            font={{ variation: FontVariation.H6 }}
            color={Color.GREY_800}
            padding={{ top: 'large', bottom: 'large' }}
          >
            {getListTitle(getString, selectedFilter, totalItems)}
          </Heading>
          <TableV2
            sortable={true}
            columns={[
              {
                Header: ' ',
                width: '2.5%',
                Cell: CategoryProps
              },
              {
                Header: getString('name'),
                width: '12.5%',
                Cell: RenderServiceName
              },
              {
                Header: getString('cv.monitoredServices.sloErrorBudget'),
                width: '14%',
                Cell: RenderSLOErrorBudgetData
              },
              {
                Header: getString('cv.monitoredServices.table.changes'),
                width: '15%',
                Cell: RenderServiceChanges
              },
              {
                Header: getString('cv.monitoredServices.table.lastestHealthTrend'),
                width: '18%',
                Cell: RenderHealthTrend
              },
              {
                Header: getString('cv.monitoredServices.table.serviceHealthScore'),
                width: '15%',
                Cell: RenderHealthScore
              },
              {
                Header: getString('cv.monitoredServices.dependenciesHealth'),
                width: '15%',
                Cell: RenderDependenciesHealth
              },
              {
                Header: getString('enabledLabel'),
                width: '8%',
                Cell: RenderStatusToggle
              }
            ]}
            data={content}
            pagination={{
              pageSize,
              pageIndex,
              pageCount: totalPages,
              itemCount: totalItems,
              gotoPage: nextPage => {
                setPage(nextPage)
                refetchServiceCountData()
              }
            }}
          />
        </>
      ) : content && !content.length ? (
        <NoDataCard
          image={noServiceAvailableImage}
          message={getString('cv.monitoredServices.youHaveNoMonitoredServices')}
          imageClassName={css.noServiceAvailableImage}
          containerClassName={css.noDataContainer}
        />
      ) : null}
    </Container>
  )
}
 
export default MonitoredServiceListView