From 18d5e17b6df5bf16eee57eeb2d666f4282302339 Mon Sep 17 00:00:00 2001 From: utgwkk Date: Sun, 29 Nov 2020 23:47:16 +0900 Subject: [PATCH 1/7] field filter functions --- formatter.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/formatter.go b/formatter.go index 6862324..c181a7e 100644 --- a/formatter.go +++ b/formatter.go @@ -24,6 +24,51 @@ var availableFormats = []string{ formatNameTSV, } +var fields = []string{ + "number", + "min", + "median", + "mean", + "p50", + "p90", + "p95", + "p99", + "max", + "name", +} + +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 AvailableFormatsForCLI() string { return strings.Join(availableFormats, ", ") } From 65853a947acfb99a151728a1702c761be3b859a2 Mon Sep 17 00:00:00 2001 From: utgwkk Date: Sun, 29 Nov 2020 23:48:00 +0900 Subject: [PATCH 2/7] invert --- formatter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formatter.go b/formatter.go index c181a7e..7057991 100644 --- a/formatter.go +++ b/formatter.go @@ -65,7 +65,7 @@ func exclude(excludedFields filterFields) filterFieldFunc { func excludePercentile() filterFieldFunc { return func(fieldName string) bool { - return strings.HasPrefix(fieldName, "p") + return !strings.HasPrefix(fieldName, "p") } } From b0e039c52258cb559b4b7aba5e14700a4dab8856 Mon Sep 17 00:00:00 2001 From: utgwkk Date: Sun, 29 Nov 2020 23:54:23 +0900 Subject: [PATCH 3/7] filter for table/markdown --- formatter.go | 62 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 16 deletions(-) diff --git a/formatter.go b/formatter.go index 7057991..c9373e3 100644 --- a/formatter.go +++ b/formatter.go @@ -99,7 +99,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) @@ -108,20 +118,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("percentile50") { + data = append(data, strconv.FormatFloat(p.Percentile50, 'f', 6, 64)) + } + if showIf("percentile90") { + data = append(data, strconv.FormatFloat(p.Percentile90, 'f', 6, 64)) + } + if showIf("percentile95") { + data = append(data, strconv.FormatFloat(p.Percentile95, 'f', 6, 64)) + } + if showIf("percentile99") { + 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) @@ -153,10 +183,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, func(_ string) bool { return true }) break case formatNameMarkdown: - WriteTable(w, profileResult, true) + WriteTable(w, profileResult, true, func(_ string) bool { return true }) break case formatNameTSV: WriteTSV(w, profileResult) From 72978d2c6aeaea2ec501e28a6bed408cfb730a49 Mon Sep 17 00:00:00 2001 From: utgwkk Date: Sun, 29 Nov 2020 23:55:43 +0900 Subject: [PATCH 4/7] move to field_filter.go --- field_filter.go | 35 +++++++++++++++++++++++++++++++++++ formatter.go | 32 -------------------------------- 2 files changed, 35 insertions(+), 32 deletions(-) create mode 100644 field_filter.go diff --git a/field_filter.go b/field_filter.go new file mode 100644 index 0000000..c9cb4b0 --- /dev/null +++ b/field_filter.go @@ -0,0 +1,35 @@ +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") + } +} diff --git a/formatter.go b/formatter.go index c9373e3..d564291 100644 --- a/formatter.go +++ b/formatter.go @@ -37,38 +37,6 @@ var fields = []string{ "name", } -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 AvailableFormatsForCLI() string { return strings.Join(availableFormats, ", ") } From e89906d1074a09875a97094236cdadbddfce5ba5 Mon Sep 17 00:00:00 2001 From: utgwkk Date: Sun, 29 Nov 2020 23:56:58 +0900 Subject: [PATCH 5/7] showAll --- field_filter.go | 6 ++++++ formatter.go | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/field_filter.go b/field_filter.go index c9cb4b0..08c5607 100644 --- a/field_filter.go +++ b/field_filter.go @@ -33,3 +33,9 @@ func excludePercentile() filterFieldFunc { return !strings.HasPrefix(fieldName, "p") } } + +func showAll() filterFieldFunc { + return func(_ string) bool { + return true + } +} diff --git a/formatter.go b/formatter.go index d564291..58e6d8e 100644 --- a/formatter.go +++ b/formatter.go @@ -151,10 +151,10 @@ func WriteWithFormat(w io.Writer, profileResult ProfileInput, format string) err WriteJSON(w, profileResult) break case formatNameTable: - WriteTable(w, profileResult, false, func(_ string) bool { return true }) + WriteTable(w, profileResult, false, showAll()) break case formatNameMarkdown: - WriteTable(w, profileResult, true, func(_ string) bool { return true }) + WriteTable(w, profileResult, true, showAll()) break case formatNameTSV: WriteTSV(w, profileResult) From 88a496721f98e5436226c97e3d9a30aac25d59ca Mon Sep 17 00:00:00 2001 From: utgwkk Date: Sun, 29 Nov 2020 23:57:36 +0900 Subject: [PATCH 6/7] make public --- field_filter.go | 8 ++++---- formatter.go | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/field_filter.go b/field_filter.go index 08c5607..3d39621 100644 --- a/field_filter.go +++ b/field_filter.go @@ -6,7 +6,7 @@ type filterFields []string type filterFieldFunc func(fieldName string) bool -func only(onlyFields filterFields) filterFieldFunc { +func Only(onlyFields filterFields) filterFieldFunc { return func(fieldName string) bool { for _, onlyField := range onlyFields { if fieldName == onlyField { @@ -17,7 +17,7 @@ func only(onlyFields filterFields) filterFieldFunc { } } -func exclude(excludedFields filterFields) filterFieldFunc { +func Exclude(excludedFields filterFields) filterFieldFunc { return func(fieldName string) bool { for _, excludedField := range excludedFields { if fieldName == excludedField { @@ -28,13 +28,13 @@ func exclude(excludedFields filterFields) filterFieldFunc { } } -func excludePercentile() filterFieldFunc { +func ExcludePercentile() filterFieldFunc { return func(fieldName string) bool { return !strings.HasPrefix(fieldName, "p") } } -func showAll() filterFieldFunc { +func ShowAll() filterFieldFunc { return func(_ string) bool { return true } diff --git a/formatter.go b/formatter.go index 58e6d8e..1470ac7 100644 --- a/formatter.go +++ b/formatter.go @@ -151,10 +151,10 @@ func WriteWithFormat(w io.Writer, profileResult ProfileInput, format string) err WriteJSON(w, profileResult) break case formatNameTable: - WriteTable(w, profileResult, false, showAll()) + WriteTable(w, profileResult, false, ShowAll()) break case formatNameMarkdown: - WriteTable(w, profileResult, true, showAll()) + WriteTable(w, profileResult, true, ShowAll()) break case formatNameTSV: WriteTSV(w, profileResult) From 39ceaef942af918369017813b76f8069200ee547 Mon Sep 17 00:00:00 2001 From: utgwkk Date: Sun, 29 Nov 2020 23:58:37 +0900 Subject: [PATCH 7/7] percentile -> p --- formatter.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/formatter.go b/formatter.go index 1470ac7..06a50f6 100644 --- a/formatter.go +++ b/formatter.go @@ -101,16 +101,16 @@ func WriteTable(w io.Writer, profileResult ProfileInput, markdown bool, showIf f if showIf("mean") { data = append(data, strconv.FormatFloat(p.Mean, 'f', 6, 64)) } - if showIf("percentile50") { + if showIf("p50") { data = append(data, strconv.FormatFloat(p.Percentile50, 'f', 6, 64)) } - if showIf("percentile90") { + if showIf("p90") { data = append(data, strconv.FormatFloat(p.Percentile90, 'f', 6, 64)) } - if showIf("percentile95") { + if showIf("p95") { data = append(data, strconv.FormatFloat(p.Percentile95, 'f', 6, 64)) } - if showIf("percentile99") { + if showIf("p99") { data = append(data, strconv.FormatFloat(p.Percentile99, 'f', 6, 64)) } if showIf("max") {