Skip to content

Commit e999f0f

Browse files
gggritsoandrewshie-sentry
authored andcommitted
ref(insights): Split out getAxisMaxForPercentageSeries (#82493)
Moves this helper into its own file.
1 parent ffed324 commit e999f0f

File tree

4 files changed

+63
-63
lines changed

4 files changed

+63
-63
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import type {Series} from 'sentry/types/echarts';
2+
import {getAxisMaxForPercentageSeries} from 'sentry/views/insights/common/utils/getAxisMaxForPercentageSeries';
3+
4+
describe('getAxisMaxForPercentageSeries', function () {
5+
it('Returns nearest significant digit for small series', function () {
6+
expect(getAxisMaxForPercentageSeries([HTTP_5XX_SERIES])).toBeCloseTo(0.0001);
7+
});
8+
9+
it('Returns 1 for larger series', function () {
10+
expect(getAxisMaxForPercentageSeries([HTTP_2XX_SERIES])).toBeCloseTo(1);
11+
});
12+
13+
it('Takes all series into account', function () {
14+
expect(getAxisMaxForPercentageSeries([HTTP_2XX_SERIES, HTTP_5XX_SERIES])).toBeCloseTo(
15+
1
16+
);
17+
});
18+
});
19+
20+
const HTTP_2XX_SERIES: Series = {
21+
seriesName: '5XX',
22+
data: [
23+
{
24+
value: 0.9812,
25+
name: '2024-03-12T13:30:00-04:00',
26+
},
27+
{
28+
value: 0.9992,
29+
name: '2024-03-12T14:00:00-04:00',
30+
},
31+
],
32+
};
33+
34+
const HTTP_5XX_SERIES: Series = {
35+
seriesName: '5XX',
36+
data: [
37+
{
38+
value: 0.00006713689346852019,
39+
name: '2024-03-12T13:30:00-04:00',
40+
},
41+
{
42+
value: 0.000041208717375685543,
43+
name: '2024-03-12T14:00:00-04:00',
44+
},
45+
],
46+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type {Series} from 'sentry/types/echarts';
2+
3+
/**
4+
* Given a set of `Series` objects that contain percentage data (i.e., every item in `data` has a `value` between 0 and 1) return an appropriate max value.
5+
*
6+
* e.g., for series with very low values (like 5xx rates), it rounds to the nearest significant digit. For other cases, it limits it to 100
7+
*/
8+
export function getAxisMaxForPercentageSeries(series: Series[]): number {
9+
const maxValue = Math.max(
10+
...series.map(serie => Math.max(...serie.data.map(datum => datum.value)))
11+
);
12+
13+
const maxNumberOfDecimalPlaces = Math.ceil(Math.min(0, Math.log10(maxValue)));
14+
15+
return Math.pow(10, maxNumberOfDecimalPlaces);
16+
}

Diff for: static/app/views/insights/http/components/charts/responseRateChart.spec.tsx

-48
This file was deleted.

Diff for: static/app/views/insights/http/components/charts/responseRateChart.tsx

+1-15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from 'sentry/views/insights/colors';
88
import Chart, {ChartType} from 'sentry/views/insights/common/components/chart';
99
import ChartPanel from 'sentry/views/insights/common/components/chartPanel';
10+
import {getAxisMaxForPercentageSeries} from 'sentry/views/insights/common/utils/getAxisMaxForPercentageSeries';
1011
import {DataTitles} from 'sentry/views/insights/common/views/spans/types';
1112
import {CHART_HEIGHT} from 'sentry/views/insights/http/settings';
1213

@@ -46,18 +47,3 @@ export function ResponseRateChart({series, isLoading, error}: Props) {
4647
</ChartPanel>
4748
);
4849
}
49-
50-
/**
51-
* Given a set of `Series` objects that contain percentage data (i.e., every item in `data` has a `value` between 0 and 1) return an appropriate max value.
52-
*
53-
* e.g., for series with very low values (like 5xx rates), it rounds to the nearest significant digit. For other cases, it limits it to 100
54-
*/
55-
export function getAxisMaxForPercentageSeries(series: Series[]): number {
56-
const maxValue = Math.max(
57-
...series.map(serie => Math.max(...serie.data.map(datum => datum.value)))
58-
);
59-
60-
const maxNumberOfDecimalPlaces = Math.ceil(Math.min(0, Math.log10(maxValue)));
61-
62-
return Math.pow(10, maxNumberOfDecimalPlaces);
63-
}

0 commit comments

Comments
 (0)