All files / modules/85-cv/pages/health-source/connectors/PrometheusHealthSource/components/PrometheusQueryBuilder PrometheusQueryBuilder.tsx

95.12% Statements 39/41
80.95% Branches 34/42
85.71% Functions 12/14
100% Lines 38/38

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              24x   24x   24x 24x 24x 24x 24x                       24x                 5x 5x 5x   5x       10x 2x 2x 2x 8x 2x       5x 5x   5x 5x 5x     5x 5x 5x     5x 4x                       35x   5x 1x     4x                           1x   1x             1x 1x                 1x   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 } from 'react'
import type { GetDataError } from 'restful-react'
import { Container, FormInput, MultiSelectOption, SelectOption } from '@wings-software/uicore'
import type { Failure, useGetLabelNames, useGetMetricNames } from 'services/cv'
import { getErrorMessage } from '@cv/utils/CommonUtils'
import { useStrings } from 'framework/strings'
import { useToaster } from '@common/exports'
import { PrometheusMonitoringSourceFieldNames } from '../../PrometheusHealthSource.constants'
import { PrometheusFilterSelector } from './components/PrometheusFilterSelector/PrometheusFilterSelector'
 
interface PrometheusQueryBuilderProps {
  onUpdateFilter: (fieldName: string, value: MultiSelectOption) => void
  onRemoveFilter: (fieldName: string, index: number) => void
  connectorIdentifier: string
  labelNamesResponse: ReturnType<typeof useGetLabelNames>
  metricNamesResponse: ReturnType<typeof useGetMetricNames>
  aggregatorValue?: string
  isManualQuery?: boolean
}
 
export function PrometheusQueryBuilder(props: PrometheusQueryBuilderProps): JSX.Element {
  const {
    onUpdateFilter,
    onRemoveFilter,
    connectorIdentifier,
    labelNamesResponse,
    metricNamesResponse,
    isManualQuery,
    aggregatorValue
  } = props
  const { showError, clear } = useToaster()
  const { getString } = useStrings()
 
  const handleLoadingAndError = (
    error: GetDataError<Failure | Error | null> | null,
    loading: boolean
  ): SelectOption[] | undefined => {
    if (error) {
      clear()
      showError(getErrorMessage(error), 7000)
      return []
    } else if (loading) {
      return [{ label: getString('loading'), value: '' }]
    }
  }
 
  const { data: metricNames, error: metricNameError, loading: loadingMetricNames } = metricNamesResponse
  const { data: labelNames, error: labelNameError, loading: loadingLabelNames } = labelNamesResponse
 
  const transformedMetricNames: SelectOption[] = useMemo(() => {
    const loadingOrError = handleLoadingAndError(metricNameError, loadingMetricNames)
    return loadingOrError || metricNames?.data?.map(metricName => ({ label: metricName, value: metricName })) || []
  }, [metricNames, metricNameError, loadingMetricNames])
 
  const transformedLabelNames: SelectOption[] = useMemo(() => {
    const options = handleLoadingAndError(labelNameError, loadingLabelNames)
    return options || labelNames?.data?.map(label => ({ label, value: label })) || []
  }, [labelNameError, labelNames, loadingLabelNames])
 
  const aggregationItems = useMemo(
    () => [
      { label: getString('cv.monitoringSources.prometheus.avgAggregator'), value: 'avg' },
      { label: getString('cv.monitoringSources.prometheus.countAggregator'), value: 'count' },
      { label: getString('cv.monitoringSources.prometheus.maxAggregator'), value: 'max' },
      { label: getString('cv.monitoringSources.prometheus.minAggregator'), value: 'min' },
      { label: getString('cv.monitoringSources.prometheus.stddevAggregator'), value: 'stddev' },
      { label: getString('cv.monitoringSources.prometheus.stdvarAggregator'), value: 'stdvar' },
      { label: getString('cv.monitoringSources.prometheus.sumAggregator'), value: 'sum' }
    ],
    []
  )
 
  const aggregatorOption = aggregationItems.find(item => item.value === aggregatorValue)
 
  if (isManualQuery) {
    return <Container data-name="emptyForManualQuery" />
  }
 
  return (
    <Container>
      <FormInput.Select
        name={PrometheusMonitoringSourceFieldNames.PROMETHEUS_METRIC}
        label={getString('cv.monitoringSources.prometheus.prometheusMetric')}
        items={transformedMetricNames}
        placeholder={transformedMetricNames?.[0]?.label === getString('loading') ? getString('loading') : undefined}
      />
      <PrometheusFilterSelector
        items={transformedLabelNames}
        name={PrometheusMonitoringSourceFieldNames.ENVIRONMENT_FILTER}
        label={getString('cv.monitoringSources.prometheus.environmentFilter')}
        connectorIdentifier={connectorIdentifier}
        onUpdateFilter={updatedItem =>
          onUpdateFilter(PrometheusMonitoringSourceFieldNames.ENVIRONMENT_FILTER, updatedItem)
        }
        onRemoveFilter={index => onRemoveFilter(PrometheusMonitoringSourceFieldNames.ENVIRONMENT_FILTER, index)}
      />
      <PrometheusFilterSelector
        items={transformedLabelNames}
        name={PrometheusMonitoringSourceFieldNames.SERVICE_FILTER}
        label={getString('cv.monitoringSources.prometheus.serviceFilter')}
        connectorIdentifier={connectorIdentifier}
        onUpdateFilter={updatedItem => onUpdateFilter(PrometheusMonitoringSourceFieldNames.SERVICE_FILTER, updatedItem)}
        onRemoveFilter={index => onRemoveFilter(PrometheusMonitoringSourceFieldNames.SERVICE_FILTER, index)}
      />
      <PrometheusFilterSelector
        items={transformedLabelNames}
        name={PrometheusMonitoringSourceFieldNames.ADDITIONAL_FILTER}
        label={getString('cv.monitoringSources.prometheus.additionalFilter')}
        connectorIdentifier={connectorIdentifier}
        isOptional={true}
        onUpdateFilter={updatedItem =>
          onUpdateFilter(PrometheusMonitoringSourceFieldNames.ADDITIONAL_FILTER, updatedItem)
        }
        onRemoveFilter={index => onRemoveFilter(PrometheusMonitoringSourceFieldNames.ADDITIONAL_FILTER, index)}
      />
      <FormInput.Select
        label={getString('cv.monitoringSources.prometheus.aggregator')}
        name={PrometheusMonitoringSourceFieldNames.AGGREGATOR}
        items={aggregationItems}
        isOptional={true}
        key={aggregatorOption?.value}
        selectProps={{
          addClearBtn: true
        }}
      />
    </Container>
  )
}