From f568fdad6a528724254092366edfc6bd85692ab9 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sat, 23 Feb 2013 13:29:22 -0800 Subject: [PATCH] moved history over to objectdb --- .../UpgradeHistorySpecificationFixture.cs | 9 +- NzbDrone.Core.Test/Framework/CoreTest.cs | 2 +- .../HistoryTests/HistoryServiceTest.cs | 108 +++++++ NzbDrone.Core.Test/NzbDrone.Core.Test.csproj | 2 +- .../DownloadProviderFixture.cs | 22 +- .../ProviderTests/HistoryProviderTest.cs | 278 ------------------ NzbDrone.Core/Datastore/ModelBase.cs | 4 +- .../UpgradeHistorySpecification.cs | 9 +- NzbDrone.Core/History/History.cs | 19 ++ NzbDrone.Core/History/HistoryRepository.cs | 41 +++ NzbDrone.Core/History/HistoryService.cs | 47 +++ NzbDrone.Core/Model/HistoryQueryModel.cs | 27 -- NzbDrone.Core/NzbDrone.Core.csproj | 10 +- .../DownloadClients/BlackholeProvider.cs | 3 +- NzbDrone.Core/Providers/DownloadProvider.cs | 17 +- NzbDrone.Core/Providers/HistoryProvider.cs | 118 -------- NzbDrone.Core/Providers/StatsProvider.cs | 2 +- NzbDrone.Core/Repository/History.cs | 29 -- NzbDrone.Core/Tv/QualityModel.cs | 5 +- NzbDrone.Core/packages.config | 1 - 20 files changed, 259 insertions(+), 494 deletions(-) create mode 100644 NzbDrone.Core.Test/HistoryTests/HistoryServiceTest.cs delete mode 100644 NzbDrone.Core.Test/ProviderTests/HistoryProviderTest.cs create mode 100644 NzbDrone.Core/History/History.cs create mode 100644 NzbDrone.Core/History/HistoryRepository.cs create mode 100644 NzbDrone.Core/History/HistoryService.cs delete mode 100644 NzbDrone.Core/Model/HistoryQueryModel.cs delete mode 100644 NzbDrone.Core/Providers/HistoryProvider.cs delete mode 100644 NzbDrone.Core/Repository/History.cs diff --git a/NzbDrone.Core.Test/DecisionEngineTests/UpgradeHistorySpecificationFixture.cs b/NzbDrone.Core.Test/DecisionEngineTests/UpgradeHistorySpecificationFixture.cs index af5d53349..c833648b4 100644 --- a/NzbDrone.Core.Test/DecisionEngineTests/UpgradeHistorySpecificationFixture.cs +++ b/NzbDrone.Core.Test/DecisionEngineTests/UpgradeHistorySpecificationFixture.cs @@ -5,6 +5,7 @@ using System.Linq; using FizzWare.NBuilder; using FluentAssertions; using NUnit.Framework; +using NzbDrone.Core.History; using NzbDrone.Core.Tv; using NzbDrone.Core.Model; using NzbDrone.Core.Providers; @@ -65,9 +66,9 @@ namespace NzbDrone.Core.Test.DecisionEngineTests firstQuality = new QualityModel(QualityTypes.Bluray1080p, true); secondQuality = new QualityModel(QualityTypes.Bluray1080p, true); - Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(fakeSeries.SeriesId, 12, 3)).Returns(firstQuality); - Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(fakeSeries.SeriesId, 12, 4)).Returns(secondQuality); - Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(fakeSeries.SeriesId, 12, 5)).Returns(null); + Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(fakeSeries.SeriesId, 12, 3)).Returns(firstQuality); + Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(fakeSeries.SeriesId, 12, 4)).Returns(secondQuality); + Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(fakeSeries.SeriesId, 12, 5)).Returns(null); } private void WithFirstReportUpgradable() @@ -123,7 +124,7 @@ namespace NzbDrone.Core.Test.DecisionEngineTests parseResultSingle.Quality = new QualityModel(QualityTypes.WEBDL1080p, false); firstQuality = new QualityModel(QualityTypes.WEBDL1080p, false); - Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(fakeSeries.SeriesId, 12, 3)).Returns(firstQuality); + Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(fakeSeries.SeriesId, 12, 3)).Returns(firstQuality); _upgradeHistory.IsSatisfiedBy(parseResultSingle).Should().BeFalse(); } diff --git a/NzbDrone.Core.Test/Framework/CoreTest.cs b/NzbDrone.Core.Test/Framework/CoreTest.cs index 3d8132e16..767202879 100644 --- a/NzbDrone.Core.Test/Framework/CoreTest.cs +++ b/NzbDrone.Core.Test/Framework/CoreTest.cs @@ -28,7 +28,7 @@ namespace NzbDrone.Core.Test.Framework } } - public abstract class CoreTest : CoreTest where TSubject: class + public abstract class CoreTest : CoreTest where TSubject : class { private TSubject _subject; diff --git a/NzbDrone.Core.Test/HistoryTests/HistoryServiceTest.cs b/NzbDrone.Core.Test/HistoryTests/HistoryServiceTest.cs new file mode 100644 index 000000000..bf8a25345 --- /dev/null +++ b/NzbDrone.Core.Test/HistoryTests/HistoryServiceTest.cs @@ -0,0 +1,108 @@ +using System; +using System.Linq; +using FizzWare.NBuilder; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.History; +using NzbDrone.Core.Tv; +using NzbDrone.Core.Repository.Quality; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.HistoryTests +{ + [TestFixture] + public class HistoryServiceTest : ObjectDbTest + { + [Test] + public void Trim_Items() + { + var historyItem = Builder.CreateListOfSize(30) + .All() + .With(c=>c.OID = 0) + .TheFirst(10).With(c => c.Date = DateTime.Now) + .TheNext(20).With(c => c.Date = DateTime.Now.AddDays(-31)) + .Build(); + + Db.InsertMany(historyItem); + + AllStoredModels.Should().HaveCount(30); + Subject.Trim(); + + AllStoredModels.Should().HaveCount(10); + AllStoredModels.Should().OnlyContain(s => s.Date > DateTime.Now.AddDays(-30)); + } + + + [Test] + public void GetBestQualityInHistory_no_result() + { + Subject.GetBestQualityInHistory(12, 12, 12).Should().Be(null); + } + + [Test] + public void GetBestQualityInHistory_single_result() + { + var series = Builder.CreateNew().Build(); + var episode = Builder.CreateNew() + .With(c => c.Series = series) + .With(c => c.SeriesId = series.OID) + .Build(); + + + + var history = Builder.CreateNew() + .With(c => c.OID = 0) + .With(h => h.Quality = new QualityModel(QualityTypes.Bluray720p, true)) + .With(h => h.Episode = episode) + .Build(); + + Db.Insert(history); + + var result = Subject.GetBestQualityInHistory(episode.SeriesId, episode.SeasonNumber, episode.EpisodeNumber); + + result.Should().NotBeNull(); + result.Quality.Should().Be(QualityTypes.Bluray720p); + result.Proper.Should().BeTrue(); + } + + [Test] + public void GetBestQualityInHistory_should_return_highest_result() + { + + var series = Builder.CreateNew().Build(); + var episode = Builder.CreateNew() + .With(c => c.Series = series) + .With(c => c.SeriesId = series.OID) + .Build(); + + + var history = Builder + .CreateListOfSize(5) + .All() + .With(c => c.OID = 0) + .With(h => h.Episode = episode) + .TheFirst(1) + .With(h => h.Quality = new QualityModel(QualityTypes.DVD, true)) + .TheNext(1) + .With(h => h.Quality = new QualityModel(QualityTypes.Bluray720p, true)) + .TheNext(1) + .With(h => h.Quality = new QualityModel(QualityTypes.Bluray720p, true)) + .TheNext(1) + .With(h => h.Quality = new QualityModel(QualityTypes.Bluray720p, false)) + .TheNext(1) + .With(h => h.Quality = new QualityModel(QualityTypes.SDTV, true)) + .Build(); + + Db.InsertMany(history); + + var result = Subject.GetBestQualityInHistory(episode.SeriesId, episode.SeasonNumber, episode.EpisodeNumber); + + result.Should().NotBeNull(); + result.Quality.Should().Be(QualityTypes.Bluray720p); + result.Proper.Should().BeTrue(); + } + + + + } +} \ No newline at end of file diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 0084db5b2..fe86bac90 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -248,7 +248,7 @@ - + diff --git a/NzbDrone.Core.Test/ProviderTests/DownloadProviderTests/DownloadProviderFixture.cs b/NzbDrone.Core.Test/ProviderTests/DownloadProviderTests/DownloadProviderFixture.cs index 6500681b4..c6e2e0879 100644 --- a/NzbDrone.Core.Test/ProviderTests/DownloadProviderTests/DownloadProviderFixture.cs +++ b/NzbDrone.Core.Test/ProviderTests/DownloadProviderTests/DownloadProviderFixture.cs @@ -5,12 +5,12 @@ using FizzWare.NBuilder; using FluentAssertions; using Moq; using NUnit.Framework; +using NzbDrone.Core.History; using NzbDrone.Core.Tv; using NzbDrone.Core.Model; using NzbDrone.Core.Providers; using NzbDrone.Core.Providers.Core; using NzbDrone.Core.Providers.DownloadClients; -using NzbDrone.Core.Repository; using NzbDrone.Core.Repository.Quality; using NzbDrone.Core.Test.Framework; @@ -98,11 +98,11 @@ namespace NzbDrone.Core.Test.ProviderTests.DownloadProviderTests Mocker.GetMock() .Verify(s => s.DownloadNzb(It.IsAny(), It.IsAny(), true), Times.Never()); - Mocker.GetMock() - .Verify(s => s.Add(It.Is(h => h.EpisodeId == 12 && h.SeriesId == 5)), Times.Once()); + Mocker.GetMock() + .Verify(s => s.Add(It.Is(h => h.Episode == parseResult.Episodes[0])), Times.Once()); - Mocker.GetMock() - .Verify(s => s.Add(It.Is(h => h.EpisodeId == 99 && h.SeriesId == 5)), Times.Once()); + Mocker.GetMock() + .Verify(s => s.Add(It.Is(h => h.Episode == parseResult.Episodes[1])), Times.Once()); Mocker.GetMock() .Verify(c => c.MarkEpisodeAsFetched(12)); @@ -133,11 +133,11 @@ namespace NzbDrone.Core.Test.ProviderTests.DownloadProviderTests Mocker.GetMock() .Verify(s => s.DownloadNzb(It.IsAny(), It.IsAny(), true), Times.Once()); - Mocker.GetMock() - .Verify(s => s.Add(It.Is(h => h.EpisodeId == 12 && h.SeriesId == 5)), Times.Once()); + Mocker.GetMock() + .Verify(s => s.Add(It.Is(h => h.Episode == parseResult.Episodes[0])), Times.Once()); - Mocker.GetMock() - .Verify(s => s.Add(It.Is(h => h.EpisodeId == 99 && h.SeriesId == 5)), Times.Once()); + Mocker.GetMock() + .Verify(s => s.Add(It.Is(h => h.Episode == parseResult.Episodes[1])), Times.Once()); Mocker.GetMock() .Verify(c => c.MarkEpisodeAsFetched(12)); @@ -161,8 +161,8 @@ namespace NzbDrone.Core.Test.ProviderTests.DownloadProviderTests //Act Mocker.Resolve().DownloadReport(parseResult); - Mocker.GetMock() - .Verify(s => s.Add(It.IsAny()), Times.Never()); + Mocker.GetMock() + .Verify(s => s.Add(It.IsAny()), Times.Never()); Mocker.GetMock() diff --git a/NzbDrone.Core.Test/ProviderTests/HistoryProviderTest.cs b/NzbDrone.Core.Test/ProviderTests/HistoryProviderTest.cs deleted file mode 100644 index 4e6457355..000000000 --- a/NzbDrone.Core.Test/ProviderTests/HistoryProviderTest.cs +++ /dev/null @@ -1,278 +0,0 @@ -using System; -using System.Linq; -using FizzWare.NBuilder; -using FluentAssertions; -using NUnit.Framework; -using NzbDrone.Core.Tv; -using NzbDrone.Core.Providers; -using NzbDrone.Core.Repository; -using NzbDrone.Core.Repository.Quality; -using NzbDrone.Core.Test.Framework; - -namespace NzbDrone.Core.Test.ProviderTests -{ - [TestFixture] - // ReSharper disable InconsistentNaming - public class HistoryProviderTest : SqlCeTest - { - [Test] - public void AllItems() - { - WithRealDb(); - //Setup - var historyItem = Builder.CreateListOfSize(10) - .All() - .With(c => c.Quality = QualityTypes.SDTV) - .Build(); - - Db.InsertMany(historyItem); - - //Act - var result = Mocker.Resolve().AllItems(); - - //Assert - result.Should().HaveSameCount(historyItem); - } - - [Test] - public void AllItemsWithRelationships() - { - WithRealDb(); - var seriesOne = Builder.CreateNew().With(s => s.SeriesId = 12345).Build(); - var seriesTwo = Builder.CreateNew().With(s => s.SeriesId = 54321).Build(); - - var episodes = Builder.CreateListOfSize(10).Build(); - - var historyItems = Builder - .CreateListOfSize(10) - .All() - .With(c => c.Quality = QualityTypes.SDTV) - .TheFirst(5) - .With(h => h.SeriesId = seriesOne.SeriesId) - .TheLast(5) - .With(h => h.SeriesId = seriesTwo.SeriesId) - .Build(); - - - Db.InsertMany(historyItems); - Db.InsertMany(episodes); - Db.Insert(seriesOne); - Db.Insert(seriesTwo); - - //Act - var result = Mocker.Resolve().AllItemsWithRelationships(); - - //Assert - result.Should().HaveSameCount(historyItems); - - foreach (var history in result) - { - Assert.NotNull(history.Episode); - Assert.That(!String.IsNullOrEmpty(history.SeriesTitle)); - } - } - - [Test] - public void PurgeItem() - { - WithRealDb(); - - var historyItem = Builder - .CreateListOfSize(10) - .All() - .With(c => c.Quality = QualityTypes.SDTV) - .Build(); - Db.InsertMany(historyItem); - - //Act - Db.Fetch().Should().HaveCount(10); - Mocker.Resolve().Purge(); - - //Assert - Db.Fetch().Should().HaveCount(0); - } - - [Test] - public void Trim_Items() - { - WithRealDb(); - - var historyItem = Builder.CreateListOfSize(30) - .All() - .With(c => c.Quality = QualityTypes.SDTV) - .TheFirst(10).With(c => c.Date = DateTime.Now) - .TheNext(20).With(c => c.Date = DateTime.Now.AddDays(-31)) - .Build(); - - Db.InsertMany(historyItem); - - //Act - Db.Fetch().Should().HaveCount(30); - Mocker.Resolve().Trim(); - - //Assert - var result = Db.Fetch(); - result.Should().HaveCount(10); - result.Should().OnlyContain(s => s.Date > DateTime.Now.AddDays(-30)); - } - - - [Test] - public void GetBestQualityInHistory_no_result() - { - WithRealDb(); - Mocker.Resolve().GetBestQualityInHistory(12, 12, 12).Should().Be(null); - } - - [Test] - public void GetBestQualityInHistory_single_result() - { - WithRealDb(); - - var episodes = Builder.CreateListOfSize(10).Build(); - var historyEpisode = episodes[6]; - - var history = Builder.CreateNew() - .With(h => h.Quality = QualityTypes.Bluray720p) - .With(h => h.IsProper = true) - .With(h => h.EpisodeId = historyEpisode.OID) - .Build(); - - Db.Insert(history); - Db.InsertMany(episodes); - - //Act - var result = Mocker.Resolve() - .GetBestQualityInHistory(historyEpisode.SeriesId, historyEpisode.SeasonNumber, historyEpisode.EpisodeNumber); - - //Assert - result.Should().NotBeNull(); - result.Quality.Should().Be(QualityTypes.Bluray720p); - result.Proper.Should().BeTrue(); - } - - [Test] - public void GetBestQualityInHistory_should_return_highest_result() - { - WithRealDb(); - - var episodes = Builder.CreateListOfSize(10).Build(); - var historyEpisode = episodes[6]; - - var history = Builder - .CreateListOfSize(5) - .All() - .With(h => h.EpisodeId = historyEpisode.OID) - .With(h => h.SeriesId = historyEpisode.SeriesId) - .TheFirst(1) - .With(h => h.Quality = QualityTypes.DVD) - .With(h => h.IsProper = true) - .TheNext(1) - .With(h => h.Quality = QualityTypes.Bluray720p) - .With(h => h.IsProper = false) - .TheNext(1) - .With(h => h.Quality = QualityTypes.Bluray720p) - .With(h => h.IsProper = true) - .TheNext(1) - .With(h => h.Quality = QualityTypes.Bluray720p) - .With(h => h.IsProper = false) - .TheNext(1) - .With(h => h.Quality = QualityTypes.SDTV) - .With(h => h.IsProper = true) - .Build(); - - Db.InsertMany(history); - Db.InsertMany(episodes); - - //Act - var result = Mocker.Resolve() - .GetBestQualityInHistory(historyEpisode.SeriesId, historyEpisode.SeasonNumber, historyEpisode.EpisodeNumber); - - //Assert - result.Should().NotBeNull(); - result.Quality.Should().Be(QualityTypes.Bluray720p); - result.Proper.Should().BeTrue(); - } - - [Test] - public void GetBestQualityInHistory_should_return_highest_weighted_result() - { - WithRealDb(); - - var episodes = Builder.CreateListOfSize(10).Build(); - var historyEpisode = episodes[6]; - - var history = Builder - .CreateListOfSize(5) - .All() - .With(h => h.EpisodeId = historyEpisode.OID) - .With(h => h.SeriesId = historyEpisode.SeriesId) - .TheFirst(1) - .With(h => h.Quality = QualityTypes.DVD) - .With(h => h.IsProper = true) - .TheNext(1) - .With(h => h.Quality = QualityTypes.WEBDL720p) - .With(h => h.IsProper = false) - .TheNext(1) - .With(h => h.Quality = QualityTypes.WEBDL720p) - .With(h => h.IsProper = true) - .TheNext(1) - .With(h => h.Quality = QualityTypes.WEBDL1080p) - .With(h => h.IsProper = false) - .TheNext(1) - .With(h => h.Quality = QualityTypes.SDTV) - .With(h => h.IsProper = true) - .Build(); - - Db.InsertMany(history); - Db.InsertMany(episodes); - - //Act - var result = Mocker.Resolve() - .GetBestQualityInHistory(historyEpisode.SeriesId, historyEpisode.SeasonNumber, historyEpisode.EpisodeNumber); - - //Assert - result.Should().NotBeNull(); - result.Quality.Should().Be(QualityTypes.WEBDL1080p); - result.Proper.Should().BeFalse(); - } - - [Test] - public void add_item() - { - WithRealDb(); - - var episode = Builder.CreateNew().Build(); - - QualityTypes quality = QualityTypes.HDTV720p; - const bool proper = true; - - var history = new History - { - Date = DateTime.Now, - EpisodeId = episode.OID, - SeriesId = episode.SeriesId, - NzbTitle = "my title", - Indexer = "Fake Indexer", - Quality = quality, - IsProper = proper - }; - - //Act - Mocker.Resolve().Add(history); - - //Assert - var storedHistory = Db.Fetch(); - - storedHistory.Should().HaveCount(1); - history.Date.Should().BeWithin(TimeSpan.FromMinutes(1)).Before(storedHistory.First().Date); - - history.EpisodeId.Should().Be(storedHistory.First().EpisodeId); - history.SeriesId.Should().Be(storedHistory.First().SeriesId); - history.NzbTitle.Should().Be(storedHistory.First().NzbTitle); - history.Indexer.Should().Be(storedHistory.First().Indexer); - history.Quality.Should().Be(storedHistory.First().Quality); - history.IsProper.Should().Be(storedHistory.First().IsProper); - } - } -} \ No newline at end of file diff --git a/NzbDrone.Core/Datastore/ModelBase.cs b/NzbDrone.Core/Datastore/ModelBase.cs index caac2cb2e..dbc965797 100644 --- a/NzbDrone.Core/Datastore/ModelBase.cs +++ b/NzbDrone.Core/Datastore/ModelBase.cs @@ -1,8 +1,10 @@ -using System.Linq; +using System.Diagnostics; +using System.Linq; using Newtonsoft.Json; namespace NzbDrone.Core.Datastore { + [DebuggerDisplay("ID = {OID}")] public abstract class ModelBase { [PetaPoco.Ignore] diff --git a/NzbDrone.Core/DecisionEngine/UpgradeHistorySpecification.cs b/NzbDrone.Core/DecisionEngine/UpgradeHistorySpecification.cs index 6f1050180..076252efb 100644 --- a/NzbDrone.Core/DecisionEngine/UpgradeHistorySpecification.cs +++ b/NzbDrone.Core/DecisionEngine/UpgradeHistorySpecification.cs @@ -1,5 +1,6 @@ using System.Linq; using NLog; +using NzbDrone.Core.History; using NzbDrone.Core.Tv; using NzbDrone.Core.Model; using NzbDrone.Core.Providers; @@ -9,14 +10,14 @@ namespace NzbDrone.Core.DecisionEngine public class UpgradeHistorySpecification { private readonly IEpisodeService _episodeService; - private readonly HistoryProvider _historyProvider; + private readonly HistoryService _historyService; private readonly QualityUpgradeSpecification _qualityUpgradeSpecification; private static readonly Logger logger = LogManager.GetCurrentClassLogger(); - public UpgradeHistorySpecification(IEpisodeService episodeService, HistoryProvider historyProvider, QualityUpgradeSpecification qualityUpgradeSpecification) + public UpgradeHistorySpecification(IEpisodeService episodeService, HistoryService historyService, QualityUpgradeSpecification qualityUpgradeSpecification) { _episodeService = episodeService; - _historyProvider = historyProvider; + _historyService = historyService; _qualityUpgradeSpecification = qualityUpgradeSpecification; } @@ -29,7 +30,7 @@ namespace NzbDrone.Core.DecisionEngine { foreach (var episode in subject.Episodes) { - var bestQualityInHistory = _historyProvider.GetBestQualityInHistory(subject.Series.SeriesId, episode.SeasonNumber, episode.EpisodeNumber); + var bestQualityInHistory = _historyService.GetBestQualityInHistory(subject.Series.SeriesId, episode.SeasonNumber, episode.EpisodeNumber); if (bestQualityInHistory != null) { logger.Trace("Comparing history quality with report. History is {0}", bestQualityInHistory); diff --git a/NzbDrone.Core/History/History.cs b/NzbDrone.Core/History/History.cs new file mode 100644 index 000000000..a9622a3dc --- /dev/null +++ b/NzbDrone.Core/History/History.cs @@ -0,0 +1,19 @@ +using System.Linq; +using System; +using NzbDrone.Core.Datastore; +using NzbDrone.Core.Tv; + +namespace NzbDrone.Core.History +{ + public class History : ModelBase + { + public string NzbTitle { get; set; } + public QualityModel Quality { get; set; } + public DateTime Date { get; set; } + public string Indexer { get; set; } + public string NzbInfoUrl { get; set; } + public string ReleaseGroup { get; set; } + + public Episode Episode { get; set; } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/History/HistoryRepository.cs b/NzbDrone.Core/History/HistoryRepository.cs new file mode 100644 index 000000000..34fcd535a --- /dev/null +++ b/NzbDrone.Core/History/HistoryRepository.cs @@ -0,0 +1,41 @@ +using System; +using System.Linq; +using NzbDrone.Core.Datastore; +using NzbDrone.Core.Tv; + +namespace NzbDrone.Core.History +{ + public interface IHistoryRepository : IBasicRepository + { + void Trim(); + QualityModel GetBestQualityInHistory(int seriesId, int seasonNumber, int episodeNumber); + } + + public class HistoryRepository : BasicRepository, IHistoryRepository + { + public HistoryRepository(IObjectDatabase objectDatabase) + : base(objectDatabase) + { + } + + public void Trim() + { + var oldIds = Queryable.Where(c => c.Date < DateTime.Now.AddDays(-30).Date).Select(c => c.OID); + DeleteMany(oldIds); + } + + + public QualityModel GetBestQualityInHistory(int seriesId, int seasonNumber, int episodeNumber) + { + var history = Queryable.OrderByDescending(c => c.Quality).FirstOrDefault(c => c.Episode.Series.SeriesId == seriesId && c.Episode.SeasonNumber == seasonNumber && + c.Episode.EpisodeNumber == episodeNumber); + + if (history != null) + { + return history.Quality; + } + + return null; + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/History/HistoryService.cs b/NzbDrone.Core/History/HistoryService.cs new file mode 100644 index 000000000..689d42db0 --- /dev/null +++ b/NzbDrone.Core/History/HistoryService.cs @@ -0,0 +1,47 @@ +using System.Collections.Generic; +using System.Linq; +using NLog; +using NzbDrone.Core.Tv; + +namespace NzbDrone.Core.History +{ + + public class HistoryService + { + private readonly IHistoryRepository _historyRepository; + private readonly Logger _logger; + + + public HistoryService(IHistoryRepository historyRepository, Logger logger) + { + _historyRepository = historyRepository; + _logger = logger; + } + + public List All() + { + return _historyRepository.All().ToList(); + } + + public void Purge() + { + _historyRepository.Purge(); + } + + public virtual void Trim() + { + _historyRepository.Trim(); + } + + public void Add(History item) + { + + } + + public virtual QualityModel GetBestQualityInHistory(int seriesId, int seasonNumber, int episodeNumber) + { + return _historyRepository.GetBestQualityInHistory(seriesId, seasonNumber, episodeNumber); + } + + } +} \ No newline at end of file diff --git a/NzbDrone.Core/Model/HistoryQueryModel.cs b/NzbDrone.Core/Model/HistoryQueryModel.cs deleted file mode 100644 index 198178f60..000000000 --- a/NzbDrone.Core/Model/HistoryQueryModel.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using NzbDrone.Core.Repository; -using NzbDrone.Core.Repository.Quality; -using PetaPoco; - -namespace NzbDrone.Core.Model -{ - public class HistoryQueryModel - { - public int HistoryId { get; set; } - public int EpisodeId { get; set; } - public int SeriesId { get; set; } - public string NzbTitle { get; set; } - public QualityTypes Quality { get; set; } - public DateTime Date { get; set; } - public bool IsProper { get; set; } - public string Indexer { get; set; } - public string NzbInfoUrl { get; set; } - public string ReleaseGroup { get; set; } - - public string EpisodeTitle { get; set; } - public int SeasonNumber { get; set; } - public int EpisodeNumber { get; set; } - public string EpisodeOverview { get; set; } - public string SeriesTitle { get; set; }public int Id { get; set; } - } -} \ No newline at end of file diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index ce30b651c..caf87d835 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -131,10 +131,6 @@ False ..\packages\Autofac.3.0.1\lib\net40\Autofac.Configuration.dll - - False - ..\packages\DataTables.Mvc.Core.0.1.0.85\lib\DataTables.Mvc.Core.dll - ..\Libraries\DeskMetrics\DeskMetrics.NET.dll @@ -268,6 +264,7 @@ + @@ -282,7 +279,6 @@ - @@ -491,7 +487,7 @@ Code - + Code @@ -592,7 +588,7 @@ - + diff --git a/NzbDrone.Core/Providers/DownloadClients/BlackholeProvider.cs b/NzbDrone.Core/Providers/DownloadClients/BlackholeProvider.cs index 242593647..d22ee5fb3 100644 --- a/NzbDrone.Core/Providers/DownloadClients/BlackholeProvider.cs +++ b/NzbDrone.Core/Providers/DownloadClients/BlackholeProvider.cs @@ -3,6 +3,7 @@ using System.IO; using System.Linq; using NLog; using NzbDrone.Common; +using NzbDrone.Core.History; using NzbDrone.Core.Model; using NzbDrone.Core.Providers.Core; using NzbDrone.Core.DecisionEngine; @@ -15,7 +16,7 @@ namespace NzbDrone.Core.Providers.DownloadClients private readonly HttpProvider _httpProvider; private readonly DiskProvider _diskProvider; private readonly UpgradeHistorySpecification _upgradeHistorySpecification; - private readonly HistoryProvider _historyProvider; + private readonly HistoryService _historyService; private static readonly Logger logger = LogManager.GetCurrentClassLogger(); diff --git a/NzbDrone.Core/Providers/DownloadProvider.cs b/NzbDrone.Core/Providers/DownloadProvider.cs index 73531c4b7..03e7aea0a 100644 --- a/NzbDrone.Core/Providers/DownloadProvider.cs +++ b/NzbDrone.Core/Providers/DownloadProvider.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using NLog; +using NzbDrone.Core.History; using NzbDrone.Core.Tv; using NzbDrone.Core.Model; using NzbDrone.Core.Providers.Core; @@ -13,7 +14,7 @@ namespace NzbDrone.Core.Providers public class DownloadProvider { private readonly SabProvider _sabProvider; - private readonly HistoryProvider _historyProvider; + private readonly HistoryService _historyService; private readonly IEpisodeService _episodeService; private readonly ExternalNotificationProvider _externalNotificationProvider; private readonly ConfigProvider _configProvider; @@ -24,14 +25,14 @@ namespace NzbDrone.Core.Providers private static readonly Logger logger = LogManager.GetCurrentClassLogger(); - public DownloadProvider(SabProvider sabProvider, HistoryProvider historyProvider, + public DownloadProvider(SabProvider sabProvider, HistoryService historyService, IEpisodeService episodeService, ExternalNotificationProvider externalNotificationProvider, ConfigProvider configProvider, BlackholeProvider blackholeProvider, SignalRProvider signalRProvider, PneumaticProvider pneumaticProvider, NzbgetProvider nzbgetProvider) { _sabProvider = sabProvider; - _historyProvider = historyProvider; + _historyService = historyService; _episodeService = episodeService; _externalNotificationProvider = externalNotificationProvider; _configProvider = configProvider; @@ -59,20 +60,18 @@ namespace NzbDrone.Core.Providers foreach (var episode in parseResult.Episodes) { - var history = new History + var history = new History.History { Date = DateTime.Now, Indexer = parseResult.Indexer, - IsProper = parseResult.Quality.Proper, - Quality = parseResult.Quality.Quality, + Quality = parseResult.Quality, NzbTitle = parseResult.OriginalString, - EpisodeId = episode.OID, - SeriesId = episode.SeriesId, + Episode = episode, NzbInfoUrl = parseResult.NzbInfoUrl, ReleaseGroup = parseResult.ReleaseGroup, }; - _historyProvider.Add(history); + _historyService.Add(history); _episodeService.MarkEpisodeAsFetched(episode.OID); _signalRProvider.UpdateEpisodeStatus(episode.OID, EpisodeStatusType.Downloading, null); diff --git a/NzbDrone.Core/Providers/HistoryProvider.cs b/NzbDrone.Core/Providers/HistoryProvider.cs deleted file mode 100644 index 31a9d3e81..000000000 --- a/NzbDrone.Core/Providers/HistoryProvider.cs +++ /dev/null @@ -1,118 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using DataTables.Mvc.Core.Helpers; -using DataTables.Mvc.Core.Models; -using NLog; -using NzbDrone.Core.Tv; -using NzbDrone.Core.Model; -using NzbDrone.Core.Repository; -using NzbDrone.Core.Repository.Quality; -using PetaPoco; - -namespace NzbDrone.Core.Providers -{ - public class HistoryProvider - { - private readonly IDatabase _database; - private static readonly Logger logger = LogManager.GetCurrentClassLogger(); - - - public HistoryProvider(IDatabase database) - { - _database = database; - } - - public HistoryProvider() - { - } - - public virtual List AllItems() - { - return _database.Fetch(""); - } - - public virtual List AllItemsWithRelationships() - { - return _database.Fetch(@" - SELECT History.*, Series.Title as SeriesTitle, Episodes.* FROM History - INNER JOIN Series ON History.SeriesId = Series.SeriesId - INNER JOIN Episodes ON History.EpisodeId = Episodes.EpisodeId - "); - } - - public virtual void Purge() - { - _database.Delete(""); - logger.Info("History has been Purged"); - } - - public virtual void Trim() - { - _database.Delete("WHERE Date < @0", DateTime.Now.AddDays(-30).Date); - logger.Info("History has been trimmed, items older than 30 days have been removed"); - } - - public virtual void Add(History item) - { - _database.Insert(item); - logger.Debug("Item added to history: {0}", item.NzbTitle); - } - - public virtual QualityModel GetBestQualityInHistory(int seriesId, int seasonNumber, int episodeNumber) - { - var quality = _database.Fetch(@"SELECT History.Quality , History.IsProper as Proper FROM History - INNER JOIN Episodes ON History.EpisodeId = Episodes.EpisodeId - WHERE Episodes.seriesId = @0 AND - Episodes.SeasonNumber = @1 AND - Episodes.EpisodeNumber = @2" - , seriesId, seasonNumber, episodeNumber); - - var best = quality.OrderByDescending(q => q).FirstOrDefault(); - - return best; - } - - public virtual void Delete(int historyId) - { - _database.Delete(historyId); - } - - public virtual Page GetPagedItems(DataTablesPageRequest pageRequest) - { - var query = Sql.Builder - .Select(@"History.*, Series.Title as SeriesTitle, Episodes.Title as EpisodeTitle, - Episodes.SeasonNumber as SeasonNumber, Episodes.EpisodeNumber as EpisodeNumber, - Episodes.Overview as EpisodeOverview") - .From("History") - .InnerJoin("Series") - .On("History.SeriesId = Series.SeriesId") - .InnerJoin("Episodes") - .On("History.EpisodeId = Episodes.EpisodeId"); - - var startPage = (pageRequest.DisplayLength == 0) ? 1 : pageRequest.DisplayStart / pageRequest.DisplayLength + 1; - - if (!string.IsNullOrEmpty(pageRequest.Search)) - { - var whereClause = string.Join(" OR ", SqlBuilderHelper.GetSearchClause(pageRequest)); - - if (!string.IsNullOrEmpty(whereClause)) - query.Append("WHERE " + whereClause, "%" + pageRequest.Search + "%"); - } - - var orderBy = string.Join(",", SqlBuilderHelper.GetOrderByClause(pageRequest)); - - if (!string.IsNullOrEmpty(orderBy)) - { - query.Append("ORDER BY " + orderBy); - } - - return _database.Page(startPage, pageRequest.DisplayLength, query); - } - - public virtual long Count() - { - return _database.Single(@"SELECT COUNT(*) from History"); - } - } -} \ No newline at end of file diff --git a/NzbDrone.Core/Providers/StatsProvider.cs b/NzbDrone.Core/Providers/StatsProvider.cs index 03f6262eb..75631f1ce 100644 --- a/NzbDrone.Core/Providers/StatsProvider.cs +++ b/NzbDrone.Core/Providers/StatsProvider.cs @@ -26,7 +26,7 @@ namespace NzbDrone.Core.Providers { var series = _database.Fetch(); var episodes = _database.Fetch(); - var history = _database.Fetch("WHERE Date >= @0", DateTime.Today.AddDays(-30)); + var history = _database.Fetch("WHERE Date >= @0", DateTime.Today.AddDays(-30)); var stats = new StatsModel(); stats.SeriesTotal = series.Count; diff --git a/NzbDrone.Core/Repository/History.cs b/NzbDrone.Core/Repository/History.cs deleted file mode 100644 index cf56bd283..000000000 --- a/NzbDrone.Core/Repository/History.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using NzbDrone.Core.Tv; -using NzbDrone.Core.Repository.Quality; -using PetaPoco; - -namespace NzbDrone.Core.Repository -{ - [PrimaryKey("HistoryId")] - public class History - { - public int HistoryId { get; set; } - - public int EpisodeId { get; set; } - public int SeriesId { get; set; } - public string NzbTitle { get; set; } - public QualityTypes Quality { get; set; } - public DateTime Date { get; set; } - public bool IsProper { get; set; } - public string Indexer { get; set; } - public string NzbInfoUrl { get; set; } - public string ReleaseGroup { get; set; } - - [ResultColumn] - public Episode Episode { get; set; } - - [ResultColumn] - public string SeriesTitle { get; set; } - } -} \ No newline at end of file diff --git a/NzbDrone.Core/Tv/QualityModel.cs b/NzbDrone.Core/Tv/QualityModel.cs index fe31765f0..740a27308 100644 --- a/NzbDrone.Core/Tv/QualityModel.cs +++ b/NzbDrone.Core/Tv/QualityModel.cs @@ -10,7 +10,10 @@ namespace NzbDrone.Core.Tv public Boolean Proper { get; set; } - public QualityModel() { } + public QualityModel():this(QualityTypes.Unknown, false) + { + + } public QualityModel(QualityTypes quality, Boolean proper) { diff --git a/NzbDrone.Core/packages.config b/NzbDrone.Core/packages.config index 1ac75e159..c53f4104f 100644 --- a/NzbDrone.Core/packages.config +++ b/NzbDrone.Core/packages.config @@ -1,7 +1,6 @@  -