From 5e5586f3195e5463fb16cfe33affc2fa9715e98a Mon Sep 17 00:00:00 2001 From: Thomas Roehl Date: Mon, 21 Aug 2023 15:47:16 +0200 Subject: [PATCH] Add unit to buffers --- api.go | 4 ++-- archive.go | 4 ++-- debug.go | 3 +++ 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/api.go b/api.go index 8d97c0e..43b9bb9 100644 --- a/api.go +++ b/api.go @@ -183,7 +183,7 @@ type ApiQueryRequest struct { From int64 `json:"from"` To int64 `json:"to"` WithStats bool `json:"with-stats"` - WithUnit bool `json:"with-unit"` + WithUnit bool `json:"with-unit,omitempty"` WithData bool `json:"with-data"` WithPadding bool `json:"with-padding"` Queries []ApiQuery `json:"queries"` @@ -303,7 +303,7 @@ func handleQuery(rw http.ResponseWriter, r *http.Request) { if req.WithPadding { data.PadDataWithNull(req.From, req.To, query.Metric) } - if req.WithUnit { + if req.WithUnit && len(unit) > 0 { data.Unit = unit } if !req.WithData { diff --git a/archive.go b/archive.go index c7d84e9..ac95104 100644 --- a/archive.go +++ b/archive.go @@ -24,7 +24,7 @@ import ( type CheckpointMetrics struct { Frequency int64 `json:"frequency"` Start int64 `json:"start"` - Unit string `json:"unit"` + Unit string `json:"unit,omitempty"` Data []Float `json:"data"` } @@ -312,7 +312,7 @@ func (l *level) loadFile(cf *CheckpointFile, m *MemoryStore) error { frequency: metric.Frequency, start: metric.Start, data: metric.Data[0:n:n], // Space is wasted here :( - unit: "", + unit: metric.Unit, prev: nil, next: nil, archived: true, diff --git a/debug.go b/debug.go index 88af59f..567943d 100644 --- a/debug.go +++ b/debug.go @@ -21,6 +21,9 @@ func (b *buffer) debugDump(buf []byte) []byte { if b.archived { buf = append(buf, `,"saved":true`...) } + if b.unit != "" { + buf = append(buf, fmt.Sprintf(`,"unit":"%s"`, b.unit)...) + } if b.next != nil { buf = append(buf, `},`...) } else {