utils.tsx 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. import moment from 'moment';
  2. import {getUtcDateString} from 'sentry/utils/dates';
  3. import {
  4. API_INTERVAL_POINTS_LIMIT,
  5. API_INTERVAL_POINTS_MIN,
  6. } from 'sentry/views/alerts/rules/metric/details/constants';
  7. import type {Incident} from 'sentry/views/alerts/types';
  8. /**
  9. * Retrieve start/end date of a metric alert incident for the events graph
  10. * Will show at least 150 and no more than 10,000 data points
  11. */
  12. export function buildMetricGraphDateRange(incident: Incident): {
  13. end: string;
  14. start: string;
  15. } {
  16. const timeWindowMillis = incident.alertRule.timeWindow * 60 * 1000;
  17. const minRange = timeWindowMillis * API_INTERVAL_POINTS_MIN;
  18. const maxRange = timeWindowMillis * API_INTERVAL_POINTS_LIMIT;
  19. const now = moment.utc();
  20. const startDate = moment.utc(incident.dateStarted);
  21. // make a copy of now since we will modify endDate and use now for comparing
  22. const endDate = incident.dateClosed ? moment.utc(incident.dateClosed) : moment(now);
  23. const incidentRange = Math.max(endDate.diff(startDate), 3 * timeWindowMillis);
  24. const range = Math.min(maxRange, Math.max(minRange, incidentRange));
  25. const halfRange = moment.duration(range / 2);
  26. return {
  27. start: getUtcDateString(startDate.subtract(halfRange)),
  28. end: getUtcDateString(moment.min(endDate.add(halfRange), now)),
  29. };
  30. }