Skip to content

Commit

Permalink
Merge pull request #960 from ripienaar/watch_sort
Browse files Browse the repository at this point in the history
Improve sorting in watch
  • Loading branch information
ripienaar authored Jan 11, 2024
2 parents 7a2037e + 41d25ce commit 31625ae
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 35 deletions.
16 changes: 8 additions & 8 deletions cli/server_watch_acct_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,23 @@ func (c *SrvWatchAccountCmd) redraw() {

switch c.sort {
case "subs":
return ai.NumSubs > aj.NumSubs
return sortMultiSort(ai.NumSubs, aj.NumSubs, ai.Conns, aj.Conns)
case "slow":
return ai.SlowConsumers > aj.SlowConsumers
return sortMultiSort(ai.SlowConsumers, aj.SlowConsumers, ai.Conns, aj.Conns)
case "sentb":
return ai.Sent.Bytes > aj.Sent.Bytes
return sortMultiSort(ai.Sent.Bytes, aj.Sent.Bytes, ai.Conns, aj.Conns)
case "sentm":
return ai.Sent.Msgs > aj.Sent.Msgs
return sortMultiSort(ai.Sent.Msgs, aj.Sent.Msgs, ai.Conns, aj.Conns)
case "recvb":
return ai.Received.Bytes > aj.Received.Bytes
return sortMultiSort(ai.Received.Bytes, aj.Received.Bytes, ai.Conns, aj.Conns)
case "recvm":
return ai.Received.Msgs > aj.Received.Msgs
return sortMultiSort(ai.Received.Msgs, aj.Received.Msgs, ai.Conns, aj.Conns)
default:
return ai.Conns > aj.Conns
return sortMultiSort(ai.Conns, aj.Conns, ai.Conns, aj.Conns)
}
})

table := newTableWriter(fmt.Sprintf("Top %d Account activity by %s", c.topCount, c.sortNames[c.sort]))
table := newTableWriter(fmt.Sprintf("Top %d Account activity by %s at %s", c.topCount, c.sortNames[c.sort], time.Now().Format(time.DateTime)))
table.AddHeaders("Account", "Servers", "Connections", "Leafnodes", "Subscriptions", "Slow", "Sent", "Received")

var matched []*server.AccountStat
Expand Down
21 changes: 6 additions & 15 deletions cli/server_watch_js_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/choria-io/fisk"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
"golang.org/x/exp/constraints"
terminal "golang.org/x/term"
)

Expand Down Expand Up @@ -122,14 +121,6 @@ func (c *SrvWatchJSCmd) handle(msg *nats.Msg) {
c.mu.Unlock()
}

func sortValueOrNames[V constraints.Ordered](i V, j V, iName string, jName string) bool {
if i > j {
return true
}

return iName > jName
}

func (c *SrvWatchJSCmd) redraw() {
c.mu.Lock()
defer c.mu.Unlock()
Expand All @@ -146,19 +137,19 @@ func (c *SrvWatchJSCmd) redraw() {

switch c.sort {
case "mem":
return sortValueOrNames(si.Memory, sj.Memory, servers[i].Server.Name, servers[i].Server.Name)
return sortMultiSort(si.Memory, sj.Memory, servers[i].Server.Name, servers[j].Server.Name)
case "file":
return sortValueOrNames(si.Store, sj.Store, servers[i].Server.Name, servers[i].Server.Name)
return sortMultiSort(si.Store, sj.Store, servers[i].Server.Name, servers[j].Server.Name)
case "api":
return sortValueOrNames(si.API.Total, sj.API.Total, servers[i].Server.Name, servers[i].Server.Name)
return sortMultiSort(si.API.Total, sj.API.Total, servers[i].Server.Name, servers[j].Server.Name)
case "err":
return sortValueOrNames(si.API.Errors, sj.API.Errors, servers[i].Server.Name, servers[i].Server.Name)
return sortMultiSort(si.API.Errors, sj.API.Errors, servers[i].Server.Name, servers[j].Server.Name)
default:
return sortValueOrNames(si.HAAssets, sj.HAAssets, servers[i].Server.Name, servers[i].Server.Name)
return sortMultiSort(si.HAAssets, sj.HAAssets, servers[i].Server.Name, servers[j].Server.Name)
}
})

