Limit tables to 20 rows

This commit is contained in:
Alfonso Acosta
2016-05-10 14:14:15 +00:00
parent 4e151c4258
commit 7a8e61454f
2 changed files with 37 additions and 12 deletions

View File

@@ -7,31 +7,51 @@ import (
"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
// 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 {
// It's enough to only include MaxTableRows+1
// since they won't be rendered anyhow
if count > MaxTableRows {
break
}
node = node.WithLatest(prefix+key, mtime.Now(), value)
count++
}
return node
}
// ExtractTable returns the key-value pairs with the given prefix from this Node,
func (node Node) ExtractTable(prefix string) map[string]string {
result := map[string]string{}
func (node Node) ExtractTable(prefix string) (rows map[string]string, truncated bool) {
rows = map[string]string{}
truncated = false
count := 0
node.Latest.ForEach(func(key, value string) {
if strings.HasPrefix(key, prefix) {
if count >= MaxTableRows {
truncated = true
return
}
label := key[len(prefix):]
result[label] = value
rows[label] = value
count++
}
})
return result
return rows, truncated
}
// 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"`
ID string `json:"id"`
Label string `json:"label"`
Rows []MetadataRow `json:"rows"`
WasTruncated bool `json:"was_truncated"`
}
type tablesByID []Table
@@ -90,12 +110,13 @@ type TableTemplates map[string]TableTemplate
func (t TableTemplates) Tables(node Node) []Table {
var result []Table
for _, template := range t {
rows, truncated := node.ExtractTable(template.Prefix)
table := Table{
ID: template.ID,
Label: template.Label,
Rows: []MetadataRow{},
ID: template.ID,
Label: template.Label,
Rows: []MetadataRow{},
WasTruncated: truncated,
}
rows := node.ExtractTable(template.Prefix)
keys := make([]string, 0, len(rows))
for k := range rows {
keys = append(keys, k)

View File

@@ -16,7 +16,11 @@ func TestTables(t *testing.T) {
nmd := report.MakeNode("foo1")
nmd = nmd.AddTable("foo_", want)
have := nmd.ExtractTable("foo_")
have, truncated := nmd.ExtractTable("foo_")
if truncated {
t.Error("Table shouldn't had been truncated")
}
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))