-
Notifications
You must be signed in to change notification settings - Fork 153
/
format.go
313 lines (283 loc) · 6.58 KB
/
format.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package execute
import (
"fmt"
"io"
"sort"
"strconv"
"strings"
"github.com/influxdata/flux"
"github.com/influxdata/flux/values"
)
const fixedWidthTimeFmt = "2006-01-02T15:04:05.000000000Z"
// FormatResult prints the result to w.
func FormatResult(w io.Writer, res flux.Result) error {
fmt.Fprintf(w, "Result: %s\n", res.Name())
if err := res.Tables().Do(func(tbl flux.Table) error {
_, err := NewFormatter(tbl, nil).WriteTo(w)
return err
}); err != nil {
return err
}
return nil
}
// Formatter writes a table to a Writer.
type Formatter struct {
tbl flux.Table
widths []int
maxWidth int
newWidths []int
pad []byte
dash []byte
// fmtBuf is used to format values
fmtBuf [64]byte
opts FormatOptions
cols orderedCols
}
type FormatOptions struct {
// RepeatHeaderCount is the number of rows to print before printing the header again.
// If zero then the headers are not repeated.
RepeatHeaderCount int
NullRepresentation string
}
func DefaultFormatOptions() *FormatOptions {
return &FormatOptions{}
}
var eol = []byte{'\n'}
// NewFormatter creates a Formatter for a given table.
// If opts is nil, the DefaultFormatOptions are used.
func NewFormatter(tbl flux.Table, opts *FormatOptions) *Formatter {
if opts == nil {
opts = DefaultFormatOptions()
}
return &Formatter{
tbl: tbl,
opts: *opts,
}
}
type writeToHelper struct {
w io.Writer
n int64
err error
}
func (w *writeToHelper) write(data []byte) {
if w.err != nil {
return
}
n, err := w.w.Write(data)
w.n += int64(n)
w.err = err
}
var minWidthsByType = map[flux.ColType]int{
flux.TBool: 12,
flux.TInt: 26,
flux.TUInt: 27,
flux.TFloat: 28,
flux.TString: 22,
flux.TTime: len(fixedWidthTimeFmt),
flux.TInvalid: 10,
}
// WriteTo writes the formatted table data to w.
func (f *Formatter) WriteTo(out io.Writer) (int64, error) {
w := &writeToHelper{w: out}
// Sort cols
cols := f.tbl.Cols()
f.cols = newOrderedCols(cols, f.tbl.Key())
sort.Sort(f.cols)
// Compute header widths
f.widths = make([]int, len(cols))
for j, c := range cols {
// Column header is "<label>:<type>"
l := len(c.Label) + len(c.Type.String()) + 1
min := minWidthsByType[c.Type]
if min > l {
l = min
}
if l > f.widths[j] {
f.widths[j] = l
}
if l > f.maxWidth {
f.maxWidth = l
}
}
// Write table header
w.write([]byte("Table: keys: ["))
labels := make([]string, len(f.tbl.Key().Cols()))
for i, c := range f.tbl.Key().Cols() {
labels[i] = c.Label
}
w.write([]byte(strings.Join(labels, ", ")))
w.write([]byte("]"))
w.write(eol)
// Check err and return early
if w.err != nil {
return w.n, w.err
}
// Write rows
r := 0
w.err = f.tbl.Do(func(cr flux.ColReader) error {
if r == 0 {
l := cr.Len()
for i := 0; i < l; i++ {
for oj, c := range f.cols.cols {
j := f.cols.Idx(oj)
buf := f.valueBuf(i, j, c.Type, cr)
l := len(buf)
if l > f.widths[j] {
f.widths[j] = l
}
if l > f.maxWidth {
f.maxWidth = l
}
}
}
f.makePaddingBuffers()
f.writeHeader(w)
f.writeHeaderSeparator(w)
f.newWidths = make([]int, len(f.widths))
copy(f.newWidths, f.widths)
}
l := cr.Len()
for i := 0; i < l; i++ {
for oj, c := range f.cols.cols {
j := f.cols.Idx(oj)
buf := f.valueBuf(i, j, c.Type, cr)
l := len(buf)
padding := f.widths[j] - l
if padding >= 0 {
w.write(f.pad[:padding])
w.write(buf)
} else {
//TODO make unicode friendly
w.write(buf[:f.widths[j]-3])
w.write([]byte{'.', '.', '.'})
}
w.write(f.pad[:2])
if l > f.newWidths[j] {
f.newWidths[j] = l
}
if l > f.maxWidth {
f.maxWidth = l
}
}
w.write(eol)
r++
if f.opts.RepeatHeaderCount > 0 && r%f.opts.RepeatHeaderCount == 0 {
copy(f.widths, f.newWidths)
f.makePaddingBuffers()
f.writeHeaderSeparator(w)
f.writeHeader(w)
f.writeHeaderSeparator(w)
}
}
return w.err
})
return w.n, w.err
}
func (f *Formatter) makePaddingBuffers() {
if len(f.pad) != f.maxWidth {
f.pad = make([]byte, f.maxWidth)
for i := range f.pad {
f.pad[i] = ' '
}
}
if len(f.dash) != f.maxWidth {
f.dash = make([]byte, f.maxWidth)
for i := range f.dash {
f.dash[i] = '-'
}
}
}
func (f *Formatter) writeHeader(w *writeToHelper) {
for oj, c := range f.cols.cols {
j := f.cols.Idx(oj)
buf := append(append([]byte(c.Label), ':'), []byte(c.Type.String())...)
w.write(f.pad[:f.widths[j]-len(buf)])
w.write(buf)
w.write(f.pad[:2])
}
w.write(eol)
}
func (f *Formatter) writeHeaderSeparator(w *writeToHelper) {
for oj := range f.cols.cols {
j := f.cols.Idx(oj)
w.write(f.dash[:f.widths[j]])
w.write(f.pad[:2])
}
w.write(eol)
}
func (f *Formatter) valueBuf(i, j int, typ flux.ColType, cr flux.ColReader) []byte {
buf := []byte(f.opts.NullRepresentation)
switch typ {
case flux.TBool:
if cr.Bools(j).IsValid(i) {
buf = strconv.AppendBool(f.fmtBuf[0:0], cr.Bools(j).Value(i))
}
case flux.TInt:
if cr.Ints(j).IsValid(i) {
buf = strconv.AppendInt(f.fmtBuf[0:0], cr.Ints(j).Value(i), 10)
}
case flux.TUInt:
if cr.UInts(j).IsValid(i) {
buf = strconv.AppendUint(f.fmtBuf[0:0], cr.UInts(j).Value(i), 10)
}
case flux.TFloat:
if cr.Floats(j).IsValid(i) {
// TODO allow specifying format and precision
buf = strconv.AppendFloat(f.fmtBuf[0:0], cr.Floats(j).Value(i), 'f', -1, 64)
}
case flux.TString:
if cr.Strings(j).IsValid(i) {
buf = []byte(cr.Strings(j).ValueString(i))
}
case flux.TTime:
if cr.Times(j).IsValid(i) {
buf = []byte(values.Time(cr.Times(j).Value(i)).String())
}
}
return buf
}
// orderedCols sorts a list of columns:
//
// * time
// * common tags sorted by label
// * other tags sorted by label
// * value
//
type orderedCols struct {
indexMap []int
cols []flux.ColMeta
key flux.GroupKey
}
func newOrderedCols(cols []flux.ColMeta, key flux.GroupKey) orderedCols {
indexMap := make([]int, len(cols))
for i := range indexMap {
indexMap[i] = i
}
cpy := make([]flux.ColMeta, len(cols))
copy(cpy, cols)
return orderedCols{
indexMap: indexMap,
cols: cpy,
key: key,
}
}
func (o orderedCols) Idx(oj int) int {
return o.indexMap[oj]
}
func (o orderedCols) Len() int { return len(o.cols) }
func (o orderedCols) Swap(i int, j int) {
o.cols[i], o.cols[j] = o.cols[j], o.cols[i]
o.indexMap[i], o.indexMap[j] = o.indexMap[j], o.indexMap[i]
}
func (o orderedCols) Less(i int, j int) bool {
ki := ColIdx(o.cols[i].Label, o.key.Cols())
kj := ColIdx(o.cols[j].Label, o.key.Cols())
if ki >= 0 && kj >= 0 {
return ki < kj
} else if ki >= 0 {
return true
} else if kj >= 0 {
return false
}
return i < j
}