@@ -216,7 +216,7 @@ func (sc *StatsCollector) Collect(report StatsReport) {
216
216
// rollup performs stats rollup for sessions that fall within the
217
217
// configured rollup window. For sessions longer than the window,
218
218
// we report them individually.
219
- func (sc * StatsCollector ) rollup () []StatsReport {
219
+ func (sc * StatsCollector ) rollup (now time. Time ) []StatsReport {
220
220
sc .mu .Lock ()
221
221
defer sc .mu .Unlock ()
222
222
@@ -259,7 +259,7 @@ func (sc *StatsCollector) rollup() []StatsReport {
259
259
rollupChanged = true
260
260
continue
261
261
}
262
- if stat .SessionEndedAt .IsZero () && sc . opts . Now () .Sub (stat .SessionStartedAt ) <= sc .opts .RollupWindow {
262
+ if stat .SessionEndedAt .IsZero () && now .Sub (stat .SessionStartedAt ) <= sc .opts .RollupWindow {
263
263
// This is an incomplete session, wait and see if it'll be rolled up or not.
264
264
newGroup = append (newGroup , stat )
265
265
continue
@@ -272,7 +272,7 @@ func (sc *StatsCollector) rollup() []StatsReport {
272
272
// Report an end time for incomplete sessions, it will
273
273
// be updated later. This ensures that data in the DB
274
274
// will have an end time even if the service is stopped.
275
- r .SessionEndedAt = sc . opts . Now () .UTC () // Use UTC like database.Now().
275
+ r .SessionEndedAt = now .UTC () // Use UTC like database.Now().
276
276
}
277
277
report = append (report , r ) // Report it (ended or incomplete).
278
278
if stat .SessionEndedAt .IsZero () {
@@ -288,7 +288,7 @@ func (sc *StatsCollector) rollup() []StatsReport {
288
288
289
289
// Keep the group around until the next rollup window has passed
290
290
// in case data was collected late.
291
- if len (newGroup ) == 1 && rolledUp .SessionEndedAt .Add (sc .opts .RollupWindow ).Before (sc . opts . Now () ) {
291
+ if len (newGroup ) == 1 && rolledUp .SessionEndedAt .Add (sc .opts .RollupWindow ).Before (now ) {
292
292
delete (sc .groupedStats , g )
293
293
}
294
294
}
@@ -321,7 +321,8 @@ func (sc *StatsCollector) flush(ctx context.Context) (err error) {
321
321
sc .backlog = nil
322
322
}
323
323
324
- stats := sc .rollup ()
324
+ now := sc .opts .Now ()
325
+ stats := sc .rollup (now )
325
326
if len (stats ) == 0 {
326
327
return nil
327
328
}
0 commit comments