parentAutogroupNode.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import type {TraceTree} from './traceTree';
  2. import {TraceTreeNode} from './traceTreeNode';
  3. export class ParentAutogroupNode extends TraceTreeNode<TraceTree.ChildrenAutogroup> {
  4. head: TraceTreeNode<TraceTree.Span>;
  5. tail: TraceTreeNode<TraceTree.Span>;
  6. groupCount: number = 0;
  7. profiles: TraceTree.Profile[] = [];
  8. private _autogroupedSegments: [number, number][] | undefined;
  9. constructor(
  10. parent: TraceTreeNode<TraceTree.NodeValue> | null,
  11. node: TraceTree.ChildrenAutogroup,
  12. metadata: TraceTree.Metadata,
  13. head: TraceTreeNode<TraceTree.Span>,
  14. tail: TraceTreeNode<TraceTree.Span>
  15. ) {
  16. super(parent, node, metadata);
  17. this.expanded = false;
  18. this.head = head;
  19. this.tail = tail;
  20. }
  21. get autogroupedSegments(): [number, number][] {
  22. if (this._autogroupedSegments) {
  23. return this._autogroupedSegments;
  24. }
  25. const children: TraceTreeNode<TraceTree.NodeValue>[] = [];
  26. let start: TraceTreeNode<TraceTree.NodeValue> | undefined = this.head;
  27. while (start && start !== this.tail) {
  28. children.push(start);
  29. start = start.children[0];
  30. }
  31. children.push(this.tail);
  32. this._autogroupedSegments = computeCollapsedBarSpace(children);
  33. return this._autogroupedSegments;
  34. }
  35. }
  36. // Returns a list of segments from a grouping sequence that can be used to render a span bar chart
  37. // It looks for gaps between spans and creates a segment for each gap. If there are no gaps, it
  38. // merges the n and n+1 segments.
  39. export function computeCollapsedBarSpace(
  40. nodes: TraceTreeNode<TraceTree.NodeValue>[]
  41. ): [number, number][] {
  42. if (nodes.length === 0) {
  43. return [];
  44. }
  45. const first = nodes[0];
  46. const segments: [number, number][] = [];
  47. let start = first.space[0];
  48. let end = first.space[0] + first.space[1];
  49. let i = 1;
  50. while (i < nodes.length) {
  51. const next = nodes[i];
  52. if (next.space[0] > end) {
  53. segments.push([start, end - start]);
  54. start = next.space[0];
  55. end = next.space[0] + next.space[1];
  56. i++;
  57. } else {
  58. end = next.space[0] + next.space[1];
  59. i++;
  60. }
  61. }
  62. segments.push([start, end - start]);
  63. return segments;
  64. }