| | | 1 | | using ConsoleLogStreaming.Core.Options; |
| | | 2 | | using Microsoft.Extensions.Options; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Diagnostics.ConsoleLogs.Services; |
| | | 5 | | |
| | | 6 | | internal sealed class ElsaConsoleLogRecentBuffer |
| | | 7 | | { |
| | 9 | 8 | | private readonly object _lock = new(); |
| | 9 | 9 | | private readonly Queue<ConsoleLogLine> _lines = new(); |
| | | 10 | | private readonly int _capacity; |
| | | 11 | | private readonly int _maxQuerySize; |
| | | 12 | | |
| | 9 | 13 | | public ElsaConsoleLogRecentBuffer(IOptions<ConsoleLogOptions> options) |
| | | 14 | | { |
| | 9 | 15 | | _capacity = Math.Max(1, options.Value.RecentCapacity); |
| | 9 | 16 | | _maxQuerySize = Math.Max(1, options.Value.MaxRecentQuerySize); |
| | 9 | 17 | | } |
| | | 18 | | |
| | | 19 | | public void Add(ConsoleLogLine line) |
| | | 20 | | { |
| | 7 | 21 | | lock (_lock) |
| | | 22 | | { |
| | 7 | 23 | | _lines.Enqueue(line); |
| | | 24 | | |
| | 7 | 25 | | while (_lines.Count > _capacity) |
| | 0 | 26 | | _lines.Dequeue(); |
| | 7 | 27 | | } |
| | 7 | 28 | | } |
| | | 29 | | |
| | | 30 | | public IReadOnlyList<ConsoleLogLine> Query(ConsoleLogFilter filter) |
| | | 31 | | { |
| | | 32 | | ConsoleLogLine[] snapshot; |
| | | 33 | | |
| | 5 | 34 | | lock (_lock) |
| | 5 | 35 | | snapshot = _lines.ToArray(); |
| | | 36 | | |
| | 5 | 37 | | var limit = filter.Limit is > 0 ? Math.Min(filter.Limit.Value, _maxQuerySize) : _maxQuerySize; |
| | 5 | 38 | | return snapshot |
| | 5 | 39 | | .Where(line => Matches(line, filter)) |
| | 5 | 40 | | .TakeLast(limit) |
| | 5 | 41 | | .ToArray(); |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | private static bool Matches(ConsoleLogLine line, ConsoleLogFilter filter) |
| | | 45 | | { |
| | 5 | 46 | | if (!string.IsNullOrWhiteSpace(filter.SourceId) && !string.Equals(line.Source.Id, filter.SourceId, StringCompari |
| | 0 | 47 | | return false; |
| | | 48 | | |
| | 5 | 49 | | if (filter.Stream.HasValue && line.Stream != filter.Stream.Value) |
| | 0 | 50 | | return false; |
| | | 51 | | |
| | 5 | 52 | | if (!string.IsNullOrWhiteSpace(filter.Query) && !line.Text.Contains(filter.Query, StringComparison.OrdinalIgnore |
| | 0 | 53 | | return false; |
| | | 54 | | |
| | 5 | 55 | | if (filter.From.HasValue && line.ReceivedAt < filter.From.Value) |
| | 0 | 56 | | return false; |
| | | 57 | | |
| | 5 | 58 | | if (filter.To.HasValue && line.ReceivedAt > filter.To.Value) |
| | 0 | 59 | | return false; |
| | | 60 | | |
| | 19 | 61 | | foreach (var (key, value) in filter.Metadata) |
| | | 62 | | { |
| | 5 | 63 | | if (!line.Metadata.TryGetValue(key, out var candidate) || !string.Equals(candidate, value, StringComparison. |
| | 1 | 64 | | return false; |
| | | 65 | | } |
| | | 66 | | |
| | 4 | 67 | | return true; |
| | 1 | 68 | | } |
| | | 69 | | } |