All files / modules/85-cv/pages/health-source/connectors/DatadogLogsHealthSource/components DatadogLogsMapToService.tsx

93.33% Statements 42/45
75% Branches 48/64
84.62% Functions 11/13
93.02% Lines 40/43

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              24x 24x 24x 24x 24x 24x 24x   24x   24x   24x   24x 7x 7x 7x 7x 7x 7x 7x 7x   7x                 7x 3x                         7x       7x 2x 2x 2x     7x 5x 5x 4x   1x     4x   4x   4x             7x 3x     3x 9x             7x                                                                                                            
/*
 * 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, { useCallback, useMemo, useState } from 'react'
import { Accordion, Container, FormInput, Layout, Utils } from '@wings-software/uicore'
import { useParams } from 'react-router-dom'
import { useStrings } from 'framework/strings'
import { SetupSourceCardHeader } from '@cv/components/CVSetupSourcesView/SetupSourceCardHeader/SetupSourceCardHeader'
import Card from '@cv/components/Card/Card'
import { QueryViewer } from '@cv/components/QueryViewer/QueryViewer'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { useGetDatadogLogIndexes, useGetDatadogLogSampleData } from 'services/cv'
 
import { MapDatadogLogsFieldNames } from '@cv/pages/health-source/connectors/DatadogLogsHealthSource/components/DatadogLogsMapToService.constants'
import type { DatadogLogsMapToServiceProps } from '@cv/pages/health-source/connectors/DatadogLogsHealthSource/components/DatadogLogsMapToService.type'
import css from '@cv/pages/health-source/connectors/DatadogLogsHealthSource/components/DatadogLogsMapToService.module.scss'
 
export default function DatadogLogsMapToService(props: DatadogLogsMapToServiceProps): JSX.Element {
  const { sourceData, formikProps } = props
  const { projectIdentifier, orgIdentifier, accountId } = useParams<ProjectPathProps & { identifier: string }>()
  const { getString } = useStrings()
  const [records, setRecords] = useState<Record<string, any>[]>([])
  const [isQueryExecuted, setIsQueryExecuted] = useState(false)
  const [logIndexesTracingId] = useMemo(() => [Utils.randomId()], [])
  const values = formikProps?.values
  const query = useMemo(() => (values?.query?.length ? values.query : ''), [values])
 
  const { data: indexes } = useGetDatadogLogIndexes({
    queryParams: {
      projectIdentifier,
      orgIdentifier,
      accountId,
      connectorIdentifier: sourceData.connectorRef as string,
      tracingId: logIndexesTracingId
    }
  })
  const logsSampleDataQueryParams = useMemo(
    () => ({
      accountId,
      projectIdentifier,
      orgIdentifier,
      tracingId: Utils.randomId(),
      connectorIdentifier: sourceData.connectorRef as string
    }),
    [accountId, projectIdentifier, orgIdentifier, sourceData.connectorRef]
  )
  const {
    mutate: queryDatadog,
    loading,
    error
  } = useGetDatadogLogSampleData({
    queryParams: logsSampleDataQueryParams
  })
 
  const fetchDatadogRecords = useCallback(async () => {
    const recordsData = await queryDatadog({ query: query }, { queryParams: { ...logsSampleDataQueryParams } })
    setRecords(recordsData?.data as Record<string, any>[])
    setIsQueryExecuted(true)
  }, [query, queryDatadog, logsSampleDataQueryParams])
 
  const hostIdentifierKeysOptions = useMemo(() => {
    const record = records?.length > 0 ? records[0] : null
    if (!record) {
      return []
    }
    return (
      Array.from(new Set((record.attributes.tags as string[]).concat('host')))
        ?.map(tag => {
          return tag.split(':')?.[0]
        })
        .filter(tagKey => !!tagKey)
        .map(tagKey => {
          return {
            label: tagKey,
            value: tagKey
          }
        }) || []
    )
  }, [records])
  const logIndexesOptions = useMemo(() => {
    Iif (!indexes?.data) {
      return []
    }
    return indexes?.data?.map(index => {
      return {
        value: index,
        label: index
      }
    })
  }, [indexes?.data])
 
  return (
    <Container className={css.main}>
      <SetupSourceCardHeader
        mainHeading={getString('cv.monitoringSources.prometheus.querySpecificationsAndMappings')}
        subHeading={getString('cv.monitoringSources.prometheus.customizeQuery')}
      />
      <Card>
        <Layout.Horizontal spacing="xlarge">
          <Accordion activeId="metricToService" className={css.accordian}>
            <Accordion.Panel
              id="metricToService"
              summary={getString('cv.monitoringSources.mapQueriesToServices')}
              details={
                <Container className={css.content}>
                  <FormInput.Text
                    label={getString('cv.monitoringSources.queryNameLabel')}
                    name={MapDatadogLogsFieldNames.METRIC_NAME}
                  />
                  <FormInput.MultiSelect
                    label={getString('cv.monitoringSources.datadogLogs.logIndexesLabel')}
                    name={MapDatadogLogsFieldNames.INDEXES}
                    items={logIndexesOptions}
                    onChange={selectedOptions => {
                      formikProps.setFieldValue(MapDatadogLogsFieldNames.INDEXES, selectedOptions)
                    }}
                  />
                  <FormInput.Select
                    label={getString('cv.monitoringSources.serviceInstanceIdentifier')}
                    name={MapDatadogLogsFieldNames.SERVICE_INSTANCE_IDENTIFIER_TAG}
                    items={hostIdentifierKeysOptions}
                    onChange={event => {
                      formikProps.setFieldValue(MapDatadogLogsFieldNames.SERVICE_INSTANCE_IDENTIFIER_TAG, event.value)
                    }}
                  />
                </Container>
              }
            />
          </Accordion>
          <QueryViewer
            queryNotExecutedMessage={getString('cv.monitoringSources.datadogLogs.submitQueryToSeeRecords')}
            isQueryExecuted={isQueryExecuted}
            className={css.validationContainer}
            records={records}
            fetchRecords={fetchDatadogRecords}
            loading={loading}
            error={error}
            query={formikProps?.values?.query || ''}
            dataTooltipId={'datadogQuery'}
          />
        </Layout.Horizontal>
      </Card>
    </Container>
  )
}