diff --git a/cmd/query-service/main.go b/cmd/query-service/main.go index 370e4e114914282b5a598e76833661721c26eee3..38f481b94466f3e29a03d8d9783bfb5a45a25a29 100644 --- a/cmd/query-service/main.go +++ b/cmd/query-service/main.go @@ -2,16 +2,19 @@ package main import ( "fmt" + "io/ioutil" "query-service/internal/aql" - "query-service/internal/consumer" "query-service/internal/errorhandler" + "query-service/internal/request" ) func main() { - exchangeID := "query-requests" - routingKey := "aql-user-request" - consumer.StartConsuming(onMessageReceived, exchangeID, routingKey) + // exchangeID := "query-requests" + // routingKey := "aql-user-request" + // consumer.StartConsuming(onMessageReceived, exchangeID, routingKey) + jsonQuery, _ := ioutil.ReadFile("./internal/data/jsonQuery.json") + onMessageReceived(&jsonQuery) } func onMessageReceived(jsonMsg *[]byte) { @@ -21,11 +24,12 @@ func onMessageReceived(jsonMsg *[]byte) { aqlQuery, err := aql.ConvertJSONToAQL(jsonMsg) errorhandler.FailWithError(err, "failed to parse incoming msg to AQL") // TODO: don't panic on error, send error message to client instead - fmt.Println(*aqlQuery) + fmt.Println("Query: " + *aqlQuery) + // TODO : Generate databse seperatly // execute and retrieve result - // convert result to general (node-link (?)) format + request.SendAQLQuery(*aqlQuery) // publish converted result } diff --git a/go.mod b/go.mod index 99a30d49101c9b0b2d6ee79779fad316cd013bc4..bb1badf6e9479156ca9a10b5b91ec929540db81d 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module query-service go 1.15 require ( + github.com/arangodb/go-driver v0.0.0-20210304082257-d7e0ea043b7f // indirect github.com/streadway/amqp v1.0.0 github.com/stretchr/testify v1.7.0 // indirect github.com/thijsheijden/alice v0.1.5 diff --git a/go.sum b/go.sum index 385ffb66da0c48ce4f8170578fc56dc04f336b06..33dad8157455b0c1ae5e7f3863e91f491258ff9e 100644 --- a/go.sum +++ b/go.sum @@ -1,14 +1,52 @@ +github.com/arangodb/go-driver v0.0.0-20210304082257-d7e0ea043b7f h1:MEdxM6EhSFo2ecumBN0CC6s1zMWDpNvcmDIHEfMvl18= +github.com/arangodb/go-driver v0.0.0-20210304082257-d7e0ea043b7f/go.mod h1:3NUekcRLpgheFIGEwcOvxilEW73MV1queNKW58k7sdc= +github.com/arangodb/go-velocypack v0.0.0-20200318135517-5af53c29c67e h1:Xg+hGrY2LcQBbxd0ZFdbGSyRKTYMZCfBbw/pMJFOk1g= +github.com/arangodb/go-velocypack v0.0.0-20200318135517-5af53c29c67e/go.mod h1:mq7Shfa/CaixoDxiyAAc5jZ6CVBAyPaNQCGS7mkj4Ho= +github.com/coreos/go-iptables v0.4.3/go.mod h1:/mVI274lEDI2ns62jHCDnCyBF9Iwsmekav8Dbxlm1MU= +github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dchest/uniuri v0.0.0-20160212164326-8902c56451e9/go.mod h1:GgB8SF9nRG+GqaDtLcwJZsQFhcogVCJ79j4EdT0c2V4= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/google/addlicense v0.0.0-20200817051935-6f4cd4aacc89/go.mod h1:EMjYTRimagHs1FwlIqKyX3wAM0u3rA+McvlIIWmSamA= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= +github.com/rs/zerolog v1.19.0/go.mod h1:IzD0RJ65iWH0w97OQQebJEvTZYvsCUm9WVLWBQrJRjo= github.com/streadway/amqp v1.0.0 h1:kuuDrUJFZL1QYL9hUNuCxNObNzB0bV/ZG5jV3RWAQgo= github.com/streadway/amqp v1.0.0/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/thijsheijden/alice v0.1.5 h1:kOZHhLGSHMja77I/Wvd19M7nvxgEWVIJ4vk5antCKdQ= github.com/thijsheijden/alice v0.1.5/go.mod h1:UypS/UTucbp+fmG1JtmXGCKPnGKimAC9AgmJ8iGoLNo= +github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200818005847-188abfa75333/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/aql/aql.go b/internal/aql/aql.go index 3d9c0f8da4f0f1c713b19655ff69037374631ac0..f4a3bbcbc8fe2ac686c0b87cc58006c0c1d364db 100644 --- a/internal/aql/aql.go +++ b/internal/aql/aql.go @@ -117,8 +117,10 @@ func createNodeQuery(node *nodeStruct, name string) *string { returnStatement := "\n\tRETURN x\n)" + forLoop := fmt.Sprintf("\nFOR n in %s", name) + "\nRETURN n" + // Concatenate all the statements - result := letStatement + forStatement + filter + returnStatement + result := letStatement + forStatement + filter + returnStatement + forLoop return &result } diff --git a/internal/data/jsonQuery.json b/internal/data/jsonQuery.json new file mode 100644 index 0000000000000000000000000000000000000000..ec66b21614d12cb2170e34780cfa669cf0c16d1e --- /dev/null +++ b/internal/data/jsonQuery.json @@ -0,0 +1,17 @@ +{ + "Return": { + "Entities": [0], + "Relations": [] + }, + "Nodes": [ + { + "NodeType": "airports", + "Constraints": + { + "country": { "Value": "USA", "DataType": "text", "MatchType": "exact" }, + "vip": { "Value": "true", "DataType": "bool", "MatchType": "exact" } + } + } + ], + "Relations": [ ] +} \ No newline at end of file diff --git a/request.go b/internal/request/request.go similarity index 62% rename from request.go rename to internal/request/request.go index d225cb171a49b042a8f27ef2d861dcb989538685..18790603282b9e9f79104920ca529239c747254f 100644 --- a/request.go +++ b/internal/request/request.go @@ -1,4 +1,4 @@ -package main +package request import ( "context" @@ -21,19 +21,20 @@ type GeneralFormat map[string]interface{} //map[1 , 2 , 3 map [ .. ]] -func main() { +func SendAQLQuery(AQLQuery string) { + var queryResult []Document conn, err := http.NewConnection(http.ConnectionConfig{ Endpoints: []string{"http://localhost:8529"}, }) if err != nil { - fmt.Printf("auwa das tut weh") // Handle error + fmt.Printf("Could not connect to the host") // Handle error } c, err := driver.NewClient(driver.ClientConfig{ Connection: conn, - Authentication: driver.BasicAuthentication("root", "openSesame"), + Authentication: driver.BasicAuthentication("root", "GRYVKVN5M977QNdT"), }) if err != nil { - fmt.Printf("doet het niet") // Handle error + fmt.Printf("Could not log in to the ArangoDB") // Handle error } ctx := context.Background() @@ -42,11 +43,19 @@ func main() { // handle error } - query := "FOR d IN Characters LIMIT 10 RETURN d" - cursor, err := db.Query(ctx, query, nil) - + //fmt.Println(AQLQuery) + // query := ` + // LET n0 = ( + // FOR x IN airports + // FILTER x.country == 'USA' + // RETURN x + // ) + // FOR n in n0 + // RETURN n + // ` + cursor, err := db.Query(ctx, AQLQuery, nil) if err != nil { - fmt.Printf("shalom") // handle error + fmt.Printf("Invalid query") // handle error } defer cursor.Close() for { @@ -58,22 +67,26 @@ func main() { // handle other errors } //fmt.Printf("%T\n", doc) - formatToJSON(doc) + queryResult = append(queryResult, formatToJSON(doc)) } + writeJSON(queryResult) } -func formatToJSON(doc Document) { +func formatToJSON(doc Document) Document { //b, err := json.Marshal(doc) //if err != nil { //handle error //} - data := parseData(doc) - fmt.Println(data) - file, _ := json.MarshalIndent(data, "", " ") + return parseData(doc) + //fmt.Println(data) +} + +func writeJSON(queryResult []Document) { + file, _ := json.MarshalIndent(queryResult, "", " ") - _ = ioutil.WriteFile("test.json", file, 0644) + _ = ioutil.WriteFile("result.json", file, 0644) } func parseData(doc Document) Document { diff --git a/result.json b/result.json new file mode 100644 index 0000000000000000000000000000000000000000..5b3220a475e637cd9797fff2351081948b141102 --- /dev/null +++ b/result.json @@ -0,0 +1,100 @@ +[ + { + "_id": "airports/AMA", + "_key": "AMA", + "_rev": "_c_FqKK2--d", + "attributes": { + "city": "Amarillo", + "country": "USA", + "lat": 35.2193725, + "long": -101.7059272, + "name": "Amarillo International", + "state": "TX", + "vip": true + } + }, + { + "_id": "airports/ATL", + "_key": "ATL", + "_rev": "_c_FqKK2-_7", + "attributes": { + "city": "Atlanta", + "country": "USA", + "lat": 33.64044444, + "long": -84.42694444, + "name": "William B Hartsfield-Atlanta Intl", + "state": "GA", + "vip": true + } + }, + { + "_id": "airports/DFW", + "_key": "DFW", + "_rev": "_c_FqKL--Cc", + "attributes": { + "city": "Dallas-Fort Worth", + "country": "USA", + "lat": 32.89595056, + "long": -97.0372, + "name": "Dallas-Fort Worth International", + "state": "TX", + "vip": true + } + }, + { + "_id": "airports/JFK", + "_key": "JFK", + "_rev": "_c_FqKLa--f", + "attributes": { + "city": "New York", + "country": "USA", + "lat": 40.63975111, + "long": -73.77892556, + "name": "John F Kennedy Intl", + "state": "NY", + "vip": true + } + }, + { + "_id": "airports/LAX", + "_key": "LAX", + "_rev": "_c_FqKLe-_g", + "attributes": { + "city": "Los Angeles", + "country": "USA", + "lat": 33.94253611, + "long": -118.4080744, + "name": "Los Angeles International", + "state": "CA", + "vip": true + } + }, + { + "_id": "airports/ORD", + "_key": "ORD", + "_rev": "_c_FqKLu-BM", + "attributes": { + "city": "Chicago", + "country": "USA", + "lat": 41.979595, + "long": -87.90446417, + "name": "Chicago O'Hare International", + "state": "IL", + "vip": true + } + }, + { + "_id": "airports/SFO", + "_key": "SFO", + "_rev": "_c_FqKM--_8", + "attributes": { + "city": "San Francisco", + "country": "USA", + "lat": 37.61900194, + "long": -122.3748433, + "name": "San Francisco International", + "state": "CA", + "vip": true + } + } +] \ No newline at end of file diff --git a/test.json b/test.json deleted file mode 100644 index 69b4fa18c6d399b382c4b81da9cdba5be5bfb9ca..0000000000000000000000000000000000000000 --- a/test.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "_id": "Characters/JonSnow", - "_key": "JonSnow", - "_rev": "_c-44iYG--C", - "attributes": { - "age": 16, - "alive": true, - "name": "Jon", - "surname": "Snow", - "traits": [ - "A", - "B", - "C", - "F" - ] - } -}