table := newTableWriter(fmt.Sprintf("Top %d Server activity by %s", c.topCount, c.sortNames[c.sort]))
table := newTableWriter(fmt.Sprintf("Top %d Server activity by %s at %s", c.topCount, c.sortNames[c.sort], time.Now().Format(time.DateTime)))
table.AddHeaders("Server", "HA Assets", "Memory", "File", "API", "API Errors")

var matched []*server.ServerStatsMsg
Expand Down
26 changes: 14 additions & 12 deletions cli/server_watch_srv_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,34 +136,36 @@ func (c *SrvWatchServerCmd) redraw() {
sort.Slice(servers, func(i, j int) bool {
si := servers[i].Stats
sj := servers[j].Stats
iName := servers[i].Server.Name
jName := servers[j].Server.Name

switch c.sort {
case "subs":
return si.NumSubs > sj.NumSubs
return sortMultiSort(si.NumSubs, sj.NumSubs, iName, jName)
case "sentb":
return si.Sent.Bytes > sj.Sent.Bytes
return sortMultiSort(si.Sent.Bytes, sj.Sent.Bytes, iName, jName)
case "sentm":
return si.Sent.Msgs > sj.Sent.Msgs
return sortMultiSort(si.Sent.Msgs, sj.Sent.Msgs, iName, jName)
case "recvb":
return si.Received.Bytes > sj.Received.Bytes
return sortMultiSort(si.Received.Bytes, sj.Received.Bytes, iName, jName)
case "recvm":
return si.Received.Msgs > sj.Received.Msgs
return sortMultiSort(si.Received.Msgs, sj.Received.Msgs, iName, jName)
case "slow":
return si.SlowConsumers > sj.SlowConsumers
return sortMultiSort(si.SlowConsumers, sj.SlowConsumers, iName, jName)
case "route":
return len(si.Routes) > len(sj.Routes)
return sortMultiSort(len(si.Routes), len(sj.Routes), iName, jName)
case "gway":
return len(si.Gateways) > len(sj.Gateways)
return sortMultiSort(len(si.Gateways), len(sj.Gateways), iName, jName)
case "mem":
return si.Mem > sj.Mem
return sortMultiSort(si.Mem, sj.Mem, iName, jName)
case "cpu":
return si.CPU > sj.CPU
return sortMultiSort(si.CPU, sj.CPU, iName, jName)
default:
return si.Connections > sj.Connections
return sortMultiSort(si.Connections, sj.Connections, iName, jName)
}
})

table := newTableWriter(fmt.Sprintf("Top %d Server activity by %s", c.topCount, c.sortNames[c.sort]))
table := newTableWriter(fmt.Sprintf("Top %d Server activity by %s at %s", c.topCount, c.sortNames[c.sort], time.Now().Format(time.DateTime)))
table.AddHeaders("Server", "Connections", "Subscription", "Slow", "Memory", "CPU", "Routes", "Gateways", "Sent", "Received")

var matched []*server.ServerStatsMsg
Expand Down
8 changes: 8 additions & 0 deletions cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import (
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
"github.com/nats-io/nuid"
"golang.org/x/exp/constraints"
terminal "golang.org/x/term"

"github.com/nats-io/jsm.go/natscontext"
Expand Down Expand Up @@ -1526,6 +1527,13 @@ func clearScreen() {
fmt.Print("\033[2J\033[1;1H\033[?25l")
}

func sortMultiSort[V constraints.Ordered, S string | constraints.Ordered](i1 V, j1 V, i2 S, j2 S) bool {
if i1 == j1 {
return i2 < j2
}
return i1 > j1
}

func mapKeys[M ~map[K]V, K comparable, V any](m M) []K {
r := make([]K, 0, len(m))
for k := range m {
Expand Down
18 changes: 18 additions & 0 deletions cli/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,21 @@ func TestHostnameCompactor(t *testing.T) {
t.Fatalf("Recevied %#v", result)
}
}

func TestMultipleSort(t *testing.T) {
if sortMultiSort(1, 1, "b", "a") {
t.Fatalf("expected true")
}

if !sortMultiSort(1, 1, "a", "b") {
t.Fatalf("expected false")
}

if sortMultiSort(1, 2, "a", "b") {
t.Fatalf("expected false")
}

if !sortMultiSort(2, 1, "a", "b") {
t.Fatalf("expected true")
}
}

0 comments on commit 31625ae

Please sign in to comment.