All files / modules/85-cv/pages/health-source/connectors/Dynatrace/components/DynatraceCustomMetrics DynatraceCustomMetrics.utils.ts

100% Statements 34/34
86.54% Branches 45/52
100% Functions 9/9
100% Lines 33/33

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                          25x     25x         2x 2x 2x   3x       25x           2x 2x 2x 1x   1x   2x 1x   2x     25x       3x                                         25x           2x   2x 2x     2x 2x                         2x 1x   2x     25x 8x 10x             25x 12x           2x 1x          
/*
 * 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 type { SelectOption } from '@wings-software/uicore'
import type { DynatraceMetricInfo } from '@cv/pages/health-source/connectors/Dynatrace/DynatraceHealthSource.types'
import type {
  CreatedMetricsWithSelectedIndex,
  SelectedAndMappedMetrics
} from '@cv/pages/health-source/connectors/Dynatrace/components/DynatraceCustomMetrics/DynatraceCustomMetrics.types'
import type { DynatraceMetricDTO } from 'services/cv'
import { DynatraceHealthSourceFieldNames } from '@cv/pages/health-source/connectors/Dynatrace/DynatraceHealthSource.constants'
import type { StringKeys } from 'framework/strings'
 
export function initializeCreatedMetrics(
  defaultSelectedMetricName: string,
  selectedMetric: string,
  mappedMetrics: Map<string, DynatraceMetricInfo>
): CreatedMetricsWithSelectedIndex {
  const createdMetrics = mappedMetrics.size ? Array.from(mappedMetrics.keys()) : [defaultSelectedMetricName]
  const metricToCheck = selectedMetric || defaultSelectedMetricName
  return {
    createdMetrics: createdMetrics,
    selectedMetricIndex: Array.from(createdMetrics).findIndex(metric => metric === metricToCheck)
  }
}
 
export function updateMappedMetricMap(
  metricValues: DynatraceMetricInfo,
  removedMetric: string,
  updatedMetric: string,
  oldState: SelectedAndMappedMetrics
): SelectedAndMappedMetrics {
  const { selectedMetric: oldMetric, mappedMetrics: oldMappedMetric } = oldState
  const updatedMap = new Map(oldMappedMetric)
  if (updatedMap.has(removedMetric)) {
    updatedMap.delete(removedMetric)
  } else {
    updatedMap.delete(oldMetric)
  }
  if (metricValues?.metricName !== removedMetric) {
    updatedMap.set(updatedMetric, { ...metricValues } || { metricName: updatedMetric })
  }
  return { selectedMetric: updatedMetric, mappedMetrics: updatedMap }
}
 
export function initializeSelectedMetricsMap(
  defaultSelectedMetricName: string,
  customMetrics?: Map<string, DynatraceMetricInfo>
): SelectedAndMappedMetrics {
  return {
    selectedMetric: (Array.from(customMetrics?.keys() || [])?.[0] as string) || defaultSelectedMetricName,
    mappedMetrics: customMetrics?.size
      ? customMetrics
      : new Map([
          [
            defaultSelectedMetricName,
            {
              identifier: defaultSelectedMetricName,
              metricSelector: '',
              sli: false,
              healthScore: false,
              continuousVerification: false,
              metricName: defaultSelectedMetricName,
              isNew: true
            }
          ]
        ])
  }
}
 
export function updateSelectedMetricsMap(
  updatedMetric: string,
  oldMetric: string,
  mappedMetrics: Map<string, DynatraceMetricInfo>,
  currentValues: DynatraceMetricInfo
): SelectedAndMappedMetrics {
  const updatedMap = new Map(mappedMetrics)
  // in the case where user updates metric name, update the key for current value
  Eif (oldMetric !== currentValues?.metricName) {
    updatedMap.delete(oldMetric)
  }
  // if newly created metric create form object
  Eif (!updatedMap.has(updatedMetric)) {
    updatedMap.set(updatedMetric, {
      metricName: updatedMetric,
      identifier: updatedMetric,
      isNew: true,
      sli: false,
      continuousVerification: false,
      healthScore: false,
      groupName: { label: '', value: '' },
      metricSelector: ''
    })
  }
 
  // update map with current form data
  if (currentValues?.metricName) {
    updatedMap.set(currentValues.metricName, currentValues)
  }
  return { selectedMetric: updatedMetric, mappedMetrics: updatedMap }
}
 
export function mapMetricSelectorsToMetricSelectorOptions(metrics: DynatraceMetricDTO[]): SelectOption[] {
  return metrics.map(metricSelector => {
    return {
      label: metricSelector.metricId || '',
      value: metricSelector.metricId || ''
    }
  })
}
 
export const editQueryConfirmationDialogProps = (getString: (key: StringKeys) => string, setField: any) => {
  return {
    titleText: getString('cv.monitoringSources.prometheus.querySettingsNotEditable'),
    contentText: getString('cv.monitoringSources.prometheus.querySettingsSubtext'),
    confirmButtonText: getString('cv.proceedToEdit'),
    cancelButtonText: getString('cancel'),
    onCloseDialog: (proceed: boolean) => {
      if (proceed) {
        setField(DynatraceHealthSourceFieldNames.MANUAL_QUERY, true)
      }
    }
  }
}