Skip to content

Commit d0476c2

Browse files
authored
MRANGE: add fill zero option (#53)
1 parent 64d3ece commit d0476c2

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

pkg/redis-time-series.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ func (ds *redisDatasource) queryTsMRange(from int64, to int64, qm queryModel, cl
161161
)
162162
}
163163

164+
// Previous time and bucket to fill missing intervals
165+
var prevTime time.Time
166+
var bucket, _ = strconv.ParseInt(qm.Bucket, 10, 64)
167+
164168
// Values
165169
for _, valueRaw := range tsArrReply[2].([]interface{}) {
166170
kvPair := valueRaw.([]interface{})
@@ -175,6 +179,20 @@ func (ds *redisDatasource) queryTsMRange(from int64, to int64, qm queryModel, cl
175179
k = kvPair[0].(int64)
176180
}
177181

182+
ts := time.Unix(k/1000, 0)
183+
184+
// Fill missing intervals
185+
if qm.Fill && bucket != 0 {
186+
if !prevTime.IsZero() {
187+
for ts.Sub(prevTime) > time.Duration(bucket)*time.Millisecond {
188+
prevTime = prevTime.Add(time.Duration(bucket) * time.Millisecond)
189+
frame.AppendRow(prevTime, float64(0))
190+
}
191+
}
192+
193+
prevTime = ts
194+
}
195+
178196
// Value
179197
switch kvPair[1].(type) {
180198
case []byte:
@@ -184,7 +202,7 @@ func (ds *redisDatasource) queryTsMRange(from int64, to int64, qm queryModel, cl
184202
}
185203

186204
// Append Row to Frame
187-
frame.AppendRow(time.Unix(k/1000, 0), v)
205+
frame.AppendRow(ts, v)
188206
}
189207

190208
// add the frames to the response

src/redis/command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,5 @@ export const CommandParameters = {
116116
legendLabel: ['ts.mrange'],
117117
section: ['info'],
118118
valueLabel: ['ts.mrange'],
119-
fill: ['ts.range'],
119+
fill: ['ts.range', 'ts.mrange'],
120120
};

0 commit comments

Comments
 (0)