Skip to content

Commit 3c81588

Browse files
authored
fix(anomaly detection): fix historical/current data time window inconsistency (#91782)
Before sending an anomaly detection request, verify that the historical and current timeseries have the same time window, and verify that the time window is consistent with what the user has specified.
1 parent 3578eb2 commit 3c81588

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

static/app/views/alerts/rules/metric/ruleForm.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,24 @@ class RuleFormContainer extends DeprecatedAsyncComponent<Props, State> {
10161016
this.setState({historicalData}, () => this.fetchAnomalies());
10171017
}
10181018

1019+
TimeWindowsAreConsistent() {
1020+
const {currentData, historicalData, timeWindow} = this.state;
1021+
const currentDataPoint1 = currentData[1];
1022+
const currentDataPoint0 = currentData[0];
1023+
if (!currentDataPoint0 || !currentDataPoint1) {
1024+
return false;
1025+
}
1026+
const historicalDataPoint1 = historicalData[1];
1027+
const historicalDataPoint0 = historicalData[0];
1028+
if (!historicalDataPoint0 || !historicalDataPoint1) {
1029+
return false;
1030+
}
1031+
1032+
const currentTimeWindow = (currentDataPoint1[0] - currentDataPoint0[0]) / 60;
1033+
const historicalTimeWindow = (historicalDataPoint1[0] - historicalDataPoint0[0]) / 60;
1034+
return currentTimeWindow === historicalTimeWindow && currentTimeWindow === timeWindow;
1035+
}
1036+
10191037
async fetchAnomalies() {
10201038
const {comparisonType, historicalData, currentData} = this.state;
10211039
if (
@@ -1030,6 +1048,7 @@ class RuleFormContainer extends DeprecatedAsyncComponent<Props, State> {
10301048

10311049
const {organization, project} = this.props;
10321050
const {timeWindow, sensitivity, seasonality, thresholdType} = this.state;
1051+
10331052
const direction =
10341053
thresholdType === AlertRuleThresholdType.ABOVE
10351054
? 'up'
@@ -1063,7 +1082,10 @@ class RuleFormContainer extends DeprecatedAsyncComponent<Props, State> {
10631082
`/organizations/${organization.slug}/events/anomalies/`,
10641083
{method: 'POST', data: params}
10651084
);
1066-
this.setState({anomalies});
1085+
// don't set the anomalies if historical and current data have incorrect time windows
1086+
if (!this.TimeWindowsAreConsistent()) {
1087+
this.setState({anomalies});
1088+
}
10671089
} catch (e) {
10681090
let chartErrorMessage: string | undefined;
10691091
if (e.responseJSON) {

0 commit comments

Comments
 (0)