Skip to content

Commit

Permalink
added check health support for all, user's email and domain
Browse files Browse the repository at this point in the history
  • Loading branch information
rakeshsharma14317 committed Dec 19, 2023
1 parent e6c5589 commit 8b56f11
Show file tree
Hide file tree
Showing 13 changed files with 2,032 additions and 1,989 deletions.
5 changes: 2 additions & 3 deletions admin/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,9 @@ type Project struct {
}

type ProjectHealth struct {
ProjectID string `db:"project_id"`
ProjectName string `db:"project_name"`
ProjectID string `db:"id"`
ProjectName string `db:"name"`
OrgID string `db:"org_id"`
OrgName string `db:"org_name"`
ProdDeploymentID *string `db:"prod_deployment_id"`
Status DeploymentStatus `db:"status"`
StatusMessage string `db:"status_message"`
Expand Down
16 changes: 8 additions & 8 deletions admin/database/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"time"

"github.com/XSAM/otelsql"

Check failure on line 10 in admin/database/postgres/postgres.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot --custom-order (gci)
"github.com/jackc/pgconn"
"github.com/jmoiron/sqlx"
"github.com/rilldata/rill/admin/database"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"

// Load postgres driver

Check failure on line 15 in admin/database/postgres/postgres.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot --custom-order (gci)
"github.com/jackc/pgconn"
_ "github.com/jackc/pgx/v4/stdlib"

Check warning on line 17 in admin/database/postgres/postgres.go

View workflow job for this annotation

GitHub Actions / lint

blank-imports: a blank import should be only in a main or test package, or have a comment justifying it (revive)
)

