getTimeRangeFromEvent.tsx 1004 B

1234567891011121314151617181920212223242526272829303132
  1. import moment from 'moment-timezone';
  2. import type {Event} from 'sentry/types/event';
  3. import type {TimeWindow} from '../types';
  4. // Stores the elapsed minutes for each selectable resolution
  5. export const resolutionElapsedMinutes: Record<TimeWindow, number> = {
  6. '1h': 60,
  7. '24h': 60 * 24,
  8. '7d': 60 * 24 * 7,
  9. '30d': 60 * 24 * 30,
  10. };
  11. /**
  12. * Given a cron event, current time, and time window, attempt to return a
  13. * centered date window (start, end) around the event. If the event happened
  14. * too recently, a last 24h, 1d, 7d, etc window will be returned instead
  15. */
  16. export function getTimeRangeFromEvent(
  17. event: Event,
  18. now: Date,
  19. timeWindow: TimeWindow
  20. ): {end: Date; start: Date} {
  21. const elapsedMinutes = resolutionElapsedMinutes[timeWindow];
  22. let end = moment(event.dateReceived).add(elapsedMinutes / 2, 'minute');
  23. if (end > moment(now)) {
  24. end = moment(now);
  25. }
  26. const start = moment(end).subtract(elapsedMinutes, 'minute');
  27. return {start: start.toDate(), end: end.toDate()};
  28. }