getChartFromNodes()

in marketing-analytics/activation/data-tasks-coordinator/src/utils/adapter/mermaid_flowchart.js [138:192]


  getChartFromNodes(nodes = [], title) {
    this.title = title;
    // Map for node Id to a sub graph it belongs to.
    const idBelongsToSubGraph = new Map();
    // Map for a tag (embeded tag or multiple tag) to a sub graph it belongs to.
    const tagToSubGraph = new Map();
    // Map for tag owner's Id and the related sub graph's Id.
    const ownerIdToSubGraphId = new Map();
    nodes.forEach((node) => {
      const {
        taskId, id, parentId,
        tagHolded,
        embeddedTag,
        multipleTag,
      } = node;
      // 'target' is the subgraph or the main graph that contains the current node
      let target;
      if (idBelongsToSubGraph.has(parentId)) {
        // belongs to the current node's parent's graph
        target = idBelongsToSubGraph.get(parentId);
      } else if (tagToSubGraph.has(embeddedTag || multipleTag)) {
        // if the current node is embedded or multipled, use its holder's graph
        // The tag holder should have been proceeded as the nodes are sorted.
        target = tagToSubGraph.get(embeddedTag || multipleTag);
        idBelongsToSubGraph.set(id, target);
      } else {// the node belongs to the main graph
        target = this;
      }

      // A tag (multiple tag or embaded tag) means there needs a new subgraph.
      if (tagHolded) {
        const subgraphId = `${taskId}-${id}`;
        const subgraph = new MermaidSubGraph(this.options, subgraphId);
        tagToSubGraph.set(tagHolded, subgraph);
        ownerIdToSubGraphId.set(id, subgraphId);
        const lineLabel = target.getEdgeLabel(node);
        target.addCode(`${id} ---|${lineLabel}| ${subgraphId}`);
        target.addSubGraph(subgraph);
      }
      // Add Mermaid element for this node.
      const mermaidNode = target.getNodeCode(node);
      if (parentId) {
        // If parent has a sub graph, the link should from the sub graph not parent node
        const sourceId = ownerIdToSubGraphId.has(parentId)
          ? ownerIdToSubGraphId.get(parentId)
          : parentId;
        target.addCode(`${sourceId} --> ${mermaidNode}`);
      } else {
        target.addCode(mermaidNode);
      }
      // Add Firstore link (if possible) to this node.
      target.addLink(node);
    });
    return this.getChartCode().join('\n');
  }