| | | 1 | | using Elsa.Diagnostics.StructuredLogs.Contracts; |
| | | 2 | | using Elsa.Diagnostics.StructuredLogs.Models; |
| | | 3 | | using Microsoft.AspNetCore.Authorization; |
| | | 4 | | using Microsoft.AspNetCore.SignalR; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Diagnostics.StructuredLogs.RealTime; |
| | | 7 | | |
| | | 8 | | [Authorize] |
| | 0 | 9 | | public class StructuredLogsHub(StructuredLogSubscriptionManager subscriptionManager) : Hub<IStructuredLogsClient> |
| | | 10 | | { |
| | | 11 | | public async Task SubscribeAsync(StructuredLogFilter? filter) |
| | | 12 | | { |
| | 0 | 13 | | await subscriptionManager.SubscribeAsync(Context.ConnectionId, ValidateFilter(filter), Context.ConnectionAborted |
| | 0 | 14 | | } |
| | | 15 | | |
| | 0 | 16 | | public Task UpdateFilterAsync(StructuredLogFilter? filter) => subscriptionManager.UpdateFilterAsync(Context.Connecti |
| | | 17 | | |
| | | 18 | | public Task UnsubscribeAsync() |
| | | 19 | | { |
| | 0 | 20 | | return subscriptionManager.UnsubscribeAsync(Context.ConnectionId); |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | public override async Task OnDisconnectedAsync(Exception? exception) |
| | | 24 | | { |
| | 0 | 25 | | await UnsubscribeAsync(); |
| | 0 | 26 | | await base.OnDisconnectedAsync(exception); |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | private static StructuredLogFilter ValidateFilter(StructuredLogFilter? filter) |
| | | 30 | | { |
| | 0 | 31 | | filter ??= new(); |
| | | 32 | | |
| | 0 | 33 | | if (filter.From is { } from && filter.To is { } to && from > to) |
| | 0 | 34 | | throw new HubException("The log filter 'from' timestamp must be earlier than or equal to 'to'."); |
| | | 35 | | |
| | 0 | 36 | | return filter; |
| | | 37 | | } |
| | | 38 | | } |