utils.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import {getFormat} from 'sentry/utils/dates';
  2. import type {TimeWindow, TimeWindowConfig} from './types';
  3. // Stores the elapsed minutes for each selectable resolution
  4. export const resolutionElapsedMinutes: Record<TimeWindow, number> = {
  5. '1h': 60,
  6. '24h': 60 * 24,
  7. '7d': 60 * 24 * 7,
  8. '30d': 60 * 24 * 30,
  9. };
  10. // The pixels to allocate to each time label based on (MMM DD HH:SS AM/PM)
  11. const TIMELABEL_WIDTH = 100;
  12. const ONE_HOUR = 60;
  13. /**
  14. * Acceptable minute durations between time labels. These will be used to
  15. * create the TimeWindowConfig when the start and end times fit into these
  16. * buckets
  17. */
  18. const CLAMPED_MINUTE_RANGES = [
  19. 1,
  20. 10,
  21. 30,
  22. ONE_HOUR,
  23. ONE_HOUR * 4,
  24. ONE_HOUR * 8,
  25. ONE_HOUR * 12,
  26. ];
  27. export function getConfigFromTimeRange(
  28. start: Date,
  29. end: Date,
  30. timelineWidth: number
  31. ): TimeWindowConfig {
  32. const elapsedMinutes = (end.getTime() - start.getTime()) / (1000 * 60);
  33. const timeLabelMinutes = elapsedMinutes * (TIMELABEL_WIDTH / timelineWidth);
  34. const subMinutePxBuckets = elapsedMinutes < timelineWidth;
  35. for (const minutes of CLAMPED_MINUTE_RANGES) {
  36. if (minutes < Math.floor(timeLabelMinutes)) {
  37. continue;
  38. }
  39. // Configuration falls into
  40. return {
  41. start,
  42. end,
  43. dateLabelFormat: getFormat({timeOnly: true, seconds: subMinutePxBuckets}),
  44. elapsedMinutes,
  45. timeMarkerInterval: minutes,
  46. dateTimeProps: {timeOnly: true},
  47. };
  48. }
  49. // Calculate days between each time label interval for larger time ranges
  50. const timeLabelIntervalDays = Math.ceil(timeLabelMinutes / (ONE_HOUR * 24));
  51. return {
  52. start,
  53. end,
  54. dateLabelFormat: getFormat(),
  55. elapsedMinutes,
  56. timeMarkerInterval: timeLabelIntervalDays * ONE_HOUR * 24,
  57. dateTimeProps: {dateOnly: true},
  58. };
  59. }