From 155a06a5dff6fd268cf028511db6a498a2196cad Mon Sep 17 00:00:00 2001 From: Nithunikzz Date: Mon, 15 Apr 2024 16:06:02 +0530 Subject: [PATCH] fix --- agent/container/pkg/handler/api_handler.go | 4 +- graphqlserver/graph/utils.go | 186 ++++++++++----------- 2 files changed, 94 insertions(+), 96 deletions(-) diff --git a/agent/container/pkg/handler/api_handler.go b/agent/container/pkg/handler/api_handler.go index 6efee78d..63ecb920 100755 --- a/agent/container/pkg/handler/api_handler.go +++ b/agent/container/pkg/handler/api_handler.go @@ -42,7 +42,7 @@ func (ah *APIHandler) BindRequest(r *gin.Engine) { apiGroup := r.Group("/") { - apiGroup.GET("/api-docs", ah.GetApiDocs) + apiGroup.GET("/api-docs", ah.GetAPIDocs) apiGroup.GET("/status", ah.GetStatus) apiGroup.POST("/event/docker/hub", ah.PostEventDockerHub) apiGroup.POST("/event/azure/container", ah.PostEventAzureContainer) @@ -54,7 +54,7 @@ func (ah *APIHandler) BindRequest(r *gin.Engine) { // GetApiDocs serves the Swagger API documentation generated from the OpenAPI YAML file. // It responds with a JSON representation of the API's endpoints, parameters, responses, and other details. // This endpoint can be used by tools like Swagger UI to provide interactive documentation for the API. -func (ah *APIHandler) GetApiDocs(c *gin.Context) { +func (ah *APIHandler) GetAPIDocs(c *gin.Context) { swagger, err := api.GetSwagger() if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) diff --git a/graphqlserver/graph/utils.go b/graphqlserver/graph/utils.go index dd7bb02a..72f406d4 100644 --- a/graphqlserver/graph/utils.go +++ b/graphqlserver/graph/utils.go @@ -2,10 +2,7 @@ package graph import ( "context" - "database/sql" "fmt" - - "github.com/intelops/kubviz/graphqlserver/graph/model" ) func (r *Resolver) fetchClustersFromDatabase(ctx context.Context) ([]string, error) { @@ -64,95 +61,96 @@ func (r *Resolver) fetchNamespacesFromDatabase(ctx context.Context, clusterName return namespaces, nil } -func (r *Resolver) fetchOutdatedImages(ctx context.Context, namespace string) ([]*model.OutdatedImage, error) { - if r.DB == nil { - return nil, fmt.Errorf("database connection is not initialized") - } - query := `SELECT ClusterName, Namespace, Pod, CurrentImage, CurrentTag, LatestVersion, VersionsBehind, EventTime FROM outdated_images WHERE Namespace = ?` - - rows, err := r.DB.QueryContext(ctx, query, namespace) - if err != nil { - if err == sql.ErrNoRows { - return []*model.OutdatedImage{}, nil - } - return nil, fmt.Errorf("error executing query: %v", err) - } - defer rows.Close() - - var outdatedImages []*model.OutdatedImage - for rows.Next() { - var oi model.OutdatedImage - if err := rows.Scan(&oi.ClusterName, &oi.Namespace, &oi.Pod, &oi.CurrentImage, &oi.CurrentTag, &oi.LatestVersion, &oi.VersionsBehind, &oi.EventTime); err != nil { - return nil, fmt.Errorf("error scanning row: %v", err) - } - outdatedImages = append(outdatedImages, &oi) - } - - if err := rows.Err(); err != nil { - return nil, fmt.Errorf("error iterating rows: %v", err) - } - - return outdatedImages, nil -} -func (r *Resolver) fetchKubeScores(ctx context.Context, namespace string) ([]*model.KubeScore, error) { - if r.DB == nil { - return nil, fmt.Errorf("database connection is not initialized") - } - - query := `SELECT id, clustername, object_name, kind, apiVersion, name, namespace, target_type, description, path, summary, file_name, file_row, EventTime FROM kubescore WHERE namespace = ?` - rows, err := r.DB.QueryContext(ctx, query, namespace) - if err != nil { - if err == sql.ErrNoRows { - // No data for the namespace, return an empty slice - return []*model.KubeScore{}, nil - } - return nil, fmt.Errorf("error executing query: %v", err) - } - defer rows.Close() - - var kubeScores []*model.KubeScore - for rows.Next() { - var ks model.KubeScore - if err := rows.Scan(&ks.ID, &ks.ClusterName, &ks.ObjectName, &ks.Kind, &ks.APIVersion, &ks.Name, &ks.Namespace, &ks.TargetType, &ks.Description, &ks.Path, &ks.Summary, &ks.FileName, &ks.FileRow, &ks.EventTime); err != nil { - return nil, fmt.Errorf("error scanning row: %v", err) - } - kubeScores = append(kubeScores, &ks) - } - - if err := rows.Err(); err != nil { - return nil, fmt.Errorf("error iterating rows: %v", err) - } - - return kubeScores, nil -} -func (r *Resolver) fetchResources(ctx context.Context, namespace string) ([]*model.Resource, error) { - if r.DB == nil { - return nil, fmt.Errorf("database connection is not initialized") - } - - query := `SELECT ClusterName, Namespace, Kind, Resource, Age, EventTime FROM getall_resources WHERE Namespace = ?` - rows, err := r.DB.QueryContext(ctx, query, namespace) - if err != nil { - if err == sql.ErrNoRows { - // No data for the namespace, return an empty slice - return []*model.Resource{}, nil - } - return nil, fmt.Errorf("error executing query: %v", err) - } - defer rows.Close() - var resources []*model.Resource - for rows.Next() { - var res model.Resource - if err := rows.Scan(&res.ClusterName, &res.Namespace, &res.Kind, &res.Resource, &res.Age, &res.EventTime); err != nil { - return nil, fmt.Errorf("error scanning row: %v", err) - } - resources = append(resources, &res) - } - - if err := rows.Err(); err != nil { - return nil, fmt.Errorf("error iterating rows: %v", err) - } - - return resources, nil -} +// func (r *Resolver) fetchOutdatedImages(ctx context.Context, namespace string) ([]*model.OutdatedImage, error) { +// if r.DB == nil { +// return nil, fmt.Errorf("database connection is not initialized") +// } +// query := `SELECT ClusterName, Namespace, Pod, CurrentImage, CurrentTag, LatestVersion, VersionsBehind, EventTime FROM outdated_images WHERE Namespace = ?` + +// rows, err := r.DB.QueryContext(ctx, query, namespace) +// if err != nil { +// if err == sql.ErrNoRows { +// return []*model.OutdatedImage{}, nil +// } +// return nil, fmt.Errorf("error executing query: %v", err) +// } +// defer rows.Close() + +// var outdatedImages []*model.OutdatedImage +// for rows.Next() { +// var oi model.OutdatedImage +// if err := rows.Scan(&oi.ClusterName, &oi.Namespace, &oi.Pod, &oi.CurrentImage, &oi.CurrentTag, &oi.LatestVersion, &oi.VersionsBehind, &oi.EventTime); err != nil { +// return nil, fmt.Errorf("error scanning row: %v", err) +// } +// outdatedImages = append(outdatedImages, &oi) +// } + +// if err := rows.Err(); err != nil { +// return nil, fmt.Errorf("error iterating rows: %v", err) +// } + +// return outdatedImages, nil +// } +// func (r *Resolver) fetchKubeScores(ctx context.Context, namespace string) ([]*model.KubeScore, error) { +// if r.DB == nil { +// return nil, fmt.Errorf("database connection is not initialized") +// } + +// query := `SELECT id, clustername, object_name, kind, apiVersion, name, namespace, target_type, description, path, summary, file_name, file_row, EventTime FROM kubescore WHERE namespace = ?` +// rows, err := r.DB.QueryContext(ctx, query, namespace) +// if err != nil { +// if err == sql.ErrNoRows { +// // No data for the namespace, return an empty slice +// return []*model.KubeScore{}, nil +// } +// return nil, fmt.Errorf("error executing query: %v", err) +// } +// defer rows.Close() + +// var kubeScores []*model.KubeScore +// for rows.Next() { +// var ks model.KubeScore +// if err := rows.Scan(&ks.ID, &ks.ClusterName, &ks.ObjectName, &ks.Kind, &ks.APIVersion, &ks.Name, &ks.Namespace, &ks.TargetType, &ks.Description, &ks.Path, &ks.Summary, &ks.FileName, &ks.FileRow, &ks.EventTime); err != nil { +// return nil, fmt.Errorf("error scanning row: %v", err) +// } +// kubeScores = append(kubeScores, &ks) +// } + +// if err := rows.Err(); err != nil { +// return nil, fmt.Errorf("error iterating rows: %v", err) +// } + +// return kubeScores, nil +// } +// func (r *Resolver) fetchResources(ctx context.Context, namespace string) ([]*model.Resource, error) { +// if r.DB == nil { +// return nil, fmt.Errorf("database connection is not initialized") +// } + +// query := `SELECT ClusterName, Namespace, Kind, Resource, Age, EventTime FROM getall_resources WHERE Namespace = ?` +// rows, err := r.DB.QueryContext(ctx, query, namespace) +// if err != nil { +// if err == sql.ErrNoRows { +// // No data for the namespace, return an empty slice +// return []*model.Resource{}, nil +// } +// return nil, fmt.Errorf("error executing query: %v", err) +// } +// defer rows.Close() + +// var resources []*model.Resource +// for rows.Next() { +// var res model.Resource +// if err := rows.Scan(&res.ClusterName, &res.Namespace, &res.Kind, &res.Resource, &res.Age, &res.EventTime); err != nil { +// return nil, fmt.Errorf("error scanning row: %v", err) +// } +// resources = append(resources, &res) +// } + +// if err := rows.Err(); err != nil { +// return nil, fmt.Errorf("error iterating rows: %v", err) +// } + +// return resources, nil +// }