optionally pad the returned data with null

This commit is contained in:
Lou Knauer 2022-01-31 16:32:50 +01:00
parent 5c16393e30
commit 371afe5ab5

23
api.go
View File

@ -56,6 +56,25 @@ func (data *ApiMetricData) AddStats() {
} }
} }
func (data *ApiMetricData) PadDataWithNull(from, to int64, metric string) {
minfo, ok := memoryStore.metrics[metric]
if !ok {
return
}
if (data.From / minfo.frequency) > (from / minfo.frequency) {
padfront := int((data.From / minfo.frequency) - (from / minfo.frequency))
ndata := make([]Float, 0, padfront+len(data.Data))
for i := 0; i < padfront; i++ {
ndata = append(ndata, NaN)
}
for j := 0; j < len(data.Data); j++ {
ndata = append(ndata, data.Data[j])
}
data.Data = ndata
}
}
func handleFree(rw http.ResponseWriter, r *http.Request) { func handleFree(rw http.ResponseWriter, r *http.Request) {
rawTo := r.URL.Query().Get("to") rawTo := r.URL.Query().Get("to")
if rawTo == "" { if rawTo == "" {
@ -130,6 +149,7 @@ type ApiQueryRequest struct {
To int64 `json:"to"` To int64 `json:"to"`
WithStats bool `json:"with-stats"` WithStats bool `json:"with-stats"`
WithData bool `json:"with-data"` WithData bool `json:"with-data"`
WithPadding bool `json:"with-padding"`
Queries []ApiQuery `json:"queries"` Queries []ApiQuery `json:"queries"`
ForAllNodes []string `json:"for-all-nodes"` ForAllNodes []string `json:"for-all-nodes"`
} }
@ -224,6 +244,9 @@ func handleQuery(rw http.ResponseWriter, r *http.Request) {
if req.WithStats { if req.WithStats {
data.AddStats() data.AddStats()
} }
if req.WithPadding {
data.PadDataWithNull(req.From, req.To, query.Metric)
}
if !req.WithData { if !req.WithData {
data.Data = nil data.Data = nil
} }