Significantly refactored code to help with readability of logging and episode processing
This commit is contained in:
@@ -11,71 +11,76 @@ using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Plugin.MediaCleaner.Helpers;
|
||||
|
||||
public class SeriesHelper
|
||||
public class SeriesHelper(ILogger logger)
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly LoggingHelper _loggingHelper;
|
||||
|
||||
public SeriesHelper(ILogger logger)
|
||||
{
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
_loggingHelper = new LoggingHelper(logger);
|
||||
}
|
||||
private readonly LoggingHelper _loggingHelper = new(logger);
|
||||
|
||||
private static PluginConfiguration Configuration =>
|
||||
Plugin.Instance!.Configuration;
|
||||
|
||||
public bool IsSeasonUserDataStale(IReadOnlyList<BaseItem> episodes)
|
||||
private List<BaseItem> ProcessEpisodes(IReadOnlyCollection<BaseItem> episodes)
|
||||
{
|
||||
bool seasonIsStale = false;
|
||||
|
||||
List<BaseItem> staleEpisodes = [];
|
||||
|
||||
var episodesWithUserData = episodes.Where(episode => episode.UserData.Where(data => data.LastPlayedDate != null).ToList().Count > 0).ToList();
|
||||
|
||||
_loggingHelper.PrintDebugEpisodesWithUserData(episodesWithUserData);
|
||||
|
||||
foreach (var episode in episodesWithUserData)
|
||||
List<BaseItem> staleEpisodes = [.. episodes
|
||||
.Where(episode =>
|
||||
{
|
||||
bool episodeIsStale = false;
|
||||
|
||||
var mostRecentUserData = episode.UserData.OrderByDescending(data => data.LastPlayedDate).First(data => data.LastPlayedDate != null);
|
||||
var staleLastPlayedDate = mostRecentUserData.LastPlayedDate < DateTime.Now.AddDays(-Configuration.StaleMediaCutoff);
|
||||
var staleCreationDate = episode.DateCreated < DateTime.Now.AddDays(-Configuration.StaleMediaCutoff);
|
||||
var hasUserDataWithLastPlayedDate = episode.UserData.Any(data => data.LastPlayedDate != null);
|
||||
|
||||
if (staleCreationDate && !hasUserDataWithLastPlayedDate){
|
||||
_loggingHelper.LogInformation("Creation date is stale, and no user data for episode {Episode}.", episode);
|
||||
episodeIsStale = true;
|
||||
}
|
||||
|
||||
if (hasUserDataWithLastPlayedDate){
|
||||
UserData mostRecentUserData = episode.UserData
|
||||
.OrderByDescending(data => data.LastPlayedDate)
|
||||
.First();
|
||||
|
||||
_loggingHelper.LogDebugInformation("User data for episode: {Episode}", episode);
|
||||
_loggingHelper.LogDebugInformation("-------------------------------------------------");
|
||||
|
||||
if(Configuration.DebugMode){
|
||||
_logger.LogInformation("User data for episode: {Episode}", episode);
|
||||
_logger.LogInformation("-------------------------------------------------");
|
||||
foreach (var property in typeof(UserData).GetProperties())
|
||||
{
|
||||
_logger.LogInformation("{PropertyName}: {PropertyValue}", property.Name, property.GetValue(mostRecentUserData));
|
||||
_loggingHelper.LogDebugInformation("{PropertyName}: {PropertyValue}", property.Name, property.GetValue(mostRecentUserData));
|
||||
}
|
||||
_logger.LogInformation("-------------------------------------------------");
|
||||
}
|
||||
|
||||
if (staleLastPlayedDate && staleCreationDate)
|
||||
{
|
||||
episodeIsStale = true;
|
||||
if(Configuration.DebugMode){
|
||||
_logger.LogInformation("Most recent user data has a last played date of: {LastPlayedDate}.", [mostRecentUserData.LastPlayedDate]);
|
||||
_logger.LogInformation("And episode created {DateCreated}.", episode.DateCreated);
|
||||
_loggingHelper.LogDebugInformation("-------------------------------------------------");
|
||||
|
||||
bool staleLastPlayedDate = mostRecentUserData.LastPlayedDate < DateTime.Now.AddDays(-Configuration.StaleMediaCutoff);
|
||||
|
||||
if (staleLastPlayedDate && staleCreationDate)
|
||||
{
|
||||
episodeIsStale = true;
|
||||
_loggingHelper.LogDebugInformation("Most recent user data has a last played date of: {LastPlayedDate}.", [mostRecentUserData.LastPlayedDate]);
|
||||
_loggingHelper.LogDebugInformation("And episode created {DateCreated}.", episode.DateCreated);
|
||||
_loggingHelper.LogDebugInformation("Episode is marked as stale.");
|
||||
}
|
||||
}
|
||||
|
||||
if (episodeIsStale)
|
||||
{
|
||||
staleEpisodes.Add(episode);
|
||||
if(Configuration.DebugMode){
|
||||
_logger.LogInformation("Episode is stale.");
|
||||
}
|
||||
}
|
||||
return episodeIsStale;
|
||||
})];
|
||||
|
||||
return staleEpisodes;
|
||||
}
|
||||
|
||||
public bool IsSeasonDataStale(IReadOnlyList<BaseItem> episodes)
|
||||
{
|
||||
if(episodes == null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(episodes);
|
||||
}
|
||||
|
||||
bool seasonIsStale = false;
|
||||
|
||||
List<BaseItem> staleEpisodes = ProcessEpisodes(episodes);
|
||||
|
||||
if(staleEpisodes.Count == episodes.Count)
|
||||
{
|
||||
seasonIsStale = true;
|
||||
_logger.LogInformation("Stale episodes count matches all episode count. Season is stale.");
|
||||
_logger.LogInformation("-------------------------------------------------");
|
||||
_loggingHelper.LogDebugInformation("Stale episodes count matches season episode count. Season is stale.");
|
||||
_loggingHelper.LogDebugInformation("-------------------------------------------------");
|
||||
}
|
||||
|
||||
return seasonIsStale;
|
||||
|
||||
Reference in New Issue
Block a user