-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathdata-transform.test.ts
More file actions
153 lines (144 loc) · 4.22 KB
/
Copy pathdata-transform.test.ts
File metadata and controls
153 lines (144 loc) · 4.22 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright The Perses Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { LegacyTimeSeries } from '@perses-dev/components';
import { TimeSeriesChartYAxisOptions } from '../time-series-chart-model';
import { convertPercentThreshold, convertPanelYAxis, roundDown } from './data-transform';
const MAX_VALUE = 120;
const MOCK_ECHART_TIME_SERIES_DATA: LegacyTimeSeries[] = [
{
data: [10, 30, 80, 50],
},
{
data: [20, MAX_VALUE, 17, 30],
},
];
describe('convertPercentThreshold', () => {
it('should return 25 if percent threshold is 25 and max is 100', () => {
const value = convertPercentThreshold(25, MOCK_ECHART_TIME_SERIES_DATA, 100);
expect(value).toEqual(25);
});
it('should return 60 if percent threshold is 50, max is 100, and min is 20', () => {
const value = convertPercentThreshold(50, MOCK_ECHART_TIME_SERIES_DATA, 100, 20);
expect(value).toEqual(60);
});
it('should return 50% of the max value in time series data if max is undefined', () => {
const value = convertPercentThreshold(50, MOCK_ECHART_TIME_SERIES_DATA);
expect(value).toEqual(0.5 * MAX_VALUE);
});
it('should use absolute values when finding the max so negated series (negativeY) still resolve correctly', () => {
// Simulates a series that was negated for display (querySettings.negativeY = true)
const negatedSeries: LegacyTimeSeries[] = [{ data: [-10, -30, -80, -50] }, { data: [-20, -MAX_VALUE, -17, -30] }];
const value = convertPercentThreshold(50, negatedSeries);
expect(value).toEqual(0.5 * MAX_VALUE);
});
});
describe('convertPanelYAxis', () => {
it('should convert a Perses yAxis spec to the ECharts equivalent', () => {
const persesAxis: TimeSeriesChartYAxisOptions = {
show: true,
label: 'Axis Label',
format: {
unit: 'percent-decimal',
decimalPlaces: 0,
},
min: 0.1,
max: 1,
};
const echartsAxis = convertPanelYAxis(persesAxis);
// Axis label is handled outside of echarts since it is built with a custom React component.
expect(echartsAxis).toEqual({
show: true,
max: 1,
min: 0.1,
});
});
it('should convert a Perses yAxis spec of type log to the ECharts equivalent', () => {
const persesAxis: TimeSeriesChartYAxisOptions = {
show: true,
label: 'Axis Label',
format: {
unit: 'percent-decimal',
decimalPlaces: 0,
},
min: 0.1,
max: 1,
logBase: 2,
};
const actualAxisLog2 = convertPanelYAxis(persesAxis);
// Axis label is handled outside of echarts since it is built with a custom React component.
expect(actualAxisLog2).toEqual({
show: true,
max: 1,
min: undefined,
type: 'log',
logBase: 2,
});
persesAxis.logBase = 10;
const actualAxisLog10 = convertPanelYAxis(persesAxis);
// Axis label is handled outside of echarts since it is built with a custom React component.
expect(actualAxisLog10).toEqual({
show: true,
max: 1,
min: undefined,
type: 'log',
logBase: 10,
});
});
});
const ROUND_DOWN_TESTS = [
{
value: -400305,
expected: -500000,
},
{
value: -633,
expected: -700,
},
{
value: -5.99,
expected: -6,
},
{
value: -0.123,
expected: -0.2,
},
{
value: -0.000543,
expected: -0.0006,
},
{
value: 0.000543,
expected: 0.0005,
},
{
value: 0.123,
expected: 0.1,
},
{
value: 5.99,
expected: 5,
},
{
value: 633,
expected: 600,
},
{
value: 400305,
expected: 400000,
},
];
describe('roundDown', () => {
it.each(ROUND_DOWN_TESTS)('returns $expected when the input is $value', ({ value, expected }) => {
expect(roundDown(value)).toEqual(expected);
});
});