Daily per-user aggregates, counted on the server clock between progress reports. Includes the cleanup task for rows past 90 days.
182 lines
6.0 KiB
C#
182 lines
6.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
|
|
namespace Jellyfin.Plugin.Herald.Stats;
|
|
|
|
/// <summary>
|
|
/// Turns playback reports into watch-time increments.
|
|
/// Time is measured on the server clock between two reports of the same playback, so seeking never adds time.
|
|
/// </summary>
|
|
public class WatchTimeTracker
|
|
{
|
|
/// <summary>
|
|
/// The most time a single report can add.
|
|
/// </summary>
|
|
public static readonly TimeSpan MaxDelta = TimeSpan.FromSeconds(60);
|
|
|
|
/// <summary>
|
|
/// A playback without reports for this long counts as closed.
|
|
/// </summary>
|
|
public static readonly TimeSpan StaleAfter = TimeSpan.FromMinutes(5);
|
|
|
|
private readonly Dictionary<string, Playback> _playbacks = new(StringComparer.Ordinal);
|
|
private readonly Lock _lock = new();
|
|
private readonly TimeZoneInfo _timeZone;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="WatchTimeTracker"/> class.
|
|
/// </summary>
|
|
/// <param name="timeZone">The zone used to assign time to days.</param>
|
|
public WatchTimeTracker(TimeZoneInfo timeZone)
|
|
{
|
|
_timeZone = timeZone;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the number of playbacks currently tracked.
|
|
/// </summary>
|
|
public int ActiveCount
|
|
{
|
|
get
|
|
{
|
|
lock (_lock)
|
|
{
|
|
return _playbacks.Count;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handles the start of a playback.
|
|
/// </summary>
|
|
/// <param name="key">Identifies the playback.</param>
|
|
/// <param name="userIds">The watching users.</param>
|
|
/// <param name="now">The time of the report.</param>
|
|
/// <returns>The increments to store.</returns>
|
|
public IReadOnlyList<WatchIncrement> Start(string key, IReadOnlyCollection<Guid> userIds, DateTimeOffset now)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(userIds);
|
|
|
|
lock (_lock)
|
|
{
|
|
_playbacks[key] = new Playback(now, false);
|
|
}
|
|
|
|
var date = LocalDate(now);
|
|
return userIds.Select(id => new WatchIncrement(id, date, 0, 1, now.UtcDateTime)).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handles a progress report.
|
|
/// </summary>
|
|
/// <param name="key">Identifies the playback.</param>
|
|
/// <param name="userIds">The watching users.</param>
|
|
/// <param name="isPaused">Whether playback is paused as of this report.</param>
|
|
/// <param name="now">The time of the report.</param>
|
|
/// <returns>The increments to store.</returns>
|
|
public IReadOnlyList<WatchIncrement> Progress(string key, IReadOnlyCollection<Guid> userIds, bool isPaused, DateTimeOffset now)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(userIds);
|
|
|
|
TimeSpan watched;
|
|
lock (_lock)
|
|
{
|
|
watched = _playbacks.TryGetValue(key, out var playback) ? Elapsed(playback, now) : TimeSpan.Zero;
|
|
_playbacks[key] = new Playback(now, isPaused);
|
|
}
|
|
|
|
return Split(userIds, watched, now);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handles the end of a playback.
|
|
/// </summary>
|
|
/// <param name="key">Identifies the playback.</param>
|
|
/// <param name="userIds">The watching users.</param>
|
|
/// <param name="now">The time of the report.</param>
|
|
/// <returns>The increments to store.</returns>
|
|
public IReadOnlyList<WatchIncrement> Stop(string key, IReadOnlyCollection<Guid> userIds, DateTimeOffset now)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(userIds);
|
|
|
|
TimeSpan watched;
|
|
lock (_lock)
|
|
{
|
|
watched = _playbacks.Remove(key, out var playback) ? Elapsed(playback, now) : TimeSpan.Zero;
|
|
}
|
|
|
|
return Split(userIds, watched, now);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Forgets playbacks that stopped reporting.
|
|
/// </summary>
|
|
/// <param name="now">The current time.</param>
|
|
/// <returns>The number of closed playbacks.</returns>
|
|
public int CloseStale(DateTimeOffset now)
|
|
{
|
|
lock (_lock)
|
|
{
|
|
var stale = _playbacks.Where(p => now - p.Value.LastReport > StaleAfter).Select(p => p.Key).ToList();
|
|
foreach (var key in stale)
|
|
{
|
|
_playbacks.Remove(key);
|
|
}
|
|
|
|
return stale.Count;
|
|
}
|
|
}
|
|
|
|
private static TimeSpan Elapsed(Playback playback, DateTimeOffset now)
|
|
{
|
|
var elapsed = now - playback.LastReport;
|
|
|
|
// Paused stretches, clock jumps and playbacks that went silent add nothing.
|
|
if (playback.Paused || elapsed <= TimeSpan.Zero || elapsed > StaleAfter)
|
|
{
|
|
return TimeSpan.Zero;
|
|
}
|
|
|
|
return elapsed > MaxDelta ? MaxDelta : elapsed;
|
|
}
|
|
|
|
private List<WatchIncrement> Split(IReadOnlyCollection<Guid> userIds, TimeSpan watched, DateTimeOffset now)
|
|
{
|
|
var increments = new List<WatchIncrement>();
|
|
if (watched <= TimeSpan.Zero)
|
|
{
|
|
return increments;
|
|
}
|
|
|
|
var localEnd = TimeZoneInfo.ConvertTime(now, _timeZone);
|
|
var localStart = TimeZoneInfo.ConvertTime(now - watched, _timeZone);
|
|
var endDate = DateOnly.FromDateTime(localEnd.DateTime);
|
|
var startDate = DateOnly.FromDateTime(localStart.DateTime);
|
|
|
|
// A delta is at most a minute long, so it touches two days at most.
|
|
var secondsToday = startDate == endDate ? watched.TotalSeconds : Math.Min(watched.TotalSeconds, localEnd.TimeOfDay.TotalSeconds);
|
|
var secondsYesterday = watched.TotalSeconds - secondsToday;
|
|
|
|
foreach (var userId in userIds)
|
|
{
|
|
if (secondsYesterday > 0)
|
|
{
|
|
increments.Add(new WatchIncrement(userId, startDate, secondsYesterday, 0, (now - TimeSpan.FromSeconds(secondsToday)).UtcDateTime));
|
|
}
|
|
|
|
if (secondsToday > 0)
|
|
{
|
|
increments.Add(new WatchIncrement(userId, endDate, secondsToday, 0, now.UtcDateTime));
|
|
}
|
|
}
|
|
|
|
return increments;
|
|
}
|
|
|
|
private DateOnly LocalDate(DateTimeOffset time) => DateOnly.FromDateTime(TimeZoneInfo.ConvertTime(time, _timeZone).DateTime);
|
|
|
|
private readonly record struct Playback(DateTimeOffset LastReport, bool Paused);
|
|
}
|