From 5bacc86a2c24c329b0d5bfed9b972665a493be11 Mon Sep 17 00:00:00 2001 From: Anchel123 <110421452+Anchel123@users.noreply.github.com> Date: Mon, 1 Apr 2024 17:06:04 +0300 Subject: [PATCH] adding option to write a visualizing path objects --- app/graph/model.ts | 189 +++++++++++++++++++++++++-------------------- 1 file changed, 104 insertions(+), 85 deletions(-) diff --git a/app/graph/model.ts b/app/graph/model.ts index c2320c41..66135b6b 100644 --- a/app/graph/model.ts +++ b/app/graph/model.ts @@ -31,7 +31,7 @@ function nodeSafeKey(key: string): string { const index = NODE_RESERVED_KEYS.indexOf(key); if (index === -1) { return key; - } + } return NODE_ALTERNATIVE_RESERVED_KEYS[index]; } @@ -42,17 +42,17 @@ function edgeSafeKey(key: string): string { const index = EDGE_RESERVED_KEYS.indexOf(key); if (index === -1) { return key; - } + } return EDGE_ALTERNATIVE_RESERVED_KEYS[index]; } export function getCategoryColorName(index: number): string { - const colorIndex = index(), new Map(), new Map()) } @@ -134,6 +134,92 @@ export class Graph { return graph } + // eslint-disable-next-line @typescript-eslint/no-explicit-any + private extendNode(cell: any, newElements: ElementDefinition[]) { + // check if category already exists in categories + let category = this.categoriesMap.get(cell.labels[0]) + if (!category) { + category = { name: cell.labels[0], index: this.categoriesMap.size, show: true } + this.categoriesMap.set(category.name, category) + this.categories.push(category) + } + + // check if node already exists in nodes or fake node was created + const currentNode = this.nodesMap.get(cell.id) + if (!currentNode) { + const node: NodeDataDefinition = { + id: cell.id.toString(), + name: cell.id.toString(), + category: category.name, + color: getCategoryColorValue(category.index) + } + Object.entries(cell.properties).forEach(([key, value]) => { + node[nodeSafeKey(key)] = value as string; + }); + this.nodesMap.set(cell.id, node) + this.elements.push({ data: node }) + newElements.push({ data: node }) + } else if (currentNode.category === "") { + // set values in a fake node + currentNode.id = cell.id.toString(); + currentNode.name = cell.id.toString(); + currentNode.category = category.name; + currentNode.color = getCategoryColorValue(category.index) + Object.entries(cell.properties).forEach(([key, value]) => { + currentNode[nodeSafeKey(key)] = value as string; + }); + newElements.push({ data: currentNode }) + } + return newElements + } + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + private extendEdge(cell: any, newElements: ElementDefinition[]) { + const currentEdge = this.edgesMap.get(cell.id) + if (!currentEdge) { + const sourceId = cell.sourceId.toString(); + const destinationId = cell.destinationId.toString() + + const edge: EdgeDataDefinition = { source: sourceId, target: destinationId, label: cell.relationshipType } + Object.entries(cell.properties).forEach(([key, value]) => { + edge[edgeSafeKey(key)] = value as string; + }); + this.edgesMap.set(cell.id, edge) + this.elements.push({ data: edge }) + newElements.push({ data: edge }) + + // creates a fakeS node for the source and target + let source = this.nodesMap.get(cell.sourceId) + if (!source) { + source = { + id: cell.sourceId.toString(), + name: cell.sourceId.toString(), + value: "", + category: "", + color: getCategoryColorValue() + } + this.nodesMap.set(cell.sourceId, source) + this.elements.push({ data: source }) + newElements.push({ data: source }) + } + + let destination = this.nodesMap.get(cell.destinationId) + if (!destination) { + destination = { + id: cell.destinationId.toString(), + name: cell.destinationId.toString(), + value: "", + category: "", + color: getCategoryColorValue() + } + this.nodesMap.set(cell.destinationId, destination) + this.elements.push({ data: destination }) + newElements.push({ data: destination }) + } + } + return newElements + } + // eslint-disable-next-line @typescript-eslint/no-explicit-any public extend(results: any): ElementDefinition[] { @@ -151,86 +237,19 @@ export class Graph { // eslint-disable-next-line @typescript-eslint/no-explicit-any Object.values(row).forEach((cell: any) => { if (cell instanceof Object) { - if (cell.relationshipType) { - - const currentEdge = this.edgesMap.get(cell.id) - if (!currentEdge) { - const sourceId = cell.sourceId.toString(); - const destinationId = cell.destinationId.toString() - - const edge: EdgeDataDefinition = { source: sourceId, target: destinationId, label: cell.relationshipType } - Object.entries(cell.properties).forEach(([key, value]) => { - edge[edgeSafeKey(key)] = value as string; - }); - this.edgesMap.set(cell.id, edge) - this.elements.push({data:edge}) - newElements.push({data:edge}) - - // creates a fakeS node for the source and target - let source = this.nodesMap.get(cell.sourceId) - if (!source) { - source = { - id: cell.sourceId.toString(), - name: cell.sourceId.toString(), - value: "", - category: "", - color: getCategoryColorValue() - } - this.nodesMap.set(cell.sourceId, source) - this.elements.push({data:source}) - newElements.push({data:source}) - } - - let destination = this.nodesMap.get(cell.destinationId) - if (!destination) { - destination = { - id: cell.destinationId.toString(), - name: cell.destinationId.toString(), - value: "", - category: "", - color: getCategoryColorValue() - } - this.nodesMap.set(cell.destinationId, destination) - this.elements.push({data:destination}) - newElements.push({data:destination}) - } - } + if (cell.nodes) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + cell.nodes.forEach((node: any) => { + this.extendNode(node, newElements) + }) + // eslint-disable-next-line @typescript-eslint/no-explicit-any + cell.edges.forEach((edge: any) => { + this.extendEdge(edge, newElements) + }) + } else if (cell.relationshipType) { + this.extendEdge(cell, newElements) } else if (cell.labels) { - - // check if category already exists in categories - let category = this.categoriesMap.get(cell.labels[0]) - if (!category) { - category = { name: cell.labels[0], index: this.categoriesMap.size, show: true } - this.categoriesMap.set(category.name, category) - this.categories.push(category) - } - - // check if node already exists in nodes or fake node was created - const currentNode = this.nodesMap.get(cell.id) - if (!currentNode) { - const node: NodeDataDefinition = { - id: cell.id.toString(), - name: cell.id.toString(), - category: category.name, - color: getCategoryColorValue(category.index) - } - Object.entries(cell.properties).forEach(([key, value]) => { - node[nodeSafeKey(key)] = value as string; - }); - this.nodesMap.set(cell.id, node) - this.elements.push({data:node}) - newElements.push({data:node}) - } else if (currentNode.category === ""){ - // set values in a fake node - currentNode.id = cell.id.toString(); - currentNode.name = cell.id.toString(); - currentNode.category = category.name; - currentNode.color = getCategoryColorValue(category.index) - Object.entries(cell.properties).forEach(([key, value]) => { - currentNode[nodeSafeKey(key)] = value as string; - }); - newElements.push({data:currentNode}) - } + this.extendNode(cell, newElements) } } })