All files / modules/85-cv/components/CloudMetricsHealthSource CloudMetricsHealthSource.tsx

80.43% Statements 37/46
50.59% Branches 43/85
50% Functions 4/8
80.43% Lines 37/46

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              24x 24x 24x 24x 24x 24x 24x 24x   24x 24x 24x 24x 24x 24x   24x 24x   24x 24x   24x                                     8x 8x 8x 8x 8x   8x 4x     8x 4x 2x     8x       8x                                     8x   8x   8x                                                                                                                                                                         4x 1x   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, { useContext, useEffect, useMemo, useState } from 'react'
import { isEmpty, omit } from 'lodash-es'
import { Container, Heading, Text, useConfirmationDialog } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { useParams } from 'react-router-dom'
import { useStrings } from 'framework/strings'
import { SetupSourceLayout } from '@cv/components/CVSetupSourcesView/SetupSourceLayout/SetupSourceLayout'
import { transformSampleDataIntoHighchartOptions } from '@cv/pages/health-source/connectors/GCOMetricsHealthSource/GCOMetricsHealthSource.utils'
 
import DrawerFooter from '@cv/pages/health-source/common/DrawerFooter/DrawerFooter'
import { QueryContent } from '@cv/components/QueryViewer/QueryViewer'
import { FieldNames } from '@cv/pages/health-source/connectors/GCOMetricsHealthSource/GCOMetricsHealthSource.constants'
import { SetupSourceTabsContext } from '@cv/components/CVSetupSourcesView/SetupSourceTabs/SetupSourceTabs'
import MetricsValidationChart from '@cv/components/CloudMetricsHealthSource/components/validationChart/MetricsValidationChart'
import MetricDashboardWidgetNav from '@cv/components/MetricDashboardWidgetNav/MetricDashboardWidgetNav'
import type { CloudMetricsHealthSourceProps } from '@cv/components/CloudMetricsHealthSource/CloudMetricsHealthSource.type'
import SelectHealthSourceServices from '@cv/pages/health-source/common/SelectHealthSourceServices/SelectHealthSourceServices'
import MetricErrorAndLoading from '@cv/pages/health-source/common/MetricErrorAndLoading/MetricErrorAndLoading'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { useGetLabelNames, useGetMetricPacks } from 'services/cv'
import css from '@cv/components/CloudMetricsHealthSource/CloudMetricHealthSource.module.scss'
 
