From b645ea85fad3ed51927d5367ec36951927987b99 Mon Sep 17 00:00:00 2001 From: ConfusedPolarBear <33811686+ConfusedPolarBear@users.noreply.github.com> Date: Sun, 19 Jun 2022 23:58:59 -0500 Subject: [PATCH] Include analysis progress from previous versions --- .../ScheduledTasks/FingerprinterTask.cs | 53 +++++++++++++++---- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/FingerprinterTask.cs b/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/FingerprinterTask.cs index 8a4b187..f3a2013 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/FingerprinterTask.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/FingerprinterTask.cs @@ -109,7 +109,9 @@ public class FingerprinterTask : IScheduledTask "No episodes to analyze: either no show libraries are defined or ffmpeg could not be found"); } - var totalProcessed = 0; + // Include the previously processed episodes in the percentage reported to the UI. + var totalProcessed = CountProcessedEpisodes(); + var options = new ParallelOptions() { MaxDegreeOfParallelism = Plugin.Instance!.Configuration.MaxParallelism @@ -121,7 +123,10 @@ public class FingerprinterTask : IScheduledTask try { - AnalyzeSeason(season, cancellationToken); + // Increment totalProcessed by the number of episodes in this season that were actually analyzed + // (instead of just using the number of episodes in the current season). + var analyzed = AnalyzeSeason(season, cancellationToken); + Interlocked.Add(ref totalProcessed, analyzed); } catch (FingerprintException ex) { @@ -149,20 +154,49 @@ public class FingerprinterTask : IScheduledTask } } - totalProcessed += season.Value.Count; progress.Report((totalProcessed * 100) / Plugin.Instance!.TotalQueued); }); return Task.CompletedTask; } - private void AnalyzeSeason( + /// + /// Count the number of previously processed episodes to ensure the reported progress is correct. + /// + /// Number of previously processed episodes. + private int CountProcessedEpisodes() + { + var previous = 0; + + foreach (var season in Plugin.Instance!.AnalysisQueue) + { + foreach (var episode in season.Value) + { + if (!Plugin.Instance!.Intros.TryGetValue(episode.EpisodeId, out var intro) || !intro.Valid) + { + continue; + } + + previous++; + } + } + + return previous; + } + + /// + /// Fingerprints all episodes in the provided season and stores the timestamps of all introductions. + /// + /// Pairing of season GUID to a list of QueuedEpisode objects. + /// Cancellation token provided by the scheduled task. + /// Number of episodes from the provided season that were analyzed. + private int AnalyzeSeason( KeyValuePair> season, CancellationToken cancellationToken) { var seasonIntros = new Dictionary(); - - var first = season.Value[0]; + var episodes = season.Value; + var first = episodes[0]; /* Don't analyze specials or seasons with an insufficient number of episodes. * A season with only 1 episode can't be analyzed as it would compare the episode to itself, @@ -170,10 +204,9 @@ public class FingerprinterTask : IScheduledTask */ if (season.Value.Count < 2 || first.SeasonNumber == 0) { - return; + return episodes.Count; } - var episodes = season.Value; var unanalyzed = false; // Only log an analysis message if there are unanalyzed episodes in this season. @@ -201,7 +234,7 @@ public class FingerprinterTask : IScheduledTask first.SeriesName, first.SeasonNumber); - return; + return 0; } // Ensure there are an even number of episodes @@ -287,6 +320,8 @@ public class FingerprinterTask : IScheduledTask { Plugin.Instance!.SaveTimestamps(); } + + return episodes.Count; } ///