From b7408b726a49391a9465a5c406bcc91265516646 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Tue, 12 Jun 2012 12:38:38 -0700 Subject: [PATCH] Added cleanup job for search history New: Search History cleanup job will only keep the last week of results --- .../SearchHistoryProviderTest.cs | 89 +++++++++++++++++++ NzbDrone.Core/Jobs/SearchHistoryCleanupJob.cs | 45 ++++++++++ NzbDrone.Core/NzbDrone.Core.csproj | 1 + .../Providers/SearchHistoryProvider.cs | 14 +++ NzbDrone.ncrunchsolution | 10 +++ 5 files changed, 159 insertions(+) create mode 100644 NzbDrone.Core/Jobs/SearchHistoryCleanupJob.cs create mode 100644 NzbDrone.ncrunchsolution diff --git a/NzbDrone.Core.Test/ProviderTests/SearchHistoryProviderTest.cs b/NzbDrone.Core.Test/ProviderTests/SearchHistoryProviderTest.cs index 7f6a80c2e..d84859332 100644 --- a/NzbDrone.Core.Test/ProviderTests/SearchHistoryProviderTest.cs +++ b/NzbDrone.Core.Test/ProviderTests/SearchHistoryProviderTest.cs @@ -66,6 +66,46 @@ namespace NzbDrone.Core.Test.ProviderTests i.SearchError = ReportRejectionType.None; } + private void WithExpiredHistory() + { + var history = Builder.CreateListOfSize(10) + .All() + .With(h => h.SearchTime = DateTime.Now.AddDays(10)) + .Build(); + + foreach(var searchHistory in history) + { + var items = Builder.CreateListOfSize(10) + .All() + .With(i => i.Id == searchHistory.Id) + .Build(); + + Db.InsertMany(items); + } + + Db.InsertMany(history); + } + + private void WithValidHistory() + { + var history = Builder.CreateListOfSize(10) + .All() + .With(h => h.SearchTime = DateTime.Now) + .Build(); + + foreach (var searchHistory in history) + { + var items = Builder.CreateListOfSize(10) + .All() + .With(i => i.Id == searchHistory.Id) + .Build(); + + Db.InsertMany(items); + } + + Db.InsertMany(history); + } + [Test] public void Add_should_add_history_and_history_items() { @@ -263,5 +303,54 @@ namespace NzbDrone.Core.Test.ProviderTests Mocker.GetMock().Verify(v => v.DownloadReport(It.IsAny()), Times.Once()); } + + [Test] + public void Cleanup_should_not_blowup_if_there_is_nothing_to_delete() + { + WithRealDb(); + + Mocker.Resolve().Cleanup(); + Db.Fetch().Should().HaveCount(0); + } + + [Test] + public void Cleanup_should_delete_searchHistory_older_than_1_week() + { + WithRealDb(); + WithExpiredHistory(); + + Mocker.Resolve().Cleanup(); + Db.Fetch().Should().HaveCount(0); + } + + [Test] + public void Cleanup_should_delete_searchHistoryItems_older_than_1_week() + { + WithRealDb(); + WithExpiredHistory(); + + Mocker.Resolve().Cleanup(); + Db.Fetch().Should().HaveCount(0); + } + + [Test] + public void Cleanup_should_not_delete_searchHistory_younger_than_1_week() + { + WithRealDb(); + WithValidHistory(); + + Mocker.Resolve().Cleanup(); + Db.Fetch().Should().HaveCount(10); + } + + [Test] + public void Cleanup_should_not_delete_searchHistoryItems_younger_than_1_week() + { + WithRealDb(); + WithValidHistory(); + + Mocker.Resolve().Cleanup(); + Db.Fetch().Should().HaveCount(100); + } } } \ No newline at end of file diff --git a/NzbDrone.Core/Jobs/SearchHistoryCleanupJob.cs b/NzbDrone.Core/Jobs/SearchHistoryCleanupJob.cs new file mode 100644 index 000000000..1da3d2796 --- /dev/null +++ b/NzbDrone.Core/Jobs/SearchHistoryCleanupJob.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Ninject; +using NLog; +using NzbDrone.Core.Helpers; +using NzbDrone.Core.Model.Notification; +using NzbDrone.Core.Providers; +using NzbDrone.Core.Providers.Core; +using NzbDrone.Core.Repository; + +namespace NzbDrone.Core.Jobs +{ + public class SearchHistoryCleanupJob : IJob + { + private readonly SearchHistoryProvider _searchHistoryProvider; + private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); + + [Inject] + public SearchHistoryCleanupJob(SearchHistoryProvider searchHistoryProvider) + { + _searchHistoryProvider = searchHistoryProvider; + } + + public SearchHistoryCleanupJob() + { + } + + public string Name + { + get { return "Search History Cleanup"; } + } + + public TimeSpan DefaultInterval + { + get { return TimeSpan.FromHours(24); } + } + + public virtual void Start(ProgressNotification notification, int targetId, int secondaryTargetId) + { + Logger.Info("Running search history cleanup."); + _searchHistoryProvider.Cleanup(); + } + } +} diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 18364ed27..b51d80ed1 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -248,6 +248,7 @@ + diff --git a/NzbDrone.Core/Providers/SearchHistoryProvider.cs b/NzbDrone.Core/Providers/SearchHistoryProvider.cs index b262302ac..e5741a045 100644 --- a/NzbDrone.Core/Providers/SearchHistoryProvider.cs +++ b/NzbDrone.Core/Providers/SearchHistoryProvider.cs @@ -117,5 +117,19 @@ namespace NzbDrone.Core.Providers logger.Info("Forcing Download of: {0}", item.ReportTitle); _downloadProvider.DownloadReport(parseResult); } + + public virtual void Cleanup() + { + var ids = _database.Fetch("SELECT Id FROM SearchHistory WHERE SearchTime > @0", DateTime.Now.AddDays(7)); + + if (ids.Any()) + { + logger.Trace("Deleting old search items"); + _database.Execute("DELETE FROM SearchHistoryItems WHERE SearchHistoryId IN (@0)", ids); + + logger.Trace("Deleting old search results"); + _database.Execute("DELETE FROM SearchHistory WHERE Id IN (@0)", ids); + } + } } } diff --git a/NzbDrone.ncrunchsolution b/NzbDrone.ncrunchsolution new file mode 100644 index 000000000..88b9960b3 --- /dev/null +++ b/NzbDrone.ncrunchsolution @@ -0,0 +1,10 @@ + + 0 + Default + false + UseDynamicAnalysis + UseStaticAnalysis + UseStaticAnalysis + UseStaticAnalysis + Run all tests automatically:BFRydWU=;Run all tests manually:BUZhbHNl;Run impacted tests automatically, others manually (experimental!):CklzSW1wYWN0ZWQ=;Run pinned tests automatically, others manually:CElzUGlubmVk + \ No newline at end of file