< Summary

Information
Class: Intentum.Core.Streaming.WindowedBatchBuffer
Assembly: Intentum.Core
File(s): /home/runner/work/Intentum/Intentum/src/Intentum.Core/Streaming/WindowedBatchBuffer.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 30
Coverable lines: 30
Total lines: 69
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 16
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%110%
Add(...)0%220%
ShouldFlush()0%12120%
FlushWindow()0%220%

File(s)

/home/runner/work/Intentum/Intentum/src/Intentum.Core/Streaming/WindowedBatchBuffer.cs

#LineLine coverage
 1using Intentum.Core.Behavior;
 2
 3namespace Intentum.Core.Streaming;
 4
 5/// <summary>
 6/// Buffers behavior events and emits a batch when the time window elapses or max size is reached (window-based bufferin
 7/// Call <see cref="Add"/> to append events; call <see cref="FlushWindow"/> to get the current buffer as a batch and cle
 8/// </summary>
 9public sealed class WindowedBatchBuffer
 10{
 011    private readonly List<BehaviorEvent> _buffer = [];
 12    private readonly TimeSpan? _windowDuration;
 13    private readonly int? _maxWindowSize;
 14    private DateTimeOffset? _windowStart;
 15
 16    /// <summary>
 17    /// Creates a windowed buffer. When flushing, all events since the last flush (or since first Add) are returned.
 18    /// </summary>
 19    /// <param name="windowDuration">Optional: after this duration, FlushWindow returns a batch (caller can call FlushWi
 20    /// <param name="maxWindowSize">Optional: when buffer reaches this size, FlushWindow is implied (caller should check
 021    public WindowedBatchBuffer(TimeSpan? windowDuration = null, int? maxWindowSize = null)
 022    {
 023        _windowDuration = windowDuration;
 024        _maxWindowSize = maxWindowSize;
 025    }
 26
 27    /// <summary>
 28    /// Adds an event to the buffer. If MaxWindowSize is set and buffer reaches it, the buffer is full; caller should fl
 29    /// </summary>
 30    public void Add(BehaviorEvent evt)
 031    {
 032        if (_windowStart == null)
 033            _windowStart = DateTimeOffset.UtcNow;
 034        _buffer.Add(evt);
 035    }
 36
 37    /// <summary>
 38    /// Returns true if the window should be flushed (duration elapsed or max size reached).
 39    /// </summary>
 40    public bool ShouldFlush()
 041    {
 042        if (_buffer.Count == 0)
 043            return false;
 044        if (_maxWindowSize.HasValue && _buffer.Count >= _maxWindowSize.Value)
 045            return true;
 046        if (_windowDuration.HasValue && _windowStart.HasValue &&
 047            DateTimeOffset.UtcNow - _windowStart.Value >= _windowDuration.Value)
 048            return true;
 049        return false;
 050    }
 51
 52    /// <summary>
 53    /// Returns the current buffer as a batch and clears the buffer. Returns null if buffer is empty.
 54    /// </summary>
 55    public BehaviorEventBatch? FlushWindow()
 056    {
 057        if (_buffer.Count == 0)
 058            return null;
 059        var batch = new BehaviorEventBatch(_buffer.ToList());
 060        _buffer.Clear();
 061        _windowStart = null;
 062        return batch;
 063    }
 64
 65    /// <summary>
 66    /// Current number of events in the buffer.
 67    /// </summary>
 068    public int Count => _buffer.Count;
 69}