From a110412665b6154cd84d576c65d0dad48dfdfed4 Mon Sep 17 00:00:00 2001 From: Qstick Date: Sat, 17 Dec 2022 10:27:14 -0600 Subject: [PATCH] Fixed: Stats failing of all indexer events are failures Fixes #1231 --- .../IndexerStatisticsServiceFixture.cs | 55 +++++++++++++++++++ .../IndexerStats/IndexerStatisticsService.cs | 7 ++- 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 src/NzbDrone.Core.Test/IndexerStatsTests/IndexerStatisticsServiceFixture.cs diff --git a/src/NzbDrone.Core.Test/IndexerStatsTests/IndexerStatisticsServiceFixture.cs b/src/NzbDrone.Core.Test/IndexerStatsTests/IndexerStatisticsServiceFixture.cs new file mode 100644 index 000000000..c0ec172b4 --- /dev/null +++ b/src/NzbDrone.Core.Test/IndexerStatsTests/IndexerStatisticsServiceFixture.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using FizzWare.NBuilder; +using FluentAssertions; +using Moq; +using NUnit.Framework; +using NzbDrone.Core.History; +using NzbDrone.Core.Indexers; +using NzbDrone.Core.IndexerStats; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.IndexerStatsTests +{ + public class IndexerStatisticsServiceFixture : CoreTest + { + private IndexerDefinition _indexer; + + [SetUp] + public void Setup() + { + _indexer = Builder.CreateNew().With(x => x.Id = 5).Build(); + + Mocker.GetMock() + .Setup(o => o.All()) + .Returns(new List { _indexer }); + } + + [Test] + public void should_pull_stats_if_all_events_are_failures() + { + var history = new List + { + new History.History + { + Date = DateTime.UtcNow.AddHours(-1), + EventType = HistoryEventType.IndexerRss, + Successful = false, + Id = 8, + IndexerId = 5, + Data = new Dictionary { { "source", "prowlarr" } } + } + }; + + Mocker.GetMock() + .Setup(o => o.Between(It.IsAny(), It.IsAny())) + .Returns((s, f) => history); + + var statistics = Subject.IndexerStatistics(DateTime.UtcNow.AddMonths(-1), DateTime.UtcNow); + + statistics.IndexerStatistics.Count.Should().Be(1); + statistics.IndexerStatistics.First().AverageResponseTime.Should().Be(0); + } + } +} diff --git a/src/NzbDrone.Core/IndexerStats/IndexerStatisticsService.cs b/src/NzbDrone.Core/IndexerStats/IndexerStatisticsService.cs index 02bbbb143..42cd79cad 100644 --- a/src/NzbDrone.Core/IndexerStats/IndexerStatisticsService.cs +++ b/src/NzbDrone.Core/IndexerStats/IndexerStatisticsService.cs @@ -56,9 +56,10 @@ namespace NzbDrone.Core.IndexerStats .ToArray(); int temp = 0; - indexerStats.AverageResponseTime = (int)sortedEvents.Where(h => int.TryParse(h.Data.GetValueOrDefault("elapsedTime"), out temp)) - .Select(h => temp) - .Average(); + var elapsedTimeEvents = sortedEvents.Where(h => int.TryParse(h.Data.GetValueOrDefault("elapsedTime"), out temp)) + .Select(h => temp); + + indexerStats.AverageResponseTime = elapsedTimeEvents.Count() > 0 ? (int)elapsedTimeEvents.Average() : 0; foreach (var historyEvent in sortedEvents) {