Skip to content

Commit fad776b

Browse files
authored
fix(anomaly thresholds): Only call endpoint if anomaly detection (#104557)
1 parent e756782 commit fad776b

File tree

4 files changed

+127
-1
lines changed

4 files changed

+127
-1
lines changed

static/app/views/detectors/components/details/metric/chart.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ export function useMetricDetectorChart({
199199

200200
const {anomalyThresholdSeries} = useMetricDetectorAnomalyThresholds({
201201
detectorId: detector.id,
202+
detectionType,
202203
startTimestamp: metricTimestamps.start,
203204
endTimestamp: metricTimestamps.end,
204205
series,

static/app/views/detectors/components/forms/metric/metricDetectorChart.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ export function MetricDetectorChart({
241241
const shouldFetchThresholds = Boolean(detectorId && isAnomalyDetection);
242242
const {anomalyThresholdSeries} = useMetricDetectorAnomalyThresholds({
243243
detectorId: detectorId ?? '',
244+
detectionType,
244245
startTimestamp: metricTimestamps.start,
245246
endTimestamp: metricTimestamps.end,
246247
series: shouldFetchThresholds ? series : [],
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import {OrganizationFixture} from 'sentry-fixture/organization';
2+
3+
import {renderHookWithProviders, waitFor} from 'sentry-test/reactTestingLibrary';
4+
5+
import {useMetricDetectorAnomalyThresholds} from 'sentry/views/detectors/hooks/useMetricDetectorAnomalyThresholds';
6+
7+
describe('useMetricDetectorAnomalyThresholds', () => {
8+
beforeEach(() => {
9+
MockApiClient.clearMockResponses();
10+
});
11+
12+
it('does not fetch data when detectionType is not dynamic', () => {
13+
const organization = OrganizationFixture({
14+
features: ['anomaly-detection-threshold-data'],
15+
});
16+
17+
const anomalyDataRequest = MockApiClient.addMockResponse({
18+
url: `/organizations/${organization.slug}/detectors/123/anomaly-data/`,
19+
body: {data: []},
20+
});
21+
22+
const series = [
23+
{
24+
seriesName: 'count()',
25+
data: [{name: 1609459200000, value: 100}],
26+
},
27+
];
28+
29+
renderHookWithProviders(
30+
() =>
31+
useMetricDetectorAnomalyThresholds({
32+
detectorId: '123',
33+
detectionType: 'static',
34+
startTimestamp: 1609459200,
35+
endTimestamp: 1609545600,
36+
series,
37+
}),
38+
{organization}
39+
);
40+
41+
expect(anomalyDataRequest).not.toHaveBeenCalled();
42+
});
43+
44+
it('fetches data when detectionType is dynamic', async () => {
45+
const organization = OrganizationFixture({
46+
features: ['anomaly-detection-threshold-data'],
47+
});
48+
49+
const mockData = [
50+
{
51+
external_alert_id: 24,
52+
timestamp: 1609459200,
53+
value: 100,
54+
yhat_lower: 80,
55+
yhat_upper: 120,
56+
},
57+
];
58+
59+
const anomalyDataRequest = MockApiClient.addMockResponse({
60+
url: `/organizations/${organization.slug}/detectors/123/anomaly-data/`,
61+
body: {data: mockData},
62+
});
63+
64+
const series = [
65+
{
66+
seriesName: 'count()',
67+
data: [{name: 1609459200000, value: 100}],
68+
},
69+
];
70+
71+
renderHookWithProviders(
72+
() =>
73+
useMetricDetectorAnomalyThresholds({
74+
detectorId: '123',
75+
detectionType: 'dynamic',
76+
startTimestamp: 1609459200,
77+
endTimestamp: 1609545600,
78+
series,
79+
}),
80+
{organization}
81+
);
82+
83+
await waitFor(() => {
84+
expect(anomalyDataRequest).toHaveBeenCalled();
85+
});
86+
});
87+
88+
it('does not fetch data when detectionType is undefined', () => {
89+
const organization = OrganizationFixture({
90+
features: ['anomaly-detection-threshold-data'],
91+
});
92+
93+
const anomalyDataRequest = MockApiClient.addMockResponse({
94+
url: `/organizations/${organization.slug}/detectors/123/anomaly-data/`,
95+
body: {data: []},
96+
});
97+
98+
const series = [
99+
{
100+
seriesName: 'count()',
101+
data: [{name: 1609459200000, value: 100}],
102+
},
103+
];
104+
105+
renderHookWithProviders(
106+
() =>
107+
useMetricDetectorAnomalyThresholds({
108+
detectorId: '123',
109+
detectionType: undefined,
110+
startTimestamp: 1609459200,
111+
endTimestamp: 1609545600,
112+
series,
113+
}),
114+
{organization}
115+
);
116+
117+
expect(anomalyDataRequest).not.toHaveBeenCalled();
118+
});
119+
});

static/app/views/detectors/hooks/useMetricDetectorAnomalyThresholds.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ interface AnomalyThresholdDataResponse {
2222

2323
interface UseMetricDetectorAnomalyThresholdsProps {
2424
detectorId: string;
25+
detectionType?: string;
2526
endTimestamp?: number;
2627
series?: Series[];
2728
startTimestamp?: number;
@@ -38,6 +39,7 @@ interface UseMetricDetectorAnomalyThresholdsResult {
3839
*/
3940
export function useMetricDetectorAnomalyThresholds({
4041
detectorId,
42+
detectionType,
4143
startTimestamp,
4244
endTimestamp,
4345
series = [],
@@ -48,6 +50,7 @@ export function useMetricDetectorAnomalyThresholds({
4850
const hasAnomalyDataFlag = organization.features.includes(
4951
'anomaly-detection-threshold-data'
5052
);
53+
const isAnomalyDetection = detectionType === 'dynamic';
5154

5255
const {
5356
data: anomalyData,
@@ -66,7 +69,9 @@ export function useMetricDetectorAnomalyThresholds({
6669
{
6770
staleTime: 0,
6871
enabled:
69-
hasAnomalyDataFlag && Boolean(detectorId && startTimestamp && endTimestamp),
72+
hasAnomalyDataFlag &&
73+
isAnomalyDetection &&
74+
Boolean(detectorId && startTimestamp && endTimestamp),
7075
}
7176
);
7277

0 commit comments

Comments
 (0)