Skip to content

Commit

Permalink
feat: add status to group list and group show (#871)
Browse files Browse the repository at this point in the history
* feat: add status to group list and group show

* fix: idle logic

* fix: drop emojis
  • Loading branch information
gris authored Jun 7, 2024
1 parent ff9d47a commit 48eea57
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
31 changes: 29 additions & 2 deletions internal/cmd/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ var groupShowCmd = &cobra.Command{
version := group.Version
fmt.Printf("Locations: %s\n", formatLocations(group.Locations, group.Primary))
fmt.Printf("Version: %s\n", internal.Emph(version))
fmt.Printf("Status: %s\n", aggregateGroupStatus(group))
return nil
},
}

var groupsListCmd = &cobra.Command{
Use: "list",
Short: "List databases groups",
Expand All @@ -71,7 +73,7 @@ var groupsListCmd = &cobra.Command{
return err
}

printTable([]string{"Name", "Locations", "Version", "Sleeping"}, groupsTable(groups))
printTable([]string{"Name", "Locations", "Version", "Status"}, groupsTable(groups))
return nil
},
}
Expand Down Expand Up @@ -215,12 +217,37 @@ func destroyGroup(client *turso.Client, name string) error {
func groupsTable(groups []turso.Group) [][]string {
var data [][]string
for _, group := range groups {
row := []string{group.Name, formatLocations(group.Locations, group.Primary), group.Version, formatBool(group.Archived)}
row := []string{group.Name, formatLocations(group.Locations, group.Primary), group.Version, aggregateGroupStatus(group)}
data = append(data, row)
}
return data
}

func aggregateGroupStatus(group turso.Group) string {
status := "Healthy"
if group.Archived {
return "Sleeping 💤"
}
allIdle := true
for _, locationStatus := range group.Status.Locations {
if group.Primary == locationStatus.Name && locationStatus.Status == "down" {
status = "Unhealthy"
break
}
if locationStatus.Status != "stopped" {
allIdle = false
}
if locationStatus.Status == "down" {
allIdle = false
status = "Degraded"
}
}
if allIdle {
status = "Idle"
}
return status
}

func getGroups(client *turso.Client, fresh ...bool) ([]turso.Group, error) {
skipCache := len(fresh) > 0 && fresh[0]
if cached := getGroupsCache(client.Org); !skipCache && cached != nil {
Expand Down
19 changes: 14 additions & 5 deletions internal/turso/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,21 @@ import (

type GroupsClient client

type LocationStatus struct {
Name string `json:"name"`
Status string `json:"status"`
}

type GroupStatus struct {
Locations []LocationStatus `json:"locations"`
}
type Group struct {
Name string `json:"name"`
Locations []string `json:"locations"`
Primary string `json:"primary"`
Archived bool `json:"archived"`
Version string `json:"version"`
Name string `json:"name"`
Locations []string `json:"locations"`
Primary string `json:"primary"`
Archived bool `json:"archived"`
Version string `json:"version"`
Status GroupStatus `json:"status"`
}

func (d *GroupsClient) List() ([]Group, error) {
Expand Down

0 comments on commit 48eea57

Please sign in to comment.