-
Notifications
You must be signed in to change notification settings - Fork 0
/
header.go
203 lines (174 loc) · 3.96 KB
/
header.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
package render
import (
"reflect"
"github.com/rs/zerolog/log"
)
const (
ageCol = "AGE"
utCol = "UPDATE-TIME"
stCol = "START-TIME"
)
// HeaderColumn represent a table header.
type HeaderColumn struct {
Name string
Align int
Decorator DecoratorFunc
Hide bool
Wide bool
MX bool
Time bool
}
// Clone copies a header.
func (h HeaderColumn) Clone() HeaderColumn {
return h
}
// ----------------------------------------------------------------------------
// Header represents a table header.
type Header []HeaderColumn
// Clone duplicates a header.
func (h Header) Clone() Header {
header := make(Header, len(h))
for i, c := range h {
header[i] = c.Clone()
}
return header
}
// Labelize returns a new Header based on labels.
func (h Header) Labelize(cols []int, labelCol int, rr RowEvents) Header {
header := make(Header, 0, len(cols)+1)
for _, c := range cols {
header = append(header, h[c])
}
cc := rr.ExtractHeaderLabels(labelCol)
for _, c := range cc {
header = append(header, HeaderColumn{Name: c})
}
return header
}
// MapIndices returns a collection of mapped column indices based of the requested columns.
func (h Header) MapIndices(cols []string, wide bool) []int {
ii := make([]int, 0, len(cols))
cc := make(map[int]struct{}, len(cols))
for _, col := range cols {
idx := h.IndexOf(col, true)
if idx < 0 {
log.Warn().Msgf("Column %q not found on resource", col)
}
ii, cc[idx] = append(ii, idx), struct{}{}
}
if !wide {
return ii
}
for i := range h {
if _, ok := cc[i]; ok {
continue
}
ii = append(ii, i)
}
return ii
}
// Customize builds a header from custom col definitions.
func (h Header) Customize(cols []string, wide bool) Header {
if len(cols) == 0 {
return h
}
cc := make(Header, 0, len(h))
xx := make(map[int]struct{}, len(h))
for _, c := range cols {
idx := h.IndexOf(c, true)
if idx == -1 {
log.Warn().Msgf("Column %s is not available on this resource", c)
col := HeaderColumn{
Name: c,
}
cc = append(cc, col)
continue
}
xx[idx] = struct{}{}
col := h[idx].Clone()
col.Wide = false
cc = append(cc, col)
}
if !wide {
return cc
}
for i, c := range h {
if _, ok := xx[i]; ok {
continue
}
col := c.Clone()
col.Wide = true
cc = append(cc, col)
}
return cc
}
// Diff returns true if the header changed.
func (h Header) Diff(header Header) bool {
if len(h) != len(header) {
return true
}
return !reflect.DeepEqual(h, header)
}
// Columns return header as a collection of strings.
func (h Header) Columns(wide bool) []string {
if len(h) == 0 {
return nil
}
cc := make([]string, 0, len(h))
for _, c := range h {
if !wide && c.Wide {
continue
}
cc = append(cc, c.Name)
}
return cc
}
// HasAge returns true if table has an age column.
func (h Header) HasAge() bool {
return h.IndexOf(ageCol, true) != -1
}
// HasUT returns true if table has an UPDATE-TIME column.
func (h Header) HasUT() bool {
return h.IndexOf(utCol, true) != -1
}
// HasST returns true if table has an START-TIME column.
func (h Header) HasST() bool {
return h.IndexOf(stCol, true) != -1
}
// IsMetricsCol checks if given column index represents metrics.
func (h Header) IsMetricsCol(col int) bool {
if col < 0 || col >= len(h) {
return false
}
return h[col].MX
}
// IsTimeCol checks if given column index represents a timestamp.
func (h Header) IsTimeCol(col int) bool {
if col < 0 || col >= len(h) {
return false
}
return h[col].Time
}
// ValidColIndex returns the valid col index or -1 if none.
func (h Header) ValidColIndex() int {
return h.IndexOf("VALID", true)
}
// IndexOf returns the col index or -1 if none.
func (h Header) IndexOf(colName string, includeWide bool) int {
for i, c := range h {
if c.Wide && !includeWide {
continue
}
if c.Name == colName {
return i
}
}
return -1
}
// Dump for debugging.
func (h Header) Dump() {
log.Debug().Msgf("HEADER")
for i, c := range h {
log.Debug().Msgf("%d %q -- %t", i, c.Name, c.Wide)
}
}