Skip to content

Commit

Permalink
Support runtime resizing watch displays
Browse files Browse the repository at this point in the history
Signed-off-by: R.I.Pienaar <rip@devco.net>
  • Loading branch information
ripienaar committed Sep 4, 2024
1 parent cda1f35 commit 1dad46b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 28 deletions.
37 changes: 28 additions & 9 deletions cli/server_watch_acct_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

type SrvWatchAccountCmd struct {
topCount int
top int
sort string
accounts map[string]map[string]server.AccountNumConns
sortNames map[string]string
Expand Down Expand Up @@ -63,27 +64,35 @@ The 'Servers' column will show how many servers sent statistics about an account
Only servers with active connections will send these updates.
`)
accounts.Flag("sort", fmt.Sprintf("Sorts by a specific property (%s)", strings.Join(sortKeys, ", "))).Default("conns").EnumVar(&c.sort, sortKeys...)
accounts.Flag("number", "Amount of Accounts to show by the selected dimension").Default("0").Short('n').IntVar(&c.topCount)
accounts.Flag("number", "Amount of Accounts to show by the selected dimension").Default("0").Short('n').IntVar(&c.top)
}

func (c *SrvWatchAccountCmd) accountsAction(_ *fisk.ParseContext) error {
func (c *SrvWatchAccountCmd) updateSizes() error {
c.topCount = c.top

_, h, err := terminal.GetSize(int(os.Stdout.Fd()))
if err != nil && c.topCount == 0 {
return fmt.Errorf("could not determine screen dimensions: %v", err)
}

maxRows := h - 7

if c.topCount == 0 {
c.topCount = h - 8
c.topCount = maxRows
}

if c.topCount > maxRows {
c.topCount = maxRows
}

if c.topCount < 1 {
return fmt.Errorf("requested render limits exceed screen size")
}

if c.topCount > h-8 {
c.topCount = h - 8
}
return nil
}

func (c *SrvWatchAccountCmd) accountsAction(_ *fisk.ParseContext) error {
nc, _, err := prepareHelper("", natsOpts()...)
if err != nil {
return err
Expand All @@ -101,7 +110,10 @@ func (c *SrvWatchAccountCmd) accountsAction(_ *fisk.ParseContext) error {
for {
select {
case <-tick.C:
c.redraw()
err = c.redraw()
if err != nil {
return err
}
case <-ctx.Done():
return nil
}
Expand All @@ -126,10 +138,15 @@ func (c *SrvWatchAccountCmd) handle(msg *nats.Msg) {
c.lastMsg = time.Now()
}

func (c *SrvWatchAccountCmd) redraw() {
func (c *SrvWatchAccountCmd) redraw() error {
c.mu.Lock()
defer c.mu.Unlock()

err := c.updateSizes()
if err != nil {
return err
}

var accounts []*server.AccountStat
seen := map[string]int{}

Expand Down Expand Up @@ -195,7 +212,9 @@ func (c *SrvWatchAccountCmd) redraw() {
)
}

fmt.Println(table.Render())
fmt.Print(table.Render())

return nil
}

func (c *SrvWatchAccountCmd) accountTotal(acct string) (int, *server.AccountStat) {
Expand Down
37 changes: 28 additions & 9 deletions cli/server_watch_js_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
)

type SrvWatchJSCmd struct {
top int
topCount int
sort string
servers map[string]*server.ServerStatsMsg
Expand Down Expand Up @@ -60,27 +61,35 @@ func configureServerWatchJSCommand(watch *fisk.CmdClause) {
Since the updates are sent on a 30 second interval this is not a point in time view.
`)
js.Flag("sort", fmt.Sprintf("Sorts by a specific property (%s)", strings.Join(sortKeys, ", "))).Default("assets").EnumVar(&c.sort, sortKeys...)
js.Flag("number", "Amount of Accounts to show by the selected dimension").Default("0").Short('n').IntVar(&c.topCount)
js.Flag("number", "Amount of Accounts to show by the selected dimension").Default("0").Short('n').IntVar(&c.top)
}

