Browse Source

fix(alerts): Show correct buckets for 1minute interval * 7days (#33084)

Scott Cooper 2 years ago
parent
commit
1d827147fe

+ 11 - 3
static/app/views/alerts/rules/details/body.tsx

@@ -16,14 +16,19 @@ import {t, tct} from 'sentry/locale';
 import space from 'sentry/styles/space';
 import {Organization, Project} from 'sentry/types';
 import getDynamicText from 'sentry/utils/getDynamicText';
-import {Dataset, IncidentRule} from 'sentry/views/alerts/incidentRules/types';
+import {Dataset, IncidentRule, TimePeriod} from 'sentry/views/alerts/incidentRules/types';
 import {extractEventTypeFilterFromRule} from 'sentry/views/alerts/incidentRules/utils/getEventTypeFilter';
 import MetricHistory from 'sentry/views/alerts/rules/details/metricHistory';
 
 import {isCrashFreeAlert} from '../../incidentRules/utils/isCrashFreeAlert';
 import {AlertRuleStatus, Incident} from '../../types';
 
-import {API_INTERVAL_POINTS_LIMIT, TIME_OPTIONS, TimePeriodType} from './constants';
+import {
+  API_INTERVAL_POINTS_LIMIT,
+  TIME_OPTIONS,
+  TIME_WINDOWS,
+  TimePeriodType,
+} from './constants';
 import MetricChart from './metricChart';
 import RelatedIssues from './relatedIssues';
 import RelatedTransactions from './relatedTransactions';
@@ -63,10 +68,13 @@ export default class DetailsBody extends React.Component<Props> {
     const startDate = moment.utc(start);
     const endDate = moment.utc(end);
     const timeWindow = rule?.timeWindow;
+    const startEndDifferenceMs = endDate.diff(startDate);
 
     if (
       timeWindow &&
-      endDate.diff(startDate) < API_INTERVAL_POINTS_LIMIT * timeWindow * 60 * 1000
+      (startEndDifferenceMs < API_INTERVAL_POINTS_LIMIT * timeWindow * 60 * 1000 ||
+        // Special case 7 days * 1m interval over the api limit
+        startEndDifferenceMs === TIME_WINDOWS[TimePeriod.SEVEN_DAYS])
     ) {
       return `${timeWindow}m`;
     }

+ 4 - 0
static/app/views/alerts/rules/details/constants.tsx

@@ -27,6 +27,10 @@ export type TimePeriodType = {
   label: string;
   period: string;
   start: string;
+  /**
+   * The start/end were chosen from the period and not the user
+   */
+  usingPeriod: boolean;
   custom?: boolean;
   utc?: boolean;
 };

+ 3 - 0
static/app/views/alerts/rules/details/index.tsx

@@ -84,6 +84,7 @@ class MetricAlertDetails extends Component<Props, State> {
         start: location.query.start as string,
         end: location.query.end as string,
         period,
+        usingPeriod: false,
         label: t('Custom time'),
         display: (
           <Fragment>
@@ -102,6 +103,7 @@ class MetricAlertDetails extends Component<Props, State> {
         start,
         end,
         period,
+        usingPeriod: false,
         label: t('Custom time'),
         display: (
           <Fragment>
@@ -125,6 +127,7 @@ class MetricAlertDetails extends Component<Props, State> {
       start,
       end,
       period,
+      usingPeriod: true,
       label: timeOption.label as string,
       display: timeOption.label as string,
     };

+ 14 - 0
static/app/views/alerts/rules/details/metricChart.tsx

@@ -54,6 +54,7 @@ import {
   AlertRuleTriggerType,
   Dataset,
   IncidentRule,
+  TimePeriod,
 } from 'sentry/views/alerts/incidentRules/types';
 import {AlertWizardAlertNames} from 'sentry/views/alerts/wizard/options';
 import {getAlertTypeFromAggregateDataset} from 'sentry/views/alerts/wizard/utils';
@@ -767,6 +768,19 @@ class MetricChart extends React.PureComponent<Props, State> {
     const {api, rule, organization, timePeriod, project, interval, query} = this.props;
     const {aggregate, timeWindow, environment, dataset} = rule;
 
+    // Fix for 7 days * 1m interval being over the max number of results from events api
+    // 10k events is the current max
+    if (
+      timePeriod.usingPeriod &&
+      timePeriod.period === TimePeriod.SEVEN_DAYS &&
+      interval === '1m'
+    ) {
+      timePeriod.start = getUtcDateString(
+        // -5 minutes provides a small cushion for rounding up minutes. This might be able to be smaller
+        moment(moment.utc(timePeriod.end).subtract(10000 - 5, 'minutes'))
+      );
+    }
+
     // If the chart duration isn't as long as the rollup duration the events-stats
     // endpoint will return an invalid timeseriesData data set
     const viableStartDate = getUtcDateString(