added sparkline

the testing is very weak and I can get it to panic.
This commit is contained in:
sm
2015-10-28 21:46:20 -07:00
parent 8652695f3b
commit 1c4f1a6f95
2 changed files with 72 additions and 0 deletions
+42
View File
@@ -131,6 +131,48 @@ func (b *BPS) HumanRate() string {
return human(uint64(b.Rate())) + "/s"
}
// Sparkline returns a human-friendly sprakline of history
func (b *BPS) Sparkline(count int) string {
if count > len(b.buckets) {
return "bucket count inappropriate"
}
sparks := []string{"▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"}
line := ""
bucketsPer := len(b.buckets) / count
b.Lock()
defer b.Unlock()
var max int64
var cur int64
// find max
for i := 0; i < len(b.buckets); i++ {
cur = b.buckets[i]
if cur > max {
max = cur
}
}
if max == 0 {
for i := 0; i < count; i++ {
line += sparks[0]
}
return line
}
cur = 0
for i := 0; i < len(b.buckets); i++ {
val := b.buckets[(b.timeI+i)%len(b.buckets)]
cur += val
if i%bucketsPer == 0 {
approx := int((float64(cur) / float64(max)) * float64(len(sparks)-1))
line += sparks[approx]
cur = 0
}
}
return line
}
// Close cleans up and shuts down a BPS.
func (b *BPS) Close() {
close(b.quit)