namespace ConfusedPolarBear.Plugin.IntroSkipper; using System; using System.Threading; using System.Text.Json; using System.Text.Json.Serialization; /// /// Detailed statistics about the last analysis operation performed. All times are represented as milliseconds. /// public class AnalysisStatistics { /// /// Gets the number of episodes that have been analyzed so far. /// public ThreadSafeInteger TotalAnalyzedEpisodes { get; } = new ThreadSafeInteger(); /// /// Gets or sets the number of episodes that need to be analyzed. /// public int TotalQueuedEpisodes { get; set; } /// /// Gets the number of times an index search successfully located a pair of introductions. /// public ThreadSafeInteger IndexSearches { get; } = new ThreadSafeInteger(); /// /// Gets the number of times a quick scan successfully located a pair of introductions. /// public ThreadSafeInteger QuickScans { get; } = new ThreadSafeInteger(); /// /// Gets the number of times a full scan successfully located a pair of introductions. /// public ThreadSafeInteger FullScans { get; } = new ThreadSafeInteger(); /// /// Gets the total CPU time spent waiting for audio fingerprints to be generated. /// public ThreadSafeInteger FingerprintCPUTime { get; } = new ThreadSafeInteger(); /// /// Gets the total CPU time spent analyzing fingerprints in the initial pass. /// public ThreadSafeInteger FirstPassCPUTime { get; } = new ThreadSafeInteger(); /// /// Gets the total CPU time spent analyzing fingerprints in the second pass. /// public ThreadSafeInteger SecondPassCPUTime { get; } = new ThreadSafeInteger(); /// /// Gets the total task runtime across all threads. /// public ThreadSafeInteger TotalCPUTime { get; } = new ThreadSafeInteger(); /// /// Gets the total task runtime as measured by a clock. /// public ThreadSafeInteger TotalTaskTime { get; } = new ThreadSafeInteger(); } /// /// Convenience wrapper around a thread safe integer. /// [JsonConverter(typeof(ThreadSafeIntegerJsonConverter))] public class ThreadSafeInteger { private int value = 0; /// /// Gets the current value stored by this integer. /// public int Value { get { return value; } } /// /// Increment the value of this integer by 1. /// public void Increment() { Add(1); } /// /// Adds the total milliseconds elapsed since a start time. /// /// Start time. public void AddDuration(DateTime start) { if (start == DateTime.MinValue) { return; } var elapsed = DateTime.Now.Subtract(start); Add((int)elapsed.TotalMilliseconds); } /// /// Adds the provided amount to this integer. /// /// Amount to add. public void Add(int amount) { Interlocked.Add(ref value, amount); } } /// /// Serialize thread safe integers to a regular integer (instead of an object with a Value property). /// public class ThreadSafeIntegerJsonConverter : JsonConverter { /// /// Deserialization of TSIs is not supported and will always throw a NotSupportedException. /// /// Reader. /// Type. /// Options. /// Never returns. public override ThreadSafeInteger? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { throw new NotSupportedException(); } /// /// Serialize the provided TSI. /// /// Writer. /// TSI. /// Options. public override void Write(Utf8JsonWriter writer, ThreadSafeInteger value, JsonSerializerOptions options) { writer.WriteNumberValue(value.Value); } }