| | | 1 | | using ConsoleLogStreaming.Core; |
| | | 2 | | using Elsa.Diagnostics.ConsoleLogs.Services; |
| | | 3 | | using Microsoft.AspNetCore.Authorization; |
| | | 4 | | using Microsoft.AspNetCore.SignalR; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Diagnostics.ConsoleLogs.RealTime; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// SignalR hub for live Elsa console log streaming. |
| | | 10 | | /// </summary> |
| | | 11 | | [Authorize] |
| | 8 | 12 | | public sealed class ElsaConsoleLogsHub( |
| | 8 | 13 | | IConsoleLogProvider provider, |
| | 8 | 14 | | IElsaConsoleLogHubAuthorizer authorizer, |
| | 8 | 15 | | ElsaConsoleLogSubscriptionManager subscriptionManager) : Hub<IElsaConsoleLogsClient> |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Streams matching console log items as a SignalR streaming method. |
| | | 19 | | /// </summary> |
| | | 20 | | public async IAsyncEnumerable<ConsoleLogStreamingItem> StreamAsync( |
| | | 21 | | ElsaConsoleLogFilter? filter, |
| | | 22 | | [EnumeratorCancellation] CancellationToken cancellationToken) |
| | | 23 | | { |
| | 2 | 24 | | await EnsureCanReadAsync(cancellationToken).ConfigureAwait(false); |
| | 2 | 25 | | filter = ValidateFilter(filter); |
| | | 26 | | |
| | 4 | 27 | | await foreach (var item in provider.SubscribeAsync(ConsoleLogFilterMapper.ToStreamingFilter(filter), cancellatio |
| | 0 | 28 | | yield return item; |
| | 2 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Starts pushing stream items to the caller through typed client methods. |
| | | 33 | | /// </summary> |
| | | 34 | | public async Task SubscribeAsync(ElsaConsoleLogFilter? filter) |
| | | 35 | | { |
| | 5 | 36 | | await EnsureCanReadAsync(Context.ConnectionAborted).ConfigureAwait(false); |
| | 4 | 37 | | await subscriptionManager.SubscribeAsync(Context.ConnectionId, ValidateFilter(filter), Context.ConnectionAborted |
| | 4 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Replaces the current pushed subscription filter. |
| | | 42 | | /// </summary> |
| | | 43 | | public async Task UpdateFilterAsync(ElsaConsoleLogFilter? filter) |
| | | 44 | | { |
| | 1 | 45 | | await EnsureCanReadAsync(Context.ConnectionAborted).ConfigureAwait(false); |
| | 0 | 46 | | await subscriptionManager.UpdateFilterAsync(Context.ConnectionId, ValidateFilter(filter), Context.ConnectionAbor |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Stops the current pushed subscription. |
| | | 51 | | /// </summary> |
| | | 52 | | public Task UnsubscribeAsync() |
| | | 53 | | { |
| | 1 | 54 | | return subscriptionManager.UnsubscribeAsync(Context.ConnectionId); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | | 58 | | public override async Task OnDisconnectedAsync(Exception? exception) |
| | | 59 | | { |
| | 0 | 60 | | await UnsubscribeAsync().ConfigureAwait(false); |
| | 0 | 61 | | await base.OnDisconnectedAsync(exception).ConfigureAwait(false); |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | private static ElsaConsoleLogFilter ValidateFilter(ElsaConsoleLogFilter? filter) |
| | | 65 | | { |
| | 6 | 66 | | filter ??= new(); |
| | | 67 | | |
| | 6 | 68 | | if (filter.From is { } from && filter.To is { } to && from > to) |
| | 0 | 69 | | throw new HubException("The console log filter 'from' timestamp must be earlier than or equal to 'to'."); |
| | | 70 | | |
| | 6 | 71 | | return filter; |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | private async ValueTask EnsureCanReadAsync(CancellationToken cancellationToken) |
| | | 75 | | { |
| | 8 | 76 | | if (!await authorizer.CanReadAsync(Context, cancellationToken).ConfigureAwait(false)) |
| | 2 | 77 | | throw new HubException("Access denied."); |
| | 6 | 78 | | } |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | public interface IElsaConsoleLogsClient |
| | | 82 | | { |
| | | 83 | | Task ReceiveConsoleLogLineAsync(ConsoleLogLine line, CancellationToken cancellationToken = default); |
| | | 84 | | Task ReceiveDroppedLinesAsync(ConsoleLogDroppedSummary summary, CancellationToken cancellationToken = default); |
| | | 85 | | Task ReceiveSourceChangedAsync(ConsoleLogSource source, CancellationToken cancellationToken = default); |
| | | 86 | | } |