1
0
mirror of https://github.com/Radarr/Radarr.git synced 2026-04-25 22:37:27 -04:00

New: Rebuilt Completed/Failed download handling from scratch

This commit is contained in:
Keivan Beigi
2014-12-18 16:26:42 -08:00
parent 264bb66c16
commit a6d34caf2c
79 changed files with 1221 additions and 2389 deletions
+5
View File
@@ -8,6 +8,8 @@ namespace NzbDrone.Core.History
{
public class History : ModelBase
{
public const string DOWNLOAD_CLIENT = "downloadClient";
public History()
{
Data = new Dictionary<string, string>();
@@ -22,6 +24,9 @@ namespace NzbDrone.Core.History
public Series Series { get; set; }
public HistoryEventType EventType { get; set; }
public Dictionary<string, string> Data { get; set; }
public string DownloadId { get; set; }
}
public enum HistoryEventType
+21 -43
View File
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using Marr.Data.QGen;
using NzbDrone.Core.Datastore;
@@ -11,31 +10,21 @@ namespace NzbDrone.Core.History
{
public interface IHistoryRepository : IBasicRepository<History>
{
void Trim();
List<QualityModel> GetBestQualityInHistory(int episodeId);
List<History> BetweenDates(DateTime startDate, DateTime endDate, HistoryEventType eventType);
List<History> Failed();
List<History> Grabbed();
List<History> Imported();
History MostRecentForEpisode(int episodeId);
List<History> FindBySourceTitle(string sourceTitle);
History MostRecentForDownloadId(string downloadId);
List<History> FindByDownloadId(string downloadId);
List<History> FindDownloadHistory(int idSeriesId, QualityModel quality);
}
public class HistoryRepository : BasicRepository<History>, IHistoryRepository
{
private readonly IDatabase _database;
public HistoryRepository(IDatabase database, IEventAggregator eventAggregator)
: base(database, eventAggregator)
{
_database = database;
}
public void Trim()
{
var cutoff = DateTime.UtcNow.AddDays(-30).Date;
Delete(c=> c.Date < cutoff);
}
public List<QualityModel> GetBestQualityInHistory(int episodeId)
{
@@ -44,30 +33,6 @@ namespace NzbDrone.Core.History
return history.Select(h => h.Quality).ToList();
}
public List<History> BetweenDates(DateTime startDate, DateTime endDate, HistoryEventType eventType)
{
return Query.Join<History, Series>(JoinType.Inner, h => h.Series, (h, s) => h.SeriesId == s.Id)
.Join<History, Episode>(JoinType.Inner, h => h.Episode, (h, e) => h.EpisodeId == e.Id)
.Where(h => h.Date >= startDate)
.AndWhere(h => h.Date <= endDate)
.AndWhere(h => h.EventType == eventType);
}
public List<History> Failed()
{
return Query.Where(h => h.EventType == HistoryEventType.DownloadFailed);
}
public List<History> Grabbed()
{
return Query.Where(h => h.EventType == HistoryEventType.Grabbed);
}
public List<History> Imported()
{
return Query.Where(h => h.EventType == HistoryEventType.DownloadFolderImported);
}
public History MostRecentForEpisode(int episodeId)
{
return Query.Where(h => h.EpisodeId == episodeId)
@@ -75,14 +40,27 @@ namespace NzbDrone.Core.History
.FirstOrDefault();
}
public List<History> FindBySourceTitle(string sourceTitle)
public History MostRecentForDownloadId(string downloadId)
{
return Query.Where(h => h.SourceTitle.Contains(sourceTitle));
return Query.Where(h => h.DownloadId == downloadId)
.OrderByDescending(h => h.Date)
.FirstOrDefault();
}
public List<History> AllForEpisode(int episodeId)
public List<History> FindByDownloadId(string downloadId)
{
return Query.Where(h => h.EpisodeId == episodeId);
return Query.Where(h => h.DownloadId == downloadId);
}
public List<History> FindDownloadHistory(int idSeriesId, QualityModel quality)
{
return Query.Where(h =>
h.SeriesId == idSeriesId &&
h.Quality == quality &&
(h.EventType == HistoryEventType.Grabbed ||
h.EventType == HistoryEventType.DownloadFailed ||
h.EventType == HistoryEventType.DownloadFolderImported)
).ToList();
}
protected override SortBuilder<History> GetPagedQuery(QueryBuilder<History> query, PagingSpec<History> pagingSpec)
+66 -60
View File
@@ -16,19 +16,12 @@ namespace NzbDrone.Core.History
{
public interface IHistoryService
{
List<History> All();
void Purge();
void Trim();
QualityModel GetBestQualityInHistory(Profile profile, int episodeId);
PagingSpec<History> Paged(PagingSpec<History> pagingSpec);
List<History> BetweenDates(DateTime startDate, DateTime endDate, HistoryEventType eventType);
List<History> Failed();
List<History> Grabbed();
List<History> Imported();
History MostRecentForEpisode(int episodeId);
History Get(int id);
List<History> FindBySourceTitle(string sourceTitle);
void UpdateHistoryData(Int32 historyId, Dictionary<String, String> data);
History MostRecentForDownloadId(string downloadId);
History Get(int historyId);
List<History> Find(string downloadId, HistoryEventType eventType);
}
public class HistoryService : IHistoryService,
@@ -46,60 +39,31 @@ namespace NzbDrone.Core.History
_logger = logger;
}
public List<History> All()
{
return _historyRepository.All().ToList();
}
public PagingSpec<History> Paged(PagingSpec<History> pagingSpec)
{
return _historyRepository.GetPaged(pagingSpec);
}
public List<History> BetweenDates(DateTime startDate, DateTime endDate, HistoryEventType eventType)
{
return _historyRepository.BetweenDates(startDate, endDate, eventType);
}
public List<History> Failed()
{
return _historyRepository.Failed();
}
public List<History> Grabbed()
{
return _historyRepository.Grabbed();
}
public List<History> Imported()
{
return _historyRepository.Imported();
}
public History MostRecentForEpisode(int episodeId)
{
return _historyRepository.MostRecentForEpisode(episodeId);
}
public History Get(int id)
public History MostRecentForDownloadId(string downloadId)
{
return _historyRepository.Get(id);
return _historyRepository.MostRecentForDownloadId(downloadId);
}
public List<History> FindBySourceTitle(string sourceTitle)
public History Get(int historyId)
{
return _historyRepository.FindBySourceTitle(sourceTitle);
return _historyRepository.Get(historyId);
}
public void Purge()
public List<History> Find(string downloadId, HistoryEventType eventType)
{
_historyRepository.Purge();
return _historyRepository.FindByDownloadId(downloadId).Where(c => c.EventType == eventType).ToList();
}
public virtual void Trim()
{
_historyRepository.Trim();
}
public QualityModel GetBestQualityInHistory(Profile profile, int episodeId)
{
@@ -109,13 +73,6 @@ namespace NzbDrone.Core.History
.FirstOrDefault();
}
public void UpdateHistoryData(Int32 historyId, Dictionary<String, String> data)
{
var history = _historyRepository.Get(historyId);
history.Data = data;
_historyRepository.Update(history);
}
public void Handle(EpisodeGrabbedEvent message)
{
foreach (var episode in message.Episode.Episodes)
@@ -128,6 +85,7 @@ namespace NzbDrone.Core.History
SourceTitle = message.Episode.Release.Title,
SeriesId = episode.SeriesId,
EpisodeId = episode.Id,
DownloadId = message.DownloadId
};
history.Data.Add("Indexer", message.Episode.Release.Indexer);
@@ -138,11 +96,6 @@ namespace NzbDrone.Core.History
history.Data.Add("PublishedDate", message.Episode.Release.PublishDate.ToString("s") + "Z");
history.Data.Add("DownloadClient", message.DownloadClient);
if (!String.IsNullOrWhiteSpace(message.DownloadClientId))
{
history.Data.Add("DownloadClientId", message.DownloadClientId);
}
if (!message.Episode.ParsedEpisodeInfo.ReleaseHash.IsNullOrWhiteSpace())
{
history.Data.Add("ReleaseHash", message.Episode.ParsedEpisodeInfo.ReleaseHash);
@@ -159,6 +112,13 @@ namespace NzbDrone.Core.History
return;
}
var downloadId = message.DownloadId;
if (downloadId.IsNullOrWhiteSpace())
{
downloadId = FindDownloadId(message);
}
foreach (var episode in message.EpisodeInfo.Episodes)
{
var history = new History
@@ -168,7 +128,8 @@ namespace NzbDrone.Core.History
Quality = message.EpisodeInfo.Quality,
SourceTitle = message.ImportedEpisode.SceneName ?? Path.GetFileNameWithoutExtension(message.EpisodeInfo.Path),
SeriesId = message.ImportedEpisode.SeriesId,
EpisodeId = episode.Id
EpisodeId = episode.Id,
DownloadId = downloadId
};
//Won't have a value since we publish this event before saving to DB.
@@ -176,12 +137,57 @@ namespace NzbDrone.Core.History
history.Data.Add("DroppedPath", message.EpisodeInfo.Path);
history.Data.Add("ImportedPath", Path.Combine(message.EpisodeInfo.Series.Path, message.ImportedEpisode.RelativePath));
history.Data.Add("DownloadClient", message.DownloadClient);
history.Data.Add("DownloadClientId", message.DownloadClientId);
_historyRepository.Insert(history);
}
}
private string FindDownloadId(EpisodeImportedEvent trackedDownload)
{
_logger.Debug("Trying to find downloadId for {0} from history", trackedDownload.ImportedEpisode.Path);
var episodeIds = trackedDownload.EpisodeInfo.Episodes.Select(c => c.Id).ToList();
var allHistory = _historyRepository.FindDownloadHistory(trackedDownload.EpisodeInfo.Series.Id, trackedDownload.ImportedEpisode.Quality);
//Find download related items for these episdoes
var episodesHistory = allHistory.Where(h => episodeIds.Contains(h.EpisodeId)).ToList();
var processedDownloadId = episodesHistory
.Where(c => c.EventType != HistoryEventType.Grabbed && c.DownloadId != null)
.Select(c => c.DownloadId);
var stillDownloading = episodesHistory.Where(c => c.EventType == HistoryEventType.Grabbed && !processedDownloadId.Contains(c.DownloadId)).ToList();
string downloadId = null;
if (stillDownloading.Any())
{
foreach (var matchingHistory in trackedDownload.EpisodeInfo.Episodes.Select(e => stillDownloading.Where(c => c.EpisodeId == e.Id).ToList()))
{
if (matchingHistory.Count != 1)
{
return null;
}
var newDownloadId = matchingHistory.Single().DownloadId;
if (downloadId == null || downloadId == newDownloadId)
{
downloadId = newDownloadId;
}
else
{
return null;
}
}
}
return downloadId;
}
public void Handle(DownloadFailedEvent message)
{
foreach (var episodeId in message.EpisodeIds)
@@ -194,10 +200,10 @@ namespace NzbDrone.Core.History
SourceTitle = message.SourceTitle,
SeriesId = message.SeriesId,
EpisodeId = episodeId,
DownloadId = message.DownloadId
};
history.Data.Add("DownloadClient", message.DownloadClient);
history.Data.Add("DownloadClientId", message.DownloadClientId);
history.Data.Add("Message", message.Message);
_historyRepository.Insert(history);