Expand Down Expand Up @@ -320,7 +320,7 @@ func (c *connection) FindProjectByName(ctx context.Context, orgName, name string
func (c *connection) FindProjectsHealth(ctx context.Context, afterName string, limit int) ([]*database.ProjectHealth, error) {
var res []*database.ProjectHealth
err := c.getDB(ctx).SelectContext(ctx, &res, `
SELECT p.*, d.status, d.status_message FROM projects p
SELECT p.id, p.name, p.org_id, p.prod_deployment_id, d.status, d.status_message, d.updated_on FROM projects p
LEFT JOIN deployments d ON p.prod_deployment_id = d.id
WHERE lower(p.name) > lower($1)
ORDER BY lower(p.name) LIMIT $2
Expand All @@ -334,9 +334,9 @@ func (c *connection) FindProjectsHealth(ctx context.Context, afterName string, l
func (c *connection) FindProjectsHealthForOrganization(ctx context.Context, orgID, afterName string, limit int) ([]*database.ProjectHealth, error) {
var res []*database.ProjectHealth
err := c.getDB(ctx).SelectContext(ctx, &res, `
SELECT p.*, d.status, d.status_message FROM projects p
SELECT p.id, p.name, p.org_id, p.prod_deployment_id, d.status, d.status_message, d.updated_on FROM projects p
LEFT JOIN deployments d ON p.prod_deployment_id = d.id
WHERE p.org_id=$1 AND lower(p.name) > lower($2)
WHERE p.org_id = $1 AND lower(p.name) > lower($2)
ORDER BY lower(p.name) LIMIT $3
`, orgID, afterName, limit)
if err != nil {
Expand All @@ -348,7 +348,7 @@ func (c *connection) FindProjectsHealthForOrganization(ctx context.Context, orgI
func (c *connection) FindProjectsHealthForUser(ctx context.Context, userID, afterName string, limit int) ([]*database.ProjectHealth, error) {
var res []*database.ProjectHealth
err := c.getDB(ctx).SelectContext(ctx, &res, `
SELECT p.*, d.status, d.status_message FROM projects p
SELECT p.id, p.name, p.org_id, p.prod_deployment_id, d.status, d.status_message, d.updated_on FROM projects p
LEFT JOIN deployments d ON p.prod_deployment_id = d.id
WHERE p.id IN (
SELECT upr.project_id FROM users_projects_roles upr WHERE upr.user_id = $1
Expand All @@ -366,15 +366,15 @@ func (c *connection) FindProjectsHealthForUser(ctx context.Context, userID, afte
func (c *connection) FindProjectsHealthForDomain(ctx context.Context, domain, afterName string, limit int) ([]*database.ProjectHealth, error) {
var res []*database.ProjectHealth
err := c.getDB(ctx).SelectContext(ctx, &res, `
SELECT p.*, d.status, d.status_message FROM projects p
SELECT p.id, p.name, p.org_id, p.prod_deployment_id, d.status, d.status_message, d.updated_on FROM projects p
LEFT JOIN deployments d ON p.prod_deployment_id = d.id
WHERE p.id IN (
SELECT upr.project_id FROM users_projects_roles upr WHERE upr.user_id IN (
SELECT u.id FROM users u WHERE u.email LIKE $1
SELECT u.id FROM users u WHERE lower(u.email) LIKE lower($1)
)
UNION
SELECT ugpr.project_id FROM usergroups_projects_roles ugpr JOIN usergroups_users uug ON ugpr.usergroup_id = uug.usergroup_id WHERE uug.user_id IN (
SELECT u.id FROM users u WHERE u.email LIKE $1
SELECT u.id FROM users u WHERE lower(u.email) LIKE lower($1)
)
) AND lower(p.name) > lower($2)
ORDER BY lower(p.name) LIMIT $3
Expand Down
36 changes: 29 additions & 7 deletions admin/server/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,18 +762,24 @@ func (s *Server) SudoListProjectsHealth(ctx context.Context, req *adminv1.SudoLi
return nil, status.Error(codes.Internal, err.Error())
}

nextToken := ""
if len(projectsHealth) >= pageSize {
nextToken = marshalPageToken(projectsHealth[len(projectsHealth)-1].ProjectName)
}

// Convert to proto
projectsHealthDtos := make([]*adminv1.ProjectHealth, len(projectsHealth))
for i, projectHealth := range projectsHealth {
projectsHealthDtos[i] = projectHealthToPB(projectHealth)
}

return &adminv1.SudoListProjectsHealthResponse{
Projects: projectsHealthDtos,
Projects: projectsHealthDtos,
NextPageToken: nextToken,
}, nil
}

// SudoListProjectsHealthForOrg returns the health of all projects in the organization
// SudoListProjectsHealthForOrganization returns the health of all projects in the organization
func (s *Server) SudoListProjectsHealthForOrganization(ctx context.Context, req *adminv1.SudoListProjectsHealthForOrganizationRequest) (*adminv1.SudoListProjectsHealthForOrganizationResponse, error) {
// Check the request is made by a superuser
claims := auth.GetClaims(ctx)
Expand All @@ -799,16 +805,21 @@ func (s *Server) SudoListProjectsHealthForOrganization(ctx context.Context, req
return nil, status.Error(codes.Internal, err.Error())
}

nextToken := ""
if len(projectsHealth) >= pageSize {
nextToken = marshalPageToken(projectsHealth[len(projectsHealth)-1].ProjectName)
}

// Convert to proto
projectsHealthDtos := make([]*adminv1.ProjectHealth, len(projectsHealth))
for i, projectHealth := range projectsHealth {
projectsHealthDtos[i] = projectHealthToPB(projectHealth)
}

return &adminv1.SudoListProjectsHealthForOrganizationResponse{
Projects: projectsHealthDtos,
Projects: projectsHealthDtos,
NextPageToken: nextToken,
}, nil

}

// SudoListProjectsHealthForUser returns the health of all projects in the organization
Expand Down Expand Up @@ -837,14 +848,20 @@ func (s *Server) SudoListProjectsHealthForUser(ctx context.Context, req *adminv1
return nil, status.Error(codes.Internal, err.Error())
}

nextToken := ""
if len(projectsHealth) >= pageSize {
nextToken = marshalPageToken(projectsHealth[len(projectsHealth)-1].ProjectName)
}

// Convert to proto
projectsHealthDtos := make([]*adminv1.ProjectHealth, len(projectsHealth))
for i, projectHealth := range projectsHealth {
projectsHealthDtos[i] = projectHealthToPB(projectHealth)
}

return &adminv1.SudoListProjectsHealthForUserResponse{
Projects: projectsHealthDtos,
Projects: projectsHealthDtos,
NextPageToken: nextToken,
}, nil
}

Expand All @@ -868,14 +885,20 @@ func (s *Server) SudoListProjectsHealthForDomain(ctx context.Context, req *admin
return nil, status.Error(codes.Internal, err.Error())
}

nextToken := ""
if len(projectsHealth) >= pageSize {
nextToken = marshalPageToken(projectsHealth[len(projectsHealth)-1].ProjectName)
}

// Convert to proto
projectsHealthDtos := make([]*adminv1.ProjectHealth, len(projectsHealth))
for i, projectHealth := range projectsHealth {
projectsHealthDtos[i] = projectHealthToPB(projectHealth)
}

return &adminv1.SudoListProjectsHealthForDomainResponse{
Projects: projectsHealthDtos,
Projects: projectsHealthDtos,
NextPageToken: nextToken,
}, nil
}

Expand All @@ -884,7 +907,6 @@ func projectHealthToPB(projectHealth *database.ProjectHealth) *adminv1.ProjectHe
ProjectId: projectHealth.ProjectID,
ProjectName: projectHealth.ProjectName,
OrgId: projectHealth.OrgID,
OrgName: projectHealth.OrgName,
DeploymentId: *projectHealth.ProdDeploymentID,
Status: adminv1.DeploymentStatus(projectHealth.Status),
StatusMessage: projectHealth.StatusMessage,
Expand Down
17 changes: 0 additions & 17 deletions cli/cmd/sudo/project/check/check.go

This file was deleted.

116 changes: 87 additions & 29 deletions cli/cmd/sudo/project/check/health.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package check

import (
"time"

"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/rilldata/rill/cli/pkg/printer"
adminv1 "github.com/rilldata/rill/proto/gen/rill/admin/v1"
"github.com/spf13/cobra"
)
Expand All @@ -14,8 +17,8 @@ func HealthCmd(ch *cmdutil.Helper) *cobra.Command {

healthCmd := &cobra.Command{
Use: "health",
Args: cobra.ExactArgs(2),
Short: "Get project details",
Args: cobra.NoArgs,
Short: "Get project health",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
cfg := ch.Config
Expand All @@ -26,46 +29,70 @@ func HealthCmd(ch *cmdutil.Helper) *cobra.Command {
}
defer client.Close()

var reqErr error
var res ProjectHealthResponse

if !cmd.Flags().Changed("org") && !cmd.Flags().Changed("email") && !cmd.Flags().Changed("domain") {
res, err = client.SudoListProjectsHealth(ctx, &adminv1.SudoListProjectsHealthRequest{
res, err := client.SudoListProjectsHealth(ctx, &adminv1.SudoListProjectsHealthRequest{
PageSize: pageSize,
PageToken: pageToken,
})
if err != nil {
return err
}

err = listProjectsHealth(cmd, ch.Printer, res.GetProjects(), pageToken, res.GetNextPageToken(), pageSize)
if err != nil {
return err
}
} else {
if cmd.Flags().Changed("org") {
res, err = client.SudoListProjectsHealthForOrganization(ctx, &adminv1.SudoListProjectsHealthForOrganizationRequest{
res, err := client.SudoListProjectsHealthForOrganization(ctx, &adminv1.SudoListProjectsHealthForOrganizationRequest{
Organization: cfg.Org,
PageSize: pageSize,
PageToken: pageToken,
})
// reqErr = handleResponse(res, ch, err, cmd, pageToken)
if err != nil {
return err
}

err = listProjectsHealth(cmd, ch.Printer, res.GetProjects(), pageToken, res.GetNextPageToken(), pageSize)
if err != nil {
return err
}
}

if cmd.Flags().Changed("email") && reqErr == nil {
res, err = client.SudoListProjectsHealthForUser(ctx, &adminv1.SudoListProjectsHealthForUserRequest{
if cmd.Flags().Changed("email") {
res, err := client.SudoListProjectsHealthForUser(ctx, &adminv1.SudoListProjectsHealthForUserRequest{
Email: email,
PageSize: pageSize,
PageToken: pageToken,
})
// reqErr = handleResponse(res, ch, err, cmd, pageToken)
if err != nil {
return err
}

err = listProjectsHealth(cmd, ch.Printer, res.GetProjects(), pageToken, res.GetNextPageToken(), pageSize)
if err != nil {
return err
}
}

if cmd.Flags().Changed("domain") && reqErr == nil {
res, err = client.SudoListProjectsHealthForDomain(ctx, &adminv1.SudoListProjectsHealthForDomainRequest{
if cmd.Flags().Changed("domain") {
res, err := client.SudoListProjectsHealthForDomain(ctx, &adminv1.SudoListProjectsHealthForDomainRequest{
Domain: domain,
PageSize: pageSize,
PageToken: pageToken,
})
// reqErr = handleResponse(res, ch, err, cmd, pageToken)
if err != nil {
return err
}

err = listProjectsHealth(cmd, ch.Printer, res.GetProjects(), pageToken, res.GetNextPageToken(), pageSize)
if err != nil {
return err
}
}
}

reqErr = handleResponse(res, ch, err, cmd, pageToken)

return reqErr
return nil
},
}

Expand All @@ -78,27 +105,58 @@ func HealthCmd(ch *cmdutil.Helper) *cobra.Command {
return healthCmd
}

type ProjectHealthResponse interface {
GetProjects() []*adminv1.ProjectHealth
GetNextPageToken() string
}

func handleResponse(res ProjectHealthResponse, ch *cmdutil.Helper, err error, cmd *cobra.Command, pageToken string) error {
if err != nil {
return err
func listProjectsHealth(cmd *cobra.Command, p *printer.Printer, projects []*adminv1.ProjectHealth, pageToken, nextPageToken string, pageSize uint32) error {
if len(projects) == 0 {
p.PrintlnWarn("No projects found")
return nil
}
projects := res.GetProjects()
nextPageToken := res.GetNextPageToken()

// If page token is empty, user is running the command first time and we print separator
if len(projects) > 0 && pageToken == "" {
cmd.Println()
}
err = ch.Printer.PrintResource(projects)

err := p.PrintResource(toTable(projects))
if err != nil {
return err
}

if nextPageToken != "" {
cmd.Println()
cmd.Printf("Next page token: inv%s\n", nextPageToken)
cmd.Printf("Next page token: %s\n", nextPageToken)
}

return nil
}

func toTable(projects []*adminv1.ProjectHealth) []*projectHealth {
projs := make([]*projectHealth, 0, len(projects))

for _, proj := range projects {
projs = append(projs, toRow(proj))
}

return projs
}

func toRow(o *adminv1.ProjectHealth) *projectHealth {
return &projectHealth{
ID: o.ProjectId,
Name: o.ProjectName,
OrgID: o.OrgId,
DeploymentID: o.DeploymentId,
Status: o.Status.String(),
StatusMessage: o.StatusMessage,
UpdatedOn: o.DeploymentStatusTimestamp.AsTime().Local().Format(time.RFC3339),
}
}

type projectHealth struct {
ID string `header:"id" json:"id"`
Name string `header:"name" json:"name"`
OrgID string `header:"orgId" json:"orgId"`
DeploymentID string `header:"deploymentId" json:"deploymentId"`
Status string `header:"status" json:"status"`
StatusMessage string `header:"statusMessage" json:"statusMessage"`
UpdatedOn string `header:"updatedOn" json:"updatedOn"`
}
2 changes: 2 additions & 0 deletions cli/cmd/sudo/project/project.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package project

import (
"github.com/rilldata/rill/cli/cmd/sudo/project/check"
"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/spf13/cobra"
)
Expand All @@ -13,6 +14,7 @@ func ProjectCmd(ch *cmdutil.Helper) *cobra.Command {

projectCmd.AddCommand(GetCmd(ch))
projectCmd.AddCommand(SearchCmd(ch))
projectCmd.AddCommand(check.HealthCmd(ch))

return projectCmd
}
Loading

0 comments on commit 8b56f11

Please sign in to comment.