utils.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import type {Organization} from 'sentry/types';
  2. import type {TimelineEvent} from './useTraceTimelineEvents';
  3. function getEventTimestamp(start: number, event: TimelineEvent) {
  4. return new Date(event.timestamp).getTime() - start;
  5. }
  6. export function getEventsByColumn(
  7. events: TimelineEvent[],
  8. durationMs: number,
  9. totalColumns: number,
  10. start: number
  11. ): Array<[column: number, events: TimelineEvent[]]> {
  12. const eventsByColumn = events.reduce((map, event) => {
  13. const columnPositionCalc =
  14. Math.floor((getEventTimestamp(start, event) / durationMs) * (totalColumns - 1)) + 1;
  15. // Should start at minimum in the first column
  16. const column = Math.max(1, columnPositionCalc);
  17. if (map.has(column)) {
  18. map.get(column)!.push(event);
  19. } else {
  20. map.set(column, [event]);
  21. }
  22. return map;
  23. }, new Map<number, TimelineEvent[]>());
  24. return Array.from(eventsByColumn.entries());
  25. }
  26. export function getChunkTimeRange(
  27. startTimestamp: number,
  28. chunkIndex: number,
  29. chunkDurationMs: number
  30. ): [number, number] {
  31. // Calculate the absolute start time of the chunk in milliseconds
  32. const chunkStartMs = chunkIndex * chunkDurationMs;
  33. // Add the chunk start time to the overall start timestamp
  34. const chunkStartTimestamp = startTimestamp + chunkStartMs;
  35. // Calculate the end timestamp by adding the chunk duration
  36. const chunkEndTimestamp = chunkStartTimestamp + chunkDurationMs;
  37. // Round up and down to the nearest second
  38. return [Math.floor(chunkStartTimestamp), Math.floor(chunkEndTimestamp) + 1];
  39. }
  40. export function hasTraceTimelineFeature(organization: Organization | null) {
  41. return organization?.features?.includes('issues-trace-timeline');
  42. }