All files / modules/75-ce/utils anomaliesUtils.ts

100% Statements 33/33
88.46% Branches 23/26
100% Functions 7/7
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              11x   11x   11x 11x 11x 11x   11x             11x                             11x       18x 18x 18x   74x 21x                           18x     11x 10x                   11x 3x 1x   2x 1x   1x     11x       2x 2x   2x   7x 7x 7x 14x     7x                             2x    
/*
 * 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 { CloudProvider } from '@ce/types'
import type { QlceViewFilterInput } from 'services/ce/services'
import { DEFAULT_GROUP_BY } from './perspectiveUtils'
 
const gcpEntities = ['gcpProduct', 'gcpProjectId', 'gcpSKUDescription']
const awsEntities = ['awsUsageAccountId', 'awsServiceCode', 'awsInstancetype', 'awsUsageType']
const azureEntities: string[] = []
const clusterEntities = ['clusterName', 'clusterId', 'namespace', 'workloadName', 'workloadType']
 
const cloudProviderToEntityMapping = {
  [CloudProvider.GCP]: gcpEntities,
  [CloudProvider.AWS]: awsEntities,
  [CloudProvider.CLUSTER]: clusterEntities,
  [CloudProvider.AZURE]: azureEntities
}
 
const fieldToFieldNameMapping: Record<string, string> = {
  gcpProduct: 'Product',
  gcpProjectId: 'Project',
  gcpSKUDescription: 'SKUs',
  clusterName: 'Cluster Name',
  namespace: 'Namespace',
  workloadName: 'Workload',
  awsUsageAccountId: 'Account',
  awsServiceCode: 'Service',
  awsInstancetype: 'Instance Type',
  awsUsageType: 'Usage Type',
  workloadType: 'Workload Type',
  gcpSkuDescription: 'SKUs'
}
 
export function generateFilters(
  entityMap: Record<string, string>,
  cloudProvider: CloudProvider
): QlceViewFilterInput[] {
  const filters: any = []
  const relatedEntities = cloudProviderToEntityMapping[cloudProvider]
  relatedEntities.length &&
    relatedEntities.forEach(entity => {
      if (entityMap[entity]) {
        filters.push({
          field: {
            fieldId: entity,
            fieldName: fieldToFieldNameMapping[entity] || '',
            identifier: cloudProvider,
            identifierName: cloudProvider
          },
          operator: 'IN',
          type: 'VIEW_ID_CONDITION',
          values: [entityMap[entity]]
        })
      }
    })
 
  return filters
}
 
export function generateGroupBy(field: string, cloudProvider: CloudProvider): any {
  return field
    ? {
        fieldId: field,
        fieldName: fieldToFieldNameMapping[field] || '',
        identifier: cloudProvider,
        identifierName: cloudProvider
      }
    : DEFAULT_GROUP_BY
}
 
export function getCloudProviderFromFields(entityMap: Record<string, string | null>): CloudProvider {
  if (entityMap.gcpProjectId) {
    return CloudProvider.GCP
  }
  if (entityMap.awsAccount) {
    return CloudProvider.AWS
  }
  return CloudProvider.CLUSTER
}
 
export function getFiltersFromEnityMap(
  entityMap: Array<Record<string, string>>,
  cloudProvider: CloudProvider
): QlceViewFilterInput[] {
  const filters: any = []
  const relatedEntities = cloudProviderToEntityMapping[cloudProvider]
 
  relatedEntities.length &&
    relatedEntities.forEach(entity => {
      Eif (entityMap.length && entityMap[0][entity]) {
        const values: string[] = []
        entityMap.forEach(obj => {
          obj[entity] && values.push(obj[entity])
        })
 
        values.length &&
          filters.push({
            field: {
              fieldId: entity,
              fieldName: fieldToFieldNameMapping[entity] || '',
              identifier: cloudProvider,
              identifierName: cloudProvider
            },
            operator: 'IN',
            type: 'VIEW_ID_CONDITION',
            values: values
          })
      }
    })
 
  return filters
}