-
Notifications
You must be signed in to change notification settings - Fork 712
/
table.go
173 lines (156 loc) · 4.35 KB
/
table.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
package report
import (
"fmt"
"sort"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/weaveworks/scope/common/mtime"
)
// MaxTableRows sets the limit on the table size to render
// TODO: this won't be needed once we send reports incrementally
const (
MaxTableRows = 20
TruncationCountPrefix = "table_truncation_count_"
)
// AddTable appends arbirary key-value pairs to the Node, returning a new node.
func (node Node) AddTable(prefix string, labels map[string]string) Node {
count := 0
for key, value := range labels {
if count >= MaxTableRows {
break
}
node = node.WithLatest(prefix+key, mtime.Now(), value)
count++
}
if len(labels) > MaxTableRows {
truncationCount := fmt.Sprintf("%d", len(labels)-MaxTableRows)
node = node.WithLatest(TruncationCountPrefix+prefix, mtime.Now(), truncationCount)
}
return node
}
// ExtractTable returns the key-value pairs with the given prefix from this Node,
func (node Node) ExtractTable(prefix string) (rows map[string]string, truncationCount int) {
rows = map[string]string{}
truncationCount = 0
node.Latest.ForEach(func(key string, _ time.Time, value string) {
if strings.HasPrefix(key, prefix) {
label := key[len(prefix):]
rows[label] = value
}
})
if str, ok := node.Latest.Lookup(TruncationCountPrefix + prefix); ok {
if n, err := fmt.Sscanf(str, "%d", &truncationCount); n != 1 || err != nil {
log.Warn("Unexpected truncation count format %q", str)
}
}
return rows, truncationCount
}
// Table is the type for a table in the UI.
type Table struct {
ID string `json:"id"`
Label string `json:"label"`
Rows []MetadataRow `json:"rows"`
TruncationCount int `json:"truncationCount,omitempty"`
}
type tablesByID []Table
func (t tablesByID) Len() int { return len(t) }
func (t tablesByID) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t tablesByID) Less(i, j int) bool { return t[i].ID < t[j].ID }
// Copy returns a copy of the Table.
func (t Table) Copy() Table {
result := Table{
ID: t.ID,
Label: t.Label,
Rows: make([]MetadataRow, 0, len(t.Rows)),
}
for _, row := range t.Rows {
result.Rows = append(result.Rows, row)
}
return result
}
// TableTemplate describes how to render a table for the UI.
type TableTemplate struct {
ID string `json:"id"`
Label string `json:"label"`
Prefix string `json:"prefix"`
}
// Copy returns a value-copy of the TableTemplate
func (t TableTemplate) Copy() TableTemplate {
return t
}
// Merge other into t, returning a fresh copy. Does fieldwise max -
// whilst this isn't particularly meaningful, at least it idempotent,
// commutativite and associative.
func (t TableTemplate) Merge(other TableTemplate) TableTemplate {
max := func(s1, s2 string) string {
if s1 > s2 {
return s1
}
return s2
}
return TableTemplate{
ID: max(t.ID, other.ID),
Label: max(t.Label, other.Label),
Prefix: max(t.Prefix, other.Prefix),
}
}
// TableTemplates is a mergeable set of TableTemplate
type TableTemplates map[string]TableTemplate
// Tables renders the TableTemplates for a given node.
func (t TableTemplates) Tables(node Node) []Table {
var result []Table
for _, template := range t {
rows, truncationCount := node.ExtractTable(template.Prefix)
table := Table{
ID: template.ID,
Label: template.Label,
Rows: []MetadataRow{},
TruncationCount: truncationCount,
}
keys := make([]string, 0, len(rows))
for k := range rows {
keys = append(keys, k)
}
sort.Strings(keys)
for _, key := range keys {
table.Rows = append(table.Rows, MetadataRow{
ID: "label_" + key,
Label: key,
Value: rows[key],
})
}
result = append(result, table)
}
sort.Sort(tablesByID(result))
return result
}
// Copy returns a value copy of the TableTemplates
func (t TableTemplates) Copy() TableTemplates {
if t == nil {
return nil
}
result := TableTemplates{}
for k, v := range t {
result[k] = v.Copy()
}
return result
}
// Merge merges two sets of TableTemplates
func (t TableTemplates) Merge(other TableTemplates) TableTemplates {
if t == nil && other == nil {
return nil
}
result := make(TableTemplates, len(t))
for k, v := range t {
result[k] = v
}
for k, v := range other {
if existing, ok := result[k]; ok {
result[k] = v.Merge(existing)
} else {
result[k] = v
}
}
return result
}