Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Nithunikzz committed Apr 15, 2024
1 parent d7107f4 commit 155a06a
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 96 deletions.
4 changes: 2 additions & 2 deletions agent/container/pkg/handler/api_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()})
Expand Down
186 changes: 92 additions & 94 deletions graphqlserver/graph/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
// }

0 comments on commit 155a06a

Please sign in to comment.