All files / modules/85-cv/pages/health-source/connectors/CustomHealthSource/components/MetricChartsValue MetricChartsValue.tsx

100% Statements 26/26
66.67% Branches 32/48
100% Functions 5/5
100% Lines 26/26

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              26x   26x 26x   26x 26x 26x 26x 26x 26x   26x 26x 26x   26x             6x 6x   6x 3x               6x         6x       6x 1x             1x         6x 4x     6x   6x                                                                                                                        
/*
 * 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, { useMemo, useCallback, useState } from 'react'
import type { GetDataError } from 'restful-react'
import { useParams } from 'react-router-dom'
import { Container, FormInput } from '@wings-software/uicore'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { InputWithDynamicModalForJson } from '@cv/components/InputWithDynamicModalForJson/InputWithDynamicModalForJson'
import MetricLineChart from '@cv/pages/health-source/common/MetricLineChart/MetricLineChart'
import { QueryType } from '@cv/pages/health-source/common/HealthSourceQueryType/HealthSourceQueryType.types'
import Button from '@rbac/components/Button/Button'
import { useStrings } from 'framework/strings'
import { TimeSeriesSampleDTO, useFetchParsedSampleData } from 'services/cv'
import type { MetricChartsValueInterface } from './MetricChartsValue.types'
import { CustomHealthSourceFieldNames } from '../../CustomHealthSource.constants'
import { getOptionsForChart } from '../../../NewRelic/components/NewRelicCustomMetricForm/NewRelicCustomMetricForm.utils'
import css from '../QueryMapping/QueryMapping.module.scss'
 
export default function MetricChartsValue({
  formikValues,
  formikSetFieldValue,
  isQueryExecuted,
  recordsData,
  isSelectingJsonPathDisabled
}: MetricChartsValueInterface): JSX.Element {
  const { getString } = useStrings()
  const { projectIdentifier, orgIdentifier, accountId } = useParams<ProjectPathProps & { identifier: string }>()
 
  const queryParamsForTimeSeriesData = useMemo(
    () => ({
      accountId,
      orgIdentifier,
      projectIdentifier
    }),
 
    [accountId, orgIdentifier, projectIdentifier]
  )
  const [customTimeSeriesData, setCustomTimeSeriesData] = useState<TimeSeriesSampleDTO[] | undefined>()
  const {
    mutate: fetchNewRelicTimeSeriesData,
    loading: timeSeriesDataLoading,
    error: timeseriesDataError
  } = useFetchParsedSampleData({
    queryParams: queryParamsForTimeSeriesData
  })
 
  const handleBuildChart = useCallback(() => {
    fetchNewRelicTimeSeriesData({
      groupName: formikValues?.groupName?.value as string,
      jsonResponse: JSON.stringify(recordsData),
      timestampFormat: formikValues?.timestampFormat,
      metricValueJSONPath: formikValues?.metricValue as string,
      timestampJSONPath: formikValues?.timestamp as string
    }).then(data => {
      setCustomTimeSeriesData(data.data)
    })
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [queryParamsForTimeSeriesData, formikValues, recordsData])
 
  const options = useMemo(() => {
    return customTimeSeriesData ? getOptionsForChart(customTimeSeriesData) : []
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [customTimeSeriesData])
  const sampleRecord = recordsData || null
 
  return (
    <Container className={css.widthHalf}>
      <InputWithDynamicModalForJson
        onChange={formikSetFieldValue}
        fieldValue={formikValues?.metricValue || ''}
        isQueryExecuted={isQueryExecuted}
        isDisabled={isSelectingJsonPathDisabled}
        sampleRecord={sampleRecord}
        inputName={CustomHealthSourceFieldNames.METRIC_VALUE}
        inputLabel={getString('cv.healthSource.connectors.NewRelic.metricFields.metricValueJsonPath.label')}
        recordsModalHeader={getString(
          'cv.healthSource.connectors.NewRelic.metricFields.metricValueJsonPath.recordsModalHeader'
        )}
        showExactJsonPath={true}
        dataTooltipId={'customhealthMetricValueJsonPath'}
      />
      <InputWithDynamicModalForJson
        onChange={formikSetFieldValue}
        fieldValue={formikValues?.timestamp || ''}
        isQueryExecuted={isQueryExecuted}
        isDisabled={isSelectingJsonPathDisabled}
        sampleRecord={sampleRecord}
        inputName={CustomHealthSourceFieldNames.TIMESTAMP_LOCATOR}
        inputLabel={getString('cv.healthSource.connectors.NewRelic.metricFields.timestampJsonPath.label')}
        noRecordInputLabel={'noRecordInputLabel'}
        recordsModalHeader={getString(
          'cv.healthSource.connectors.NewRelic.metricFields.timestampJsonPath.recordsModalHeader'
        )}
        showExactJsonPath={true}
        dataTooltipId={'customhealthTimestampJsonPath'}
      />
      {formikValues?.queryType === QueryType.HOST_BASED ? (
        <InputWithDynamicModalForJson
          onChange={formikSetFieldValue}
          fieldValue={formikValues?.serviceInstanceIdentifier || ''}
          isQueryExecuted={isQueryExecuted}
          isDisabled={isSelectingJsonPathDisabled}
          sampleRecord={sampleRecord}
          inputName={CustomHealthSourceFieldNames.SERVICE_INSTANCE}
          inputLabel={getString('cv.customHealthSource.ServiceInstance.pathLabel')}
          recordsModalHeader={getString('cv.customHealthSource.ServiceInstance.pathModalHeader')}
          showExactJsonPath={true}
          dataTooltipId={'customhealthServiceInstance'}
        />
      ) : null}
      <FormInput.Text
        label={getString('cv.healthSource.connectors.NewRelic.metricFields.timestampFormat')}
        name={CustomHealthSourceFieldNames.TIMESTAMP_FORMAT}
      />
      <Button intent="primary" text={getString('cv.healthSource.connectors.buildChart')} onClick={handleBuildChart} />
      <Container padding={{ top: 'small' }}>
        <MetricLineChart
          loading={timeSeriesDataLoading}
          error={timeseriesDataError as GetDataError<Error>}
          options={options}
        />
      </Container>
    </Container>
  )
}