package traceviewer
import (
"fmt"
"html/template"
"math"
"strings"
"time"
)
type TimeHistogram struct {
Count int
Buckets []int
MinBucket, MaxBucket int
}
var logDiv = math .Log (math .Pow (10 , 1.0 /5 ))
func (h *TimeHistogram ) Add (d time .Duration ) {
var bucket int
if d > 0 {
bucket = int (math .Log (float64 (d )) / logDiv )
}
if len (h .Buckets ) <= bucket {
h .Buckets = append (h .Buckets , make ([]int , bucket -len (h .Buckets )+1 )...)
h .Buckets = h .Buckets [:cap (h .Buckets )]
}
h .Buckets [bucket ]++
if bucket < h .MinBucket || h .MaxBucket == 0 {
h .MinBucket = bucket
}
if bucket > h .MaxBucket {
h .MaxBucket = bucket
}
h .Count ++
}
func (h *TimeHistogram ) BucketMin (bucket int ) time .Duration {
return time .Duration (math .Exp (float64 (bucket ) * logDiv ))
}
func (h *TimeHistogram ) ToHTML (urlmaker func (min , max time .Duration ) string ) template .HTML {
if h == nil || h .Count == 0 {
return template .HTML ("" )
}
const barWidth = 400
maxCount := 0
for _ , count := range h .Buckets {
if count > maxCount {
maxCount = count
}
}
w := new (strings .Builder )
fmt .Fprintf (w , `<table>` )
for i := h .MinBucket ; i <= h .MaxBucket ; i ++ {
if h .Buckets [i ] > 0 {
fmt .Fprintf (w , `<tr><td class="histoTime" align="right"><a href=%s>%s</a></td>` , urlmaker (h .BucketMin (i ), h .BucketMin (i +1 )), h .BucketMin (i ))
} else {
fmt .Fprintf (w , `<tr><td class="histoTime" align="right">%s</td>` , h .BucketMin (i ))
}
width := h .Buckets [i ] * barWidth / maxCount
fmt .Fprintf (w , `<td><div style="width:%dpx;background:blue;position:relative"> </div></td>` , width )
fmt .Fprintf (w , `<td align="right"><div style="position:relative">%d</div></td>` , h .Buckets [i ])
fmt .Fprintf (w , "</tr>\n" )
}
fmt .Fprintf (w , `<tr><td align="right">%s</td></tr>` , h .BucketMin (h .MaxBucket +1 ))
fmt .Fprintf (w , `</table>` )
return template .HTML (w .String ())
}
The pages are generated with Golds v0.7.0-preview . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .