< Summary

Information
Class: Intentum.Core.UBA.UserProfile
Assembly: Intentum.Core
File(s): /home/runner/work/Intentum/Intentum/src/Intentum.Core/UBA/UserProfile.cs
Line coverage
92%
Covered lines: 37
Uncovered lines: 3
Coverable lines: 40
Total lines: 69
Line coverage: 92.5%
Branch coverage
92%
Covered branches: 13
Total branches: 14
Branch coverage: 92.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%22100%
Learn(...)100%22100%
CalculateDeviation(...)90%101095.23%

File(s)

/home/runner/work/Intentum/Intentum/src/Intentum.Core/UBA/UserProfile.cs

#LineLine coverage
 1using Intentum.Core.Behavior;
 2
 3namespace Intentum.Core.UBA;
 4
 5/// <summary>
 6/// Tracks a user's baseline behavior for deviation detection.
 7/// </summary>
 8public sealed class UserProfile
 9{
 010    public string UserId { get; }
 011    public DateTimeOffset CreatedAt { get; }
 1012    public int TotalSessions { get; private set; }
 3113    public Dictionary<string, int> ActionFrequency { get; } = new(StringComparer.OrdinalIgnoreCase);
 1314    public List<DateTimeOffset> ActivityTimestamps { get; } = [];
 15
 516    public UserProfile(string userId)
 517    {
 518        UserId = userId ?? throw new ArgumentNullException(nameof(userId));
 419        CreatedAt = DateTimeOffset.UtcNow;
 420    }
 21
 22    /// <summary>
 23    /// Learns from a behavior space, updating the baseline profile.
 24    /// </summary>
 25    public void Learn(BehaviorSpace space)
 326    {
 327        TotalSessions++;
 2528        foreach (var evt in space.Events)
 829        {
 830            ActionFrequency.TryGetValue(evt.Action, out var count);
 831            ActionFrequency[evt.Action] = count + 1;
 832            ActivityTimestamps.Add(evt.OccurredAt);
 833        }
 334    }
 35
 36    /// <summary>
 37    /// Calculates a deviation score [0-1] between the current behavior and the baseline.
 38    /// Higher values indicate more unusual behavior.
 39    /// </summary>
 40    public double CalculateDeviation(BehaviorSpace currentSpace)
 341    {
 342        if (TotalSessions == 0 || ActionFrequency.Count == 0)
 143            return 0;
 44
 245        var totalActions = ActionFrequency.Values.Sum();
 246        var unknownActionCount = 0;
 247        var knownActionDeviation = 0.0;
 48
 1449        foreach (var evt in currentSpace.Events)
 450        {
 451            if (!ActionFrequency.TryGetValue(evt.Action, out var baselineCount))
 252            {
 253                unknownActionCount++;
 254                continue;
 55            }
 56
 257            var normalizedBaseline = baselineCount / (double)totalActions;
 258            knownActionDeviation += Math.Abs(1.0 / currentSpace.Events.Count - normalizedBaseline);
 259        }
 60
 261        if (currentSpace.Events.Count == 0)
 062            return 0;
 63
 264        var unknownRatio = unknownActionCount / (double)currentSpace.Events.Count;
 265        var deviation = (unknownRatio * 0.7) + (Math.Min(knownActionDeviation, 1.0) * 0.3);
 66
 267        return Math.Clamp(deviation, 0, 1);
 368    }
 69}