Skip to content

Commit

Permalink
Fork and update guptarohit/asciigraph
Browse files Browse the repository at this point in the history
Here we fork guptarohit/asciigraph and add it to our internal
packages.

We have added the ability to specify a `ValueFormatter` option
for a graph, allowing us to prettify the lables shown on the
y-axis of a graph.

With that we have added the ability to specify `AlwaysY`, which
will force the graph to draw the y-axis even in cases where
the input data is 0, allowing us to draw multiple graphs in
the terminal with consistent size and clariy.
  • Loading branch information
ploubser committed Nov 14, 2024
1 parent 392437f commit 1a7c4ff
Show file tree
Hide file tree
Showing 18 changed files with 1,454 additions and 31 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ jobs:
shell: bash --noprofile --norc -x -eo pipefail {0}
run: |
PATH=$PATH:$GOPATH/bin
GO_LIST=$(go list ./...)
GO_LIST=$(go list ./... | grep -F -e asciigraph -v)
$(exit $(go fmt $GO_LIST | wc -l))
go vet -composites=false $GO_LIST
find . -type f -name "*.go" | xargs misspell -error -locale US
find . -type f -name "*.go" | grep -F -e asciigraph -v | xargs misspell -error -locale US
staticcheck -f stylish $GO_LIST
- name: Run tests
shell: bash --noprofile --norc -x -eo pipefail {0}
run: |
set -e
go test -v --failfast -p=1 ./...
go list ./... | grep -F -e asciigraph -v | xargs go test -v --failfast -p=1
set +e
6 changes: 3 additions & 3 deletions ABTaskFile
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ commands:

{{ if .Flags.spell }}
ab_say Checking spelling
find . -type f -name "*.go" | xargs misspell -error -locale US -i flavour
find . -type f -name "*.go" | grep -F -e asciigraph -v | xargs misspell -error -locale US -i flavour
{{ end }}

{{ if .Flags.vet }}
ab_say Performing go vet
go vet ./...
go list ./... | grep -F -e asciigraph -v |xargs go vet
{{ end }}

{{ if .Flags.staticcheck }}
ab_say Running staticcheck
staticcheck ./...
go list ./... | grep -F -e asciigraph -v |xargs staticcheck
{{ end }}

