-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.js
417 lines (349 loc) · 10.1 KB
/
web.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
const username = 'root'
const password = process.env.DB_PASSWORD
const MAX_STR_LENGTH = 2048
const DB_HOST = process.env.DB_HOST || 'http://localhost'
const DB = process.env.DB_NAME || 'kukako'
const PORT = process.env.DB_PORT || 2480
const URL = `${DB_HOST}:${PORT}/api/v1/command/${DB}`
const GROUP_THRESHOLD = process.env.GROUP_THRESHOLD || 1
let web = {}
web.getURL = function() {
return URL
}
web.dbStatus = async function() {
const { default: got } = await import('got');
var url = URL.replace(`/command/${DB}`, '/ready')
try {
await got.get(url)
} catch(e) {
throw({message: "DB connection failed", code: e.code})
}
}
web.createDB = async function() {
const { default: got } = await import('got');
var url = URL.replace(`/command/${DB}`, '/server')
var data = {
username: username,
password: password,
json: {
command: `create database ${DB}`
}
};
try {
await got.post(url, data)
} catch(e) {
console.log(e.message)
throw({message: "Database creation failed"})
}
}
web.createVertexType = async function(type) {
var query = `CREATE vertex type ${type}`
try {
await this.sql(query)
} catch (e) {
//console.log(e.message)
//console.log(`${type} exists`)
}
}
web.sql = async function(query, options) {
const { default: got } = await import('got');
var data = {
username: username,
password: password,
json: {
command:query,
language:'sql'
}
};
var response = await got.post(URL, data).json()
return response
}
// function that bypasses Cypher issue for deleting data
web.clearGraph = async function(query) {
const { default: got } = await import('got');
query = 'g.V().not(hasLabel("Schema")).drop()'
var data = {
username: username,
password: password,
json: {
command:query,
language:'gremlin'
}
}
return await got.post(URL, data).json()
}
web.getGraph = async function(query, options, debug) {
const { default: got } = await import('got');
if(!options) var options = {}
var data = {
username: username,
password: password,
json: {
command:query,
language:'cypher',
serializer: 'graph'
}
};
try {
var response = await got.post(URL, data).json()
options.schemas = await getSchemaRelations(data)
options.labels = await getSchemaVertexLabels(data)
console.log(options)
console.log(response)
return convert2CytoScapeJs(response, options)
} catch(e) {
throw({message: e.message, code: e.code})
}
}
web.cypher = async function(query, options, debug) {
const { default: got } = await import('got');
if(!options) var options = {}
if(options.current && !options.current.includes('#')) options.current = '#' + options.current
var data = {
username: username,
password: password,
json: {
command:query,
language:'cypher'
}
};
if(options.serializer) data.json.serializer = options.serializer
if(debug) console.log(query)
try {
var response = await got.post(URL, data).json()
if(query && query.toLowerCase().includes('create')) return response
else if(!options.serializer) return response
else if(options.serializer == 'graph' && options.format == 'cytoscape') {
options.labels = await getSchemaVertexLabels(data)
return convert2CytoScapeJs(response, options)
} else {
return response
}
} catch(e) {
throw({message: e.message, code: e.code})
}
}
async function getSchemaVertexLabels(data) {
const { default: got } = await import('got');
const query = "MATCH (s:Schema) RETURN COALESCE(s.label, s._type) as label, s._type as type"
data.json = {
command:query,
language:'cypher'
}
try {
var response = await got.post(URL, data).json()
var labels = response.result.reduce(
(obj, item) => Object.assign(obj, { [item.type]: item.label }), {});
return labels
} catch(e) {
console.log(e.response)
console.log(query)
throw(e)
}
}
async function getSchemaRelations(data) {
const { default: got } = await import('got');
const query = 'MATCH (s:Schema)-[r]->(s2:Schema) return type(r) as type, r.label as label, r.label_rev as label_rev, COALESCE(r.label_inactive, r.label) as label_inactive, s._type as from, s2._type as to, r.tags as tags, r.compound as compound'
data.json = {
command:query,
language:'cypher'
}
var schema_relations = {}
var schemas = await got.post(URL, data).json()
schemas.result.forEach(x => {
schema_relations[`${x.from}:${x.type}:${x.to}`] = x
})
return schema_relations
}
function setParent(vertices, child, parent) {
for(var node of vertices) {
if(node.data.id == child) {
node.data.parent = parent
}
}
}
// group relations by their type if count > grouping threshold
// this groups relations only for one "central node" (homepage of person)
function nodeGrouping(nodes, edges, vertex_types, options) {
var unique_links = {}
var cluster_types = {}
var cluster_nodes = []
var cluster_edges = []
var clustered_links = []
var clustered_edges = []
var clustered_nodes = []
clustered_nodes = nodes
for(var edge of edges) {
// currently cluster only if source is Person TODO: remove this constraint!
if(vertex_types[options.current] == 'Person') {
var cluster_id = edge.data.source + '__' + edge.data.type + '__' + vertex_types[edge.data.target]
if(unique_links[cluster_id]) {
unique_links[cluster_id].push(edge.data.target)
} else {
unique_links[cluster_id] = [edge.data.target]
}
}
}
for(var cluster_id in unique_links) {
if(unique_links[cluster_id].length > GROUP_THRESHOLD) {
var splitted = cluster_id.split('__')
var source = splitted[0]
var rel = splitted[1]
var target = splitted[2]
clustered_links.push(cluster_id)
var schema_id = `Person:${rel}:${target}`
var cluster_label = rel
if(options.schemas[schema_id]) {
cluster_label = options.schemas[schema_id].label
}
// add cluster node
cluster_nodes.push({data: {name: cluster_label, type_label: 'Cluster', id: cluster_id, type:'Cluster', width:100, active:true}})
// add link from "central node" to cluster node
cluster_edges.push({data: {label: '', source: source, target: cluster_id, active: true}})
}
}
clustered_edges = edges.filter(edge => {
// filter out clustered links
var found = false
for(var cluster_id of clustered_links) {
var splitted = cluster_id.split('__')
var source = splitted[0]
var rel = splitted[1]
if(edge.data.source == source) {
if(unique_links[cluster_id].includes(edge.data.target) && edge.data.type === rel)
found = true
}
}
if(!found) return edge
})
var multiples = []
// set .parent to Cluster node to all clustered notes
clustered_nodes = nodes.filter(node => {
var count = 0
for(var cluster_id of clustered_links) {
if(unique_links[cluster_id].includes(node.data.id)) {
// create copies of nodes that are on multiple clusters
if(node.data.parent) {
const clone = JSON.parse(JSON.stringify(node))
clone.data.id = clone.data.id + '_' + count
clone.data.parent = cluster_id
multiples.push(clone)
count++
} else {
node.data.parent = cluster_id
}
}
}
return node
})
clustered_nodes = clustered_nodes.concat(multiples)
// add cluster nodes and edges to output
clustered_nodes = clustered_nodes.concat(cluster_nodes)
clustered_edges = clustered_edges.concat(cluster_edges)
if(clustered_links.length === 0) {
clustered_nodes = nodes
clustered_edges = edges
}
// console.log('Edges to be clustered:')
// console.log(clustered_links)
// console.log('clustered edge count: ' + edges.length)
// console.log(cluster_nodes)
return {edges: clustered_edges, nodes: clustered_nodes}
}
function convert2CytoScapeJs(data, options) {
if(!options) var options = {labels:{}}
var vertex_ids = []
var nodes = []
var inactive_nodes = []
var vertex_types = {}
if(data.result.vertices) {
for(var v of data.result.vertices) {
if(!vertex_ids.includes(v.r)) {
var node = {}
if(v.p._type) { // schema
node = {
data:{
id:v.r,
name:options.labels[v.p._type],
type: v.p._type,
type_label: v.p._type,
active: true,
width: 100,
idc: v.r.replace('#','')
}
}
} else {
vertex_types[v.r] = v.t
node = {
data:{
id:v.r,
name:v.p.label,
type: v.t,
type_label: options.labels[v.t],
active: v.p._active,
width: 100,
idc: v.r.replace('#','')
}
}
if(!node.data.active) inactive_nodes.push(v.r)
}
if(v.r == options.current) node.data.current = 'yes'
if(options.me && v.r == options.me.rid ) node.data.me = 'yes'
if(v.p._image) node.data.image = v.p._image
nodes.push(node)
vertex_ids.push(v.r)
//console.log(node)
}
}
}
var edges = []
var ids = []
if(data.result.edges) {
for(var v of data.result.edges) {
if(!ids.includes(v.r)) {
var edge = {data:{id:v.r, source:v.o, target:v.i, label:v.t, type:v.t, active:v.p._active}}
ids.push(v.r)
if(typeof v.p._active == 'undefined') edge.data.active = true
else edge.data.active = v.p._active
// links to inactive node are also inactive
if(inactive_nodes.includes(edge.data.source) || inactive_nodes.includes(edge.data.target))
edge.data.active = false
// add relationship labels to graph from schema relations
if(options.schemas) {
var edge_id = `${vertex_types[edge.data.source]}:${v.t}:${vertex_types[edge.data.target]}`
if(options.schemas[edge_id]) {
if(options.schemas[edge_id].label) {
if(edge.data.active) {
edge.data.label = options.schemas[edge_id].label.toUpperCase()
} else {
edge.data.label = options.schemas[edge_id].label_inactive
}
} else {
edge.data.label = edge.data.label
}
if(options.schemas[edge_id].compound === true) {
if(options.current == v.o)
edges.push(edge)
else
setParent(nodes, v.o, v.i)
} else {
edges.push(edge)
}
} else {
edges.push(edge)
}
} else {
edges.push(edge)
}
}
}
}
// group only if we are on homepage
if(options.current) {
var grouped = nodeGrouping(nodes, edges, vertex_types, options)
return {nodes:grouped.nodes, edges: grouped.edges}
} else {
return {nodes:nodes, edges: edges}
}
}
module.exports = web