Skip to content

Commit 48eea57

Browse files
authored
feat: add status to group list and group show (#871)
* feat: add status to group list and group show * fix: idle logic * fix: drop emojis
1 parent ff9d47a commit 48eea57

2 files changed

Lines changed: 43 additions & 7 deletions

File tree

internal/cmd/group.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ var groupShowCmd = &cobra.Command{
5151
version := group.Version
5252
fmt.Printf("Locations: %s\n", formatLocations(group.Locations, group.Primary))
5353
fmt.Printf("Version: %s\n", internal.Emph(version))
54+
fmt.Printf("Status: %s\n", aggregateGroupStatus(group))
5455
return nil
5556
},
5657
}
58+
5759
var groupsListCmd = &cobra.Command{
5860
Use: "list",
5961
Short: "List databases groups",
@@ -71,7 +73,7 @@ var groupsListCmd = &cobra.Command{
7173
return err
7274
}
7375

74-
printTable([]string{"Name", "Locations", "Version", "Sleeping"}, groupsTable(groups))
76+
printTable([]string{"Name", "Locations", "Version", "Status"}, groupsTable(groups))
7577
return nil
7678
},
7779
}
@@ -215,12 +217,37 @@ func destroyGroup(client *turso.Client, name string) error {
215217
func groupsTable(groups []turso.Group) [][]string {
216218
var data [][]string
217219
for _, group := range groups {
218-
row := []string{group.Name, formatLocations(group.Locations, group.Primary), group.Version, formatBool(group.Archived)}
220+
row := []string{group.Name, formatLocations(group.Locations, group.Primary), group.Version, aggregateGroupStatus(group)}
219221
data = append(data, row)
220222
}
221223
return data
222224
}
223225

226+
func aggregateGroupStatus(group turso.Group) string {
227+
status := "Healthy"
228+
if group.Archived {
229+
return "Sleeping 💤"
230+
}
231+
allIdle := true
232+
for _, locationStatus := range group.Status.Locations {
233+
if group.Primary == locationStatus.Name && locationStatus.Status == "down" {
234+
status = "Unhealthy"
235+
break
236+
}
237+
if locationStatus.Status != "stopped" {
238+
allIdle = false
239+
}
240+
if locationStatus.Status == "down" {
241+
allIdle = false
242+
status = "Degraded"
243+
}
244+
}
245+
if allIdle {
246+
status = "Idle"
247+
}
248+
return status
249+
}
250+
224251
func getGroups(client *turso.Client, fresh ...bool) ([]turso.Group, error) {
225252
skipCache := len(fresh) > 0 && fresh[0]
226253
if cached := getGroupsCache(client.Org); !skipCache && cached != nil {

internal/turso/groups.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,21 @@ import (
99

1010
type GroupsClient client
1111

12+
type LocationStatus struct {
13+
Name string `json:"name"`
14+
Status string `json:"status"`
15+
}
16+
17+
type GroupStatus struct {
18+
Locations []LocationStatus `json:"locations"`
19+
}
1220
type Group struct {
13-
Name string `json:"name"`
14-
Locations []string `json:"locations"`
15-
Primary string `json:"primary"`
16-
Archived bool `json:"archived"`
17-
Version string `json:"version"`
21+
Name string `json:"name"`
22+
Locations []string `json:"locations"`
23+
Primary string `json:"primary"`
24+
Archived bool `json:"archived"`
25+
Version string `json:"version"`
26+
Status GroupStatus `json:"status"`
1827
}
1928

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

0 commit comments

Comments
 (0)