- name: dependencies
Expand Down
4 changes: 4 additions & 0 deletions cli/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ func fiBytes(v uint64) string {
func f(v any) string {
return columns.F(v)
}

func fFloat2Int(v any) string {
return columns.F(uint64(v.(float64)))
}
6 changes: 5 additions & 1 deletion cli/consumer_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import (
"strings"
"time"

"github.com/guptarohit/asciigraph"
"github.com/nats-io/natscli/columns"
"github.com/nats-io/natscli/internal/asciigraph"
iu "github.com/nats-io/natscli/internal/util"
terminal "golang.org/x/term"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -515,6 +515,7 @@ func (c *consumerCmd) graphAction(_ *fisk.ParseContext) error {
asciigraph.Height(height/4-2),
asciigraph.LowerBound(0),
asciigraph.Precision(0),
asciigraph.ValueFormatter(f),
)

ackedPlot := asciigraph.Plot(ackedRates,
Expand All @@ -523,6 +524,7 @@ func (c *consumerCmd) graphAction(_ *fisk.ParseContext) error {
asciigraph.Height(height/4-2),
asciigraph.LowerBound(0),
asciigraph.Precision(0),
asciigraph.ValueFormatter(f),
)

unprocessedPlot := asciigraph.Plot(unprocessedMessages,
Expand All @@ -531,6 +533,7 @@ func (c *consumerCmd) graphAction(_ *fisk.ParseContext) error {
asciigraph.Height(height/4-2),
asciigraph.LowerBound(0),
asciigraph.Precision(0),
asciigraph.ValueFormatter(fFloat2Int),
)

outstandingPlot := asciigraph.Plot(outstandingMessages,
Expand All @@ -539,6 +542,7 @@ func (c *consumerCmd) graphAction(_ *fisk.ParseContext) error {
asciigraph.Height(height/4-2),
asciigraph.LowerBound(0),
asciigraph.Precision(0),
asciigraph.ValueFormatter(fFloat2Int),
)

iu.ClearScreen()
Expand Down
55 changes: 38 additions & 17 deletions cli/server_graph_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ package cli
import (
"encoding/json"
"fmt"
"os"
"os/signal"
"strings"
"time"

"github.com/choria-io/fisk"
"github.com/guptarohit/asciigraph"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
"github.com/nats-io/natscli/internal/asciigraph"
iu "github.com/nats-io/natscli/internal/util"
terminal "golang.org/x/term"
"os"
"os/signal"
"strings"
"time"
)

type SrvGraphCmd struct {
Expand Down Expand Up @@ -183,35 +184,45 @@ func (c *SrvGraphCmd) graphJetStream() error {
asciigraph.Caption(fmt.Sprintf("CPU %% Used (normalized for %d cores)", vz.Cores)),
asciigraph.Height(height/6-2),
asciigraph.Width(width),
asciigraph.Precision(0))
asciigraph.Precision(0),
asciigraph.ValueFormatter(f))

memPlot := asciigraph.Plot(memUsed,
asciigraph.Caption("Memory Storage in GB"),
asciigraph.Height(height/6-2),
asciigraph.Width(width))
asciigraph.Width(width),
asciigraph.ValueFormatter(func(x any) string {
return fiBytes(uint64(x.(float64)))
}))

filePlot := asciigraph.Plot(fileUsed,
asciigraph.Caption("File Storage in GB"),
asciigraph.Height(height/6-2),
asciigraph.Width(width))
asciigraph.Width(width),
asciigraph.ValueFormatter(func(x any) string {
return fiBytes(uint64(x.(float64)))
}))

assetsPlot := asciigraph.Plot(haAssets,
asciigraph.Caption("HA Assets"),
asciigraph.Height(height/6-2),
asciigraph.Width(width),
asciigraph.Precision(0))
asciigraph.Precision(0),
asciigraph.ValueFormatter(fFloat2Int))

apiRatesPlot := asciigraph.Plot(apiRates,
asciigraph.Caption("API Requests / second"),
asciigraph.Height(height/6-2),
asciigraph.Width(width),
asciigraph.Precision(0))
asciigraph.Precision(0),
asciigraph.ValueFormatter(f))

pendingPlot := asciigraph.Plot(pending,
asciigraph.Caption("Pending API Requests"),
asciigraph.Height(height/6-2),
asciigraph.Width(width),
asciigraph.Precision(0))
asciigraph.Precision(0),
asciigraph.ValueFormatter(fFloat2Int))

return []string{cpuPlot, assetsPlot, apiRatesPlot, pendingPlot, filePlot, memPlot}, nil
})
Expand Down Expand Up @@ -257,36 +268,46 @@ func (c *SrvGraphCmd) graphServer() error {
asciigraph.Caption(fmt.Sprintf("CPU %% Used (normalized for %d cores)", vz.Cores)),
asciigraph.Height(height/6-2),
asciigraph.Width(width),
asciigraph.Precision(0))
asciigraph.Precision(0),
asciigraph.ValueFormatter(f))

memPlot := asciigraph.Plot(memUsed,
asciigraph.Caption("Memory Used in MB"),
asciigraph.Height(height/6-2),
asciigraph.Width(width))
asciigraph.Width(width),
asciigraph.ValueFormatter(func(x any) string {
return fiBytes(uint64(x.(float64)))
}))

connectionsPlot := asciigraph.Plot(connections,
asciigraph.Caption("Connections"),
asciigraph.Height(height/6-2),
asciigraph.Width(width),
asciigraph.Precision(0))
asciigraph.Precision(0),
asciigraph.ValueFormatter(fFloat2Int))

subscriptionsPlot := asciigraph.Plot(subscriptions,
asciigraph.Caption("Subscriptions"),
asciigraph.Height(height/6-2),
asciigraph.Width(width),
asciigraph.Precision(0))
asciigraph.Precision(0),
asciigraph.ValueFormatter(fFloat2Int))

messagesPlot := asciigraph.Plot(messagesRate,
asciigraph.Caption("Messages In+Out / second"),
asciigraph.Height(height/6-2),
asciigraph.Width(width),
asciigraph.Precision(0))
asciigraph.Precision(0),
asciigraph.ValueFormatter(f))

bytesPlot := asciigraph.Plot(bytesRate,
asciigraph.Caption("Bytes In+Out / second"),
asciigraph.Height(height/6-2),
asciigraph.Width(width),
asciigraph.Precision(0))
asciigraph.Precision(0),
asciigraph.ValueFormatter(func(x any) string {
return fiBytes(uint64(x.(float64)))
}))

