Group strategies by base type and rank them by median hold time
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d2cbab5d4b
commit
cbd9830143
12 files changed
+301
-35
No files matched your search
@@ -1,4 +1,7 @@
|
||||
using System.Net;
|
||||
using Flypaper.Core.Models;
|
||||
using Flypaper.Storage.Entities;
|
||||
using Flypaper.Web.Components.Shared;
|
||||
using Flypaper.Storage.Dashboard;
|
||||
using Flypaper.Tests.Fakes;
|
||||
using Flypaper.Tests.Mcp;
|
||||
@@ -47,19 +50,42 @@ public sealed class RecentCatchesTests : IDisposable
|
||||
[Fact]
|
||||
public async Task Matches_strategy_exactly()
|
||||
{
|
||||
var recent = await _queries.GetRecentAsync(new RecentFilter(Strategy: "env-drip"));
|
||||
var recent = await _queries.GetRecentAsync(new RecentFilter(Strategy: StrategyNames.Group("env-drip")));
|
||||
|
||||
recent.Total.ShouldBe(2);
|
||||
recent.Items.ShouldAllBe(e => e.Strategy == "env-drip");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Dashboard_ranks_strategies_among_catches()
|
||||
public async Task Dashboard_lists_strategy_durations_among_catches()
|
||||
{
|
||||
var data = await _queries.GetAsync();
|
||||
|
||||
data.TopStrategies.Select(s => (s.Key, s.Count)).ShouldBe([("html-drip", 4), ("env-drip", 1)]);
|
||||
data.TopStrategies[1].HoldTime.ShouldBe(TimeSpan.FromHours(1));
|
||||
data.StrategyCatches.Count.ShouldBe(5);
|
||||
data.StrategyCatches.Count(s => s.Strategy == "env-drip").ShouldBe(1);
|
||||
data.StrategyCatches.Single(s => s.Strategy == "env-drip").DurationMs.ShouldBe(3_600_000);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Strategy_group_matches_all_variants_of_the_base_type_and_its_old_name()
|
||||
{
|
||||
await using (var db = _data.Factory.CreateDbContext())
|
||||
{
|
||||
foreach (var strategy in new[] { "drip", "html-drip 50-500ms 1-640B", "html-drip 5-20ms 1-64B head 1024B", "html-drip 50-500ms 1-1024B repeat", "html-drip 50-500ms 1-64B head 1024B repeat" })
|
||||
{
|
||||
db.TrapEvents.Add(TrapEventEntity.From(new TrapEvent(Guid.NewGuid(), TrapProtocol.Http, IPAddress.Parse("192.0.2.1"), strategy, McpTestData.Now, McpTestData.Now, 0, TrapOutcome.ClientAborted, null, null)));
|
||||
}
|
||||
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
async Task<IReadOnlyList<string>> Find(string group) =>
|
||||
(await _queries.GetRecentAsync(new RecentFilter(Strategy: StrategyNames.Group(group)))).Items.Select(e => e.Strategy).OrderBy(s => s).ToList();
|
||||
|
||||
(await Find("html-drip")).Where(s => s != "html-drip").ShouldBe(["drip", "html-drip 50-500ms 1-640B"]);
|
||||
(await Find("html-drip head")).ShouldBe(["html-drip 5-20ms 1-64B head 1024B"]);
|
||||
(await Find("html-drip repeat")).ShouldBe(["html-drip 50-500ms 1-1024B repeat"]);
|
||||
(await Find("html-drip head repeat")).ShouldBe(["html-drip 50-500ms 1-64B head 1024B repeat"]);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
|
||||
@@ -13,7 +13,11 @@ public sealed class DashboardFilterTests
|
||||
var filter = DashboardFilter.Parse("24h", "Abgelehnt", "hk", "as4134", " 203.0.113.9 ", "GET /.env", " env-drip ", " wp ", "100");
|
||||
|
||||
filter.ShouldBe(new DashboardFilter("24h", "abgelehnt", "HK", "AS4134", "203.0.113.9", "GET /.env", "env-drip", "wp", 100));
|
||||
filter.ToRecentFilter().ShouldBe(new RecentFilter(TimeSpan.FromHours(24), TrapOutcome.Rejected, "HK", 4134, "203.0.113.9", "GET /.env", "env-drip", "wp", 100));
|
||||
var recent = filter.ToRecentFilter();
|
||||
recent.ShouldBe(new RecentFilter(TimeSpan.FromHours(24), TrapOutcome.Rejected, "HK", 4134, "203.0.113.9", "GET /.env", recent.Strategy, "wp", 100));
|
||||
recent.Strategy!.Bases.ShouldBe(["env-drip"]);
|
||||
recent.Strategy.With.ShouldBeEmpty();
|
||||
recent.Strategy.Without.ShouldBe(["head", "link", "repeat"]);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -44,8 +48,8 @@ public sealed class DashboardFilterTests
|
||||
public void Builds_links_without_defaults_and_with_escaping()
|
||||
{
|
||||
new DashboardFilter().Href().ShouldBe("/#faenge");
|
||||
new DashboardFilter(Land: "CN", Ziel: "GET example.com/a&b", Strategie: "html-drip 50-500ms 1-1024B", Limit: 100).Href("f50")
|
||||
.ShouldBe("/?land=CN&ziel=GET%20example.com%2Fa%26b&strategie=html-drip%2050-500ms%201-1024B&limit=100#f50");
|
||||
new DashboardFilter(Land: "CN", Ziel: "GET example.com/a&b", Strategie: "html-drip head", Limit: 100).Href("f50")
|
||||
.ShouldBe("/?land=CN&ziel=GET%20example.com%2Fa%26b&strategie=html-drip%20head&limit=100#f50");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -58,6 +62,13 @@ public sealed class DashboardFilterTests
|
||||
.ShouldBe(filter);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("html-drip 50-500ms 1-1024B", "html-drip")]
|
||||
[InlineData("drip", "html-drip")]
|
||||
[InlineData("html-drip 50-500ms 1-64B head 1024B", "html-drip head")]
|
||||
public void Normalizes_strategy_links_to_the_group(string strategy, string expected) =>
|
||||
DashboardFilter.Parse(null, null, null, null, null, null, strategy, null, null).Strategie.ShouldBe(expected);
|
||||
|
||||
[Fact]
|
||||
public void Doubles_the_limit_up_to_the_maximum()
|
||||
{
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Flypaper.Core.Strategies;
|
||||
using Flypaper.Storage.Dashboard;
|
||||
using Flypaper.Web.Components.Shared;
|
||||
using Shouldly;
|
||||
|
||||
@@ -63,4 +64,44 @@ public sealed class StrategyNamesTests
|
||||
StrategyNames.Matching("").ShouldBeEmpty();
|
||||
StrategyNames.Matching("nichts").ShouldBeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Groups_variants_by_base_type_and_shows_only_content_differences_as_badges()
|
||||
{
|
||||
StrategyNames.Describe("html-drip 50-500ms 1-1024B").GroupKey.ShouldBe("html-drip");
|
||||
StrategyNames.Describe("html-drip 50-500ms 1-640B").GroupKey.ShouldBe("html-drip");
|
||||
var variant = StrategyNames.Describe("html-drip 5-20ms 1-64B head 1024B repeat");
|
||||
variant.GroupKey.ShouldBe("html-drip head repeat");
|
||||
variant.Badges.ShouldBe(["Schnellstart", "Wiederholer"]);
|
||||
StrategyNames.Describe("maze-drip 50-500ms 1-1024B head 1024B link 160B").Badges.ShouldBe(["Schnellstart"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Treats_drip_as_the_old_name_of_the_endless_page()
|
||||
{
|
||||
var info = StrategyNames.Describe("drip");
|
||||
|
||||
info.DisplayName.ShouldBe("Endlose Webseite");
|
||||
info.GroupKey.ShouldBe("html-drip");
|
||||
StrategyNames.Matching("endlose webseite").ShouldBe(["html-drip", "drip"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ranks_groups_by_median_and_sinks_groups_with_few_catches()
|
||||
{
|
||||
var samples = new List<StrategySample>();
|
||||
samples.AddRange(Enumerable.Repeat(new StrategySample("html-drip 50-500ms 1-1024B", 10_000), 3));
|
||||
samples.AddRange(Enumerable.Repeat(new StrategySample("html-drip 50-500ms 1-640B", 20_000), 2));
|
||||
samples.Add(new StrategySample("drip", 1_000_000));
|
||||
samples.AddRange(Enumerable.Repeat(new StrategySample("env-drip 50-500ms 1-1024B", 30_000), 5));
|
||||
samples.Add(new StrategySample("text-drip", 500_000_000));
|
||||
|
||||
var ranked = StrategyNames.Rank(samples, 10);
|
||||
|
||||
ranked.Select(r => r.Key).ShouldBe(["env-drip", "html-drip", "text-drip"]);
|
||||
ranked[0].MedianHold.ShouldBe(TimeSpan.FromSeconds(30));
|
||||
ranked[1].Count.ShouldBe(6);
|
||||
ranked[1].MedianHold.ShouldBe(TimeSpan.FromSeconds(15));
|
||||
ranked[1].HoldTime.ShouldBe(TimeSpan.FromMilliseconds(1_070_000));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user