Skip to content

Commit

Permalink
Provide truncation counts to the UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfonso Acosta committed May 10, 2016
1 parent 7a8e614 commit 925ad63
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 25 deletions.
51 changes: 28 additions & 23 deletions report/table.go
Original file line number Diff line number Diff line change
@@ -1,57 +1,62 @@
package report

import (
"fmt"
"sort"
"strings"

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
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 {
// It's enough to only include MaxTableRows+1
// since they won't be rendered anyhow
if count > MaxTableRows {
if count >= MaxTableRows {
break
}
node = node.WithLatest(prefix+key, mtime.Now(), value)
count++

}
if len(labels) > MaxTableRows {
truncation_count := fmt.Sprintf("%d", len(labels)-MaxTableRows)
node = node.WithLatest(TruncationCountPrefix+prefix, mtime.Now(), truncation_count)
}
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, truncated bool) {
func (node Node) ExtractTable(prefix string) (rows map[string]string, truncationCount int) {
rows = map[string]string{}
truncated = false
count := 0
truncationCount = 0
node.Latest.ForEach(func(key, value string) {
if strings.HasPrefix(key, prefix) {
if count >= MaxTableRows {
truncated = true
return
}
label := key[len(prefix):]
rows[label] = value
count++
}
})
return rows, truncated
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"`
WasTruncated bool `json:"was_truncated"`
ID string `json:"id"`
Label string `json:"label"`
Rows []MetadataRow `json:"rows"`
TruncationCount int `json:"truncation_count,omitempty"`
}

type tablesByID []Table
Expand Down Expand Up @@ -110,12 +115,12 @@ 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)
rows, truncationCount := node.ExtractTable(template.Prefix)
table := Table{
ID: template.ID,
Label: template.Label,
Rows: []MetadataRow{},
WasTruncated: truncated,
ID: template.ID,
Label: template.Label,
Rows: []MetadataRow{},
TruncationCount: truncationCount,
}
keys := make([]string, 0, len(rows))
for k := range rows {
Expand Down
28 changes: 26 additions & 2 deletions report/table_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package report_test

import (
"fmt"
"reflect"
"testing"

Expand All @@ -16,13 +17,36 @@ func TestTables(t *testing.T) {
nmd := report.MakeNode("foo1")

nmd = nmd.AddTable("foo_", want)
have, truncated := nmd.ExtractTable("foo_")
have, truncationCount := nmd.ExtractTable("foo_")

if truncated {
if truncationCount != 0 {
t.Error("Table shouldn't had been truncated")
}

if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}
}

func TestTruncation(t *testing.T) {
wantTruncationCount := uint(1)
want := map[string]string{}
for i := 0; i < report.MaxTableRows+wantTruncationCount; i++ {
key := fmt.Sprintf("key%d", i)
value := fmt.Sprintf("value%d", i)
want[key] = value
}

nmd := report.MakeNode("foo1")

nmd = nmd.AddTable("foo_", want)
_, truncationCount := nmd.ExtractTable("foo_")

if truncationCount != wantTruncationCount {
t.Error(
"Table should had been truncated by %d and not %d",
wantTruncationCount,
truncationCount,
)
}
}

0 comments on commit 925ad63

Please sign in to comment.