func (c *SrvWatchJSCmd) jetstreamAction(_ *fisk.ParseContext) error {
func (c *SrvWatchJSCmd) updateSizes() error {
c.topCount = c.top

_, h, err := terminal.GetSize(int(os.Stdout.Fd()))
if err != nil && c.topCount == 0 {
return fmt.Errorf("could not determine screen dimensions: %v", err)
}

maxRows := h - 9

if c.topCount == 0 {
c.topCount = h - 8
c.topCount = maxRows
}

if c.topCount > maxRows {
c.topCount = maxRows
}

if c.topCount < 1 {
return fmt.Errorf("requested render limits exceed screen size")
}

if c.topCount > h-8 {
c.topCount = h - 8
}
return nil
}

func (c *SrvWatchJSCmd) jetstreamAction(_ *fisk.ParseContext) error {
nc, _, err := prepareHelper("", natsOpts()...)
if err != nil {
return err
Expand All @@ -99,7 +108,10 @@ func (c *SrvWatchJSCmd) jetstreamAction(_ *fisk.ParseContext) error {
for {
select {
case <-tick.C:
c.redraw()
err = c.redraw()
if err != nil {
return err
}
case <-ctx.Done():
return nil
}
Expand All @@ -123,10 +135,15 @@ func (c *SrvWatchJSCmd) handle(msg *nats.Msg) {
c.mu.Unlock()
}

func (c *SrvWatchJSCmd) redraw() {
func (c *SrvWatchJSCmd) redraw() error {
c.mu.Lock()
defer c.mu.Unlock()

err := c.updateSizes()
if err != nil {
return err
}

var (
servers []*server.ServerStatsMsg
assets int
Expand Down Expand Up @@ -197,5 +214,7 @@ func (c *SrvWatchJSCmd) redraw() {
table.AddFooter("Totals (All Servers)", f(assets), fiBytes(mem), fiBytes(store), f(api), f(apiError))

clearScreen()
fmt.Println(table.Render())
fmt.Printf(table.Render())

return nil
}
37 changes: 27 additions & 10 deletions cli/server_watch_srv_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

type SrvWatchServerCmd struct {
topCount int
top int
sort string
servers map[string]*server.ServerStatsMsg
sortNames map[string]string
Expand Down Expand Up @@ -66,27 +67,34 @@ func configureServerWatchServerCommand(watch *fisk.CmdClause) {
Since the updates are sent on a 30 second interval this is not a point in time view.
`)
servers.Flag("sort", fmt.Sprintf("Sorts by a specific property (%s)", strings.Join(sortKeys, ", "))).Default("conns").EnumVar(&c.sort, sortKeys...)
servers.Flag("number", "Amount of Accounts to show by the selected dimension").Default("0").Short('n').IntVar(&c.topCount)
servers.Flag("number", "Amount of Accounts to show by the selected dimension").Default("0").Short('n').IntVar(&c.top)
}

func (c *SrvWatchServerCmd) serversAction(_ *fisk.ParseContext) error {
func (c *SrvWatchServerCmd) updateSizes() error {
c.topCount = c.top

_, h, err := terminal.GetSize(int(os.Stdout.Fd()))
if err != nil && c.topCount == 0 {
return fmt.Errorf("could not determine screen dimensions: %v", err)
}

maxRows := h - 9

if c.topCount == 0 {
c.topCount = h - 8
c.topCount = maxRows
}

if c.topCount < 1 {
return fmt.Errorf("requested render limits exceed screen size")
if c.topCount > maxRows {
c.topCount = maxRows
}

if c.topCount > h-8 {
c.topCount = h - 8
if c.topCount < 1 {
return fmt.Errorf("requested render limits exceed screen size")
}

return nil
}
func (c *SrvWatchServerCmd) serversAction(_ *fisk.ParseContext) error {
nc, _, err := prepareHelper("", natsOpts()...)
if err != nil {
return err
Expand All @@ -105,7 +113,10 @@ func (c *SrvWatchServerCmd) serversAction(_ *fisk.ParseContext) error {
for {
select {
case <-tick.C:
c.redraw()
err = c.redraw()
if err != nil {
return err
}
case <-ctx.Done():
return nil
}
Expand All @@ -125,10 +136,15 @@ func (c *SrvWatchServerCmd) handle(msg *nats.Msg) {
c.mu.Unlock()
}

func (c *SrvWatchServerCmd) redraw() {
func (c *SrvWatchServerCmd) redraw() error {
c.mu.Lock()
defer c.mu.Unlock()

err := c.updateSizes()
if err != nil {
return err
}

var servers []*server.ServerStatsMsg

var (
Expand Down Expand Up @@ -220,5 +236,6 @@ func (c *SrvWatchServerCmd) redraw() {
table.AddFooter("Totals (All Servers)", f(conns), f(subs), f(slow), fiBytes(uint64(mem)), "", "", "", fmt.Sprintf("%s / %s", f(sentM), fiBytes(uint64(sentB))), fmt.Sprintf("%s / %s", f(recvM), fiBytes(uint64(recvB))))

clearScreen()
fmt.Println(table.Render())
fmt.Print(table.Render())
return nil
}

0 comments on commit 1dad46b

Please sign in to comment.