Rapporto di diagnostica della raccolta (#39)
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>
This commit is contained in:
1 parent
bf03cb45a5
commit
badd1681a0
55 files changed
+5395
-134
No files matched your search
@@ -1,6 +1,7 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using Febe.Core.Collection.Privacy;
|
||||
|
||||
namespace Febe.Core.Collection;
|
||||
|
||||
@@ -85,6 +86,11 @@ public sealed partial class SiteMap
|
||||
private readonly Dictionary<string, string> _pages;
|
||||
private readonly HashSet<string> _configuredTargets;
|
||||
private readonly HashSet<string> _knownTargets;
|
||||
private readonly IReadOnlyList<(string Name, Regex Shape)> _classifier;
|
||||
private readonly HashSet<string> _literals;
|
||||
|
||||
/// <summary>The fixed words and the placeholder names of the shipped maps (the only ones a report may show).</summary>
|
||||
private static readonly Lazy<(HashSet<string> Words, HashSet<string> Placeholders)> Shipped = new(LoadShippedWords);
|
||||
|
||||
public SiteMap(string site, IReadOnlyDictionary<string, string?> pages, IEnumerable<string> configuredTargets, IEnumerable<string> knownTargets, string? source = null)
|
||||
{
|
||||
@@ -93,6 +99,20 @@ public sealed partial class SiteMap
|
||||
_pages = pages.Where(p => !string.IsNullOrWhiteSpace(p.Value)).ToDictionary(p => p.Key, p => p.Value!.Trim(), StringComparer.Ordinal);
|
||||
_configuredTargets = new HashSet<string>(configuredTargets, StringComparer.Ordinal);
|
||||
_knownTargets = new HashSet<string>(knownTargets, StringComparer.Ordinal);
|
||||
|
||||
// More literal segments first (the highlight viewer before the story viewer, whose {username} would take
|
||||
// «highlights»), shapes without a query before the same shape with one, then the order of SitePages.All: a page
|
||||
// made only of placeholders (the profile, «/{username}/») comes after every page with a word of its own.
|
||||
_classifier = [.. _pages
|
||||
.Select(p => (Name: p.Key, Pattern: p.Value, Shape: p.Value.Split('?')[0]))
|
||||
.OrderByDescending(p => LiteralSegments(p.Shape))
|
||||
.ThenBy(p => p.Pattern.Contains('?', StringComparison.Ordinal) ? 1 : 0)
|
||||
.ThenBy(p => IndexOfPage(p.Name))
|
||||
.ThenBy(p => p.Name, StringComparer.Ordinal)
|
||||
.Select(p => (p.Name, new Regex(ShapeRegex(p.Shape, out _), RegexOptions.CultureInvariant | RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(100))))];
|
||||
_literals = new HashSet<string>(
|
||||
_pages.Values.SelectMany(v => v.Split('?')[0].Split('/', StringSplitOptions.RemoveEmptyEntries)).Where(segment => !Placeholder().IsMatch(segment)),
|
||||
StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
public string Site { get; }
|
||||
@@ -144,26 +164,8 @@ public sealed partial class SiteMap
|
||||
return null;
|
||||
}
|
||||
|
||||
var shape = pattern.Split('?')[0];
|
||||
var names = new List<string>();
|
||||
var regex = new StringBuilder("^");
|
||||
var last = 0;
|
||||
foreach (Match m in Placeholder().Matches(shape))
|
||||
{
|
||||
regex.Append(Regex.Escape(shape[last..m.Index]));
|
||||
regex.Append("([^/]+)");
|
||||
names.Add(m.Groups[1].Value);
|
||||
last = m.Index + m.Length;
|
||||
}
|
||||
|
||||
regex.Append(Regex.Escape(shape[last..]));
|
||||
if (!shape.EndsWith('/'))
|
||||
{
|
||||
regex.Append("/?");
|
||||
}
|
||||
|
||||
regex.Append('$');
|
||||
var match = Regex.Match(path, regex.ToString(), RegexOptions.CultureInvariant | RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(100));
|
||||
var regex = ShapeRegex(pattern.Split('?')[0], out var names);
|
||||
var match = Regex.Match(path, regex, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(100));
|
||||
if (!match.Success)
|
||||
{
|
||||
return null;
|
||||
@@ -178,6 +180,75 @@ public sealed partial class SiteMap
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Which page of the map an address path has the shape of, or null when none (data endpoints, pages the map does not
|
||||
/// know). Pages with more words of their own are tried first, so «/stories/highlights/1/» is the highlight viewer
|
||||
/// and not the story viewer of someone called «highlights»; the profile, made only of a placeholder, comes last.
|
||||
/// A placeholder never takes a fixed word of the site (<see cref="PathPatterns.IsKnownSegment"/>) or of a map:
|
||||
/// «/challenge/», «/explore/», «/reels/» are not profiles. Only the page NAME comes out: never the values (issue #39).
|
||||
/// </summary>
|
||||
public string? Classify(string? path)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach (var (name, shape) in _classifier)
|
||||
{
|
||||
try
|
||||
{
|
||||
var match = shape.Match(path);
|
||||
if (match.Success && !TakesAWord(match))
|
||||
{
|
||||
return name;
|
||||
}
|
||||
}
|
||||
catch (RegexMatchTimeoutException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The shape of the address of page <paramref name="name"/> for a report: without its query, with its
|
||||
/// {placeholders} and no value (e.g. «/{username}/»); null when the page is not set. A map may come from Alberto's
|
||||
/// data folder, so the shape is reduced like any path: only the fixed words of the site and of the shipped maps
|
||||
/// and their placeholders stay, any other segment becomes {numero} or {testo} («/alberto.rossi/following/» written
|
||||
/// in place of «/{username}/following/» gives «/{testo}/following/»).
|
||||
/// </summary>
|
||||
public string? PatternOf(string name) =>
|
||||
_pages.TryGetValue(name, out var pattern) ? ReportShape(pattern.Split('?')[0]) : null;
|
||||
|
||||
/// <summary>A page shape reduced for a report (<see cref="PatternOf"/>).</summary>
|
||||
public static string ReportShape(string shape)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(shape);
|
||||
var (words, placeholders) = Shipped.Value;
|
||||
var segments = shape.Split('/');
|
||||
for (var i = 0; i < segments.Length; i++)
|
||||
{
|
||||
var segment = segments[i];
|
||||
if (segment.Length == 0 || PathPatterns.IsKnownSegment(segment) || words.Contains(segment))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var whole = Placeholder().Match(segment);
|
||||
if (whole.Success && whole.Length == segment.Length && placeholders.Contains(whole.Groups[1].Value))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
segments[i] = PathPatterns.Of("/" + segment)[1..];
|
||||
}
|
||||
|
||||
return string.Join('/', segments);
|
||||
}
|
||||
|
||||
/// <summary>The target map of the local test site (embedded <c>bersagli.prova.json</c>).</summary>
|
||||
public static SiteMap LoadTestSite() => LoadEmbedded("bersagli.prova.json");
|
||||
|
||||
@@ -261,6 +332,85 @@ public sealed partial class SiteMap
|
||||
}
|
||||
}
|
||||
|
||||
private static string ShapeRegex(string shape, out List<string> names)
|
||||
{
|
||||
names = [];
|
||||
var regex = new StringBuilder("^");
|
||||
var last = 0;
|
||||
foreach (Match m in Placeholder().Matches(shape))
|
||||
{
|
||||
regex.Append(Regex.Escape(shape[last..m.Index]));
|
||||
regex.Append("([^/]+)");
|
||||
names.Add(m.Groups[1].Value);
|
||||
last = m.Index + m.Length;
|
||||
}
|
||||
|
||||
regex.Append(Regex.Escape(shape[last..]));
|
||||
if (!shape.EndsWith('/'))
|
||||
{
|
||||
regex.Append("/?");
|
||||
}
|
||||
|
||||
regex.Append('$');
|
||||
return regex.ToString();
|
||||
}
|
||||
|
||||
/// <summary>True when a placeholder of the match took a fixed word of the site or of a map (not a value, so not this page).</summary>
|
||||
private bool TakesAWord(Match match)
|
||||
{
|
||||
var shippedWords = Shipped.Value.Words;
|
||||
for (var i = 1; i < match.Groups.Count; i++)
|
||||
{
|
||||
var value = match.Groups[i].Value;
|
||||
if (PathPatterns.IsKnownSegment(value) || PathPatterns.IsKnownSegment(value.ToLowerInvariant())
|
||||
|| _literals.Contains(value) || shippedWords.Contains(value))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static (HashSet<string> Words, HashSet<string> Placeholders) LoadShippedWords()
|
||||
{
|
||||
var words = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
var placeholders = new HashSet<string>(StringComparer.Ordinal);
|
||||
foreach (var file in new[] { "bersagli.instagram.json", "bersagli.prova.json" })
|
||||
{
|
||||
foreach (var pattern in LoadEmbedded(file)._pages.Values)
|
||||
{
|
||||
// The names of the values the recipes fill in, in the path or in the query («{cursor}»).
|
||||
placeholders.UnionWith(Placeholder().Matches(pattern).Select(m => m.Groups[1].Value));
|
||||
foreach (var segment in pattern.Split('?')[0].Split('/', StringSplitOptions.RemoveEmptyEntries))
|
||||
{
|
||||
if (!Placeholder().IsMatch(segment))
|
||||
{
|
||||
words.Add(segment);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (words, placeholders);
|
||||
}
|
||||
|
||||
private static int LiteralSegments(string shape) =>
|
||||
shape.Split('/', StringSplitOptions.RemoveEmptyEntries).Count(segment => !Placeholder().IsMatch(segment));
|
||||
|
||||
private static int IndexOfPage(string name)
|
||||
{
|
||||
for (var i = 0; i < SitePages.All.Count; i++)
|
||||
{
|
||||
if (SitePages.All[i] == name)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return int.MaxValue;
|
||||
}
|
||||
|
||||
private static bool IsConfigured(JsonElement target)
|
||||
{
|
||||
foreach (var key in new[] { "role", "name", "href" })
|
||||
|
||||
Reference in new issue
Block a user