export default function CloudMetricsHealthSource<T>(props: CloudMetricsHealthSourceProps<T>): JSX.Element {
  const {
    metricDetailsContent,
    selectedMetricInfo,
    onFetchTimeseriesData,
    timeseriesDataLoading,
    timeseriesDataError,
    dashboards,
    connectorRef,
    onWidgetMetricSelected,
    onNextClicked,
    manualQueries,
    addManualQueryTitle,
    dataSourceType,
    dashboardDetailRequest,
    dashboardDetailMapper,
    formikProps,
    onChangeManualEditQuery,
    serviceInstanceList
  } = props
  const { getString } = useStrings()
  const { onPrevious } = useContext(SetupSourceTabsContext)
  const [shouldShowChart, setShouldShowChart] = useState<boolean>(false)
  const { projectIdentifier, orgIdentifier, accountId } = useParams<ProjectPathProps>()
 
  const sampleData = useMemo(() => {
    return transformSampleDataIntoHighchartOptions(selectedMetricInfo?.timeseriesData || [])
  }, [selectedMetricInfo?.timeseriesData])
 
  useEffect(() => {
    if (!selectedMetricInfo?.timeseriesData) {
      setShouldShowChart(false)
    }
  }, [selectedMetricInfo?.timeseriesData])
  const metricPackResponse = useGetMetricPacks({
    queryParams: { projectIdentifier, orgIdentifier, accountId, dataSourceType: dataSourceType }
  })
 
  const { openDialog } = useConfirmationDialog({
    titleText: getString('cv.monitoringSources.prometheus.querySettingsNotEditable'),
    contentText: getString('cv.monitoringSources.prometheus.querySettingsSubtext'),
    confirmButtonText: getString('cv.proceedToEdit'),
    cancelButtonText: getString('cancel'),
    onCloseDialog: (proceed: boolean) => {
      if (proceed) {
        onChangeManualEditQuery?.(true)
      }
    }
  })
  const {
    sli = false,
    healthScore = false,
    continuousVerification = false,
    isManualQuery,
    isCustomCreatedMetric,
    serviceInstance,
    riskCategory
  } = formikProps?.values
 
  const { loading: metricLoading } = dashboardDetailRequest
 
  return (
    <Container>
      <SetupSourceLayout
        leftPanelContent={
          <MetricDashboardWidgetNav<T>
            dashboardWidgetMapper={dashboardDetailMapper}
            dashboardDetailsRequest={dashboardDetailRequest}
            addManualQueryTitle={addManualQueryTitle}
            connectorIdentifier={connectorRef}
            manuallyInputQueries={manualQueries}
            dashboards={dashboards}
            showSpinnerOnLoad={!selectedMetricInfo}
            onSelectMetric={(id, metricName, query, widget, dashboardId, dashboardTitle) => {
              onWidgetMetricSelected({
                id,
                metricName,
                query,
                widgetName: widget,
                dashboardTitle,
                dashboardId
              })
            }}
          />
        }
        content={
          <Container className={css.setupContainer}>
            <Heading level={3} color={Color.BLACK} className={css.sectionHeading}>
              {getString('cv.monitoringSources.gco.mapMetricsToServicesPage.querySpecifications')}
            </Heading>
            {formikProps.values?.isManualQuery && (
              <Container className={css.manualQueryWarning}>
                <Text icon="warning-sign" iconProps={{ size: 14 }}>
                  {getString('cv.monitoringSources.prometheus.isManualQuery')}
                </Text>
                <Text intent="primary" onClick={() => onChangeManualEditQuery?.(false)}>
                  {getString('cv.monitoringSources.prometheus.undoManualQuery')}
                </Text>
              </Container>
            )}
            <MetricErrorAndLoading isEmpty={isEmpty(omit(formikProps.values, 'identifier'))} loading={metricLoading}>
              <Container className={css.metricsMappingContent}>
                <Container className={css.metricsQueryBuilderContainer}>
                  {metricDetailsContent}
                  <Container className={css.healthServicesContainer}>
                    <SelectHealthSourceServices
                      values={{
                        sli,
                        healthScore,
                        riskCategory,
                        serviceInstance,
                        continuousVerification
                      }}
                      metricPackResponse={metricPackResponse}
                      labelNamesResponse={
                        { data: { data: serviceInstanceList } } as ReturnType<typeof useGetLabelNames>
                      }
                    />
                  </Container>
                </Container>
                <Container className={css.validationContainer}>
                  <QueryContent
                    textAreaProps={{ readOnly: !selectedMetricInfo?.queryEditable }}
                    handleFetchRecords={async () => {
                      if (!shouldShowChart) {
                        setShouldShowChart(true)
                      }
                      if (!formikProps?.values?.query?.length) {
                        return
                      }
                      onFetchTimeseriesData(formikProps.values.query)
                    }}
                    isDialogOpen={false}
                    onEditQuery={isCustomCreatedMetric && !isManualQuery ? openDialog : undefined}
                    query={selectedMetricInfo.query}
                    loading={!selectedMetricInfo}
                    textAreaName={FieldNames.QUERY}
                  />
                  <MetricsValidationChart
                    submitQueryText={'cv.monitoringSources.datadogLogs.submitQueryToSeeRecords'}
                    loading={timeseriesDataLoading}
                    error={timeseriesDataError}
                    sampleData={sampleData}
                    queryValue={selectedMetricInfo?.query}
                    isQueryExecuted={shouldShowChart}
                    onRetry={async () => {
                      if (!formikProps?.values?.query?.length) {
                        return
                      }
                      onFetchTimeseriesData(formikProps.values.query)
                    }}
                  />
                </Container>
              </Container>
            </MetricErrorAndLoading>
            <DrawerFooter onPrevious={onPrevious} isSubmit onNext={onNextClicked} />
          </Container>
        }
      />
    </Container>
  )
}