All files / modules/85-cv/pages/health-source/connectors/PrometheusHealthSource/components/PrometheusQueryViewer PrometheusQueryViewer.utils.ts

44.19% Statements 19/43
33.33% Branches 16/48
100% Functions 3/3
44.19% Lines 19/43

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                  24x   24x   24x             24x     1x 1x                                                             3x 3x                           24x 1x 1x       1x 1x   1x   1x         1x       1x       1x       1x    
/*
 * 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 { MultiSelectOption } from '@wings-software/uicore'
import type Highcharts from 'highcharts'
import { isNumber } from 'lodash-es'
import type { PrometheusSampleData } from 'services/cv'
import { chartsConfig } from '@cv/pages/health-source/connectors/GCOMetricsHealthSource/GCOWidgetChartConfig'
import type { MapPrometheusQueryToService } from '../../PrometheusHealthSource.constants'
import { formatJSON } from '../../../GCOMetricsHealthSource/GCOMetricsHealthSource.utils'
 
type PrometheusHighchartsOptionAndRecords = {
  options: Highcharts.Options
  records: string[]
}
 
export function transformPrometheusSampleData(
  sampleData?: PrometheusSampleData[]
): PrometheusHighchartsOptionAndRecords {
  Eif (!sampleData?.length) {
    return { options: {}, records: [] }
  }
 
  const data: Highcharts.SeriesLineOptions[] = []
  const transformedValue: PrometheusHighchartsOptionAndRecords = { options: {}, records: [] }
  for (const sample of sampleData) {
    const option: Highcharts.SeriesLineOptions = {
      name: '',
      data: [],
      type: 'line'
    }
 
    const formattedJson = formatJSON(sample.metricDetails)
    if (formattedJson) {
      transformedValue.records.push(formattedJson)
    }
 
    for (const point of sample?.data || []) {
      if (point?.timestamp && isNumber(point.value)) {
        option.data?.push([point.timestamp * 1000, point.value])
      }
    }
 
    data.push(option)
  }
 
  transformedValue.options = chartsConfig(data)
  return transformedValue
}
 
function convertFilterToQueryString(filters?: MultiSelectOption[]): string {
  Eif (!filters?.length) {
    return ''
  }
 
  let convertedString = ''
  for (const filter of filters) {
    if (filter.label !== filter.value) {
      const splitString = filter.label.split(':')
      convertedString += `\t\t${splitString[0]}="${splitString[1]}",\n`
    }
  }
 
  return convertedString
}
 
export function createPrometheusQuery(values?: MapPrometheusQueryToService): string {
  let queryString = ''
  Iif (!values) {
    return ''
  }
 
  const convertedEnvFilter = convertFilterToQueryString(values.envFilter)
  const convertedServiceFilter = convertFilterToQueryString(values.serviceFilter)
 
  queryString += `${convertedEnvFilter}${convertedServiceFilter}${convertFilterToQueryString(values.additionalFilter)}`
 
  Iif (queryString?.length) {
    queryString = `\n${queryString}`
  }
 
  // remove trailing comma if there is one
  Iif (queryString[queryString.length - 2] === ',') {
    queryString = queryString.substr(0, queryString.length - 2) + '\n'
  }
 
  Iif (values.prometheusMetric) {
    queryString = `${values.prometheusMetric}\t{\n${queryString}\n}`
  }
 
  Iif (values.aggregator) {
    queryString = `${values.aggregator}(\n\t${queryString})`
  }
 
  return queryString
}