| | | 1 | | using Intentum.Core.Behavior; |
| | | 2 | | |
| | | 3 | | namespace 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> |
| | | 9 | | public sealed class WindowedBatchBuffer |
| | | 10 | | { |
| | 0 | 11 | | 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 |
| | 0 | 21 | | public WindowedBatchBuffer(TimeSpan? windowDuration = null, int? maxWindowSize = null) |
| | 0 | 22 | | { |
| | 0 | 23 | | _windowDuration = windowDuration; |
| | 0 | 24 | | _maxWindowSize = maxWindowSize; |
| | 0 | 25 | | } |
| | | 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) |
| | 0 | 31 | | { |
| | 0 | 32 | | if (_windowStart == null) |
| | 0 | 33 | | _windowStart = DateTimeOffset.UtcNow; |
| | 0 | 34 | | _buffer.Add(evt); |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Returns true if the window should be flushed (duration elapsed or max size reached). |
| | | 39 | | /// </summary> |
| | | 40 | | public bool ShouldFlush() |
| | 0 | 41 | | { |
| | 0 | 42 | | if (_buffer.Count == 0) |
| | 0 | 43 | | return false; |
| | 0 | 44 | | if (_maxWindowSize.HasValue && _buffer.Count >= _maxWindowSize.Value) |
| | 0 | 45 | | return true; |
| | 0 | 46 | | if (_windowDuration.HasValue && _windowStart.HasValue && |
| | 0 | 47 | | DateTimeOffset.UtcNow - _windowStart.Value >= _windowDuration.Value) |
| | 0 | 48 | | return true; |
| | 0 | 49 | | return false; |
| | 0 | 50 | | } |
| | | 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() |
| | 0 | 56 | | { |
| | 0 | 57 | | if (_buffer.Count == 0) |
| | 0 | 58 | | return null; |
| | 0 | 59 | | var batch = new BehaviorEventBatch(_buffer.ToList()); |
| | 0 | 60 | | _buffer.Clear(); |
| | 0 | 61 | | _windowStart = null; |
| | 0 | 62 | | return batch; |
| | 0 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Current number of events in the buffer. |
| | | 67 | | /// </summary> |
| | 0 | 68 | | public int Count => _buffer.Count; |
| | | 69 | | } |