aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/stat
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2024-07-24 12:26:10 +0200
committerDmitry Vyukov <dvyukov@google.com>2024-07-24 14:39:45 +0000
commit339edef584b68d656e2c5202d85eb705ee88874e (patch)
tree533b719df2ad9ac56b50dca68b069fa291b1e34a /pkg/stat
parent49e6369fe732c0f81e5b03b36e345afbf3c79a15 (diff)
pkg/stat: sort graph lines
Currently lines in graphs are reordered on each page reload since use map iteration which is randomized. This is very inconvinient. Sort lines based on creation order. This provides stable order and gives a user some control over order.
Diffstat (limited to 'pkg/stat')
-rw-r--r--pkg/stat/set.go30
1 files changed, 22 insertions, 8 deletions
diff --git a/pkg/stat/set.go b/pkg/stat/set.go
index 690d0406a..4bcf2d2c6 100644
--- a/pkg/stat/set.go
+++ b/pkg/stat/set.go
@@ -57,6 +57,7 @@ type set struct {
mu sync.Mutex
vals map[string]*Val
graphs map[string]*graph
+ nextOrder atomic.Uint64
totalTicks int
historySize int
historyTicks int
@@ -71,10 +72,12 @@ type graph struct {
}
type line struct {
- desc string
- rate bool
- data []float64
- hist []*gohistogram.NumericHistogram
+ name string
+ desc string
+ order uint64
+ rate bool
+ data []float64
+ hist []*gohistogram.NumericHistogram
}
const (
@@ -187,6 +190,7 @@ func (s *set) New(name, desc string, opts ...any) *Val {
name: name,
desc: desc,
graph: name,
+ order: s.nextOrder.Add(1),
fmt: func(v int, period time.Duration) string { return strconv.Itoa(v) },
}
stacked := false
@@ -245,6 +249,7 @@ type Val struct {
link string
graph string
level Level
+ order uint64
val atomic.Uint64
ext func() int
fmt func(int, time.Duration) string
@@ -316,8 +321,10 @@ func (s *set) tick() {
ln := graph.lines[v.name]
if ln == nil {
ln = &line{
- desc: v.desc,
- rate: v.rate,
+ name: v.name,
+ desc: v.desc,
+ order: v.order,
+ rate: v.rate,
}
if v.hist {
ln.hist = make([]*gohistogram.NumericHistogram, s.historySize)
@@ -405,6 +412,13 @@ func (s *set) RenderHTML() ([]byte, error) {
if len(graph.lines) == 0 {
continue
}
+ var lines []*line
+ for _, ln := range graph.lines {
+ lines = append(lines, ln)
+ }
+ sort.Slice(lines, func(i, j int) bool {
+ return lines[i].order < lines[j].order
+ })
g := Graph{
ID: len(graphs),
Title: title,
@@ -415,9 +429,9 @@ func (s *set) RenderHTML() ([]byte, error) {
for i := 0; i < s.historyPos; i++ {
g.Points[i].X = i * tick
}
- for name, ln := range graph.lines {
+ for _, ln := range lines {
if ln.hist == nil {
- g.Lines = append(g.Lines, name+": "+ln.desc)
+ g.Lines = append(g.Lines, ln.name+": "+ln.desc)
for i := 0; i < s.historyPos; i++ {
g.Points[i].Y = append(g.Points[i].Y, ln.data[i])
}