All files / modules/85-cv/pages/health-source/connectors/SplunkHealthSource SplunkHealthSource.utils.ts

95% Statements 19/20
75% Branches 63/84
100% Functions 4/4
94.74% Lines 18/19

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              25x   25x       25x 1x                     1x 1x 1x           1x     25x       2x                       25x 2x 2x 2x 2x 2x 2x           2x          
/*
 * 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 { isEmpty } from 'lodash-es'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { HealthSourceTypes } from '../../types'
import type { SplunkHealthSourcePayload, MapSplunkQueryToService } from './components/MapQueriesToHarnessService/types'
import type { SplunkHealthSourceInfo } from './SplunkHealthSource.types'
 
export function createSplunkHealthSourcePayload(setupSource: SplunkHealthSourceInfo): SplunkHealthSourcePayload {
  const splunkHealthSourcePayload: SplunkHealthSourcePayload = {
    type: HealthSourceTypes.Splunk,
    identifier: setupSource?.identifier as string,
    name: setupSource?.name as string,
    spec: {
      connectorRef: setupSource.connectorRef as string,
      feature: 'Splunk Cloud Logs',
      queries: []
    }
  }
 
  for (const entry of setupSource?.mappedServicesAndEnvs?.entries()) {
    const { metricName, query, serviceInstance }: MapSplunkQueryToService = entry[1]
    splunkHealthSourcePayload.spec.queries.push({
      name: metricName,
      query,
      serviceInstanceIdentifier: serviceInstance
    })
  }
  return splunkHealthSourcePayload
}
 
export function buildSplunkHealthSourceInfo(
  params: ProjectPathProps,
  data: any
): SplunkHealthSourceInfo & ProjectPathProps {
  return {
    ...params,
    name: data?.healthSourceName,
    identifier: data?.healthSourceIdentifier,
    connectorRef: data?.connectorRef,
    isEdit: data?.isEdit,
    product: data?.product?.value,
    type: HealthSourceTypes.Splunk,
    mappedServicesAndEnvs: getMappedServicesAndEnvs(data)
  }
}
 
const getMappedServicesAndEnvs = (data: any): Map<string, MapSplunkQueryToService> => {
  const currentHealthSource = data?.healthSourceList?.find((el: any) => el?.identifier === data?.healthSourceIdentifier)
  const mappedQueries = currentHealthSource?.spec?.queries
  Eif (currentHealthSource && !isEmpty(mappedQueries)) {
    const mappedServicesAndEnvs = new Map<string, MapSplunkQueryToService>()
    for (const query of mappedQueries) {
      mappedServicesAndEnvs.set(query?.name, {
        metricName: query?.name,
        serviceInstance: query?.serviceInstanceIdentifier,
        query: query?.query
      })
    }
    return mappedServicesAndEnvs
  } else {
    return new Map<string, MapSplunkQueryToService>()
  }
}