All files / modules/75-ce/components/PerspectiveBuilderPreview PerspectiveBuilderPreview.tsx

96.67% Statements 29/30
80.36% Branches 45/56
50% Functions 1/2
96.67% Lines 29/30

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              3x 3x 3x 3x 3x 3x   3x               3x 3x                 3x 3x 3x   3x 3x 3x 3x                     3x               2x   2x   2x   2x   2x         2x                                 2x                             2x 2x   2x 2x                                                                                                 3x  
/*
 * 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 { useParams } from 'react-router-dom'
import { Container, Text, Layout } from '@wings-software/uicore'
import { FontVariation } from '@harness/design-system'
import { noop } from 'lodash-es'
import { useStrings } from 'framework/strings'
import type { CEView } from 'services/ce'
import {
  QlceViewFieldInputInput,
  ViewChartType,
  useFetchPerspectiveTimeSeriesQuery,
  QlceViewTimeGroupType,
  useFetchperspectiveGridQuery,
  ViewTimeRangeType
} from 'services/ce/services'
import CloudCostInsightChart from '@ce/components/CloudCostInsightChart/CloudCostInsightChart'
import {
  normalizeViewRules,
  getGroupByFilter,
  getTimeRangeFilter,
  getTimeFilters,
  getViewFilterForId,
  perspectiveDefaultTimeRangeMapper,
  getIdFiltersFromRules
} from '@ce/utils/perspectiveUtils'
import { CCM_CHART_TYPES } from '@ce/constants'
import { DAYS_FOR_TICK_INTERVAL } from '@ce/components/CloudCostInsightChart/Chart'
import { CE_DATE_FORMAT_INTERNAL, getGMTEndDateTime, getGMTStartDateTime } from '@ce/utils/momentUtils'
import type { TimeRangeFilterType } from '@ce/types'
import { AGGREGATE_FUNCTION } from '../PerspectiveGrid/Columns'
import PerspectiveGrid from '../PerspectiveGrid/PerspectiveGrid'
import GroupByView from './GroupByView/GroupByView'
import css from './PerspectiveBuilderPreview.module.scss'
 
interface PerspectiveBuilderPreviewProps {
  groupBy: QlceViewFieldInputInput
  setGroupBy: (groupBy: QlceViewFieldInputInput) => void
  chartType: ViewChartType
  setChartType: (type: ViewChartType) => void
  formValues: CEView
  showBusinessMappingButton?: boolean
}
 
const PerspectiveBuilderPreview: React.FC<PerspectiveBuilderPreviewProps> = ({
  groupBy,
  setGroupBy,
  chartType,
  setChartType,
  formValues,
  showBusinessMappingButton
}) => {
  const { perspectiveId } = useParams<{ perspectiveId: string }>()
 
  const timeRangeMapper = perspectiveDefaultTimeRangeMapper
 
  const timeRange = formValues.viewTimeRange?.viewTimeRangeType || ViewTimeRangeType.Last_7
 
  const dateRange = timeRangeMapper[timeRange]
 
  const timeRangeObj: TimeRangeFilterType = {
    to: dateRange[1].format(CE_DATE_FORMAT_INTERNAL),
    from: dateRange[0].format(CE_DATE_FORMAT_INTERNAL)
  }
 
  const [chartResult] = useFetchPerspectiveTimeSeriesQuery({
    variables: {
      filters: [
        getViewFilterForId(perspectiveId, true),
        ...getTimeFilters(getGMTStartDateTime(timeRangeObj.from), getGMTEndDateTime(timeRangeObj.to)),
        ...getIdFiltersFromRules(normalizeViewRules(formValues.viewRules))
      ],
      limit: 12,
      groupBy: [
        getTimeRangeFilter(
          (formValues.viewVisualization?.granularity as QlceViewTimeGroupType) || QlceViewTimeGroupType.Day
        ),
        getGroupByFilter(groupBy)
      ]
    }
  })
 
  const [gridResults] = useFetchperspectiveGridQuery({
    variables: {
      aggregateFunction: AGGREGATE_FUNCTION.DEFAULT,
      filters: [
        getViewFilterForId(perspectiveId, true),
        ...getTimeFilters(dateRange[0].valueOf(), dateRange[1].valueOf()),
        ...getIdFiltersFromRules(normalizeViewRules(formValues.viewRules))
      ],
      limit: 100,
      offset: 0,
      isClusterOnly: false,
      groupBy: [getGroupByFilter(groupBy)]
    }
  })
 
  const { data: gridData, fetching: gridFetching } = gridResults
  const { data: chartData, fetching } = chartResult
 
  const { getString } = useStrings()
  return (
    <Container padding="xxlarge" background="white" className={css.previewMainContainer}>
      <Layout.Vertical spacing="xlarge">
        <Container>
          <Text font={{ variation: FontVariation.H4 }} margin={{ bottom: 'medium' }}>
            {getString('ce.perspectives.createPerspective.preview.title')}
          </Text>
          <GroupByView
            timeRange={timeRangeObj}
            setGroupBy={setGroupBy}
            groupBy={groupBy}
            chartType={chartType}
            setChartType={setChartType}
            showBusinessMappingButton={showBusinessMappingButton}
          />
 
          <CloudCostInsightChart
            chartType={chartType === ViewChartType.StackedLineChart ? CCM_CHART_TYPES.AREA : CCM_CHART_TYPES.COLUMN}
            columnSequence={[]}
            setFilterUsingChartClick={() => {
              noop
            }}
            fetching={fetching}
            showLegends={false}
            data={chartData?.perspectiveTimeSeriesStats as any}
            aggregation={
              (formValues.viewVisualization?.granularity as QlceViewTimeGroupType) || QlceViewTimeGroupType.Day
            }
            xAxisPointCount={chartData?.perspectiveTimeSeriesStats?.stats?.length || DAYS_FOR_TICK_INTERVAL + 1}
          />
        </Container>
        <Container width="100%">
          <Text color="grey900">Cost Breakdown</Text>
          {gridData?.perspectiveGrid?.data && (
            <PerspectiveGrid
              gridFetching={gridFetching}
              gridData={gridData?.perspectiveGrid?.data as any}
              groupBy={groupBy}
              showColumnSelector={false}
              tempGridColumns={true}
              showPagination={false}
            />
          )}
        </Container>
      </Layout.Vertical>
    </Container>
  )
}
 
export default PerspectiveBuilderPreview