feat: track watch time from playback events
Daily per-user aggregates, counted on the server clock between progress reports. Includes the cleanup task for rows past 90 days.
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Jellyfin.Plugin.Herald.Stats;
|
||||
|
||||
/// <summary>
|
||||
/// Builds per-user summaries from the daily aggregates.
|
||||
/// </summary>
|
||||
public class StatsService
|
||||
{
|
||||
/// <summary>
|
||||
/// How many days of aggregates are kept.
|
||||
/// </summary>
|
||||
public const int RetentionDays = 90;
|
||||
|
||||
private readonly IWatchStatsStore _store;
|
||||
private readonly TimeProvider _timeProvider;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="StatsService"/> class.
|
||||
/// </summary>
|
||||
/// <param name="store">The stats store.</param>
|
||||
/// <param name="timeProvider">The time provider.</param>
|
||||
public StatsService(IWatchStatsStore store, TimeProvider timeProvider)
|
||||
{
|
||||
_store = store;
|
||||
_timeProvider = timeProvider;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current server-local day.
|
||||
/// </summary>
|
||||
public DateOnly Today => DateOnly.FromDateTime(_timeProvider.GetLocalNow().DateTime);
|
||||
|
||||
/// <summary>
|
||||
/// Summarizes the last <paramref name="days"/> days (today included) per user.
|
||||
/// </summary>
|
||||
/// <param name="days">The number of days, clamped to 1..<see cref="RetentionDays"/>.</param>
|
||||
/// <param name="userNames">All existing users by id. Users without activity are listed with zeros.</param>
|
||||
/// <returns>One summary per user, ordered by name.</returns>
|
||||
public IReadOnlyList<UserWatchSummary> GetSummary(int days, IReadOnlyDictionary<Guid, string> userNames)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(userNames);
|
||||
|
||||
days = Math.Clamp(days, 1, RetentionDays);
|
||||
var byUser = _store.GetSince(Today.AddDays(1 - days)).ToLookup(s => s.UserId);
|
||||
|
||||
return userNames.Keys
|
||||
.Union(byUser.Select(g => g.Key))
|
||||
.Select(userId =>
|
||||
{
|
||||
var rows = byUser[userId].ToList();
|
||||
var seconds = rows.Sum(r => r.WatchedSeconds);
|
||||
return new UserWatchSummary
|
||||
{
|
||||
UserId = userId,
|
||||
UserName = userNames.GetValueOrDefault(userId),
|
||||
WatchedSeconds = (long)Math.Round(seconds),
|
||||
WatchedHours = Math.Round(seconds / 3600d, 2),
|
||||
PlayCount = rows.Sum(r => r.PlayCount),
|
||||
LastActivity = rows.Count == 0 ? null : rows.Max(r => r.LastActivity)
|
||||
};
|
||||
})
|
||||
.OrderBy(s => s.UserName is null)
|
||||
.ThenBy(s => s.UserName, StringComparer.OrdinalIgnoreCase)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes aggregates that fell out of the retention window.
|
||||
/// </summary>
|
||||
/// <returns>The number of deleted rows.</returns>
|
||||
public int Cleanup() => _store.DeleteBefore(Today.AddDays(-RetentionDays));
|
||||
}
|
||||
Reference in New Issue
Block a user