27 lines
1.1 KiB
C#
27 lines
1.1 KiB
C#
using PrivaPub.Infrastructure.Statistics;
|
|||
|
|
using PrivaPub.Models.Statistics;
|
||
|
|
|
||
|
|
using System.Collections.Concurrent;
|
||
|
|
|
||
|
|
namespace PrivaPub.Tests.Support
|
||
|
|
{
|
||
|
|
public sealed class MemoryLedger : IInteractionLedger
|
||
|
|
{
|
||
|
|
public ConcurrentQueue<(InteractionEvent Event, string ActorUri, bool HostClaimed)> Events { get; } = new();
|
||
|
|
public ConcurrentDictionary<(string Host, string Key), long> Counts { get; } = new();
|
||
|
|
public ConcurrentDictionary<(string Section, string Key), long> ServerCounts { get; } = new();
|
||
|
|
|
||
|
|
public long Dropped => 0;
|
||
|
|
|
||
|
|
public void Record(InteractionEvent interaction, string actorUri = default, bool hostClaimed = false) =>
|
||
|
|
Events.Enqueue((Interactions.Sanitize(interaction), actorUri, hostClaimed));
|
||
|
|
|
||
|
|
public void Count(string host, string key, long bytes = 0) =>
|
||
|
|
Counts.AddOrUpdate((Interactions.Host(host), key), 1, (_, count) => count + 1);
|
||
|
|
|
||
|
|
public void CountServer(string section, string key, int? latencyMs = default) =>
|
||
|
|
ServerCounts.AddOrUpdate((section, key), 1, (_, count) => count + 1);
|
||
|
|
|
||
|
|
public IReadOnlyList<InteractionEvent> Of(string channel) => Events.Select(e => e.Event).Where(e => e.Channel == channel).ToList();
|
||
|
|
}
|
||
|
|
}
|