utils.tsx 1.5 KB

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