Thanks to visit codestin.com
Credit goes to Github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions acceptance.tests
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ RUN bars -m "\[(.+?)\].* (\d{3}) (\d+)" -e "{buckettime {1} year}" -e '{2}' cmd/
Matched: 50 / 50
END

NAME bars inline key
RUN bars -k -m "\[(.+?)\].* (\d{3}) (\d+)" -e "{buckettime {1} year}" -e "{2}" cmd/testdata/access.txt
2019 200 ██████████████████████████████████████████████████ 47
301 █ 1
404 ██▏ 2
Matched: 50 / 50
END

NAME bars inline key no sub
RUN bars -k -m "\[(.+?)\].* (\d{3}) (\d+)" -e "{buckettime {1} year}" cmd/testdata/access.txt
2019 ██████████████████████████████████████████████████ 50
Matched: 50 / 50
END

NAME bars stacked
RUN bars -s -m "\[(.+?)\].* (\d{3}) (\d+)" -e "{buckettime {1} year}" -e '{2}' cmd/testdata/access.txt
Expand Down
18 changes: 14 additions & 4 deletions cmd/bargraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,27 @@ go run . bars -sz -m "\[(.+?)\].*\" (\d+)" -e "{$ {buckettime {1} year nginx} {2

func bargraphFunction(c *cli.Context) error {
var (
stacked = c.Bool("stacked")
sortName = c.String(helpers.DefaultSortFlag.Name)
scaleName = c.String(helpers.ScaleFlag.Name)
formatName = c.String(helpers.FormatFlag.Name)
stacked = c.Bool("stacked")
inlineSubkey = c.Bool("inline-key")
sortName = c.String(helpers.DefaultSortFlag.Name)
scaleName = c.String(helpers.ScaleFlag.Name)
formatName = c.String(helpers.FormatFlag.Name)
)

vt := helpers.BuildVTermFromArguments(c)
counter := aggregation.NewSubKeyCounter()
writer := termrenderers.NewBarGraph(vt)
writer.Stacked = stacked
writer.InlineSubkey = inlineSubkey
if c.IsSet(helpers.ScaleFlag.Name) {
if stacked {
return cli.Exit("Unable to set graph scale on stacked graphs", helpers.ExitCodeInvalidUsage)
}
writer.Scaler = helpers.BuildScalerOrFail(scaleName)
}
if stacked && inlineSubkey {
return cli.Exit("Unable to use inline-key on stacked graphs", helpers.ExitCodeInvalidUsage)
}
writer.Formatter = helpers.BuildFormatterOrFail(formatName)

batcher := helpers.BuildBatcherFromArguments(c)
Expand Down Expand Up @@ -77,6 +82,11 @@ func bargraphCommand() *cli.Command {
Aliases: []string{"s"},
Usage: "Display bargraph as stacked",
},
&cli.BoolFlag{
Name: "inline-key",
Aliases: []string{"k"},
Usage: "For unstacked graph, display subkey on each bar",
},
helpers.DefaultSortFlag,
helpers.SnapshotFlag,
helpers.NoOutFlag,
Expand Down
7 changes: 7 additions & 0 deletions cmd/bargraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ func TestBarGraph(t *testing.T) {
`-m "(.+) (\d+)" -e "{$ {1} {2}}" testdata/graph.txt`,
`-o - -m "(.+) (\d+)" -e "{$ {1} {2}}" testdata/graph.txt`,
`-o - -m "(.+) (\d+)" -e "{$ {1} {2}}" --scale log10 testdata/graph.txt`,
`-o - -m "(.+) (\d+)" -e "{$ {1} {2}}" -s testdata/graph.txt`,
`-o - -m "(.+) (\d+)" -e "{$ {1} {2}}" -k testdata/graph.txt`,
)
}

func TestBarGraphCantScaleAndStack(t *testing.T) {
err := testCommand(bargraphCommand(), "--stacked --scale log10 testdata/graph.txt")
assert.Error(t, err)
}

func TestBarGraphInlineKey(t *testing.T) {
err := testCommand(bargraphCommand(), "-s -k testdata/graph.txt")
assert.Error(t, err)
}
34 changes: 28 additions & 6 deletions pkg/multiterm/termrenderers/bargraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,25 @@ type BarGraph struct {

maxKeyLength int
subKeys []string
maxSubkeyLen int
rows []barGraphPair
maxLineVal int64
maxRows int
prefixLines int

BarSize int
Stacked bool
Scaler termscaler.Scaler
Formatter termformat.Formatter
BarSize int
Stacked bool
InlineSubkey bool // Inline subkey (only when unstacked)
Scaler termscaler.Scaler
Formatter termformat.Formatter
}

