-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
186 lines (149 loc) · 4.97 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"bufio"
"fmt"
"io"
"math"
"os"
"strconv"
"strings"
flag "github.com/spf13/pflag"
"github.com/mattn/go-runewidth"
)
func main() {
var usage = `Usage: freq [options] [-]
Options:
-h, --help Show this help message and exit.
--histogram Prints a histogram. Requires numeric-only input.
--summary Prints a summary.
--sort-by=<order> Specify sort order: one of count, label (default is count).
--desc Sort buckets in descending order (default is false).
--justify Aligns categories and ranges to the right.
--column-width=<num> Width of the largest bin (default is 30).
--buckets=<num> Number of histogram buckets (default is 10).
`
var (
histogram = flag.BoolP("histogram", "", false, "")
summary = flag.BoolP("summary", "", false, "")
sortBy = flag.StringP("sort-by", "", "count", "")
columnWidth = flag.Float64P("column-width", "", 30, "")
desc = flag.BoolP("desc", "", false, "")
justify = flag.BoolP("justify", "", false, "")
buckets = flag.IntP("buckets", "", 10, "")
)
flag.Usage = func() {
fmt.Fprint(os.Stderr, usage)
}
flag.Parse()
scanner := bufio.NewScanner(os.Stdin)
var vals []string
for scanner.Scan() {
vals = append(vals, strings.TrimSpace(scanner.Text()))
}
if len(vals) == 0 {
os.Exit(0)
}
if *histogram {
// Histograms requires numerical data, so we need to make sure every
// line is a number.
var samples []float64
for _, v := range vals {
sample, err := strconv.ParseFloat(v, 64)
if err != nil {
fmt.Fprintln(os.Stderr, "found non-numerical input:", v)
os.Exit(1)
}
samples = append(samples, sample)
}
buckets, edges := hist(samples, *buckets)
printHistogram(os.Stdout, buckets, edges, *columnWidth, *justify)
if *summary {
printSummary(os.Stdout, samples)
}
} else {
buckets := categoricalBuckets(vals)
// In contrast to histograms, bar charts allow you to sort the bars.
sortBuckets(buckets, *sortBy, *desc)
printBarChart(os.Stdout, buckets, *columnWidth, *justify)
if *summary {
printSummary(os.Stdout, frequenciesFloat64(buckets))
}
}
}
// printHistogram displays a histogram. The bar width determines the width of
// the widest bar. Labels can optionally be right justified.
func printHistogram(out io.Writer, buckets [][]float64, edges []float64, barWidth float64, justify bool) {
var labels []string
for i := 0; i < len(edges)-1; i++ {
labels = append(labels, fmt.Sprintf("%.6g-%.6g", edges[i], edges[i+1]))
}
var (
maxFreq = maxFrequency(buckets)
labelWidth = maxStringWidth(labels)
)
for idx, bucket := range buckets {
var (
normalizedWidth = float64(len(bucket)) / maxFreq
width = normalizedWidth * barWidth
prefix = paddedString(labels[idx], labelWidth, justify)
)
fmt.Fprintf(out, "%s %s %d\n", prefix, column(width), len(bucket))
}
}
// printBarChart displays a bar chart. The bar width determines the width of
// the widest bar. Categories can optionally be right justified.
func printBarChart(out io.Writer, buckets []bucket, barWidth float64, justify bool) {
var (
maxFreq = maxInt(frequencies(buckets))
categoryWidth = maxStringWidth(categories(buckets))
)
for _, bucket := range buckets {
var (
normalizedWidth = float64(bucket.Frequency) / float64(maxFreq)
width = normalizedWidth * barWidth
prefix = paddedString(bucket.Category, categoryWidth, justify)
)
fmt.Fprintf(out, "%s %s %d\n", prefix, column(width), bucket.Frequency)
}
}
// paddedString returns the string justified in a string of given width.
func paddedString(str string, width int, justify bool) string {
if justify {
return just(str, width)
}
return fill(str, width)
}
// printSummary displays additional statistics about the dataset.
func printSummary(out io.Writer, numbers []float64) {
stats := []string{
fmt.Sprintf("%s=%g", "p50", percentile(0.5, numbers)),
fmt.Sprintf("%s=%g", "p90", percentile(0.9, numbers)),
fmt.Sprintf("%s=%g", "p95", percentile(0.95, numbers)),
fmt.Sprintf("%s=%g", "p99", percentile(0.99, numbers)),
fmt.Sprintf("%s=%g", "min", minFloat64(numbers)),
fmt.Sprintf("%s=%g", "max", maxFloat64(numbers)),
fmt.Sprintf("%s=%g", "avg", avgFloat64(numbers)),
}
fmt.Fprintln(out)
fmt.Fprintln(out, "summary:")
fmt.Fprintln(out, " "+strings.Join(stats, ", "))
}
var boxes = []string{"▏", "▎", "▍", "▌", "▋", "▊", "▉", "█"}
// columns returns a horizontal bar of a given size.
func column(size float64) string {
fraction := size - math.Floor(size)
index := int(fraction * float64(len(boxes)))
return strings.Repeat(boxes[len(boxes)-1], int(size)) + boxes[index]
}
// maxStringWidth returns the width of the widest string in a string slice. It
// supports CJK through the go-runewidth package.
func maxStringWidth(strs []string) int {
var max int
for _, str := range strs {
w := runewidth.StringWidth(str)
if w > max {
max = w
}
}
return max
}