From 2a0df7e83e535e423a3931831bee18c10d17418a Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sun, 22 Oct 2017 12:05:21 -0700 Subject: [PATCH] history/since API endpoint Closes #2208 --- src/NzbDrone.Api/History/HistoryModule.cs | 27 ++++++++++++++++++- .../118_add_history_eventType_index.cs | 14 ++++++++++ .../History/HistoryRepository.cs | 20 ++++++++++++-- src/NzbDrone.Core/History/HistoryService.cs | 10 +++++-- src/NzbDrone.Core/NzbDrone.Core.csproj | 1 + 5 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 src/NzbDrone.Core/Datastore/Migration/118_add_history_eventType_index.cs diff --git a/src/NzbDrone.Api/History/HistoryModule.cs b/src/NzbDrone.Api/History/HistoryModule.cs index d85cf74d8..4902c195a 100644 --- a/src/NzbDrone.Api/History/HistoryModule.cs +++ b/src/NzbDrone.Api/History/HistoryModule.cs @@ -1,7 +1,10 @@ -using System; +using System; +using System.Collections.Generic; +using System.Linq; using Nancy; using NzbDrone.Api.Episodes; using NzbDrone.Api.Extensions; +using NzbDrone.Api.REST; using NzbDrone.Api.Series; using NzbDrone.Core.Datastore; using NzbDrone.Core.DecisionEngine; @@ -25,6 +28,7 @@ namespace NzbDrone.Api.History _failedDownloadService = failedDownloadService; GetResourcePaged = GetHistory; + Get["/since"] = x => GetHistorySince(); Post["/failed"] = x => MarkAsFailed(); } @@ -64,6 +68,27 @@ namespace NzbDrone.Api.History return ApplyToPage(_historyService.Paged, pagingSpec, MapToResource); } + private List GetHistorySince() + { + var queryDate = Request.Query.Date; + var queryEventType = Request.Query.EventType; + + if (!queryDate.HasValue) + { + throw new BadRequestException("date is missing"); + } + + DateTime date = DateTime.Parse(queryDate.Value); + HistoryEventType? eventType = null; + + if (queryEventType.HasValue) + { + eventType = (HistoryEventType)Convert.ToInt32(queryEventType.Value); + } + + return _historyService.Since(date, eventType).Select(MapToResource).ToList(); + } + private Response MarkAsFailed() { var id = (int)Request.Form.Id; diff --git a/src/NzbDrone.Core/Datastore/Migration/118_add_history_eventType_index.cs b/src/NzbDrone.Core/Datastore/Migration/118_add_history_eventType_index.cs new file mode 100644 index 000000000..15f73b167 --- /dev/null +++ b/src/NzbDrone.Core/Datastore/Migration/118_add_history_eventType_index.cs @@ -0,0 +1,14 @@ +using FluentMigrator; +using NzbDrone.Core.Datastore.Migration.Framework; + +namespace NzbDrone.Core.Datastore.Migration +{ + [Migration(118)] + public class add_history_eventType_index : NzbDroneMigrationBase + { + protected override void MainDbUpgrade() + { + Create.Index().OnTable("History").OnColumn("EventType"); + } + } +} diff --git a/src/NzbDrone.Core/History/HistoryRepository.cs b/src/NzbDrone.Core/History/HistoryRepository.cs index 35199a878..bfc1aeb85 100644 --- a/src/NzbDrone.Core/History/HistoryRepository.cs +++ b/src/NzbDrone.Core/History/HistoryRepository.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using Marr.Data.QGen; using NzbDrone.Core.Datastore; @@ -16,6 +17,7 @@ namespace NzbDrone.Core.History List FindByDownloadId(string downloadId); List FindDownloadHistory(int idSeriesId, QualityModel quality); void DeleteForSeries(int seriesId); + List Since(DateTime date, HistoryEventType? eventType); } public class HistoryRepository : BasicRepository, IHistoryRepository @@ -76,5 +78,19 @@ namespace NzbDrone.Core.History return base.GetPagedQuery(baseQuery, pagingSpec); } + + public List Since(DateTime date, HistoryEventType? eventType) + { + var query = Query.Where(h => h.Date >= date); + + if (eventType.HasValue) + { + query.AndWhere(h => h.EventType == eventType); + } + + query.OrderBy(h => h.Date); + + return query; + } } -} \ No newline at end of file +} diff --git a/src/NzbDrone.Core/History/HistoryService.cs b/src/NzbDrone.Core/History/HistoryService.cs index 38450727a..0f835b5a4 100644 --- a/src/NzbDrone.Core/History/HistoryService.cs +++ b/src/NzbDrone.Core/History/HistoryService.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -25,6 +25,7 @@ namespace NzbDrone.Core.History History Get(int historyId); List Find(string downloadId, HistoryEventType eventType); List FindByDownloadId(string downloadId); + List Since(DateTime date, HistoryEventType? eventType); } public class HistoryService : IHistoryService, @@ -290,5 +291,10 @@ namespace NzbDrone.Core.History { _historyRepository.DeleteForSeries(message.Series.Id); } + + public List Since(DateTime date, HistoryEventType? eventType) + { + return _historyRepository.Since(date, eventType); + } } -} \ No newline at end of file +} diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index f9478e284..844aa9c9f 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -224,6 +224,7 @@ +