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,103 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Jellyfin.Plugin.Herald.Stats;
|
||||
|
||||
namespace Jellyfin.Plugin.Herald.Storage;
|
||||
|
||||
/// <summary>
|
||||
/// SQLite backed <see cref="IWatchStatsStore"/>.
|
||||
/// </summary>
|
||||
public class SqliteWatchStatsStore : IWatchStatsStore
|
||||
{
|
||||
private readonly HeraldDatabase _database;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SqliteWatchStatsStore"/> class.
|
||||
/// </summary>
|
||||
/// <param name="database">The database.</param>
|
||||
public SqliteWatchStatsStore(HeraldDatabase database)
|
||||
{
|
||||
_database = database;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Add(IReadOnlyCollection<WatchIncrement> increments)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(increments);
|
||||
if (increments.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_database.Write((connection, transaction) =>
|
||||
{
|
||||
using var command = Db.Command(
|
||||
connection,
|
||||
transaction,
|
||||
"""
|
||||
INSERT INTO watch_daily (user_id, date, watched_seconds, play_count, last_activity)
|
||||
VALUES ($userId, $date, $seconds, $plays, $activity)
|
||||
ON CONFLICT (user_id, date) DO UPDATE SET
|
||||
watched_seconds = watched_seconds + excluded.watched_seconds,
|
||||
play_count = play_count + excluded.play_count,
|
||||
last_activity = max(last_activity, excluded.last_activity);
|
||||
""");
|
||||
var userId = command.Parameters.Add("$userId", Microsoft.Data.Sqlite.SqliteType.Text);
|
||||
var date = command.Parameters.Add("$date", Microsoft.Data.Sqlite.SqliteType.Text);
|
||||
var seconds = command.Parameters.Add("$seconds", Microsoft.Data.Sqlite.SqliteType.Real);
|
||||
var plays = command.Parameters.Add("$plays", Microsoft.Data.Sqlite.SqliteType.Integer);
|
||||
var activity = command.Parameters.Add("$activity", Microsoft.Data.Sqlite.SqliteType.Text);
|
||||
|
||||
foreach (var increment in increments)
|
||||
{
|
||||
userId.Value = Db.Id(increment.UserId);
|
||||
date.Value = Db.Date(increment.Date);
|
||||
seconds.Value = increment.Seconds;
|
||||
plays.Value = increment.Plays;
|
||||
activity.Value = Db.Time(increment.Activity);
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
return increments.Count;
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyList<DailyWatchStat> GetSince(DateOnly from)
|
||||
{
|
||||
return _database.Read(connection =>
|
||||
{
|
||||
using var command = Db.Command(
|
||||
connection,
|
||||
null,
|
||||
"SELECT user_id, date, watched_seconds, play_count, last_activity FROM watch_daily WHERE date >= $from ORDER BY date, user_id;");
|
||||
command.Parameters.AddWithValue("$from", Db.Date(from));
|
||||
using var reader = command.ExecuteReader();
|
||||
var stats = new List<DailyWatchStat>();
|
||||
while (reader.Read())
|
||||
{
|
||||
stats.Add(new DailyWatchStat
|
||||
{
|
||||
UserId = Db.Id(reader.GetString(0)),
|
||||
Date = Db.Date(reader.GetString(1)),
|
||||
WatchedSeconds = reader.GetDouble(2),
|
||||
PlayCount = reader.GetInt32(3),
|
||||
LastActivity = Db.Time(reader.GetString(4))
|
||||
});
|
||||
}
|
||||
|
||||
return stats;
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public int DeleteBefore(DateOnly cutoff)
|
||||
{
|
||||
return _database.Write((connection, transaction) =>
|
||||
{
|
||||
using var command = Db.Command(connection, transaction, "DELETE FROM watch_daily WHERE date < $cutoff;");
|
||||
command.Parameters.AddWithValue("$cutoff", Db.Date(cutoff));
|
||||
return command.ExecuteNonQuery();
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user