All files / modules/75-ce/pages/node-recommendation-details NodeRecommendationDetailsPage.tsx

94.44% Statements 34/36
71.93% Branches 41/57
75% Functions 3/4
94.44% Lines 34/36

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              1x 1x 1x 1x 1x 1x   1x 1x 1x 1x                 1x 1x 1x 1x 1x 1x 1x               1x 1x 1x 1x       1x   1x 1x     1x                   1x   1x 1x   1x     1x               1x       1x                                                                     3x                                                                                 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, { useMemo, useState } from 'react'
import { useParams } from 'react-router-dom'
import { Layout, Text, Card, Popover, Container } from '@wings-software/uicore'
import { Color, FontVariation } from '@harness/design-system'
import { Position, Menu, MenuItem } from '@blueprintjs/core'
import { defaultTo } from 'lodash-es'
 
import { NodepoolTimeRange, NodepoolTimeRangeType, NodepoolTimeRangeValue } from '@ce/types'
import { GET_NODEPOOL_DATE_RANGE } from '@ce/utils/momentUtils'
import { Page } from '@common/exports'
import {
  NodeRecommendationDto,
  RecommendationItemDto,
  RecommendationOverviewStats,
  RecommendNodePoolClusterRequest,
  ResourceType,
  useFetchNodeRecommendationRequestQuery,
  useFetchRecommendationQuery
} from 'services/ce/services'
import routes from '@common/RouteDefinitions'
import { useStrings } from 'framework/strings'
import { getProviderIcon } from '@ce/utils/recommendationUtils'
import { NGBreadcrumbs } from '@common/components/NGBreadcrumbs/NGBreadcrumbs'
import { ViewNodepoolTimeRange } from '@ce/components/RecommendationDetails/constants'
import NodeRecommendationDetails from '@ce/components/NodeRecommendation/NodeRecommendation'
import css from './NodeRecommendationDetailsPage.module.scss'
 
interface Params {
  recommendation: string
  recommendationName: string
  accountId: string
}
 
const NodeRecommendationDetailsPage = () => {
  const { getString } = useStrings()
  const { recommendation, accountId, recommendationName } = useParams<Params>()
  const [timeRange, setTimeRange] = useState<NodepoolTimeRangeValue>({
    value: NodepoolTimeRangeType.LAST_7,
    label: NodepoolTimeRange.LAST_7
  })
  const timeRangeFilter = GET_NODEPOOL_DATE_RANGE[timeRange.value]
 
  const breadCrumbLinks = useMemo(() => {
    return [{ url: routes.toCERecommendations({ accountId }), label: getString('ce.recommendation.sideNavText') }]
  }, [])
 
  const [{ data, fetching }] = useFetchRecommendationQuery({
    variables: {
      id: recommendation,
      resourceType: ResourceType.NodePool,
      startTime: timeRangeFilter[0],
      endTime: timeRangeFilter[1]
    }
  })
 
  const nodePoolData =
    (data?.recommendationsV2?.items?.length && data?.recommendationsV2?.items[0]) || ({} as RecommendationItemDto)
 
  const recommendationStats = (data?.recommendationStatsV2 || {}) as RecommendationOverviewStats
  const recommendationDetails = (data?.recommendationDetails || {}) as NodeRecommendationDto
 
  const clusterId = recommendationDetails.nodePoolId?.clusterid
 
  const [{ data: nodeRecommendationRequestData, fetching: nodeRecommendationDataFetching }] =
    useFetchNodeRecommendationRequestQuery({
      variables: {
        nodePoolId: { clusterid: clusterId || '', nodepoolname: recommendationName },
        startTime: timeRangeFilter[0],
        endTime: timeRangeFilter[1]
      }
    })
 
  Iif (fetching || nodeRecommendationDataFetching) {
    return <Page.Spinner />
  }
 
  return (
    <>
      <Page.Header
        title={
          <Layout.Horizontal spacing="small">
            <Text
              icon={getProviderIcon(defaultTo(recommendationDetails.recommended?.provider, ''))}
              font={{ variation: FontVariation.BODY1 }}
            >
              {getString('ce.recommendation.detailsPage.headerText')}
            </Text>
            <Text font={{ variation: FontVariation.H4 }} style={{ verticalAlign: 'middle' }}>
              {nodePoolData.resourceName}
            </Text>
          </Layout.Horizontal>
        }
        breadcrumbs={<NGBreadcrumbs className={css.breadCrumb} links={breadCrumbLinks} />}
      />
      <Page.Body>
        <Card style={{ width: '100%' }}>
          <Layout.Horizontal spacing="small">
            <Text color={Color.GREY_800} font={{ variation: FontVariation.H6 }}>
              {getString('ce.recommendation.detailsPage.utilizationDataComputation')}
            </Text>
            <Popover
              position={Position.BOTTOM_LEFT}
              modifiers={{
                arrow: { enabled: false },
                flip: { enabled: true },
                keepTogether: { enabled: true },
                preventOverflow: { enabled: true }
              }}
              content={
                <Menu>
                  {ViewNodepoolTimeRange.map(viewTimeRange => (
                    <MenuItem
                      onClick={() => {
                        setTimeRange(viewTimeRange)
                      }}
                      text={viewTimeRange.label}
                      key={viewTimeRange.value}
                    />
                  ))}
                </Menu>
              }
            >
              <Text
                color={Color.PRIMARY_5}
                rightIcon="caret-down"
                rightIconProps={{
                  color: Color.PRIMARY_5
                }}
                className={css.actionText}
              >
                {timeRange?.label}
              </Text>
            </Popover>
          </Layout.Horizontal>
        </Card>
        <Container className={css.body}>
          <NodeRecommendationDetails
            recommendationStats={recommendationStats}
            recommendationDetails={recommendationDetails}
            timeRange={timeRange}
            recommendationName={recommendationName}
            nodeRecommendationRequestData={
              nodeRecommendationRequestData?.nodeRecommendationRequest as RecommendNodePoolClusterRequest
            }
            nodePoolData={nodePoolData as RecommendationItemDto}
          />
        </Container>
      </Page.Body>
    </>
  )
}
 
export default NodeRecommendationDetailsPage