func NewBarGraph(term multiterm.MultilineTerm) *BarGraph {
return &BarGraph{
writer: term,
maxKeyLength: 4,
Stacked: false,
InlineSubkey: false,
BarSize: 50,
Scaler: termscaler.ScalerLinear,
Formatter: termformat.Default,
Expand All @@ -46,9 +49,14 @@ func NewBarGraph(term multiterm.MultilineTerm) *BarGraph {
func (s *BarGraph) SetKeys(keyItems ...string) {
s.subKeys = keyItems

if len(keyItems) > 1 || (len(keyItems) == 1 && keyItems[0] != "") {
for _, item := range keyItems {
s.maxSubkeyLen = max(s.maxSubkeyLen, len(item))
}

if !s.InlineSubkey && (len(keyItems) > 1 || (len(keyItems) == 1 && keyItems[0] != "")) {
s.prefixLines = 1

// draw key
var sb strings.Builder
sb.WriteString(strings.Repeat(" ", s.maxKeyLength+2))
for idx, item := range keyItems {
Expand Down Expand Up @@ -145,16 +153,30 @@ func (s *BarGraph) writeBarGrouped(idx int, key string, vals ...int64) {
}

for i := 0; i < len(vals); i++ {
// Offset top-level group
if i > 0 {
sb.WriteString(strings.Repeat(" ", s.maxKeyLength+2))
}

// Write subkey
if s.InlineSubkey && s.maxSubkeyLen > 0 && len(s.subKeys) >= len(vals) {
color.Write(&sb, color.BrightBlue, func(w io.StringWriter) {
w.WriteString(s.subKeys[i])
})
writeRepeat(&sb, ' ', s.maxSubkeyLen-len(s.subKeys[i])+2)
}

// Write bar
color.Write(&sb, color.GroupColors[i%len(color.GroupColors)], func(w io.StringWriter) {
termunicode.BarWrite(w, s.Scaler.Scale(vals[i], 0, s.maxLineVal), s.BarSize)
})
sb.WriteString(" ")

// Write value
sb.WriteString(s.Formatter(vals[i], 0, s.maxLineVal))
s.writer.WriteForLine(line+i, sb.String())

// Output
s.writer.WriteForLine(line+i, sb.String())
sb.Reset()
}
}
Expand Down
35 changes: 35 additions & 0 deletions pkg/multiterm/termrenderers/bargraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestBargraphRendering(t *testing.T) {
bg.WriteBar(0, "test", 1, 2)
bg.WriteBar(1, "tes2", 1, 3)
bg.WriteFooter(0, "abc")
bg.Close()

assert.Equal(t, " 0 a 1 b", v.Get(0))
assert.Equal(t, "test ████████████████▊ 1", v.Get(1))
Expand All @@ -27,6 +28,40 @@ func TestBargraphRendering(t *testing.T) {
assert.Equal(t, "abc", v.Get(5))
}

func TestBargraphInlineKeyRendering(t *testing.T) {
v := multiterm.NewVirtualTerm()
bg := NewBarGraph(v)
bg.Stacked = false
bg.InlineSubkey = true

bg.SetKeys("a", "bb")
bg.WriteBar(0, "test", 1, 2)
bg.WriteBar(1, "tes2", 1, 3)
bg.WriteFooter(0, "abc")

assert.Equal(t, "test a ████████████████▊ 1", v.Get(0))
assert.Equal(t, " bb █████████████████████████████████▍ 2", v.Get(1))
assert.Equal(t, "tes2 a ████████████████▊ 1", v.Get(2))
assert.Equal(t, " bb ██████████████████████████████████████████████████ 3", v.Get(3))
assert.Equal(t, "abc", v.Get(4))
}

func TestBargraphInlineKeyRenderingSingleKey(t *testing.T) {
v := multiterm.NewVirtualTerm()
bg := NewBarGraph(v)
bg.Stacked = false
bg.InlineSubkey = true

bg.SetKeys("")
bg.WriteBar(0, "test", 1)
bg.WriteBar(1, "tes2", 1)
bg.WriteFooter(0, "abc")

assert.Equal(t, "test ██████████████████████████████████████████████████ 1", v.Get(0))
assert.Equal(t, "tes2 ██████████████████████████████████████████████████ 1", v.Get(1))
assert.Equal(t, "abc", v.Get(2))
}

func TestBargraphRenderingLog10(t *testing.T) {
v := multiterm.NewVirtualTerm()
bg := NewBarGraph(v)
Expand Down
Loading