@@ -14,15 +14,17 @@ import axios from "axios";
14
14
import { aws4Interceptor } from "aws4-axios" ;
15
15
import { fromNodeProviderChain } from "@aws-sdk/credential-providers" ;
16
16
import { NeptunedataClient , ExecuteOpenCypherQueryCommand } from "@aws-sdk/client-neptunedata" ;
17
+ import { loggerLog } from "./logger.js" ;
17
18
18
19
let HOST = '' ;
19
20
let PORT = 8182 ;
20
21
let REGION = ''
21
22
let SAMPLE = 5000 ;
22
- let VERBOSE = false ;
23
+ let VERBOSE = false ;
24
+ let NEPTUNE_TYPE = 'neptune-db' ;
23
25
let language = 'openCypher' ;
24
26
let useSDK = false ;
25
-
27
+ let msg = '' ;
26
28
27
29
async function getAWSCredentials ( ) {
28
30
const credentialProvider = fromNodeProviderChain ( ) ;
@@ -31,7 +33,7 @@ async function getAWSCredentials() {
31
33
const interceptor = aws4Interceptor ( {
32
34
options : {
33
35
region : REGION ,
34
- service : "neptune-db" ,
36
+ service : NEPTUNE_TYPE ,
35
37
} ,
36
38
credentials : cred
37
39
} ) ;
@@ -55,6 +57,7 @@ function consoleOut(text) {
55
57
if ( VERBOSE ) {
56
58
console . log ( text ) ;
57
59
}
60
+ loggerLog ( text ) ;
58
61
}
59
62
60
63
@@ -67,11 +70,18 @@ async function queryNeptune(q) {
67
70
const response = await axios . post ( `https://${ HOST } :${ PORT } /${ language } ` , `query=${ encodeURIComponent ( q ) } ` ) ;
68
71
return response . data ;
69
72
} catch ( error ) {
70
- console . error ( "Http query request failed: " , error . message ) ;
71
- consoleOut ( "Trying with the AWS SDK" ) ;
72
- const response = await queryNeptuneSDK ( q ) ;
73
- useSDK = true ;
74
- return response ;
73
+ msg = `Http query request failed: ${ error . message } ` ;
74
+ consoleOut . error ( msg ) ;
75
+ loggerLog ( msg ) ;
76
+
77
+ if ( NEPTUNE_TYPE == 'neptune-db' ) {
78
+ consoleOut ( "Trying with the AWS SDK" ) ;
79
+ const response = await queryNeptuneSDK ( q ) ;
80
+ useSDK = true ;
81
+ return response ;
82
+ }
83
+
84
+ throw new Error ( 'AWS SDK for Neptune Analytics is not available, yet.' ) ;
75
85
}
76
86
}
77
87
}
@@ -91,7 +101,9 @@ async function queryNeptuneSDK(q) {
91
101
return response ;
92
102
93
103
} catch ( error ) {
94
- console . error ( "SDK query request failed: " , error . message ) ;
104
+ msg = `SDK query request failed: ${ error . message } ` ;
105
+ consoleOut . error ( msg ) ;
106
+ loggerLog ( msg ) ;
95
107
process . exit ( 1 ) ;
96
108
}
97
109
}
@@ -100,10 +112,11 @@ async function queryNeptuneSDK(q) {
100
112
async function getNodesNames ( ) {
101
113
let query = `MATCH (a) RETURN labels(a), count(a)` ;
102
114
let response = await queryNeptune ( query ) ;
115
+ loggerLog ( 'Getting nodes names' ) ;
103
116
104
117
try {
105
118
response . results . forEach ( result => {
106
- schema . nodeStructures . push ( { label : result [ 'labels(a)' ] [ 0 ] , properties : [ ] } ) ;
119
+ schema . nodeStructures . push ( { label : result [ 'labels(a)' ] [ 0 ] , properties : [ ] } ) ;
107
120
consoleOut ( ' Found Node: ' + yellow ( result [ 'labels(a)' ] [ 0 ] ) ) ;
108
121
} ) ;
109
122
}
@@ -117,6 +130,7 @@ async function getNodesNames() {
117
130
async function getEdgesNames ( ) {
118
131
let query = `MATCH ()-[e]->() RETURN type(e), count(e)` ;
119
132
let response = await queryNeptune ( query ) ;
133
+ loggerLog ( 'Getting edges names' ) ;
120
134
121
135
try {
122
136
response . results . forEach ( result => {
@@ -132,8 +146,9 @@ async function getEdgesNames() {
132
146
}
133
147
134
148
135
- async function findFromAndToLabels ( edgeStructure ) {
136
- let query = `MATCH (from)-[r:${ edgeStructure . label } ]->(to) RETURN DISTINCT labels(from) as fromLabel, labels(to) as toLabel` ;
149
+ async function checkEdgeDirection ( direction ) {
150
+ let query = `MATCH (from:${ direction . from } )-[r:${ direction . edge . label } ]->(to:${ direction . to } ) RETURN r as edge LIMIT 1` ;
151
+ loggerLog ( `Checking edge direction: ${ query } ` ) ;
137
152
let response = await queryNeptune ( query ) ;
138
153
for ( let result of response . results ) {
139
154
for ( let fromLabel of result . fromLabel ) {
@@ -190,7 +205,8 @@ function addUpdateEdgeProperty(edgeName, name, value) {
190
205
191
206
192
207
async function getEdgeProperties ( edge ) {
193
- let query = `MATCH ()-[n:${ edge . label } ]->() RETURN properties(n) as properties LIMIT ${ SAMPLE } ` ;
208
+ let query = `MATCH ()-[n:${ edge . label } ]->() RETURN properties(n) as properties LIMIT ${ SAMPLE } ` ;
209
+ loggerLog ( `Getting properties for edge: ${ query } ` ) ;
194
210
try {
195
211
let response = await queryNeptune ( query ) ;
196
212
let result = response . results ;
@@ -214,7 +230,8 @@ async function getEdgesProperties() {
214
230
215
231
216
232
async function getNodeProperties ( node ) {
217
- let query = `MATCH (n:${ node . label } ) RETURN properties(n) as properties LIMIT ${ SAMPLE } ` ;
233
+ let query = `MATCH (n:${ node . label } ) RETURN properties(n) as properties LIMIT ${ SAMPLE } ` ;
234
+ loggerLog ( `Getting properties for node: ${ query } ` ) ;
218
235
try {
219
236
let response = await queryNeptune ( query ) ;
220
237
let result = response . results ;
@@ -238,10 +255,12 @@ async function getNodesProperties() {
238
255
239
256
240
257
async function checkEdgeDirectionCardinality ( d ) {
241
- let queryFrom = `MATCH (from:${ d . from } )-[r:${ d . edge . label } ]->(to:${ d . to } ) WITH to, count(from) as rels WHERE rels > 1 RETURN rels LIMIT 1` ;
258
+ let queryFrom = `MATCH (from:${ d . from } )-[r:${ d . edge . label } ]->(to:${ d . to } ) WITH to, count(from) as rels WHERE rels > 1 RETURN rels LIMIT 1` ;
259
+ loggerLog ( `Checking edge direction cardinality: ${ queryFrom } ` ) ;
242
260
let responseFrom = await queryNeptune ( queryFrom ) ;
243
261
let resultFrom = responseFrom . results [ 0 ] ;
244
- let queryTo = `MATCH (from:${ d . from } )-[r:${ d . edge . label } ]->(to:${ d . to } ) WITH from, count(to) as rels WHERE rels > 1 RETURN rels LIMIT 1` ;
262
+ let queryTo = `MATCH (from:${ d . from } )-[r:${ d . edge . label } ]->(to:${ d . to } ) WITH from, count(to) as rels WHERE rels > 1 RETURN rels LIMIT 1` ;
263
+ loggerLog ( `Checking edge direction cardinality: ${ queryTo } ` ) ;
245
264
let responseTo = await queryNeptune ( queryTo ) ;
246
265
let resultTo = responseTo . results [ 0 ] ;
247
266
let c = '' ;
@@ -277,11 +296,12 @@ async function getEdgesDirectionsCardinality() {
277
296
}
278
297
279
298
280
- function setGetNeptuneSchemaParameters ( host , port , region , verbose = false ) {
299
+ function setGetNeptuneSchemaParameters ( host , port , region , verbose = false , neptuneType ) {
281
300
HOST = host ;
282
301
PORT = port ;
283
302
REGION = region ;
284
303
VERBOSE = verbose ;
304
+ NEPTUNE_TYPE = neptuneType ;
285
305
}
286
306
287
307
0 commit comments