Commit 0e216ee9 authored by Martin Wortschack's avatar Martin Wortschack

Merge branch 'jivanvl-fix-single-stat-percentile-format' into 'master'

Fix single stat panel percentile format support

See merge request gitlab-org/gitlab!28365
parents 9ae6dc68 f33521f0
<script> <script>
import { GlSingleStat } from '@gitlab/ui/dist/charts'; import { GlSingleStat } from '@gitlab/ui/dist/charts';
import { roundOffFloat } from '~/lib/utils/common_utils'; import { SUPPORTED_FORMATS, getFormatter } from '~/lib/utils/unit_format';
import { graphDataValidatorForValues } from '../../utils'; import { graphDataValidatorForValues } from '../../utils';
const defaultPrecision = 2;
export default { export default {
components: { components: {
GlSingleStat, GlSingleStat,
...@@ -25,15 +27,19 @@ export default { ...@@ -25,15 +27,19 @@ export default {
/** /**
* This method formats the query result from a promQL expression * This method formats the query result from a promQL expression
* allowing a user to format the data in percentile values * allowing a user to format the data in percentile values
* by using the `max_value` inner property from the graphData prop * by using the `maxValue` inner property from the graphData prop
* @returns {(String)} * @returns {(String)}
*/ */
statValue() { statValue() {
const chartValue = this.graphData?.max_value let formatter;
? (this.queryResult / Number(this.graphData.max_value)) * 100
: this.queryResult; if (this.graphData?.maxValue) {
formatter = getFormatter(SUPPORTED_FORMATS.percent);
return formatter(this.queryResult / Number(this.graphData.maxValue), defaultPrecision);
}
return `${roundOffFloat(chartValue, 1)}${this.queryInfo.unit}`; formatter = getFormatter(SUPPORTED_FORMATS.number);
return `${formatter(this.queryResult, defaultPrecision)}${this.queryInfo.unit}`;
}, },
graphTitle() { graphTitle() {
return this.queryInfo.label; return this.queryInfo.label;
......
...@@ -109,6 +109,7 @@ const mapPanelToViewModel = ({ ...@@ -109,6 +109,7 @@ const mapPanelToViewModel = ({
y_label, y_label,
y_axis = {}, y_axis = {},
metrics = [], metrics = [],
max_value,
}) => { }) => {
// Both `x_axis.name` and `x_label` are supported for now // Both `x_axis.name` and `x_label` are supported for now
// https://gitlab.com/gitlab-org/gitlab/issues/210521 // https://gitlab.com/gitlab-org/gitlab/issues/210521
...@@ -125,6 +126,7 @@ const mapPanelToViewModel = ({ ...@@ -125,6 +126,7 @@ const mapPanelToViewModel = ({
y_label: yAxis.name, // Changing y_label to yLabel is pending https://gitlab.com/gitlab-org/gitlab/issues/207198 y_label: yAxis.name, // Changing y_label to yLabel is pending https://gitlab.com/gitlab-org/gitlab/issues/207198
yAxis, yAxis,
xAxis, xAxis,
maxValue: max_value,
metrics: mapToMetricsViewModel(metrics, yAxis.name), metrics: mapToMetricsViewModel(metrics, yAxis.name),
}; };
}; };
......
---
title: Fix single stat panel percentile format support
merge_request: 28365
author:
type: fixed
...@@ -20,25 +20,25 @@ describe('Single Stat Chart component', () => { ...@@ -20,25 +20,25 @@ describe('Single Stat Chart component', () => {
describe('computed', () => { describe('computed', () => {
describe('statValue', () => { describe('statValue', () => {
it('should interpolate the value and unit props', () => { it('should interpolate the value and unit props', () => {
expect(singleStatChart.vm.statValue).toBe('91MB'); expect(singleStatChart.vm.statValue).toBe('91.00MB');
}); });
it('should change the value representation to a percentile one', () => { it('should change the value representation to a percentile one', () => {
singleStatChart.setProps({ singleStatChart.setProps({
graphData: { graphData: {
...graphDataPrometheusQuery, ...graphDataPrometheusQuery,
max_value: 120, maxValue: 120,
}, },
}); });
expect(singleStatChart.vm.statValue).toContain('75.8'); expect(singleStatChart.vm.statValue).toContain('75.83%');
}); });
it('should display NaN for non numeric max_value values', () => { it('should display NaN for non numeric maxValue values', () => {
singleStatChart.setProps({ singleStatChart.setProps({
graphData: { graphData: {
...graphDataPrometheusQuery, ...graphDataPrometheusQuery,
max_value: 'not a number', maxValue: 'not a number',
}, },
}); });
...@@ -60,7 +60,7 @@ describe('Single Stat Chart component', () => { ...@@ -60,7 +60,7 @@ describe('Single Stat Chart component', () => {
], ],
}, },
], ],
max_value: 120, maxValue: 120,
}, },
}); });
......
...@@ -220,6 +220,15 @@ describe('mapToDashboardViewModel', () => { ...@@ -220,6 +220,15 @@ describe('mapToDashboardViewModel', () => {
expect(getMappedPanel().yAxis.format).toBe(SUPPORTED_FORMATS.number); expect(getMappedPanel().yAxis.format).toBe(SUPPORTED_FORMATS.number);
}); });
// This property allows single_stat panels to render percentile values
it('group maxValue', () => {
setupWithPanel({
max_value: 100,
});
expect(getMappedPanel().maxValue).toBe(100);
});
}); });
describe('metrics mapping', () => { describe('metrics mapping', () => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment