selectNearestFrame.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import {FlamegraphFrame} from '../flamegraphFrame';
  2. export type DirectionX = 'left' | 'right';
  3. export type DirectionY = 'up' | 'down';
  4. export type Direction = DirectionY | DirectionX;
  5. /**
  6. * selectNearestFrame walks a FlamegraphFrame tree the direction specified and returns the nearest
  7. * FlamegraphFrame relative to the starting node.
  8. */
  9. export function selectNearestFrame(frame: FlamegraphFrame, direction: Direction) {
  10. if (direction === 'up') {
  11. const parent = frame.parent;
  12. // if there is an immediate parent, goto parent
  13. if (parent) {
  14. return parent;
  15. }
  16. // if there is no parent, we attempt to move left to the next stack
  17. const leftSibling = getSibling(frame, 'left');
  18. if (leftSibling) {
  19. return leftSibling;
  20. }
  21. return frame;
  22. }
  23. if (direction === 'down') {
  24. // if there is an immediate child, goto child
  25. const child = frame.children?.[0];
  26. if (child) {
  27. return child;
  28. }
  29. // if there is no child, we attempt to move right to the next stack
  30. const sibling = scanForNearestSibling(frame, 'right');
  31. if (sibling) {
  32. return sibling;
  33. }
  34. return frame;
  35. }
  36. // scan for the nearest sibling in either the left or right direction
  37. // this function will walk us up the tree to a new branch
  38. const sibling = scanForNearestSibling(frame, direction);
  39. // if we find an adjacent sibling we attempt to walk down to a depth that
  40. // matches our targetDepth
  41. if (sibling) {
  42. const targetDepth = frame.depth;
  43. const nodeWithDepth = scanForNearestFrameWithDepth(sibling, targetDepth, direction);
  44. if (nodeWithDepth) {
  45. return nodeWithDepth;
  46. }
  47. }
  48. return frame;
  49. }
  50. /**
  51. * scanForNearestSibling will walk up a branch looking for an adjacent sibling
  52. */
  53. function scanForNearestSibling(frame: FlamegraphFrame, directionX: DirectionX) {
  54. let node = frame;
  55. while (node) {
  56. const sibling = getSibling(node, directionX);
  57. if (sibling) {
  58. return sibling;
  59. }
  60. if (!node.parent) {
  61. break;
  62. }
  63. node = node.parent;
  64. }
  65. return frame;
  66. }
  67. /**
  68. * scanForNearestFrameWithDepth will walk down a FlamegraphFrame looking for a target depth.
  69. * it will follow either the furthest left or right branch based on direction.
  70. * if target depth is not found, we return the frame with the max depth along that branch.
  71. */
  72. function scanForNearestFrameWithDepth(
  73. frame: FlamegraphFrame,
  74. depth: number,
  75. directionX: DirectionX
  76. ) {
  77. let node = frame;
  78. let nextNode = node.children[directionX === 'right' ? 0 : node.children.length - 1];
  79. while (node && nextNode) {
  80. if (node.depth === depth) {
  81. return node;
  82. }
  83. nextNode = node.children[directionX === 'right' ? 0 : node.children.length - 1];
  84. if (nextNode) {
  85. node = nextNode;
  86. }
  87. }
  88. return node;
  89. }
  90. function getSibling(frame: FlamegraphFrame, directionX: DirectionX) {
  91. const parent = frame.parent;
  92. if (!parent) {
  93. return null;
  94. }
  95. // get the index of the current frame relative to its siblings
  96. const indexOfFrame = parent.children.indexOf(frame);
  97. // this should never happen, it would make our data structure invalid
  98. if (indexOfFrame === -1) {
  99. throw Error('frame.parent.children does not include current frame');
  100. }
  101. if (directionX === 'right') {
  102. const hasRightSiblings = indexOfFrame < parent.children.length - 1;
  103. if (hasRightSiblings) {
  104. return parent.children[indexOfFrame + 1];
  105. }
  106. return null;
  107. }
  108. const hasLeftSiblings = indexOfFrame > 0;
  109. if (hasLeftSiblings) {
  110. return parent.children[indexOfFrame - 1];
  111. }
  112. return null;
  113. }