2023-09-30 16:10:59 +02:00
|
|
|
import { getConfig } from '../../diagram-api/diagramAPI.js';
|
2023-06-18 00:32:45 +02:00
|
|
|
import common from '../common/common.js';
|
|
|
|
import {
|
2023-06-18 00:32:45 +02:00
|
|
|
setAccTitle,
|
|
|
|
getAccTitle,
|
|
|
|
getAccDescription,
|
|
|
|
setAccDescription,
|
2023-06-18 00:32:45 +02:00
|
|
|
setDiagramTitle,
|
|
|
|
getDiagramTitle,
|
|
|
|
clear as commonClear,
|
2023-09-02 19:07:56 +02:00
|
|
|
} from '../common/commonDb.js';
|
2023-06-18 00:32:45 +02:00
|
|
|
|
2023-06-20 01:37:27 +02:00
|
|
|
// Sankey diagram represented by nodes and links between those nodes
|
2023-06-24 23:42:06 +02:00
|
|
|
let links: SankeyLink[] = [];
|
2023-06-27 16:50:51 +02:00
|
|
|
// Array of nodes guarantees their order
|
2023-06-24 23:42:06 +02:00
|
|
|
let nodes: SankeyNode[] = [];
|
2023-06-27 16:50:51 +02:00
|
|
|
// We also have to track nodes uniqueness (by ID)
|
2023-06-24 21:02:18 +02:00
|
|
|
let nodesMap: Record<string, SankeyNode> = {};
|
2023-06-18 00:32:45 +02:00
|
|
|
|
2023-06-24 21:02:18 +02:00
|
|
|
const clear = (): void => {
|
2023-06-18 00:32:45 +02:00
|
|
|
links = [];
|
2023-06-18 00:32:45 +02:00
|
|
|
nodes = [];
|
2023-06-24 21:02:18 +02:00
|
|
|
nodesMap = {};
|
2023-06-18 00:32:45 +02:00
|
|
|
commonClear();
|
|
|
|
};
|
|
|
|
|
2023-06-20 01:37:27 +02:00
|
|
|
class SankeyLink {
|
2024-03-23 07:09:02 +01:00
|
|
|
constructor(
|
|
|
|
public source: SankeyNode,
|
|
|
|
public target: SankeyNode,
|
|
|
|
public value: number = 0
|
|
|
|
) {}
|
2023-06-18 00:32:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-06-18 00:32:45 +02:00
|
|
|
* @param source - Node where the link starts
|
|
|
|
* @param target - Node where the link ends
|
2023-08-19 07:49:18 +02:00
|
|
|
* @param value - Describes the amount to be passed
|
2023-06-18 00:32:45 +02:00
|
|
|
*/
|
2023-06-24 21:02:18 +02:00
|
|
|
const addLink = (source: SankeyNode, target: SankeyNode, value: number): void => {
|
|
|
|
links.push(new SankeyLink(source, target, value));
|
2023-06-18 00:32:45 +02:00
|
|
|
};
|
2023-06-18 00:32:45 +02:00
|
|
|
|
2023-06-20 01:37:27 +02:00
|
|
|
class SankeyNode {
|
2023-06-24 21:02:18 +02:00
|
|
|
constructor(public ID: string) {}
|
2023-06-18 00:32:45 +02:00
|
|
|
}
|
|
|
|
|
2023-06-24 21:02:18 +02:00
|
|
|
const findOrCreateNode = (ID: string): SankeyNode => {
|
2023-09-30 16:10:59 +02:00
|
|
|
ID = common.sanitizeText(ID, getConfig());
|
2023-06-27 13:11:06 +02:00
|
|
|
|
2023-06-29 14:39:50 +02:00
|
|
|
if (!nodesMap[ID]) {
|
|
|
|
nodesMap[ID] = new SankeyNode(ID);
|
|
|
|
nodes.push(nodesMap[ID]);
|
|
|
|
}
|
2023-06-27 13:11:06 +02:00
|
|
|
return nodesMap[ID];
|
2023-06-18 00:32:45 +02:00
|
|
|
};
|
2023-06-18 00:32:45 +02:00
|
|
|
|
2023-06-18 03:33:20 +02:00
|
|
|
const getNodes = () => nodes;
|
|
|
|
const getLinks = () => links;
|
|
|
|
|
2023-06-20 01:37:27 +02:00
|
|
|
const getGraph = () => ({
|
2023-06-24 21:02:18 +02:00
|
|
|
nodes: nodes.map((node) => ({ id: node.ID })),
|
2023-06-20 01:37:27 +02:00
|
|
|
links: links.map((link) => ({
|
|
|
|
source: link.source.ID,
|
|
|
|
target: link.target.ID,
|
|
|
|
value: link.value,
|
|
|
|
})),
|
|
|
|
});
|
|
|
|
|
2023-06-18 00:32:45 +02:00
|
|
|
export default {
|
2023-06-24 21:02:18 +02:00
|
|
|
nodesMap,
|
2023-09-30 16:10:59 +02:00
|
|
|
getConfig: () => getConfig().sankey,
|
2023-06-18 03:33:20 +02:00
|
|
|
getNodes,
|
|
|
|
getLinks,
|
2023-06-20 01:37:27 +02:00
|
|
|
getGraph,
|
2023-06-18 00:32:45 +02:00
|
|
|
addLink,
|
2023-06-20 01:37:27 +02:00
|
|
|
findOrCreateNode,
|
2023-06-18 00:32:45 +02:00
|
|
|
getAccTitle,
|
|
|
|
setAccTitle,
|
|
|
|
getAccDescription,
|
|
|
|
setAccDescription,
|
2023-06-18 00:32:45 +02:00
|
|
|
getDiagramTitle,
|
|
|
|
setDiagramTitle,
|
2023-06-18 00:32:45 +02:00
|
|
|
clear,
|
2023-06-18 00:32:45 +02:00
|
|
|
};
|