utils.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. dateLabelFormat: getFormat({timeOnly: true, seconds: subMinutePxBuckets}),
  42. elapsedMinutes,
  43. timeMarkerInterval: minutes,
  44. dateTimeProps: {timeOnly: true},
  45. };
  46. }
  47. // Calculate days between each time label interval for larger time ranges
  48. const timeLabelIntervalDays = Math.ceil(timeLabelMinutes / (ONE_HOUR * 24));
  49. return {
  50. dateLabelFormat: getFormat(),
  51. elapsedMinutes,
  52. timeMarkerInterval: timeLabelIntervalDays * ONE_HOUR * 24,
  53. dateTimeProps: {dateOnly: true},
  54. };
  55. }