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,64 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Model.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Plugin.Herald.Stats;
|
||||
|
||||
/// <summary>
|
||||
/// Daily task that removes watch-time aggregates past the retention window.
|
||||
/// </summary>
|
||||
public class StatsCleanupTask : IScheduledTask
|
||||
{
|
||||
private readonly StatsService _stats;
|
||||
private readonly ILogger<StatsCleanupTask> _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="StatsCleanupTask"/> class.
|
||||
/// </summary>
|
||||
/// <param name="stats">The stats service.</param>
|
||||
/// <param name="logger">The logger.</param>
|
||||
public StatsCleanupTask(StatsService stats, ILogger<StatsCleanupTask> logger)
|
||||
{
|
||||
_stats = stats;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Name => "Clean up Herald watch-time stats";
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Key => "HeraldStatsCleanup";
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Description => "Deletes daily watch-time aggregates older than 90 days.";
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Category => "Herald";
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(progress);
|
||||
|
||||
var deleted = _stats.Cleanup();
|
||||
_logger.LogInformation("Removed {Count} watch-time rows older than {Days} days", deleted, StatsService.RetentionDays);
|
||||
progress.Report(100);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
|
||||
{
|
||||
return
|
||||
[
|
||||
new TaskTriggerInfo
|
||||
{
|
||||
Type = TaskTriggerInfoType.DailyTrigger,
|
||||
TimeOfDayTicks = TimeSpan.FromHours(3.5).Ticks
|
||||
}
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user