getTimeRangeFromEvent.tsx 924 B

123456789101112131415161718192021222324
  1. import moment from 'moment';
  2. import type {Event} from 'sentry/types';
  3. import type {TimeWindow} from 'sentry/views/monitors/components/overviewTimeline/types';
  4. import {resolutionElapsedMinutes} from 'sentry/views/monitors/components/overviewTimeline/utils';
  5. /**
  6. * Given a cron event, current time, and time window, attempt to return a
  7. * centered date window (start, end) around the event. If the event happened
  8. * too recently, a last 24h, 1d, 7d, etc window will be returned instead
  9. */
  10. export function getTimeRangeFromEvent(
  11. event: Event,
  12. now: Date,
  13. timeWindow: TimeWindow
  14. ): {end: Date; start: Date} {
  15. const elapsedMinutes = resolutionElapsedMinutes[timeWindow];
  16. let end = moment(event.dateReceived).add(elapsedMinutes / 2, 'minute');
  17. if (end > moment(now)) {
  18. end = moment(now);
  19. }
  20. const start = moment(end).subtract(elapsedMinutes, 'minute');
  21. return {start: start.toDate(), end: end.toDate()};
  22. }