Dopo una prova dal vivo andata male, Alberto deve poter mandare un solo file che spieghi perché, senza che nessuno veda dati di altre persone. Attività ha ora il pulsante «Diagnostica», che scarica il rapporto delle sessioni degli ultimi 14 giorni: pagine aperte e forma degli indirizzi, risposte per tipo, stato e dimensione con ciò che i lettori ne hanno fatto, struttura stretta delle forme sconosciute, ricette, segnali, permessi negati, file in coda e scaricati, e il motivo di ogni arresto. Nel rapporto entrano solo valori da elenchi chiusi, numeri, orari e forme: mai nomi, indirizzi completi, codici, cursori, cookie o pezzi di risposte. Le forme degli indirizzi e la regola delle chiavi-dato passano dalla sonda a Core, e la sonda le richiama. Il registratore non lancia mai eccezioni verso la raccolta e tiene i file in Dati/diagnostica per 14 giorni. Prova che il rapporto non contiene valori, con controlli di mutazione; tre giri di revisione avversaria (25 difetti confermati, tutti corretti). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
273 lines
8.7 KiB
C#
273 lines
8.7 KiB
C#
using System.Text.Json;
|
|
using Febe.Core.Abstractions;
|
|
using Febe.Core.Collection.Diagnostics;
|
|
using Febe.Core.Collection.Readers;
|
|
|
|
namespace Febe.Core.Collection.Recipes;
|
|
|
|
/// <summary>One response read by the reader, in arrival order.</summary>
|
|
/// <param name="Sequence">Arrival number, from 1.</param>
|
|
/// <param name="Response">Sanitized descriptor (no query string).</param>
|
|
/// <param name="Result">What the reader recognized, detached from the body.</param>
|
|
public sealed record CapturedRead(long Sequence, ResponseDescriptor Response, ReadResult Result);
|
|
|
|
/// <summary>
|
|
/// Bridge between the response tap and the recipes (spec §6.2, §6.4): every JSON body the site sends is read by the
|
|
/// <see cref="ShapeReader"/> as it arrives, and the recipes wait for what they expect after a gesture. Bodies are
|
|
/// never kept: only the reader's detached results are.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// It also tracks which data responses are still on their way (their body not read yet), so a recipe can wait until
|
|
/// the page has settled. Thread-safe.
|
|
/// </remarks>
|
|
public sealed class CaptureCollector : IResponseConsumer, IResponseObserver
|
|
{
|
|
private const int MaxKept = 2000;
|
|
|
|
private readonly ShapeReader _reader;
|
|
private readonly IClock _clock;
|
|
private readonly Lock _gate = new();
|
|
private readonly List<CapturedRead> _reads = [];
|
|
private readonly Dictionary<string, DateTimeOffset> _pending = new(StringComparer.Ordinal);
|
|
private long _sequence;
|
|
private string? _viewerId;
|
|
|
|
public CaptureCollector(ShapeReader reader, IClock clock)
|
|
{
|
|
_reader = reader ?? throw new ArgumentNullException(nameof(reader));
|
|
_clock = clock ?? throw new ArgumentNullException(nameof(clock));
|
|
}
|
|
|
|
public ShapeReader Reader => _reader;
|
|
|
|
/// <summary>
|
|
/// The recorder of the diagnostic report (issue #39), told what the readers did with every body, also the ones
|
|
/// not kept (another host, unrelated); null: nothing is recorded.
|
|
/// </summary>
|
|
public CollectionDiagnostics? Diagnostics { get; init; }
|
|
|
|
/// <summary>How long a data response may stay «on its way» before it is forgotten (its body may never come).</summary>
|
|
public TimeSpan PendingLifetime { get; init; } = TimeSpan.FromSeconds(15);
|
|
|
|
/// <summary>
|
|
/// Identifier of the connected account in the latest site answer that gave one (<see cref="ReadResult.ViewerId"/>),
|
|
/// also an answer that is otherwise <see cref="ReadShape.Unrelated"/> and not kept.
|
|
/// </summary>
|
|
public string? ViewerId
|
|
{
|
|
get
|
|
{
|
|
lock (_gate)
|
|
{
|
|
return _viewerId;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>Sequence number of the last read: pass it to <see cref="Since"/> to get what arrives after now.</summary>
|
|
public long Mark()
|
|
{
|
|
lock (_gate)
|
|
{
|
|
return _sequence;
|
|
}
|
|
}
|
|
|
|
/// <summary>Reads that arrived after <paramref name="mark"/>, oldest first.</summary>
|
|
public IReadOnlyList<CapturedRead> Since(long mark)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
return [.. _reads.Where(r => r.Sequence > mark)];
|
|
}
|
|
}
|
|
|
|
/// <summary>Data responses whose body has not been read yet.</summary>
|
|
public int Pending
|
|
{
|
|
get
|
|
{
|
|
lock (_gate)
|
|
{
|
|
ExpirePending();
|
|
return _pending.Count;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Waits until the reads after <paramref name="mark"/> satisfy <paramref name="until"/>, or <paramref name="timeout"/>
|
|
/// passes. Returns the reads after the mark either way.
|
|
/// </summary>
|
|
public async Task<IReadOnlyList<CapturedRead>> WaitAsync(long mark, Func<IReadOnlyList<CapturedRead>, bool> until, TimeSpan timeout, TimeSpan poll, CancellationToken ct)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(until);
|
|
var deadline = _clock.Now + timeout;
|
|
while (true)
|
|
{
|
|
var reads = Since(mark);
|
|
if (until(reads) || _clock.Now >= deadline)
|
|
{
|
|
return reads;
|
|
}
|
|
|
|
await _clock.Delay(poll, ct).ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Waits until no data response is on its way and nothing new arrived for <paramref name="quiet"/>, at most
|
|
/// <paramref name="timeout"/>.
|
|
/// </summary>
|
|
public async Task SettleAsync(TimeSpan quiet, TimeSpan timeout, TimeSpan poll, CancellationToken ct)
|
|
{
|
|
var deadline = _clock.Now + timeout;
|
|
var lastSeen = Mark();
|
|
var quietSince = _clock.Now;
|
|
while (_clock.Now < deadline)
|
|
{
|
|
await _clock.Delay(poll, ct).ConfigureAwait(false);
|
|
var current = Mark();
|
|
if (current != lastSeen || Pending > 0)
|
|
{
|
|
lastSeen = current;
|
|
quietSince = _clock.Now;
|
|
continue;
|
|
}
|
|
|
|
if (_clock.Now - quietSince >= quiet)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>Forgets everything (start of a recipe).</summary>
|
|
public void Clear()
|
|
{
|
|
lock (_gate)
|
|
{
|
|
_reads.Clear();
|
|
_pending.Clear();
|
|
}
|
|
}
|
|
|
|
ValueTask IResponseConsumer.ConsumeAsync(ResponseDescriptor response, JsonDocument body, CancellationToken ct)
|
|
{
|
|
Accept(response, body.RootElement);
|
|
return ValueTask.CompletedTask;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads one body (also used by tests and the probe samples). Returns null, and keeps nothing, for responses that
|
|
/// are not the site's or that are <see cref="ReadShape.Unrelated"/>.
|
|
/// </summary>
|
|
public CapturedRead? Accept(ResponseDescriptor response, JsonElement body)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(response);
|
|
lock (_gate)
|
|
{
|
|
_pending.Remove(response.RequestId);
|
|
}
|
|
|
|
if (response.HostKind != HostKind.Site)
|
|
{
|
|
Observe(response, null, body);
|
|
return null;
|
|
}
|
|
|
|
var result = _reader.Read(body);
|
|
Observe(response, result, body);
|
|
if (result.ViewerId is { } viewerId)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
_viewerId = viewerId;
|
|
}
|
|
}
|
|
|
|
if (result.Shape == ReadShape.Unrelated)
|
|
{
|
|
// Not about contents (promotions, logging): neither data nor an empty page, and not «something new
|
|
// arrived» for SettleAsync, or a page that keeps logging would never look settled.
|
|
return null;
|
|
}
|
|
|
|
lock (_gate)
|
|
{
|
|
var read = new CapturedRead(++_sequence, response, result);
|
|
_reads.Add(read);
|
|
if (_reads.Count > MaxKept)
|
|
{
|
|
_reads.RemoveRange(0, _reads.Count - MaxKept);
|
|
}
|
|
|
|
return read;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tells the diagnostics what the readers made of a body; for an unknown shape, its strict key structure, taken
|
|
/// now, while the document is alive. Never a reason to fail the reading.
|
|
/// </summary>
|
|
private void Observe(ResponseDescriptor response, ReadResult? result, JsonElement body)
|
|
{
|
|
if (Diagnostics is not { } diagnostics)
|
|
{
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
var structure = result?.Shape == ReadShape.Unrecognized ? KeyStructure.DescribeStrict(body) : null;
|
|
diagnostics.ReadObserved(response, result, structure);
|
|
}
|
|
catch (Exception ex) when (ex is not OutOfMemoryException)
|
|
{
|
|
// Diagnostics are optional: the reading goes on as if they were not there.
|
|
}
|
|
}
|
|
|
|
void IResponseObserver.OnResponse(ResponseDescriptor response)
|
|
{
|
|
if (response.HostKind == HostKind.Site
|
|
&& DataResponses.IsData(response.MimeType, response.ResourceType)
|
|
&& response.Status is >= 200 and < 600 && response.Status != 204)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
_pending[response.RequestId] = _clock.Now;
|
|
}
|
|
}
|
|
}
|
|
|
|
void IResponseObserver.OnBodyDropped(string requestId)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
_pending.Remove(requestId);
|
|
}
|
|
}
|
|
|
|
void IResponseObserver.OnNavigation(NavigationDescriptor navigation)
|
|
{
|
|
if (!navigation.SameDocument)
|
|
{
|
|
// Bodies of the previous document will not be read any more (the tap forgets them too).
|
|
lock (_gate)
|
|
{
|
|
_pending.Clear();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ExpirePending()
|
|
{
|
|
var limit = _clock.Now - PendingLifetime;
|
|
foreach (var key in _pending.Where(p => p.Value < limit).Select(p => p.Key).ToList())
|
|
{
|
|
_pending.Remove(key);
|
|
}
|
|
}
|
|
}
|