Skip to content

Commit

Permalink
Merge pull request #289 from k1LoW/swap-xy
Browse files Browse the repository at this point in the history
Swap table XY if custom metrics have many columns.
  • Loading branch information
k1LoW authored Oct 17, 2023
2 parents bc5ee5d + 10fcb68 commit 626a9d0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
27 changes: 27 additions & 0 deletions report/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/xeipuuv/gojsonschema"
)

const swapXYMin = 5

//go:embed custom_metrics_schema.json
var schema []byte

Expand Down Expand Up @@ -59,6 +61,9 @@ func (s *CustomMetricSet) Table() string {
if len(s.Metrics) == 0 {
return ""
}
if len(s.Metrics) >= swapXYMin {
return s.tableSwaped()
}
var (
h []string
d []string
Expand Down Expand Up @@ -86,6 +91,28 @@ func (s *CustomMetricSet) Table() string {
return strings.Replace(buf.String(), "---|", "--:|", len(h))
}

func (s *CustomMetricSet) tableSwaped() string {
buf := new(bytes.Buffer)
_, _ = buf.WriteString(fmt.Sprintf("## %s\n\n", s.Name)) //nostyle:handlerrors
table := tablewriter.NewWriter(buf)
table.SetAutoFormatHeaders(false)
table.SetAutoWrapText(false)
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.SetHeader([]string{"", makeHeadTitleWithLink(s.report.Ref, s.report.Commit, nil)})
for _, m := range s.Metrics {
var v string
if isInt(m.Value) {
v = fmt.Sprintf("%d%s", int(m.Value), m.Unit)
} else {
v = fmt.Sprintf("%.1f%s", m.Value, m.Unit)
}
table.Append([]string{m.Name, v})
}
table.Render()
return strings.Replace(buf.String(), "---|", "--:|", len(s.Metrics))
}

func (s *CustomMetricSet) MetadataTable() string {
if len(s.Metadata) == 0 {
return ""
Expand Down
18 changes: 18 additions & 0 deletions report/custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,25 @@ func TestCustomMetricSetTable(t *testing.T) {
{Key: "NsPerOp", Name: "Nanoseconds per iteration", Value: 1340.0, Unit: " ns/op"},
},
}},
{&CustomMetricSet{
Key: "many_metrics",
Name: "Many Metrics",
Metrics: []*CustomMetric{
{Key: "A", Name: "Metrics A", Value: 1500.0, Unit: ""},
{Key: "B", Name: "Metrics B", Value: 1340.0, Unit: ""},
{Key: "C", Name: "Metrics C", Value: 1600.0, Unit: ""},
{Key: "D", Name: "Metrics D", Value: 1010.0, Unit: ""},
{Key: "E", Name: "Metrics E", Value: 1800.0, Unit: ""},
},
report: &Report{
Ref: "main",
Commit: "1234567890",
covPaths: []string{"testdata/cover.out"},
},
}},
}
t.Setenv("GITHUB_SERVER_URL", "https://github.com")
t.Setenv("GITHUB_REPOSITORY", "owner/repo")
for i, tt := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
got := tt.s.Table()
Expand Down
9 changes: 9 additions & 0 deletions testdata/custom_metrics/custom_metric_set_table.3.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## Many Metrics

| | main ([1234567](https://github.com/owner/repo/commit/1234567890)) |
|----------:|------------------------------------------------------------------:|
| Metrics A | 1500 |
| Metrics B | 1340 |
| Metrics C | 1600 |
| Metrics D | 1010 |
| Metrics E | 1800 |

0 comments on commit 626a9d0

Please sign in to comment.