All files / modules/85-cv/pages/health-source/connectors/AppDynamics/Components/MetricPath MetricPath.utils.ts

94.59% Statements 35/37
70.73% Branches 29/41
100% Functions 4/4
93.75% Lines 30/32

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                26x   26x     26x           1x 1x 1x 1x 1x 1x   1x     1x 1x 1x 1x 2x       1x   1x 1x           1x                   26x 1x   26x 1x       1x           26x 3x 3x 3x 2x   3x    
/*
 * 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 type { IconName, SelectOption } from '@wings-software/uicore'
import { cloneDeep } from 'lodash-es'
import type { AppDynamicsFileDefinition } from 'services/cv'
import { MetricPathKeyPrefix } from './MetricPath.constants'
import type { MetricPathData } from './MetricPath.types'
 
export const onMetricPathChange = (
  selectedOption: SelectOption,
  index: number,
  metricPathData: MetricPathData
): MetricPathData => {
  // new logic
  const selectedPath = selectedOption.value as string
  const currentKey = MetricPathKeyPrefix + index
  const nextItemKey = MetricPathKeyPrefix + parseInt((index + 1).toString())
  const prvKey = MetricPathKeyPrefix + index
  const path = metricPathData[prvKey]?.path
  const lastItem = Object.keys(metricPathData)[Object.keys(metricPathData).length - 1]
 
  let metricPathDetails = cloneDeep(metricPathData)
 
  // delete values after index
  const lastIndex = parseInt(lastItem.split('_')[1])
  const lastItemexist = !!metricPathDetails[lastItem]
  Eif (index < lastIndex && lastItemexist) {
    for (let idx = index + 1; idx <= lastIndex; idx++) {
      delete metricPathDetails[MetricPathKeyPrefix + idx]
    }
  }
 
  Eif (!metricPathDetails[nextItemKey] && selectedPath) {
    // adding first time
    const nextPath = path?.length ? `${path}|${selectedPath}` : selectedPath
    metricPathDetails = {
      ...metricPathDetails,
      [nextItemKey]: { value: '', path: nextPath, isMetric: false }
    }
  }
 
  return {
    ...metricPathDetails,
    [currentKey]: {
      value: selectedPath,
      path,
      isMetric: selectedOption?.icon?.name ? isTypeMetric(getTypeByIconName(selectedOption?.icon?.name)) : false
    }
  }
}
 
export const isTypeMetric = (selctedType: AppDynamicsFileDefinition['type'] | undefined): boolean =>
  selctedType === 'leaf'
 
export const getTypeByIconName = (icon: IconName): AppDynamicsFileDefinition['type'] | undefined => {
  switch (icon) {
    case 'main-folder-open':
      return 'folder'
    case 'main-like':
      return 'leaf'
    default:
      return
  }
}
 
export const getSecondLastItemKey = (metricPathValue: MetricPathData): string => {
  let secondLastIndex = 0
  const metrickeysArray = Object.keys(metricPathValue)
  if (metrickeysArray.length > 1) {
    secondLastIndex = metrickeysArray.length - 2
  }
  return metrickeysArray[secondLastIndex]
}