@@ -1030,21 +1030,10 @@ class RuleFormContainer extends DeprecatedAsyncComponent<Props, State> {
1030
1030
this . setState ( { historicalData} , ( ) => this . fetchAnomalies ( ) ) ;
1031
1031
}
1032
1032
1033
- TimeWindowsAreConsistent ( ) {
1034
- const { currentData, historicalData, timeWindow} = this . state ;
1035
- const currentDataPoint1 = currentData [ 1 ] ;
1036
- const currentDataPoint0 = currentData [ 0 ] ;
1037
- if ( ! currentDataPoint0 || ! currentDataPoint1 ) {
1038
- return false ;
1039
- }
1040
- const historicalDataPoint1 = historicalData [ 1 ] ;
1041
- const historicalDataPoint0 = historicalData [ 0 ] ;
1042
- if ( ! historicalDataPoint0 || ! historicalDataPoint1 ) {
1043
- return false ;
1044
- }
1045
-
1046
- const currentTimeWindow = ( currentDataPoint1 [ 0 ] - currentDataPoint0 [ 0 ] ) / 60 ;
1047
- const historicalTimeWindow = ( historicalDataPoint1 [ 0 ] - historicalDataPoint0 [ 0 ] ) / 60 ;
1033
+ timeWindowsAreConsistent ( ) {
1034
+ const { currentData = [ ] , historicalData = [ ] , timeWindow} = this . state ;
1035
+ const currentTimeWindow = getTimeWindowFromDataset ( currentData , timeWindow ) ;
1036
+ const historicalTimeWindow = getTimeWindowFromDataset ( historicalData , timeWindow ) ;
1048
1037
return currentTimeWindow === historicalTimeWindow && currentTimeWindow === timeWindow ;
1049
1038
}
1050
1039
@@ -1054,7 +1043,8 @@ class RuleFormContainer extends DeprecatedAsyncComponent<Props, State> {
1054
1043
comparisonType !== AlertRuleComparisonType . DYNAMIC ||
1055
1044
! ( Array . isArray ( currentData ) && Array . isArray ( historicalData ) ) ||
1056
1045
currentData . length === 0 ||
1057
- historicalData . length === 0
1046
+ historicalData . length === 0 ||
1047
+ ! this . timeWindowsAreConsistent ( )
1058
1048
) {
1059
1049
return ;
1060
1050
}
@@ -1096,10 +1086,7 @@ class RuleFormContainer extends DeprecatedAsyncComponent<Props, State> {
1096
1086
`/organizations/${ organization . slug } /events/anomalies/` ,
1097
1087
{ method : 'POST' , data : params }
1098
1088
) ;
1099
- // don't set the anomalies if historical and current data have incorrect time windows
1100
- if ( ! this . TimeWindowsAreConsistent ( ) ) {
1101
- this . setState ( { anomalies} ) ;
1102
- }
1089
+ this . setState ( { anomalies} ) ;
1103
1090
} catch ( e ) {
1104
1091
let chartErrorMessage : string | undefined ;
1105
1092
if ( e . responseJSON ) {
@@ -1468,6 +1455,25 @@ function formatStatsToHistoricalDataset(
1468
1455
: [ ] ;
1469
1456
}
1470
1457
1458
+ function getTimeWindowFromDataset (
1459
+ data : ReturnType < typeof formatStatsToHistoricalDataset > ,
1460
+ defaultWindow : TimeWindow
1461
+ ) : number {
1462
+ for ( let i = 0 ; i < data . length ; i ++ ) {
1463
+ const [ timestampA ] = data [ i ] ?? [ ] ;
1464
+ const [ timestampB ] = data [ i + 1 ] ?? [ ] ;
1465
+ if ( ! timestampA || ! timestampB ) {
1466
+ break ;
1467
+ }
1468
+ // ignore duplicate timestamps
1469
+ if ( timestampA === timestampB ) {
1470
+ continue ;
1471
+ }
1472
+ return Math . abs ( timestampB - timestampA ) / 60 ;
1473
+ }
1474
+ return defaultWindow ;
1475
+ }
1476
+
1471
1477
const Main = styled ( Layout . Main ) `
1472
1478
max-width: 1000px;
1473
1479
` ;
0 commit comments