All files / modules/75-ce/pages/recommendationDetails RecommendationDetailsPage.tsx

90.57% Statements 48/53
62.07% Branches 36/58
70% Functions 7/10
90.38% Lines 47/52

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              1x 1x   1x                         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x                                                   1x 3x               3x   3x                                                                                             1x                                                                                                                                 1x 4x         3x 3x 3x 3x   3x 3x   3x 2x     3x   3x                 3x   3x 3x 3x 3x   3x                                     3x                                                               6x                                                         3x 3x   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, { useEffect, useState } from 'react'
import { useParams, useHistory } from 'react-router-dom'
 
import {
  Container,
  Layout,
  Text,
  Button,
  Card,
  PageBody,
  Popover,
  ButtonVariation,
  ButtonSize,
  PillToggle,
  Icon
} from '@wings-software/uicore'
import { Color, FontVariation } from '@harness/design-system'
import { Position, Menu, MenuItem, Slider } from '@blueprintjs/core'
import { NGBreadcrumbs } from '@common/components/NGBreadcrumbs/NGBreadcrumbs'
import { useStrings } from 'framework/strings'
import { Page } from '@common/exports'
import routes from '@common/RouteDefinitions'
import { GET_DATE_RANGE } from '@ce/utils/momentUtils'
import { QualityOfService, RecommendationItem, TimeRangeValue, TimeRange, TimeRangeType } from '@ce/types'
import { ViewTimeRange } from '@ce/components/RecommendationDetails/constants'
import { RecommendationOverviewStats, ResourceType, useFetchRecommendationQuery } from 'services/ce/services'
import { useTelemetry } from '@common/hooks/useTelemetry'
import { PAGE_NAMES, USER_JOURNEY_EVENTS } from '@ce/TrackingEventsConstants'
import CustomizeRecommendationsImg from './images/custom-recommendations.gif'
 
import RecommendationDetails from '../../components/RecommendationDetails/RecommendationDetails'
import css from './RecommendationDetailsPage.module.scss'
 
interface ResourceDetails {
  cpu?: string
  memory: string
}
interface ResourceObject {
  limits: ResourceDetails
  requests: ResourceDetails
}
interface ContainerRecommendaitons {
  current: ResourceObject
}
interface RecommendationDetails {
  items: Array<RecommendationItem>
  containerRecommendations: Record<string, ContainerRecommendaitons>
}
interface WorkloadDetailsProps {
  workloadData: WorkloadDataType
  goToWorkloadDetails: () => void
  qualityOfService: QualityOfService
  setQualityOfService: React.Dispatch<React.SetStateAction<QualityOfService>>
  cpuAndMemoryValueBuffer: number
  setCpuAndMemoryValueBuffer: React.Dispatch<React.SetStateAction<number>>
}
 
