utils.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import moment from 'moment';
  2. import {getFormat} from 'sentry/utils/dates';
  3. import type {TimeWindow, TimeWindowOptions} 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. export function getStartFromTimeWindow(end: Date, timeWindow: TimeWindow): Date {
  12. const start = moment(end).subtract(resolutionElapsedMinutes[timeWindow], 'minute');
  13. return start.toDate();
  14. }
  15. // The pixels to allocate to each time label based on (MMM DD HH:SS AM/PM)
  16. const TIMELABEL_WIDTH = 100;
  17. export function getConfigFromTimeRange(
  18. start: Date,
  19. end: Date,
  20. timelineWidth: number
  21. ): TimeWindowOptions {
  22. // Acceptable intervals between time labels, in minutes
  23. const minuteRanges = [1, 10, 30, 60, 4 * 60, 8 * 60, 12 * 60];
  24. const startEndMinutes = (end.getTime() - start.getTime()) / (1000 * 60);
  25. const timeLabelMinutes = startEndMinutes * (TIMELABEL_WIDTH / timelineWidth);
  26. const subMinutePxBuckets = startEndMinutes < timelineWidth;
  27. for (const minutes of minuteRanges) {
  28. if (minutes >= Math.floor(timeLabelMinutes)) {
  29. return {
  30. dateLabelFormat: getFormat({timeOnly: true, seconds: subMinutePxBuckets}),
  31. elapsedMinutes: startEndMinutes,
  32. timeMarkerInterval: minutes,
  33. dateTimeProps: {timeOnly: true},
  34. };
  35. }
  36. }
  37. // Calculate days between each time label interval for larger time ranges
  38. const timeLabelIntervalDays = Math.ceil(timeLabelMinutes / (60 * 24));
  39. return {
  40. dateLabelFormat: getFormat(),
  41. elapsedMinutes: startEndMinutes,
  42. timeMarkerInterval: timeLabelIntervalDays * 60 * 24,
  43. dateTimeProps: {dateOnly: true},
  44. };
  45. }