-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathneo4j.js
462 lines (404 loc) · 13.6 KB
/
neo4j.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
const neo4j = require('neo4j-driver').v1;
const fs = require('fs');
const _ = require('underscore');
var config = null;
try {
config = JSON.parse(fs.readFileSync("config.json"))
} catch {
config = {
NEO4J_PASSWORD: process.env.NEO4J_PASSWORD,
NEO4J_URL: process.env.NEO4J_URL,
NEO4J_USER: process.env.NEO4J_USER,
GRAPH_SERVICE: process.env.GRAPH_SERVICE,
GALLERY_SERVICE: process.env.GALLERY_SERVICE }
}
const driver = neo4j.driver(config.NEO4J_URL, neo4j.auth.basic(config.NEO4J_USER, config.NEO4J_PASSWORD));
const session = driver.session();
function constructDataQuery(traceString) {
const trace = traceString.split('&')
var conditions = "n1.label = $label";
var returns = "n1";
var query = "(n1)";
var args =
{ label:trace[0],
}
var numNodes = trace.length/2;
for(var n = 1; n <= numNodes; n++)
{
var nodeName = trace[(n-1)*2+1];
if (n > 1)
{
returns += ", n" + n;
query += "-[r" + (n-1) + "]->"
query += "(n" + n + ")";
conditions += " AND type(r" + (n-1) + ") = $r" + (n-1) + "type"
args["r" + (n-1) + "type"] = trace[(n-1)*2];
}
if (nodeName != "[any]") {
conditions += " AND n" + n + ".name = $n" + n + "name"
args["n" + n + "name"] = nodeName;
}
}
return ['MATCH ' + query + ' WHERE ' + conditions + ' RETURN ' + returns, args];
}
function list_of_key_and_type(traceString){
var [query, args] = constructDataQuery(traceString)
var key = [];
var type = [];
const resultPromise = session.run(query, args);
return resultPromise.then(result => {
session.close();
for(var res of result.records) {
for(var i = 0; i < res._fields.length; i++) {
for(var prop of Object.keys(res._fields[i].properties)) {
key.push("node" + (i+1) + "." + prop);
type.push(typeof(res._fields[i].properties[prop]));
}
}
}
type.forEach(function(item, i) { if (item == "object") type[i] = "float"; });
let unique_key = [...new Set(key)];
var unique_type = []
for(var k of unique_key){
unique_type.push(type[key.indexOf(k)]);
}
return [unique_key , unique_type];
});
}
function add_label(){
const resultPromise = session.run(
'MATCH (n) SET n.label = labels(n)[0]' ,
);
}
function All_nodes(){
//--list all types of nodes
const resultPromise = session.run(
'MATCH (n) RETURN n',
);
return resultPromise.then(result => {
session.close();
var jsonString = ""
for (var i = 0; i < result.records.length ; i++) {
const singleRecord = result.records[i];
const node = singleRecord.get(0);
const node_obj = {
name: node.labels[0],
trace:[node.labels[0]],
returns:{"kind":"nested","endpoint": "/" + node.labels[0] + "/nodes_of_type/"+node.labels},
}
jsonString += JSON.stringify(node_obj) + ";";
}
var arr = jsonString.slice(0, jsonString.length-1);
arr = arr.split(";");
let unique = [...new Set(arr)];
var nl = ""
for (var i = 1; i < unique.length ; i++) {
nl += unique[i] + ",";
}
jsonString = "[" + nl.slice(0, nl.length-1) + "]";
return jsonString;
}).catch(
(reason) => {
console.log('Handle rejected promise ('+reason+') here.');
});
}
function nodes_of_type(label_type, trace){
//--list all nodes of type
add_label();
const resultPromise = session.run(
'MATCH (n) WHERE n.label = $label RETURN n',
{label: label_type}
);
return resultPromise.then(result => {
session.close();
var jsonArray = result.records.map(record => {
const node = record.get(0);
return {
name: node.properties.name,
returns:{"kind":"nested","endpoint":trace +"&" + node.properties.name + "/links_from_node/"+node.properties.name},
trace:[node.properties.name]
} });
var any = {
name: "[any]",
returns:{"kind":"nested","endpoint":trace + "&[any]" +"/links_from_any_node/" + label_type},
trace:["[any]"]
};
jsonArray.push(any);
var jsonString = JSON.stringify(jsonArray);
return jsonString;
}).catch(
(reason) => {
console.log('Handle rejected promise ('+reason+') here.');
});
}
function links_from_any_node(label_type, traceString){
add_label();
return list_of_key_and_type(traceString).then(res => {
const key = res[0];
const type = res[1];
const resultPromise = session.run(
'MATCH (a {label: $label}) OPTIONAL MATCH (a {label:$label })-[r]-(b) return type(r), properties(a)',
{label: label_type}
);
return resultPromise.then(result => {
session.close();
var propertie_string = [];
for (var i = 0; i < key.length; i++) {
obj = {
name: key[i],
type: type[i]
};
propertie_string.push(obj);
}
var jsonString = "";
const prop_obj = {
name: "get_properties",
trace:[],
returns:{
kind:"primitive",
type: {name:"seq", params:[
{ name:"record",
fields: propertie_string
} ]},
endpoint:"/get_properties_of_node" }
}
jsonString += JSON.stringify(prop_obj) + ";";
const url = config.GRAPH_SERVICE + "/drWho/" + traceString + "/get_propertiescsv"
const explore_prop_obj = {
name: "explore_properties",
trace:[],
returns:{
kind:"nested",
endpoint: config.GALLERY_SERVICE + "/providers/data/upload/" + encodeURIComponent(url) }
}
jsonString += JSON.stringify(explore_prop_obj) + ";";
for (var i = 0; i < result.records.length; i++) {
const singleRecord = result.records[i];
const node = singleRecord.get(0);
if (node != null) {
const node_obj = {
name: node,
trace:[node],
returns:{"kind":"nested","endpoint":traceString + "&" + node + "/linked_from_node/"+ "any" + "/"+ node},
}
jsonString += JSON.stringify(node_obj) + ";";
}
}
var arr = jsonString.slice(0, jsonString.length-1);
arr = arr.split(";");
let unique = [...new Set(arr)];
var nl = "";
for (var i = 0; i < unique.length; i++) {
nl += unique[i] + ",";
}
jsonString = "[" + nl.slice(0, nl.length-1) + "]";
return jsonString;
}).catch(
(reason) => {
console.log('Handle rejected promise ('+reason+') here.');
});
});
}
function links_from_node(node_id, traceString){
//--list all relations linking node with with something
add_label();
return list_of_key_and_type(traceString).then(res => {
const key = res[0];
const type = res[1];
const resultPromise = session.run(
'MATCH (a {name: $name}) OPTIONAL MATCH (a {name:$name })-[r]-(b) return type(r), properties(a)',
{name: node_id}
);
return resultPromise.then(result => {
session.close();
var propertie_string = [];
for (var i = 0; i < Object.keys(result.records[0].get(1)).length; i++) {
obj = {
name: key[i],
type: type[i]
}
propertie_string.push(obj);
}
var jsonString = "";
const prop_obj = {
name: "get_properties",
trace:[],
returns:{
kind:"primitive",
type: {name:"seq", params:[
{ name:"record",
fields: propertie_string
} ]},
endpoint:"/get_properties_of_node" }
}
jsonString += JSON.stringify(prop_obj) + ";";
const url = config.GRAPH_SERVICE + "/drWho/" + traceString + "/get_propertiescsv"
const explore_prop_obj = {
name: "explore_properties",
trace:[],
returns:{
kind:"nested",
endpoint:config.GALLERY_SERVICE + "/providers/data/upload/" + encodeURIComponent(url) }
}
jsonString += JSON.stringify(explore_prop_obj) + ";";
for (var i = 0; i < result.records.length; i++) {
const singleRecord = result.records[i];
const node = singleRecord.get(0);
if (node != null) {
const node_obj = {
name: node,
trace:[node],
returns:{"kind":"nested","endpoint":traceString + "&" + node +"/linked_from_node/"+ node_id + "/"+ node},
}
jsonString += JSON.stringify(node_obj) + ";";
}
}
var arr = jsonString.slice(0, jsonString.length-1);
arr = arr.split(";");
let unique = [...new Set(arr)];
var nl = "";
for (var i = 0; i < unique.length; i++) {
nl += unique[i] + ",";
}
jsonString = "[" + nl.slice(0, nl.length-1) + "]";
return jsonString;
}).catch(
(reason) => {
console.log('Handle rejected promise ('+reason+') here.');
});
})
}
// linked from node marche pas a cause promise
function linked_any(link, trace){
add_label();
const resultPromise = session.run(
'MATCH (a)-[r]-(b) WHERE type(r) = $link RETURN b, a.label',
{link: link}
);
return resultPromise.then(result => {
session.close();
var jsonArray = result.records.map(record => {
const node = record.get(0);
return {
id: node.identity.toNumber(),
name: node.properties.name,
properties: node.properties,
trace:[node.properties.name],
returns:{"kind":"nested","endpoint":trace + "&"+ node.properties.name + "/links_from_node/"+node.properties.name},
} });
var any = {
name: "[any]",
returns:{"kind":"nested","endpoint":trace + "&"+ "[any]" + "/links_from_any_node/" + result.records[0].get(1)},
trace:["[any]"]
};
jsonArray.push(any);
var cache = {};
jsonArray = jsonArray.filter(function(elem,index,array){
return cache[elem.id]?0:cache[elem.id]=1;
});
var jsonString = JSON.stringify(jsonArray);
return jsonString;
}).catch(
(reason) => {
console.log('Handle rejected promise ('+reason+') here.');
});
}
function linked_from_node(name, link, trace){
//--list all nodes connected to by relation
add_label();
const resultPromise = session.run(
'MATCH (a)-[r]-(b) WHERE type(r) = $link and a.name = $name RETURN b, a.label',
{name: name, link: link}
);
return resultPromise.then(result => {
session.close();
var jsonArray = result.records.map(record => {
const node = record.get(0);
return {
id: node.identity.toNumber(),
name: node.properties.name,
properties: node.properties,
trace:[node.properties.name],
returns:{"kind":"nested","endpoint":trace + "&" + node.properties.name + "/links_from_node/"+node.properties.name},
} });
var any = {
name: "[any]",
returns:{"kind":"nested","endpoint":trace + "&" + "[any]" +"/links_from_any_node/" + result.records[0].get(1)},
trace:["[any]"]
};
jsonArray.push(any);
var cache = {};
jsonArray = jsonArray.filter(function(elem,index,array){
return cache[elem.id]?0:cache[elem.id]=1;
});
const jsonString = JSON.stringify(jsonArray);
return jsonString;
}).catch(
(reason) => {
console.log('Handle rejected promise ('+reason+') here.');
});
}
function get_properties(traceString){
return list_of_key_and_type(traceString).then(res => {
const key = res[0];
var [query, args] = constructDataQuery(traceString);
const resultPromise = session.run(query, args);
return resultPromise.then(result => {
session.close();
var key_label = [];
for (var i = 0; i < key.length; i++) {
key_label.push(key[i].split('.'));
}
var key_label_obj = [];
for (var i = 0; i < key.length; i++) {
var temp_obj = {
'node' : key_label[i][0],
'key' : key_label[i][1],
};
key_label_obj.push(temp_obj);
}
var grouped = _.mapObject(_.groupBy(key_label_obj, 'node'),
clist => clist.map(a => _.omit(a, 'node')));
key_label = [];
for (var i = 0; i < Object.keys(grouped).length; i++) {
var temp_arr = [];
for (var j = 0; j < grouped[Object.keys(grouped)[i]].length; j++) {
temp_arr.push(grouped[Object.keys(grouped)[i]][j].key);
}
key_label.push(temp_arr);
}
var jsonArray = result.records.map(record => {
var obj = {};
for (var j = 0; j < record.keys.length; j++) {
for (var n in key_label[j]) {
var prop_name_list = Object.keys(record._fields[j].properties);
var arr = key_label[j];
for(var i = 0, len = arr.length; i < len; i++) {
arr[i] = arr[i].replace(record.keys[j].replace(/n/g, "node") + ".", '');
}
if (prop_name_list.includes(arr[n])) {
if (typeof(record._fields[j].properties[arr[n]]) == typeof({})) {
obj[j+"-"+ key_label[j][n]] = record._fields[j].properties[arr[n]].toNumber();
} else {
obj[j+"-"+key_label[j][n]] = record._fields[j].properties[arr[n]];
}
} else {
obj[j+"-"+key_label[j][n]] = 0 ;
}
}
}
return obj;
});
//fs.writeFile("test2.json", JSON.stringify(jsonArray), function(err) { });
return [JSON.stringify(jsonArray), Object.keys(jsonArray[0])];
;
});
});
}
exports.linked_any = linked_any;
exports.links_from_any_node = links_from_any_node;
exports.get_properties = get_properties;
exports.All_nodes = All_nodes;
exports.nodes_of_type = nodes_of_type;
exports.links_from_node = links_from_node;
exports.linked_from_node = linked_from_node;