forked from mongodb/mongo-tools
/
grid_line_formatter.go
116 lines (94 loc) · 2.82 KB
/
grid_line_formatter.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
// Copyright (C) MongoDB, Inc. 2014-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package stat_consumer
import (
"bytes"
"fmt"
"sort"
"strings"
"github.com/wNee/mongo-tools-common/text"
"github.com/wNee/mongo-tools/mongostat/stat_consumer/line"
)
// GridLineFormatter uses a text.GridWriter to format the StatLines as a grid
type GridLineFormatter struct {
*limitableFormatter
*text.GridWriter
// If true, enables printing of headers to output
includeHeader bool
// Counter for periodic headers
index int
// Tracks number of hosts so we can reprint headers when it changes
prevLineCount int
}
func NewGridLineFormatter(maxRows int64, includeHeader bool) LineFormatter {
return &GridLineFormatter{
limitableFormatter: &limitableFormatter{maxRows: maxRows},
includeHeader: includeHeader,
GridWriter: &text.GridWriter{ColumnPadding: 1},
}
}
func init() {
FormatterConstructors[""] = NewGridLineFormatter
}
// headerInterval is the number of chunks before the header is re-printed in GridLineFormatter
const headerInterval = 10
func (glf *GridLineFormatter) Finish() {
}
// FormatLines formats the StatLines as a grid
func (glf *GridLineFormatter) FormatLines(lines []*line.StatLine, headerKeys []string, keyNames map[string]string) string {
buf := &bytes.Buffer{}
// Sort the stat lines by hostname, so that we see the output
// in the same order for each snapshot
sort.Sort(line.StatLines(lines))
// Print the columns that are enabled
for _, key := range headerKeys {
header := keyNames[key]
glf.WriteCell(header)
}
glf.EndRow()
for _, l := range lines {
if l.Printed && l.Error == nil {
l.Error = fmt.Errorf("no data received")
}
l.Printed = true
if l.Error != nil {
glf.WriteCell(l.Fields["host"])
glf.Feed(l.Error.Error())
continue
}
for _, key := range headerKeys {
glf.WriteCell(l.Fields[key])
}
glf.EndRow()
}
glf.Flush(buf)
// clear the flushed data
glf.Reset()
gridLine := buf.String()
if glf.prevLineCount != len(lines) {
glf.index = 0
}
glf.prevLineCount = len(lines)
if !glf.includeHeader || glf.index != 0 {
// Strip out the first line of the formatted output,
// which contains the headers. They've been left in up until this point
// in order to force the formatting of the columns to be wide enough.
firstNewLinePos := strings.Index(gridLine, "\n")
if firstNewLinePos >= 0 {
gridLine = gridLine[firstNewLinePos+1:]
}
}
glf.index++
if glf.index == headerInterval {
glf.index = 0
}
if len(lines) > 1 {
// For multi-node stats, add an extra newline to tell each block apart
gridLine = fmt.Sprintf("\n%s", gridLine)
}
glf.increment()
return gridLine
}