All files / modules/75-ce/components/CloudCostInsightChart chartUtils.ts

65.52% Statements 19/29
57.78% Branches 26/45
85.71% Functions 6/7
65.52% Lines 19/29

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                                                8x       15x 15x   15x 66x 180x 180x   180x 36x     180x 36x         180x       15x   15x   15x       15x 36x                           8x 15x                                          
/*
 * 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 { DataPoint, TimeSeriesDataPoints } from 'services/ce/services'
 
type appMapType = Record<string, Array<[number, Array<DataPoint>, string]>>
type idMapType = Record<string, { id: string; name: string }>
 
export interface ChartConfigType {
  name: string
  nodeId: string
  data: Array<[number, Array<DataPoint>, string]>
  showInLegend: boolean
  marker: {
    symbol: string
  }
  color: string
  keys: Array<string>
}
 
export function transformTimeSeriesData(
  data: TimeSeriesDataPoints[] = [],
  columnSequence: string[] = []
): ChartConfigType[] {
  const appMap: appMapType = {}
  const idMap: idMapType = {}
 
  data.forEach(item => {
    item.values.forEach(value => {
      Eif (value) {
        const key = value?.key?.id || 'Total'
 
        if (!appMap[key]) {
          appMap[key] = []
        }
 
        if (!idMap[key]) {
          idMap[key] = {
            id: value?.key?.id,
            name: value?.key?.name
          }
        }
        appMap[key].push([+item.time, value.value, `${item.time}`])
      }
    })
  })
  const config = getChartConfig(appMap, idMap)
 
  sortCCMData(config, columnSequence, 'nodeId')
 
  return config
}
 
function getChartConfig(appMap: appMapType, idMap: idMapType): ChartConfigType[] {
  return Object.keys(appMap).map(key => {
    return {
      name: idMap[key].name,
      nodeId: idMap[key].id,
      data: appMap[key],
      showInLegend: false,
      marker: {
        symbol: 'circle'
      },
      color: '',
      keys: ['x', 'y', 'id']
    }
  })
}
 
export function sortCCMData(data: ChartConfigType[], columnSequence: string[] = [], key: 'nodeId'): void {
  Iif (columnSequence.length) {
    data.sort((configA, configB) => {
      const indexA = columnSequence.indexOf(configA[key]),
        indexB = columnSequence.indexOf(configB[key])
 
      if (indexA === indexB) {
        return 0
      }
 
      if (indexA === -1) {
        return 1
      }
 
      if (indexB === -1) {
        return -1
      }
 
      return indexA >= indexB ? 1 : -1
    })
  }
}