const WorkloadDetails: React.FC<WorkloadDetailsProps> = props => {
  const { getString } = useStrings()
  const {
    workloadData,
    goToWorkloadDetails,
    qualityOfService,
    setQualityOfService,
    cpuAndMemoryValueBuffer,
    setCpuAndMemoryValueBuffer
  } = props
 
  return (
    <Container padding="xlarge">
      <Layout.Vertical spacing="medium">
        <Layout.Horizontal margin={{ bottom: 'medium' }} flex={{ justifyContent: 'space-between' }}>
          <Text font={{ variation: FontVariation.H5 }} tooltipProps={{ dataTooltipId: 'workloadDetails' }}>
            {getString('ce.perspectives.workloadDetails.workloadDetailsText')}
          </Text>
          <Button variation={ButtonVariation.SECONDARY} size={ButtonSize.SMALL} onClick={goToWorkloadDetails}>
            {getString('ce.recommendation.detailsPage.viewMoreDetailsText')}
          </Button>
        </Layout.Horizontal>
        <Layout.Vertical margin={{ bottom: 'medium' }}>
          <Text font={{ variation: FontVariation.SMALL_SEMI }} color={Color.GREY_500}>
            {getString('common.cluster')}
          </Text>
          <Text font={{ variation: FontVariation.SMALL_SEMI }} color={Color.GREY_800}>
            {workloadData.clusterName}
          </Text>
        </Layout.Vertical>
        <Layout.Vertical margin={{ bottom: 'medium' }}>
          <Text font={{ variation: FontVariation.SMALL_SEMI }} color={Color.GREY_500}>
            {getString('ce.perspectives.workloadDetails.fieldNames.workload')}
          </Text>
          <Text font={{ variation: FontVariation.SMALL_SEMI }} color={Color.GREY_800}>
            {workloadData.resourceName}
          </Text>
        </Layout.Vertical>
        <Text font={{ variation: FontVariation.H5 }} tooltipProps={{ dataTooltipId: 'tuneRecommendations' }}>
          {getString('ce.recommendation.detailsPage.tuneRecommendations')}
        </Text>
        <Card style={{ padding: 0, border: 0 }}>
          <Container padding="medium">
            <Text font={{ variation: FontVariation.H6 }} tooltipProps={{ dataTooltipId: 'setQoSAndBuffer' }}>
              {getString('ce.recommendation.detailsPage.setQoSAndBuffer')}
            </Text>
          </Container>
          <Container padding="medium" background={Color.PRIMARY_1} className={css.tuneRecomContainer}>
            <Text font={{ variation: FontVariation.SMALL_SEMI }} margin={{ bottom: 'small' }}>
              {getString('ce.recommendation.detailsPage.qualityOfService')}
            </Text>
            <PillToggle
              selectedView={qualityOfService}
              options={[
                { label: getString('ce.recommendation.detailsPage.burstable'), value: QualityOfService.BURSTABLE },
                { label: getString('ce.recommendation.detailsPage.guaranteed'), value: QualityOfService.GUARANTEED }
              ]}
              className={css.pillToggle}
              onChange={val => setQualityOfService(val)}
            />
            <Text font={{ variation: FontVariation.SMALL_SEMI }} margin={{ bottom: 'medium', top: 'medium' }}>
              {getString('ce.recommendation.detailsPage.memoryValueBuffer')}
            </Text>
            <Container className={css.sliderContainer}>
              <Text
                font={{ variation: FontVariation.SMALL_SEMI, align: 'right' }}
                margin={{ bottom: 'small', top: 'small', right: 'xxlarge' }}
              >
                {`${cpuAndMemoryValueBuffer}%`}
              </Text>
              <Layout.Horizontal style={{ alignItems: 'center' }} spacing="medium">
                <Text font={{ variation: FontVariation.TINY }} color={Color.GREY_600}>
                  0
                </Text>
                <Slider
                  min={0}
                  max={100}
                  stepSize={1}
                  labelRenderer={false}
                  value={cpuAndMemoryValueBuffer}
                  onChange={val => setCpuAndMemoryValueBuffer(val)}
                  className={css.bufferSlider}
                />
                <Text font={{ variation: FontVariation.TINY }} color={Color.GREY_600}>
                  100
                </Text>
              </Layout.Horizontal>
            </Container>
          </Container>
        </Card>
        <Container padding="medium" background={Color.BLUE_50}>
          <Layout.Horizontal spacing="small">
            <Icon name="info-messaging" />
            <Container>
              <Layout.Horizontal spacing="small">
                <Text color={Color.GREY_700} font={{ variation: FontVariation.SMALL }}>
                  {getString('ce.recommendation.detailsPage.customDetailsText1')}
                </Text>
                <img className={css.customImage} src={CustomizeRecommendationsImg} alt="custom-recommendation-img" />
              </Layout.Horizontal>
              <Container padding={{ top: 'small' }}>
                <Text inline color={Color.GREY_700} font={{ variation: FontVariation.SMALL }}>
                  {getString('ce.recommendation.detailsPage.customDetailsText2')}
                </Text>
                <Text inline color={Color.GREY_800} font={{ variation: FontVariation.SMALL_SEMI }}>
                  {getString('ce.recommendation.detailsPage.customDetailsText3')}
                </Text>
              </Container>
            </Container>
          </Layout.Horizontal>
        </Container>
      </Layout.Vertical>
    </Container>
  )
}
 
interface WorkloadDataType {
  clusterName?: string
  namespace?: string
  id?: string
  resourceName?: string
}
 
