export function computeLevels()

in src/views/dashboard/related/topology/components/utils/layout.ts [144:203]


export function computeLevels(calls: Call[], nodeList: Node[], arr: Node[][]) {
  const levels: Node[][] = [];
  const node = findMostFrequent(calls);
  let nodes = JSON.parse(JSON.stringify(nodeList)).sort((a: Node, b: Node) => {
    if (a.name.toLowerCase() < b.name.toLowerCase()) {
      return -1;
    }
    if (a.name.toLowerCase() > b.name.toLowerCase()) {
      return 1;
    }
    return 0;
  });
  const index = nodes.findIndex((n: Node) => n.type === "USER");
  let key = index;
  if (index < 0) {
    key = nodes.findIndex((n: Node) => n.id === node.id);
  }
  levels.push([nodes[key]]);
  nodes = nodes.filter((_: unknown, index: number) => index !== key);
  for (const level of levels) {
    const a = [];
    for (const l of level) {
      for (const n of calls) {
        if (n.target === l.id) {
          const i = nodes.findIndex((d: Node) => d.id === n.source);
          if (i > -1 && nodes[i]) {
            a.push(nodes[i]);
            nodes = nodes.filter((_: unknown, index: number) => index !== i);
          }
        }
        if (n.source === l.id) {
          const i = nodes.findIndex((d: Node) => d.id === n.target);
          if (i > -1 && nodes[i]) {
            a.push(nodes[i]);
            nodes = nodes.filter((_: unknown, index: number) => index !== i);
          }
        }
      }
    }
    if (a.length) {
      levels.push(a);
    }
  }
  const list = levels.length > arr.length ? levels : arr;
  const subList = levels.length > arr.length ? arr : levels;
  arr = list.map((subArray: Node[], index: number) => {
    if (subList[index]) {
      return subArray.concat(subList[index]);
    } else {
      return subArray;
    }
  });

  if (nodes.length) {
    const ids = nodes.map((d: Node) => d.id);
    const links = calls.filter((item: Call) => ids.includes(item.source) || ids.includes(item.target));
    arr = computeLevels(links, nodes, arr);
  }
  return arr;
}