You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@age.apache.org by ak...@apache.org on 2023/07/25 13:24:20 UTC

[age] branch ageviewer_go updated: Output formate change as required in the frontend (#1011)

This is an automated email from the ASF dual-hosted git repository.

ako pushed a commit to branch ageviewer_go
in repository https://gitbox.apache.org/repos/asf/age.git


The following commit(s) were added to refs/heads/ageviewer_go by this push:
     new 65797a5e Output formate change as required in the frontend (#1011)
65797a5e is described below

commit 65797a5e006e04cca3458725219b35313ffefc78
Author: Kamlesh Kumar <95...@users.noreply.github.com>
AuthorDate: Tue Jul 25 18:24:15 2023 +0500

    Output formate change as required in the frontend (#1011)
---
 backend/APIfunctions.js          |  8 +++--
 backend/miscellaneous/cypher.go  | 66 ++++++++++++++++++++++++++++++++++------
 backend/miscellaneous/results.go |  9 ++++--
 backend/routes/query.go          |  2 +-
 4 files changed, 69 insertions(+), 16 deletions(-)

diff --git a/backend/APIfunctions.js b/backend/APIfunctions.js
index 34ef1c0e..13b05440 100644
--- a/backend/APIfunctions.js
+++ b/backend/APIfunctions.js
@@ -32,7 +32,6 @@ function connect() {
     body: JSON.stringify(conn),
   })
     .then((response) => {
-      s;
 
       // Store the cookies from the response
       cookies = response.headers.raw()['set-cookie'];
@@ -59,6 +58,7 @@ function queryMetadata() {
       return response.json();
     })
     .then((data) => {
+      console.log(data); 
       // visualize the data in formatted ways
     })
     .catch((error) => {
@@ -69,7 +69,7 @@ function queryMetadata() {
 function query() {
   const payload = {
     query:
-      "SELECT * FROM cypher('demo_graph', $$ MATCH (v) RETURN v $$) as (v agtype);",
+      "SELECT * from cypher('test_graph', $$ MATCH (V)-[R]-(V2) RETURN V,R,V2 $$) as (V agtype, R agtype, V2 agtype);      ",
   };
 
   fetch('http://localhost:8080/query', {
@@ -83,7 +83,9 @@ function query() {
     .then((response) => {
       return response.json();
     })
-    .then((data) => {})
+    .then((data) => {
+      console.log("Query result: ", JSON.stringify(data, null, 2));
+    })
     .catch((error) => {
       console.error(error);
     });
diff --git a/backend/miscellaneous/cypher.go b/backend/miscellaneous/cypher.go
index 2bad06bc..fe2eb27c 100644
--- a/backend/miscellaneous/cypher.go
+++ b/backend/miscellaneous/cypher.go
@@ -2,14 +2,36 @@ package miscellaneous
 
 import (
 	"database/sql"
+	"encoding/json"
+	"fmt"
+	"strings"
 )
 
+type Vertex struct {
+	ID         int64                  `json:"id"`
+	Label      string                 `json:"label"`
+	Properties map[string]interface{} `json:"properties"`
+}
+
+type Edge struct {
+	ID         int64                  `json:"id"`
+	Label      string                 `json:"label"`
+	EndID      int64                  `json:"end_id"`
+	StartID    int64                  `json:"start_id"`
+	Properties map[string]interface{} `json:"properties"`
+}
+
+type Row struct {
+	V  Vertex `json:"v"`
+	R  Edge   `json:"r"`
+	V2 Vertex `json:"v2"`
+}
+
 func CypherCall(db *sql.DB, q map[string]string, c chan<- ChannelResults) {
 	data := ChannelResults{}
-	results := []interface{}{}
+	var results []map[string]interface{}
 	rows, err := db.Query(q["query"])
 	if err != nil {
-		data.Err = err
 		c <- data
 		return
 	}
@@ -21,20 +43,46 @@ func CypherCall(db *sql.DB, q map[string]string, c chan<- ChannelResults) {
 		c <- data
 		return
 	}
+	columns := make([]string, len(cols))
+	for i, col := range cols {
+		columns[i] = col.Name()
+	}
+	data.Columns = columns
+	data.Command = "SELECT"
 
 	for rows.Next() {
-		rawData := make([]any, len(cols))
+		rawResult := make([]interface{}, len(cols))
+		result := make(map[string]interface{})
 		for i := 0; i < len(cols); i++ {
-			rawData[i] = new(string)
+			rawResult[i] = new(string)
 		}
-
-		err := rows.Scan(rawData...)
+		err := rows.Scan(rawResult...)
 		if err != nil {
 			data.Err = err
-			break
+			c <- data
+			return
+		}
+		for i, raw := range rawResult {
+			str := *raw.(*string)
+
+			// split the string at "::" and only keep the first part (the JSON)
+			parts := strings.SplitN(str, "::", 2)
+			jsonStr := parts[0]
+
+			var val interface{}
+			err := json.Unmarshal([]byte(jsonStr), &val)
+			if err != nil {
+				fmt.Println("Failed to unmarshal value:", err)
+				c <- data
+				return
+			}
+
+			result[columns[i]] = val
 		}
-		results = append(results, rawData)
+
+		results = append(results, result)
 	}
-	data.Res = results
+	data.Rows = results
+	data.RowCount = len(results)
 	c <- data
 }
diff --git a/backend/miscellaneous/results.go b/backend/miscellaneous/results.go
index 963fd404..d8889f18 100644
--- a/backend/miscellaneous/results.go
+++ b/backend/miscellaneous/results.go
@@ -1,7 +1,10 @@
 package miscellaneous
 
 type ChannelResults struct {
-	Res any
-	Msg map[string]string
-	Err error
+	Rows     []map[string]interface{} `json:"rows"`
+	Columns  []string                 `json:"columns"`
+	Err      error                    `json:"error,omitempty"`
+	Msg      map[string]string        `json:"message,omitempty"`
+	RowCount int                      `json:"rowCount"`
+	Command  string                   `json:"command"`
 }
diff --git a/backend/routes/query.go b/backend/routes/query.go
index c5da65ff..a76f92e3 100644
--- a/backend/routes/query.go
+++ b/backend/routes/query.go
@@ -86,7 +86,7 @@ func Cypher(ctx echo.Context) error {
 	m.CypherCall(conn, q, c)
 	data := <-c
 	err = data.Err
-	res := data.Res
+	res := data
 	msg := data.Msg
 	if err != nil {
 		return echo.NewHTTPError(400, fmt.Sprintf("unable to process query. error: %s", err.Error()))