Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field filter #17

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
41 changes: 41 additions & 0 deletions field_filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ghaprofiler

import "strings"

type filterFields []string

type filterFieldFunc func(fieldName string) bool

func Only(onlyFields filterFields) filterFieldFunc {
return func(fieldName string) bool {
for _, onlyField := range onlyFields {
if fieldName == onlyField {
return true
}
}
return false
}
}

func Exclude(excludedFields filterFields) filterFieldFunc {
return func(fieldName string) bool {
for _, excludedField := range excludedFields {
if fieldName == excludedField {
return false
}
}
return true
}
}

func ExcludePercentile() filterFieldFunc {
return func(fieldName string) bool {
return !strings.HasPrefix(fieldName, "p")
}
}

func ShowAll() filterFieldFunc {
return func(_ string) bool {
return true
}
}
75 changes: 59 additions & 16 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ var availableFormats = []string{
formatNameTSV,
}

var fields = []string{
"number",
"min",
"median",
"mean",
"p50",
"p90",
"p95",
"p99",
"max",
"name",
}

func AvailableFormatsForCLI() string {
return strings.Join(availableFormats, ", ")
}
Expand Down Expand Up @@ -54,7 +67,17 @@ func WriteJSON(w io.Writer, profileResult ProfileInput) (err error) {
return
}

func WriteTable(w io.Writer, profileResult ProfileInput, markdown bool) error {
func showHeader(showIf filterFieldFunc) []string {
var header []string
for _, field := range fields {
if showIf(field) {
header = append(header, field)
}
}
return header
}

func WriteTable(w io.Writer, profileResult ProfileInput, markdown bool, showIf filterFieldFunc) error {
for _, p := range profileResult {
table := tablewriter.NewWriter(w)
table.SetAutoFormatHeaders(false)
Expand All @@ -63,20 +86,40 @@ func WriteTable(w io.Writer, profileResult ProfileInput, markdown bool) error {
table.SetCenterSeparator("|")
table.SetAutoWrapText(false)
}
table.SetHeader([]string{"Number", "Min", "Median", "Mean", "P50", "P90", "P95", "P99", "Max", "Name"})
table.SetHeader(showHeader(showIf))
for _, p := range p.Profile {
table.Append([]string{
strconv.FormatInt(p.Number, 10),
strconv.FormatFloat(p.Min, 'f', 6, 64),
strconv.FormatFloat(p.Median, 'f', 6, 64),
strconv.FormatFloat(p.Mean, 'f', 6, 64),
strconv.FormatFloat(p.Percentile50, 'f', 6, 64),
strconv.FormatFloat(p.Percentile90, 'f', 6, 64),
strconv.FormatFloat(p.Percentile95, 'f', 6, 64),
strconv.FormatFloat(p.Percentile99, 'f', 6, 64),
strconv.FormatFloat(p.Max, 'f', 6, 64),
p.Name,
})
var data []string
if showIf("number") {
data = append(data, strconv.FormatInt(p.Number, 10))
}
if showIf("min") {
data = append(data, strconv.FormatFloat(p.Min, 'f', 6, 64))
}
if showIf("median") {
data = append(data, strconv.FormatFloat(p.Median, 'f', 6, 64))
}
if showIf("mean") {
data = append(data, strconv.FormatFloat(p.Mean, 'f', 6, 64))
}
if showIf("p50") {
data = append(data, strconv.FormatFloat(p.Percentile50, 'f', 6, 64))
}
if showIf("p90") {
data = append(data, strconv.FormatFloat(p.Percentile90, 'f', 6, 64))
}
if showIf("p95") {
data = append(data, strconv.FormatFloat(p.Percentile95, 'f', 6, 64))
}
if showIf("p99") {
data = append(data, strconv.FormatFloat(p.Percentile99, 'f', 6, 64))
}
if showIf("max") {
data = append(data, strconv.FormatFloat(p.Max, 'f', 6, 64))
}
if showIf("name") {
data = append(data, p.Name)
}
table.Append(data)
}
if markdown {
fmt.Fprintf(w, "# Job: %s\n", p.Name)
Expand Down Expand Up @@ -108,10 +151,10 @@ func WriteWithFormat(w io.Writer, profileResult ProfileInput, format string) err
WriteJSON(w, profileResult)
break
case formatNameTable:
WriteTable(w, profileResult, false)
WriteTable(w, profileResult, false, ShowAll())
break
case formatNameMarkdown:
WriteTable(w, profileResult, true)
WriteTable(w, profileResult, true, ShowAll())
break
case formatNameTSV:
WriteTSV(w, profileResult)
Expand Down