@@ -43,9 +43,9 @@ func (c Chunk) spaceFor(ts model.Time) bool {
43
43
}
44
44
45
45
// ForRange returns samples with only the values
46
- // in the given range [start:end).
47
- // start and end are in milliseconds since epoch.
48
- func (c Chunk ) ForRange (start , end model.Time ) []logproto.PatternSample {
46
+ // in the given range [start:end) and aggregates them by step duration .
47
+ // start and end are in milliseconds since epoch. step is a duration in milliseconds.
48
+ func (c Chunk ) ForRange (start , end , step model.Time ) []logproto.PatternSample {
49
49
if len (c .Samples ) == 0 {
50
50
return nil
51
51
}
@@ -66,11 +66,34 @@ func (c Chunk) ForRange(start, end model.Time) []logproto.PatternSample {
66
66
return c .Samples [i ].Timestamp >= end
67
67
})
68
68
}
69
- return c .Samples [lo :hi ]
69
+
70
+ // Re-scale samples into step-sized buckets
71
+ currentStep := truncateTimestamp (c .Samples [lo ].Timestamp , step )
72
+ outputSamples := []logproto.PatternSample {
73
+ {
74
+ Timestamp : currentStep ,
75
+ Value : 0 ,
76
+ },
77
+ }
78
+ for _ , sample := range c .Samples [lo :hi ] {
79
+ if sample .Timestamp >= currentStep + step {
80
+ stepForSample := truncateTimestamp (sample .Timestamp , step )
81
+ for i := currentStep + step ; i <= stepForSample ; i += step {
82
+ outputSamples = append (outputSamples , logproto.PatternSample {
83
+ Timestamp : i ,
84
+ Value : 0 ,
85
+ })
86
+ }
87
+ currentStep = stepForSample
88
+ }
89
+ outputSamples [len (outputSamples )- 1 ].Value += sample .Value
90
+ }
91
+
92
+ return outputSamples
70
93
}
71
94
72
95
func (c * Chunks ) Add (ts model.Time ) {
73
- t := truncateTimestamp (ts )
96
+ t := truncateTimestamp (ts , timeResolution )
74
97
75
98
if len (* c ) == 0 {
76
99
* c = append (* c , newChunk (t ))
@@ -91,10 +114,10 @@ func (c *Chunks) Add(ts model.Time) {
91
114
})
92
115
}
93
116
94
- func (c Chunks ) Iterator (pattern string , from , through model.Time ) iter.Iterator {
117
+ func (c Chunks ) Iterator (pattern string , from , through , step model.Time ) iter.Iterator {
95
118
iters := make ([]iter.Iterator , 0 , len (c ))
96
119
for _ , chunk := range c {
97
- samples := chunk .ForRange (from , through )
120
+ samples := chunk .ForRange (from , through , step )
98
121
if len (samples ) == 0 {
99
122
continue
100
123
}
@@ -173,4 +196,4 @@ func (c *Chunks) size() int {
173
196
return size
174
197
}
175
198
176
- func truncateTimestamp (ts model.Time ) model.Time { return ts - ts % timeResolution }
199
+ func truncateTimestamp (ts , step model.Time ) model.Time { return ts - ts % step }
0 commit comments