Files
Febe/tools/Febe.Sonda/ProbeRecorder.cs
T

273 lines
11 KiB
C#

using System.Globalization;
using System.Text.Json;
using System.Text.Json.Nodes;
using Febe.Browser.Capture;
using Febe.Browser.Cdp;
using Febe.Core.Abstractions;
using Febe.Core.Collection;
namespace Febe.Sonda;
/// <summary>Refuses sample folders inside a git repository (spec §14.5: raw samples stay outside the repository).</summary>
public static class RepositoryGuard
{
/// <summary>The repository that contains <paramref name="folder"/> (its root), or null.</summary>
public static string? FindRepository(string folder)
{
var current = new DirectoryInfo(Path.GetFullPath(folder));
while (current is not null)
{
var marker = Path.Combine(current.FullName, ".git");
if (Directory.Exists(marker) || File.Exists(marker))
{
return current.FullName;
}
current = current.Parent;
}
return null;
}
/// <exception cref="InvalidOperationException">The folder is inside a repository.</exception>
public static void EnsureOutside(string folder)
{
if (FindRepository(folder) is { } repository)
{
throw new InvalidOperationException(
$"La cartella «{Path.GetFullPath(folder)}» sta dentro un repository git ({repository}): i campioni grezzi contengono dati di altre persone e devono stare fuori dal repository (specifica §14.5). Scegli un'altra cartella.");
}
}
}
/// <summary>Counters of a recording.</summary>
public sealed record ProbeStats(int Responses, int Navigations, int Interactions);
/// <summary>
/// The passive probe of M0 (spec §14.5): while Alberto browses by hand, it stores every JSON response the page receives
/// (sanitized descriptor + body) and, for the elements he hovers and focuses, the attributes useful to the target map
/// (role, accessible name, aria-label, link shape), read with a read-only <c>Runtime.evaluate</c> polled every few
/// hundred milliseconds. It never navigates and never performs a gesture: no <c>Input.*</c> command is ever sent.
/// </summary>
/// <remarks>Raw files: <c>risposte/NNNNNN.json</c>, <c>navigazioni.jsonl</c>, <c>interazioni.jsonl</c>.</remarks>
public sealed class ProbeRecorder : IResponseConsumer, IResponseObserver, IAsyncDisposable
{
/// <summary>Read-only description of the hovered and focused interactive elements.</summary>
internal const string InteractionScript = """
(() => {
const clean = (s) => (s || '').replace(/\s+/g, ' ').trim().slice(0, 160);
const interactive = (el) => el && el.closest ? el.closest('a[href], button, [role], input, textarea, select, summary, [tabindex]') : null;
const shape = (href) => {
try {
const u = new URL(href, document.baseURI);
return { host: u.host, path: u.pathname };
} catch { return null; }
};
const describe = (el) => {
if (!el || el === document.body || el === document.documentElement) return null;
const link = el.getAttribute('href');
return {
tag: el.tagName.toLowerCase(),
role: clean(el.getAttribute('role')),
ariaLabel: clean(el.getAttribute('aria-label')),
name: clean(el.getAttribute('aria-label') || el.getAttribute('alt') || el.getAttribute('title') || el.textContent),
href: link ? shape(link) : null,
type: clean(el.getAttribute('type')),
};
};
const hovered = document.querySelectorAll(':hover');
return {
path: location.pathname,
hover: describe(interactive(hovered.length ? hovered[hovered.length - 1] : null)),
focus: describe(interactive(document.activeElement)),
};
})()
""";
private readonly ICdpSession _cdp;
private readonly HostClassifier _hosts;
private readonly IClock _clock;
private readonly string _folder;
private readonly TimeSpan _pollEvery;
private readonly SemaphoreSlim _write = new(1, 1);
private readonly CancellationTokenSource _stop = new();
private ResponseTap? _tap;
private Task? _poller;
private int _responses;
private int _navigations;
private int _interactions;
private string? _lastHover;
private string? _lastFocus;
public ProbeRecorder(ICdpSession cdp, HostClassifier hosts, string folder, IClock? clock = null, TimeSpan? pollEvery = null)
{
_cdp = cdp ?? throw new ArgumentNullException(nameof(cdp));
_hosts = hosts ?? throw new ArgumentNullException(nameof(hosts));
_folder = Path.GetFullPath(folder);
_clock = clock ?? new SystemClock();
_pollEvery = pollEvery ?? TimeSpan.FromMilliseconds(400);
}
public string Folder => _folder;
public ProbeStats Stats => new(Volatile.Read(ref _responses), Volatile.Read(ref _navigations), Volatile.Read(ref _interactions));
public async Task StartAsync(CancellationToken ct = default)
{
RepositoryGuard.EnsureOutside(_folder);
Directory.CreateDirectory(Path.Combine(_folder, "risposte"));
await File.WriteAllTextAsync(
Path.Combine(_folder, "LEGGIMI.txt"),
"Campioni GREZZI della sonda di Febe: contengono dati personali di Alberto e di altre persone.\n"
+ "Non vanno nel repository e non si inviano: si ripuliscono con «febe-sonda ripulisci», si controllano con «febe-sonda verifica»\n"
+ "e si cancellano alla fine di M0 (specifica §14.5).\n",
ct).ConfigureAwait(false);
_tap = new ResponseTap(_cdp, _hosts, _clock);
_tap.AddConsumer(this);
_tap.AddObserver(this);
await _tap.StartAsync(ct).ConfigureAwait(false);
_poller = Task.Run(() => PollAsync(_stop.Token), CancellationToken.None);
}
public async Task StopAsync()
{
await _stop.CancelAsync().ConfigureAwait(false);
if (_poller is not null)
{
try
{
await _poller.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
}
}
if (_tap is not null)
{
await _tap.DisposeAsync().ConfigureAwait(false);
_tap = null;
}
}
public async ValueTask DisposeAsync()
{
await StopAsync().ConfigureAwait(false);
_stop.Dispose();
_write.Dispose();
}
async ValueTask IResponseConsumer.ConsumeAsync(ResponseDescriptor response, JsonDocument body, CancellationToken ct)
{
var number = Interlocked.Increment(ref _responses);
var sample = new JsonObject
{
["descrittore"] = new JsonObject
{
["indirizzo"] = response.Url,
["host"] = response.HostKind switch { HostKind.Site => "sito", HostKind.Cdn => "cdn", _ => "altro" },
["stato"] = response.Status,
["tipo"] = response.MimeType,
["risorsa"] = response.ResourceType,
["quando"] = response.At.UtcDateTime.ToString("yyyy-MM-dd'T'HH:mm:ss'Z'", CultureInfo.InvariantCulture),
},
["corpo"] = JsonNode.Parse(body.RootElement.GetRawText()),
};
var file = Path.Combine(_folder, "risposte", number.ToString("000000", CultureInfo.InvariantCulture) + ".json");
await File.WriteAllTextAsync(file, sample.ToJsonString(), ct).ConfigureAwait(false);
}
void IResponseObserver.OnResponse(ResponseDescriptor response)
{
}
void IResponseObserver.OnNavigation(NavigationDescriptor navigation)
{
Interlocked.Increment(ref _navigations);
var line = new JsonObject
{
["quando"] = navigation.At.UtcDateTime.ToString("yyyy-MM-dd'T'HH:mm:ss'Z'", CultureInfo.InvariantCulture),
["indirizzo"] = navigation.Url,
["host"] = navigation.HostKind switch { HostKind.Site => "sito", HostKind.Cdn => "cdn", _ => "altro" },
["stesso_documento"] = navigation.SameDocument,
};
_ = AppendLineAsync("navigazioni.jsonl", line);
}
private async Task PollAsync(CancellationToken ct)
{
while (!ct.IsCancellationRequested)
{
try
{
var state = await _cdp.EvaluateAsync(InteractionScript, ct).ConfigureAwait(false);
if (state.ValueKind == JsonValueKind.Object)
{
_lastHover = await RecordAsync(state, "hover", "sopra", _lastHover).ConfigureAwait(false);
_lastFocus = await RecordAsync(state, "focus", "fuoco", _lastFocus).ConfigureAwait(false);
}
}
catch (OperationCanceledException) when (ct.IsCancellationRequested)
{
return;
}
catch (Exception ex) when (ex is CdpCommandException or CdpEvaluationException or CdpDisconnectedException or BrowserUnavailableException or TimeoutException)
{
// Page changing or browser restarting: try again at the next tick.
}
await _clock.Delay(_pollEvery, ct).ConfigureAwait(false);
}
}
/// <summary>Writes a line when the described element changed; returns the new «last seen» value.</summary>
private async Task<string?> RecordAsync(JsonElement state, string property, string kind, string? last)
{
if (!state.TryGetProperty(property, out var element) || element.ValueKind != JsonValueKind.Object)
{
return null;
}
var raw = element.GetRawText();
if (raw == last)
{
return last;
}
Interlocked.Increment(ref _interactions);
var node = JsonNode.Parse(raw)!.AsObject();
if (node["href"] is JsonObject href && href["host"]?.GetValue<string>() is { } linkHost)
{
// Only whether the link stays on the site: the host of an external link may say something about a person.
href["host"] = _hosts.Classify($"https://{linkHost}/") switch { HostKind.Site => "sito", HostKind.Cdn => "cdn", _ => "altro" };
}
var line = new JsonObject
{
["quando"] = _clock.Now.UtcDateTime.ToString("yyyy-MM-dd'T'HH:mm:ss'Z'", CultureInfo.InvariantCulture),
["evento"] = kind,
["percorso"] = state.TryGetProperty("path", out var path) ? path.GetString() : null,
["elemento"] = node,
};
await AppendLineAsync("interazioni.jsonl", line).ConfigureAwait(false);
return raw;
}
private async Task AppendLineAsync(string name, JsonObject line)
{
await _write.WaitAsync().ConfigureAwait(false);
try
{
await File.AppendAllTextAsync(Path.Combine(_folder, name), line.ToJsonString() + "\n").ConfigureAwait(false);
}
catch (ObjectDisposedException)
{
}
finally
{
_write.Release();
}
}
}