|
| 1 | +from node_modules.vis import vis |
| 2 | +import json |
| 3 | + |
| 4 | +class graph(data): |
| 5 | + nodes = vis.DataSet([]) |
| 6 | + edges = vis.DataSet([]) |
| 7 | + |
| 8 | + counter = 0 |
| 9 | + |
| 10 | + def head_dealer(self, jsonMessage): |
| 11 | + |
| 12 | + counter += 1 |
| 13 | + nodesLabel = "" |
| 14 | + |
| 15 | + jsonObject = json.loads(jsonMessage) |
| 16 | + |
| 17 | + for jsonPlan in jsonObject["Plan"]: |
| 18 | + for attribute, value in jsonPlan.items(): |
| 19 | + if(attribute != "Plans"): |
| 20 | + nodesLabel = nodesLabel + attribute + ": " + value + '\n' |
| 21 | + |
| 22 | + node_creator(nodesLabel, counter, jsonObject["Plan"]["Node Type"]) |
| 23 | + |
| 24 | + if(jsonObject["Plan"]["Plans"] != undefined): |
| 25 | + body_dealer(jsonObject["Plan"]["Plans"],counter) # pass to recursive function |
| 26 | + |
| 27 | + |
| 28 | + def node_creator(nodesLabel, counter, nodeType): |
| 29 | + if (nodeType == "Hash Join"): |
| 30 | + nodes.add({id: counter, color : "#e0dab3", label: nodesLabel, shape : "circle"}) |
| 31 | + |
| 32 | + elif (nodeType == "Hash"): |
| 33 | + nodes.add({id: counter, color : "#c1bfae", label: nodesLabel, shape : "circle"}) |
| 34 | + |
| 35 | + elif (nodeType == "Nested Loop"): |
| 36 | + nodes.add({id: counter, color : "#ad6e4a", label: nodesLabel, shape : "circle"}) |
| 37 | + |
| 38 | + elif (nodeType == "Merge Join"): |
| 39 | + nodes.add({id: counter, color : "#4aad69", label: nodesLabel, shape : "circle"}) |
| 40 | + |
| 41 | + elif (nodeType == "Seq Scan"): |
| 42 | + nodes.add({id: counter, color : "#c1bfae", label: nodesLabel, shape : "box"}) |
| 43 | + |
| 44 | + elif (nodeType == "Index Scan"): |
| 45 | + nodes.add({id: counter, color : "#ad6e4a", label: nodesLabel, shape : "box"}) |
| 46 | + |
| 47 | + elif (nodeType == "Values Scan"): |
| 48 | + nodes.add({id: counter, color : "#4aad69", label: nodesLabel, shape : "box"}) |
| 49 | + |
| 50 | + elif (nodeType == "Index Only Scan"): |
| 51 | + nodes.add({id: counter, color : "#c1bfae", label: nodesLabel, shape : "box"}) |
| 52 | + |
| 53 | + elif (nodeType == "Subquery Scan"): |
| 54 | + nodes.add({id: counter, color : "#8aad4a", label: nodesLabel, shape : "box"}) |
| 55 | + |
| 56 | + elif (nodeType == "Function Scan"): |
| 57 | + nodes.add({id: counter, color : "#a37d7d", label: nodesLabel, shape : "box"}) |
| 58 | + |
| 59 | + elif (nodeType == "Sort"): |
| 60 | + nodes.add({id: counter, color : "#8aad4a", label: nodesLabel, shape : "diamond"}) |
| 61 | + |
| 62 | + elif (nodeType == "Aggregate"): |
| 63 | + nodes.add({id: counter, color : "#a37d7d", label: nodesLabel, shape : "diamond"}) |
| 64 | + |
| 65 | + else: |
| 66 | + nodes.add({id: counter,color : "#4aad69", label: nodesLabel}) |
| 67 | + |
| 68 | + # 'Materialize' |
| 69 | + # 'Limit' |
| 70 | + # 'Result' |
| 71 | + # 'Gather' |
| 72 | + # 'Gather Merge' |
| 73 | + |
| 74 | + # 'BitmapAnd' |
| 75 | + # 'BitmapOr' |
| 76 | + # 'Bitmap Heap Scan' |
| 77 | + # 'Bitmap Index Scan' |
| 78 | + # 'CTE Scan' |
| 79 | + # 'Append' |
| 80 | + # 'Unique' |
| 81 | + |
| 82 | + |
| 83 | + #deal with everything that has plans recursively |
| 84 | + def body_dealer(bodyMessage, parentCounter): |
| 85 | + bodyObject = json.loads(bodyMessage) |
| 86 | + |
| 87 | + for i in range len(bodyObject): |
| 88 | + counter += 1 |
| 89 | + nodesLabel = "" |
| 90 | + |
| 91 | + for attribute, value in bodyObject[i].items(): |
| 92 | + if(attribute != "Plans"): |
| 93 | + nodesLabel = nodesLabel + attribute + ": " + value + '\n' |
| 94 | + |
| 95 | + node_creator(nodesLabel, counter, bodyObject[i]["Node Type"]) |
| 96 | + |
| 97 | + edges.add({from: counter, to: parentCounter, arrows : "to"}) |
| 98 | + if(bodyObject[i].Plans != undefined): |
| 99 | + body_dealer(bodyObject[i].Plans, counter) |
| 100 | + |
| 101 | + |
| 102 | + # create an array with edges |
| 103 | + |
| 104 | + |
| 105 | + # create a network |
| 106 | + container = document.getElementById('mynetwork'); |
| 107 | + |
| 108 | + # provide the data in the vis format |
| 109 | + data = { |
| 110 | + nodes: nodes, |
| 111 | + edges: edges |
| 112 | + } |
| 113 | + |
| 114 | + options = { edges: { |
| 115 | + font: { |
| 116 | + size: 12 |
| 117 | + } |
| 118 | + } , |
| 119 | + nodes: { |
| 120 | + shape: 'box', |
| 121 | + font: { |
| 122 | + bold: { |
| 123 | + color: '#0077aa' |
| 124 | + } |
| 125 | + }}, |
| 126 | + layout: { |
| 127 | + hierarchical: { |
| 128 | + direction: "DU", |
| 129 | + sortMethod: "directed", |
| 130 | + levelSeparation: 256, |
| 131 | + nodeSpacing: 720, |
| 132 | + treeSpacing: 1000, |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + # initialize your network! |
| 138 | + def initialize_network(self, container, ) |
| 139 | + network = vis.Network(container, data, options) |
0 commit comments