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 a12042f7 [Ageviewer_go] Retrieve the graphs name when connecting to 
DB. (#962)
a12042f7 is described below

commit a12042f75a26df82d29962d98d3304bdd018e375
Author: Kamlesh Kumar <[email protected]>
AuthorDate: Fri Jun 9 12:15:50 2023 +0500

    [Ageviewer_go] Retrieve the graphs name when connecting to DB. (#962)
    
    * Function to disconnect database add to backend
    
    * Added functionalities to read query response dynamically
    
    * Meatdata functionalities changes to retrieve data of all graphs, rather a 
single graph
    
    * retrieve all graph names and first graph names when user connectes to db
---
 backend/APIfunctions.js      |  4 ++--
 backend/db/age.go            |  2 ++
 backend/main.go              |  4 ++--
 backend/models/connection.go |  5 ++---
 backend/models/graph.go      | 25 +++++++++++++++++++++++++
 backend/routes/connect.go    | 14 +++++++++++---
 6 files changed, 44 insertions(+), 10 deletions(-)

diff --git a/backend/APIfunctions.js b/backend/APIfunctions.js
index d96bc2d7..cb6af52f 100644
--- a/backend/APIfunctions.js
+++ b/backend/APIfunctions.js
@@ -10,11 +10,11 @@ const rl = readline.createInterface({
 
 // Test data which will come from the frontend
 const conn = {
-  port: "5432",
+  port: 5432,
   host: "localhost",
   password: "Welcome@1",
   user: "kamleshk",
-  dbname: "demodb",
+  dbname: "testdb",
   ssl: "disable",
   graph_init: true,
   version: 11,
diff --git a/backend/db/age.go b/backend/db/age.go
index f826bbd6..aff51402 100644
--- a/backend/db/age.go
+++ b/backend/db/age.go
@@ -34,6 +34,8 @@ INNER JOIN ag_label as label
 ON label.name = q1.label
 AND label.graph = g.graphid;`
 
+const GET_ALL_GRAPHS = `SELECT DISTINCT(split_part(relation::text, '.', 1)) as 
graph_name FROM ag_catalog.ag_label;`
+
 const ANALYZE = `ANALYZE;`
 
 const PG_VERSION = `show server_version_num;`
diff --git a/backend/main.go b/backend/main.go
index 07614104..397b47fd 100644
--- a/backend/main.go
+++ b/backend/main.go
@@ -18,10 +18,10 @@ func main() {
        app.Use(middleware.CORSWithConfig(middleware.CORSConfig{
                AllowOrigins: []string{"*"},
                AllowHeaders: []string{echo.HeaderOrigin, 
echo.HeaderContentType, echo.HeaderAccept},
-         }))
+       }))
        gob.Register(models.Connection{})
        app.Use(session.UserSessions())
-       
+
        app.POST("/connect", routes.ConnectToDb)
        app.GET("/status", routes.StatusDB)
        app.POST("/disconnect", routes.DisconnectFromDb)
diff --git a/backend/models/connection.go b/backend/models/connection.go
index 3f2617a1..e8220446 100644
--- a/backend/models/connection.go
+++ b/backend/models/connection.go
@@ -18,9 +18,8 @@ type Connection struct {
        SSL       string `default:"require"`
        GraphInit bool
        Version   int
-       Graphs  []string
-       Graph    string
-
+       Graphs    []string
+       Graph     string
 }
 
 /*
diff --git a/backend/models/graph.go b/backend/models/graph.go
index 545bb182..07ed3357 100644
--- a/backend/models/graph.go
+++ b/backend/models/graph.go
@@ -33,3 +33,28 @@ func GetMetaData(conn *sql.DB, v int, dataChan chan<- 
*sql.Rows, errorChan chan<
        errorChan <- err
        dataChan <- data
 }
+
+// GetGraphNamesFromDB retrieves all unique graph names from the database
+// and returns a slice of graph names, the first graph name, and an error (if 
any).
+// The first graph name will be an empty string if there are no graph names.
+func GetGraphNamesFromDB(conn *sql.DB) ([]string, string, error) {
+       data, err := conn.Query(db.GET_ALL_GRAPHS)
+       if err != nil {
+               return nil, "", err
+       }
+       defer data.Close()
+       graphNames := make([]string, 0)
+       for data.Next() {
+               var graphName string
+               err := data.Scan(&graphName)
+               if err != nil {
+                       return nil, "", err
+               }
+               graphNames = append(graphNames, graphName)
+       }
+       firstGraphName := ""
+       if len(graphNames) > 0 {
+               firstGraphName = graphNames[0]
+       }
+       return graphNames, firstGraphName, nil
+}
diff --git a/backend/routes/connect.go b/backend/routes/connect.go
index bac2e226..020c52d1 100644
--- a/backend/routes/connect.go
+++ b/backend/routes/connect.go
@@ -12,9 +12,8 @@ import (
 /*
 This function takes user data from the request body, establishes a database 
connection, saves the
 connection information in the session, and it returns a JSON response with 
fields
-containing the host, postgres version, port, database, user, password, list of 
graphs 
+containing the host, postgres version, port, database, user, password, list of 
graphs
 and current graph. It handles errors related to invalid data, connection 
establishment, and session saving.
-
 */
 func ConnectToDb(c echo.Context) error {
        udata, err := models.FromRequestBody(c)
@@ -32,13 +31,22 @@ func ConnectToDb(c echo.Context) error {
        }
        defer db.Close()
        sess := c.Get("database").(*sessions.Session)
-       udata.Graphs = []string{}
+
        sess.Values["db"] = udata
 
+       // Call GetGraphNamesFromDB from the models package to retrieve
+       // the graph names and the first graph name from the database.
+       graphNames, firstGraphName, err := models.GetGraphNamesFromDB(db)
+       if err != nil {
+               return echo.NewHTTPError(400, fmt.Sprintf("could not retrieve 
graph names due to %s", err.Error()))
+       }
+
        err = sess.Save(c.Request(), c.Response().Writer)
        if err != nil {
                return echo.NewHTTPError(400, "could not save session")
        }
+       udata.Graphs = graphNames
+       udata.Graph = firstGraphName
 
        return c.JSON(200, udata)
 }

Reply via email to