All files / app/assets/javascripts/monitoring/components/charts single_stat.vue

100% Statements 19/19
100% Branches 21/21
100% Functions 7/7
100% Lines 19/19

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  11x 11x 11x 11x   11x 11x                               16x     2x     14x                         16x 2x     14x 3x 3x           11x 11x     16x     16x         81x          
<script>
import { GlSingleStat } from '@gitlab/ui/dist/charts';
import { SUPPORTED_FORMATS, getFormatter } from '~/lib/utils/unit_format';
import { __ } from '~/locale';
import { graphDataValidatorForValues } from '../../utils';
 
const defaultPrecision = 2;
const emptyStateMsg = __('No data to display');
 
export default {
  components: {
    GlSingleStat,
  },
  inheritAttrs: false,
  props: {
    graphData: {
      type: Object,
      required: true,
      validator: graphDataValidatorForValues.bind(null, true),
    },
  },
  computed: {
    queryInfo() {
      return this.graphData.metrics[0];
    },
    queryMetric() {
      return this.queryInfo.result[0]?.metric;
    },
    queryResult() {
      return this.queryInfo.result[0]?.value[1];
    },
    /**
     * This method formats the query result from a promQL expression
     * allowing a user to format the data in percentile values
     * by using the `maxValue` inner property from the graphData prop
     * @returns {(String)}
     */
    statValue() {
      let formatter;
 
      // if field is present the metric value is not displayed. Hence
      // the early exit without formatting.
      if (this.graphData?.field) {
        return this.queryMetric?.[this.graphData.field] ?? emptyStateMsg;
      }
 
      if (this.graphData?.maxValue) {
        formatter = getFormatter(SUPPORTED_FORMATS.number);
        return formatter(
          (this.queryResult / Number(this.graphData.maxValue)) * 100,
          defaultPrecision,
        );
      }
 
      formatter = getFormatter(SUPPORTED_FORMATS.number);
      return `${formatter(this.queryResult, defaultPrecision)}`;
    },
    unit() {
      return this.graphData?.maxValue ? '%' : this.queryInfo.unit;
    },
    graphTitle() {
      return this.queryInfo.label;
    },
  },
};
</script>
<template>
  <div>
    <gl-single-stat :value="statValue" :title="graphTitle" :unit="unit" variant="success" />
  </div>
</template>