Skip to content

Commit

Permalink
Limit tables to 20 rows
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfonso Acosta committed May 10, 2016
1 parent 4e151c4 commit acc4070
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions report/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 += 1

}
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 += 1
}
})
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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit acc4070

Please sign in to comment.