Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ export function useMetricDetectorChart({

const {anomalyThresholdSeries} = useMetricDetectorAnomalyThresholds({
detectorId: detector.id,
detectionType,
startTimestamp: metricTimestamps.start,
endTimestamp: metricTimestamps.end,
series,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ export function MetricDetectorChart({
const shouldFetchThresholds = Boolean(detectorId && isAnomalyDetection);
const {anomalyThresholdSeries} = useMetricDetectorAnomalyThresholds({
detectorId: detectorId ?? '',
detectionType,
startTimestamp: metricTimestamps.start,
endTimestamp: metricTimestamps.end,
series: shouldFetchThresholds ? series : [],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import {OrganizationFixture} from 'sentry-fixture/organization';

import {renderHookWithProviders, waitFor} from 'sentry-test/reactTestingLibrary';

import {useMetricDetectorAnomalyThresholds} from 'sentry/views/detectors/hooks/useMetricDetectorAnomalyThresholds';

describe('useMetricDetectorAnomalyThresholds', () => {
beforeEach(() => {
MockApiClient.clearMockResponses();
});

it('does not fetch data when detectionType is not dynamic', () => {
const organization = OrganizationFixture({
features: ['anomaly-detection-threshold-data'],
});

const anomalyDataRequest = MockApiClient.addMockResponse({
url: `/organizations/${organization.slug}/detectors/123/anomaly-data/`,
body: {data: []},
});

const series = [
{
seriesName: 'count()',
data: [{name: 1609459200000, value: 100}],
},
];

renderHookWithProviders(
() =>
useMetricDetectorAnomalyThresholds({
detectorId: '123',
detectionType: 'static',
startTimestamp: 1609459200,
endTimestamp: 1609545600,
series,
}),
{organization}
);

expect(anomalyDataRequest).not.toHaveBeenCalled();
});

it('fetches data when detectionType is dynamic', async () => {
const organization = OrganizationFixture({
features: ['anomaly-detection-threshold-data'],
});

const mockData = [
{
external_alert_id: 24,
timestamp: 1609459200,
value: 100,
yhat_lower: 80,
yhat_upper: 120,
},
];

const anomalyDataRequest = MockApiClient.addMockResponse({
url: `/organizations/${organization.slug}/detectors/123/anomaly-data/`,
body: {data: mockData},
});

const series = [
{
seriesName: 'count()',
data: [{name: 1609459200000, value: 100}],
},
];

renderHookWithProviders(
() =>
useMetricDetectorAnomalyThresholds({
detectorId: '123',
detectionType: 'dynamic',
startTimestamp: 1609459200,
endTimestamp: 1609545600,
series,
}),
{organization}
);

await waitFor(() => {
expect(anomalyDataRequest).toHaveBeenCalled();
});
});

it('does not fetch data when detectionType is undefined', () => {
const organization = OrganizationFixture({
features: ['anomaly-detection-threshold-data'],
});

const anomalyDataRequest = MockApiClient.addMockResponse({
url: `/organizations/${organization.slug}/detectors/123/anomaly-data/`,
body: {data: []},
});

const series = [
{
seriesName: 'count()',
data: [{name: 1609459200000, value: 100}],
},
];

renderHookWithProviders(
() =>
useMetricDetectorAnomalyThresholds({
detectorId: '123',
detectionType: undefined,
startTimestamp: 1609459200,
endTimestamp: 1609545600,
series,
}),
{organization}
);

expect(anomalyDataRequest).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface AnomalyThresholdDataResponse {

interface UseMetricDetectorAnomalyThresholdsProps {
detectorId: string;
detectionType?: string;
endTimestamp?: number;
series?: Series[];
startTimestamp?: number;
Expand All @@ -38,6 +39,7 @@ interface UseMetricDetectorAnomalyThresholdsResult {
*/
export function useMetricDetectorAnomalyThresholds({
detectorId,
detectionType,
startTimestamp,
endTimestamp,
series = [],
Expand All @@ -48,6 +50,7 @@ export function useMetricDetectorAnomalyThresholds({
const hasAnomalyDataFlag = organization.features.includes(
'anomaly-detection-threshold-data'
);
const isAnomalyDetection = detectionType === 'dynamic';

const {
data: anomalyData,
Expand All @@ -66,7 +69,9 @@ export function useMetricDetectorAnomalyThresholds({
{
staleTime: 0,
enabled:
hasAnomalyDataFlag && Boolean(detectorId && startTimestamp && endTimestamp),
hasAnomalyDataFlag &&
isAnomalyDetection &&
Boolean(detectorId && startTimestamp && endTimestamp),
}
);

Expand Down
Loading