return []string{cpuPlot, memPlot, connectionsPlot, subscriptionsPlot, messagesPlot, bytesPlot}, nil
})
Expand Down
2 changes: 1 addition & 1 deletion cli/server_ping_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (
"time"

"github.com/choria-io/fisk"
"github.com/guptarohit/asciigraph"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
"github.com/nats-io/natscli/internal/asciigraph"
)

type SrvPingCmd struct {
Expand Down
5 changes: 4 additions & 1 deletion cli/stream_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"syscall"
"time"

"github.com/guptarohit/asciigraph"
"github.com/nats-io/natscli/internal/asciigraph"
iu "github.com/nats-io/natscli/internal/util"
terminal "golang.org/x/term"

Expand Down Expand Up @@ -503,6 +503,7 @@ func (c *streamCmd) graphAction(_ *fisk.ParseContext) error {
asciigraph.Height(height/3-2),
asciigraph.LowerBound(0),
asciigraph.Precision(0),
asciigraph.ValueFormatter(fFloat2Int),
)

limitedRatePlot := asciigraph.Plot(limitedRates,
Expand All @@ -511,6 +512,7 @@ func (c *streamCmd) graphAction(_ *fisk.ParseContext) error {
asciigraph.Height(height/3-2),
asciigraph.LowerBound(0),
asciigraph.Precision(0),
asciigraph.ValueFormatter(f),
)

msgRatePlot := asciigraph.Plot(messageRates,
Expand All @@ -519,6 +521,7 @@ func (c *streamCmd) graphAction(_ *fisk.ParseContext) error {
asciigraph.Height(height/3-2),
asciigraph.LowerBound(0),
asciigraph.Precision(0),
asciigraph.ValueFormatter(f),
)

iu.ClearScreen()
Expand Down
3 changes: 2 additions & 1 deletion cli/sub_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import (

"github.com/choria-io/fisk"
"github.com/dustin/go-humanize"
"github.com/guptarohit/asciigraph"
"github.com/nats-io/jsm.go"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
"github.com/nats-io/natscli/internal/asciigraph"
iu "github.com/nats-io/natscli/internal/util"
terminal "golang.org/x/term"
)
Expand Down Expand Up @@ -191,6 +191,7 @@ func (c *subCmd) startGraph(ctx context.Context, mu *sync.Mutex) {
asciigraph.Height((c.height/(len(c.subjects)+1))-1),
asciigraph.LowerBound(0),
asciigraph.Precision(0),
asciigraph.ValueFormatter(f),
)
fmt.Println(msgRatePlot)
fmt.Println()
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ require (
github.com/google/go-cmp v0.6.0
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/gosuri/uiprogress v0.0.1
github.com/guptarohit/asciigraph v0.7.3
github.com/jedib0t/go-pretty/v6 v6.6.1
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/klauspost/compress v1.17.11
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ github.com/gosuri/uilive v0.0.4 h1:hUEBpQDj8D8jXgtCdBu7sWsy5sbW/5GhuO8KBwJ2jyY=
github.com/gosuri/uilive v0.0.4/go.mod h1:V/epo5LjjlDE5RJUcqx8dbw+zc93y5Ya3yg8tfZ74VI=
github.com/gosuri/uiprogress v0.0.1 h1:0kpv/XY/qTmFWl/SkaJykZXrBBzwwadmW8fRb7RJSxw=
github.com/gosuri/uiprogress v0.0.1/go.mod h1:C1RTYn4Sc7iEyf6j8ft5dyoZ4212h8G1ol9QQluh5+0=
github.com/guptarohit/asciigraph v0.7.3 h1:p05XDDn7cBTWiBqWb30mrwxd6oU0claAjqeytllnsPY=
github.com/guptarohit/asciigraph v0.7.3/go.mod h1:dYl5wwK4gNsnFf9Zp+l06rFiDZ5YtXM6x7SRWZ3KGag=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68=
github.com/huandu/xstrings v1.5.0 h1:2ag3IFq9ZDANvthTwTiqSSZLjDc+BedvHPAp5tJy2TI=
Expand Down
29 changes: 29 additions & 0 deletions internal/asciigraph/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2018, Rohit Gupta
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Loading

0 comments on commit 1a7c4ff

Please sign in to comment.