const RecommendationDetailsPage: React.FC = () => {
  const { recommendation, accountId, recommendationName } = useParams<{
    recommendation: string
    recommendationName: string
    accountId: string
  }>()
  const { getString } = useStrings()
  const { trackPage, trackEvent } = useTelemetry()
  const history = useHistory()
  const [timeRange, setTimeRange] = useState<TimeRangeValue>({ value: TimeRangeType.LAST_7, label: TimeRange.LAST_7 })
 
  const [qualityOfService, setQualityOfService] = useState<QualityOfService>(QualityOfService.BURSTABLE)
  const [cpuAndMemoryValueBuffer, setCpuAndMemoryValueBuffer] = useState(0)
 
  useEffect(() => {
    trackPage(PAGE_NAMES.RECOMMENDATIONS_DETAILS_PAGE, {})
  }, [])
 
  const timeRangeFilter = GET_DATE_RANGE[timeRange.value]
 
  const [result] = useFetchRecommendationQuery({
    variables: {
      id: recommendation,
      resourceType: ResourceType.Workload,
      startTime: timeRangeFilter[0],
      endTime: timeRangeFilter[1]
    }
  })
 
  const { data, fetching } = result
 
  const recommendationDetails = (data?.recommendationDetails as RecommendationDetails) || {}
  const recommendationStats = data?.recommendationStatsV2 as RecommendationOverviewStats
  const recommendationItems = recommendationDetails?.items || []
  const workloadData = data?.recommendationsV2?.items?.length && data?.recommendationsV2?.items[0]
 
  const goToWorkloadDetails = () => {
    if (workloadData) {
      trackEvent(USER_JOURNEY_EVENTS.RECOMMENDATION_VIEW_MORE_CLICK, {})
      workloadData.clusterName &&
        workloadData.resourceName &&
        workloadData.namespace &&
        history.push(
          routes.toCERecommendationWorkloadDetails({
            accountId,
            recommendation,
            recommendationName,
            clusterName: workloadData.clusterName,
            namespace: workloadData.namespace,
            workloadName: workloadData.resourceName
          })
        )
    }
  }
 
  return (
    <>
      <Page.Header
        title={recommendationName}
        breadcrumbs={
          <NGBreadcrumbs
            links={[
              {
                url: routes.toCERecommendations({ accountId }),
                label: getString('ce.recommendation.sideNavText')
              }
            ]}
          />
        }
      />
      <PageBody loading={fetching}>
        <Card style={{ width: '100%' }}>
          <Layout.Horizontal spacing="small">
            <Text color={Color.GREY_800} font={{ weight: 'semi-bold' }}>
              {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>
                  {ViewTimeRange.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>
 
        {recommendationItems.length ? (
          <Container className={css.detailsContainer} padding="xxlarge">
            <Layout.Vertical spacing="large">
              {Object.keys(recommendationDetails.containerRecommendations || {}).map((cRKey, index) => {
                const item = recommendationItems.find(rI => rI.containerName === cRKey) || ({} as RecommendationItem)
                const currentResources = recommendationDetails.containerRecommendations[cRKey].current || {}
 
                return (
                  <RecommendationDetails
                    key={`${item.containerName}-${index}-${timeRange.label}`}
                    histogramData={item}
                    currentResources={currentResources}
                    timeRange={timeRange}
                    recommendationStats={recommendationStats}
                    qualityOfService={qualityOfService}
                    timeRangeFilter={timeRangeFilter}
                    cpuAndMemoryValueBuffer={cpuAndMemoryValueBuffer}
                    currentContainer={index + 1}
                    totalContainers={Object.keys(recommendationDetails.containerRecommendations || {}).length}
                  />
                )
              })}
            </Layout.Vertical>
            <WorkloadDetails
              goToWorkloadDetails={goToWorkloadDetails}
              workloadData={workloadData as WorkloadDataType}
              qualityOfService={qualityOfService}
              setQualityOfService={setQualityOfService}
              cpuAndMemoryValueBuffer={cpuAndMemoryValueBuffer}
              setCpuAndMemoryValueBuffer={setCpuAndMemoryValueBuffer}
            />
          </Container>
        ) : null}
      </PageBody>
    </>
  )
}
 
export default RecommendationDetailsPage