1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-06 02:52:41 +01:00

New: Updated Nzbget Download Client proxy with time estimation for both download and post-processing stages.

This commit is contained in:
Taloth Saldono 2014-05-24 12:48:55 +02:00
parent b8c9f6d42e
commit f304ad50d1
14 changed files with 218 additions and 119 deletions

View File

@ -52,7 +52,12 @@ public void Setup()
Name = "Droned.S01E01.Pilot.1080p.WEB-DL-DRONE", Name = "Droned.S01E01.Pilot.1080p.WEB-DL-DRONE",
DestDir = "somedirectory", DestDir = "somedirectory",
Parameters = new List<NzbgetParameter> { new NzbgetParameter { Name = "drone", Value = "id" } }, Parameters = new List<NzbgetParameter> { new NzbgetParameter { Name = "drone", Value = "id" } },
ParStatus = "Some Error" ParStatus = "Some Error",
UnpackStatus = "NONE",
MoveStatus = "NONE",
ScriptStatus = "NONE",
DeleteStatus = "NONE",
MarkStatus = "NONE"
}; };
_completed = new NzbgetHistoryItem _completed = new NzbgetHistoryItem
@ -63,8 +68,19 @@ public void Setup()
DestDir = "somedirectory", DestDir = "somedirectory",
Parameters = new List<NzbgetParameter> { new NzbgetParameter { Name = "drone", Value = "id" } }, Parameters = new List<NzbgetParameter> { new NzbgetParameter { Name = "drone", Value = "id" } },
ParStatus = "SUCCESS", ParStatus = "SUCCESS",
ScriptStatus = "NONE" UnpackStatus = "NONE",
MoveStatus = "SUCCESS",
ScriptStatus = "NONE",
DeleteStatus = "NONE",
MarkStatus = "NONE"
}; };
Mocker.GetMock<INzbgetProxy>()
.Setup(s => s.GetGlobalStatus(It.IsAny<NzbgetSettings>()))
.Returns(new NzbgetGlobalStatus
{
DownloadRate = 7000000
});
} }
protected void WithFailedDownload() protected void WithFailedDownload()
@ -93,6 +109,10 @@ protected virtual void WithQueue(NzbgetQueueItem queue)
Mocker.GetMock<INzbgetProxy>() Mocker.GetMock<INzbgetProxy>()
.Setup(s => s.GetQueue(It.IsAny<NzbgetSettings>())) .Setup(s => s.GetQueue(It.IsAny<NzbgetSettings>()))
.Returns(list); .Returns(list);
Mocker.GetMock<INzbgetProxy>()
.Setup(s => s.GetPostQueue(It.IsAny<NzbgetSettings>()))
.Returns(new List<NzbgetPostQueueItem>());
} }
protected virtual void WithHistory(NzbgetHistoryItem history) protected virtual void WithHistory(NzbgetHistoryItem history)
@ -134,7 +154,7 @@ public void queued_item_should_have_required_properties()
[Test] [Test]
public void paused_item_should_have_required_properties() public void paused_item_should_have_required_properties()
{ {
_queued.PausedSizeLo = _queued.FileSizeLo; _queued.PausedSizeLo = _queued.RemainingSizeLo;
WithQueue(_queued); WithQueue(_queued);
WithHistory(null); WithHistory(null);

View File

@ -44,7 +44,7 @@ private void GivenCompletedDownloadHandling(bool? enabled = null)
Mocker.GetMock<IDownloadTrackingService>() Mocker.GetMock<IDownloadTrackingService>()
.Setup(v => v.GetCompletedDownloads()) .Setup(v => v.GetCompletedDownloads())
.Returns(_completed.ToList()); .Returns(_completed.ToArray());
} }
private void GivenDroneFactoryFolder(bool exists = false) private void GivenDroneFactoryFolder(bool exists = false)

View File

@ -8,7 +8,7 @@ public class NzbgetQueueItem
public Int32 NzbId { get; set; } public Int32 NzbId { get; set; }
public Int32 FirstId { get; set; } public Int32 FirstId { get; set; }
public Int32 LastId { get; set; } public Int32 LastId { get; set; }
public string NzbName { get; set; } public String NzbName { get; set; }
public String Category { get; set; } public String Category { get; set; }
public UInt32 FileSizeLo { get; set; } public UInt32 FileSizeLo { get; set; }
public UInt32 FileSizeHi { get; set; } public UInt32 FileSizeHi { get; set; }
@ -16,6 +16,8 @@ public class NzbgetQueueItem
public UInt32 RemainingSizeHi { get; set; } public UInt32 RemainingSizeHi { get; set; }
public UInt32 PausedSizeLo { get; set; } public UInt32 PausedSizeLo { get; set; }
public UInt32 PausedSizeHi { get; set; } public UInt32 PausedSizeHi { get; set; }
public Int32 MinPriority { get; set; }
public Int32 MaxPriority { get; set; }
public Int32 ActiveDownloads { get; set; } public Int32 ActiveDownloads { get; set; }
public List<NzbgetParameter> Parameters { get; set; } public List<NzbgetParameter> Parameters { get; set; }
} }

View File

@ -56,11 +56,15 @@ public override string Download(RemoteEpisode remoteEpisode)
private IEnumerable<DownloadClientItem> GetQueue() private IEnumerable<DownloadClientItem> GetQueue()
{ {
NzbgetGlobalStatus globalStatus;
List<NzbgetQueueItem> queue; List<NzbgetQueueItem> queue;
Dictionary<Int32, NzbgetPostQueueItem> postQueue;
try try
{ {
globalStatus = _proxy.GetGlobalStatus(Settings);
queue = _proxy.GetQueue(Settings); queue = _proxy.GetQueue(Settings);
postQueue = _proxy.GetPostQueue(Settings).ToDictionary(v => v.NzbId);
} }
catch (DownloadClientException ex) catch (DownloadClientException ex)
{ {
@ -70,28 +74,57 @@ private IEnumerable<DownloadClientItem> GetQueue()
var queueItems = new List<DownloadClientItem>(); var queueItems = new List<DownloadClientItem>();
Int64 totalRemainingSize = 0;
foreach (var item in queue) foreach (var item in queue)
{ {
var postQueueItem = postQueue.GetValueOrDefault(item.NzbId);
Int64 totalSize = MakeInt64(item.FileSizeHi, item.FileSizeLo);
Int64 pausedSize = MakeInt64(item.PausedSizeHi, item.PausedSizeLo);
Int64 remainingSize = MakeInt64(item.RemainingSizeHi, item.RemainingSizeLo);
var droneParameter = item.Parameters.SingleOrDefault(p => p.Name == "drone"); var droneParameter = item.Parameters.SingleOrDefault(p => p.Name == "drone");
var queueItem = new DownloadClientItem(); var queueItem = new DownloadClientItem();
queueItem.DownloadClientId = droneParameter == null ? item.NzbId.ToString() : droneParameter.Value.ToString(); queueItem.DownloadClientId = droneParameter == null ? item.NzbId.ToString() : droneParameter.Value.ToString();
queueItem.Title = item.NzbName; queueItem.Title = item.NzbName;
queueItem.TotalSize = MakeInt64(item.FileSizeHi, item.FileSizeLo); queueItem.TotalSize = totalSize;
queueItem.RemainingSize = MakeInt64(item.RemainingSizeHi, item.RemainingSizeLo);
queueItem.Category = item.Category; queueItem.Category = item.Category;
if (queueItem.TotalSize == MakeInt64(item.PausedSizeHi, item.PausedSizeLo)) if (postQueueItem != null)
{
queueItem.Status = DownloadItemStatus.Downloading;
queueItem.Message = postQueueItem.ProgressLabel;
if (postQueueItem.StageProgress != 0)
{
queueItem.RemainingTime = TimeSpan.FromSeconds(postQueueItem.StageTimeSec * 1000 / postQueueItem.StageProgress - postQueueItem.StageTimeSec);
}
}
else if (globalStatus.DownloadPaused || remainingSize == pausedSize)
{ {
queueItem.Status = DownloadItemStatus.Paused; queueItem.Status = DownloadItemStatus.Paused;
} queueItem.RemainingSize = remainingSize;
else if (item.ActiveDownloads == 0 && queueItem.RemainingSize != 0)
{
queueItem.Status = DownloadItemStatus.Queued;
} }
else else
{ {
queueItem.Status = DownloadItemStatus.Downloading; if (item.ActiveDownloads == 0 && remainingSize != 0)
{
queueItem.Status = DownloadItemStatus.Queued;
}
else
{
queueItem.Status = DownloadItemStatus.Downloading;
}
queueItem.RemainingSize = remainingSize - pausedSize;
if (globalStatus.DownloadRate != 0)
{
queueItem.RemainingTime = TimeSpan.FromSeconds((totalRemainingSize + queueItem.RemainingSize) / globalStatus.DownloadRate);
totalRemainingSize += queueItem.RemainingSize;
}
} }
queueItems.Add(queueItem); queueItems.Add(queueItem);
@ -130,13 +163,17 @@ private IEnumerable<DownloadClientItem> GetHistory()
historyItem.Category = item.Category; historyItem.Category = item.Category;
historyItem.Message = String.Format("PAR Status: {0} - Unpack Status: {1} - Move Status: {2} - Script Status: {3} - Delete Status: {4} - Mark Status: {5}", item.ParStatus, item.UnpackStatus, item.MoveStatus, item.ScriptStatus, item.DeleteStatus, item.MarkStatus); historyItem.Message = String.Format("PAR Status: {0} - Unpack Status: {1} - Move Status: {2} - Script Status: {3} - Delete Status: {4} - Mark Status: {5}", item.ParStatus, item.UnpackStatus, item.MoveStatus, item.ScriptStatus, item.DeleteStatus, item.MarkStatus);
historyItem.Status = DownloadItemStatus.Completed; historyItem.Status = DownloadItemStatus.Completed;
historyItem.RemainingTime = TimeSpan.Zero;
if (item.DeleteStatus == "MANUAL")
{
continue;
}
if (!successStatus.Contains(item.ParStatus) || if (!successStatus.Contains(item.ParStatus) ||
!successStatus.Contains(item.UnpackStatus) || !successStatus.Contains(item.UnpackStatus) ||
!successStatus.Contains(item.MoveStatus) || !successStatus.Contains(item.MoveStatus) ||
!successStatus.Contains(item.ScriptStatus) || !successStatus.Contains(item.ScriptStatus))
!successStatus.Contains(item.DeleteStatus) ||
!successStatus.Contains(item.MarkStatus))
{ {
historyItem.Status = DownloadItemStatus.Failed; historyItem.Status = DownloadItemStatus.Failed;
} }
@ -179,7 +216,7 @@ public override void Test()
_proxy.GetVersion(Settings); _proxy.GetVersion(Settings);
} }
private VersionResponse GetVersion(string host = null, int port = 0, string username = null, string password = null) private String GetVersion(string host = null, int port = 0, string username = null, string password = null)
{ {
return _proxy.GetVersion(Settings); return _proxy.GetVersion(Settings);
} }

View File

@ -1,10 +0,0 @@
using System;
namespace NzbDrone.Core.Download.Clients.Nzbget
{
public class NzbgetBooleanResponse
{
public String Version { get; set; }
public Boolean Result { get; set; }
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NzbDrone.Core.Download.Clients.Nzbget
{
public class NzbgetGlobalStatus
{
public UInt32 RemainingSizeLo { get; set; }
public UInt32 RemainingSizeHi { get; set; }
public UInt32 DownloadedSizeLo { get; set; }
public UInt32 DownloadedSizeHi { get; set; }
public UInt32 DownloadRate { get; set; }
public UInt32 AverageDownloadRate { get; set; }
public UInt32 DownloadLimit { get; set; }
public Boolean DownloadPaused { get; set; }
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NzbDrone.Core.Download.Clients.Nzbget
{
public class NzbgetPostQueueItem
{
public Int32 NzbId { get; set; }
public String NzbName { get; set; }
public String Stage { get; set; }
public String ProgressLabel { get; set; }
public Int32 FileProgress { get; set; }
public Int32 StageProgress { get; set; }
public Int32 TotalTimeSec { get; set; }
public Int32 StageTimeSec { get; set; }
}
}

View File

@ -13,9 +13,11 @@ namespace NzbDrone.Core.Download.Clients.Nzbget
public interface INzbgetProxy public interface INzbgetProxy
{ {
string DownloadNzb(Stream nzb, string title, string category, int priority, NzbgetSettings settings); string DownloadNzb(Stream nzb, string title, string category, int priority, NzbgetSettings settings);
NzbgetGlobalStatus GetGlobalStatus(NzbgetSettings settings);
List<NzbgetQueueItem> GetQueue(NzbgetSettings settings); List<NzbgetQueueItem> GetQueue(NzbgetSettings settings);
List<NzbgetPostQueueItem> GetPostQueue(NzbgetSettings settings);
List<NzbgetHistoryItem> GetHistory(NzbgetSettings settings); List<NzbgetHistoryItem> GetHistory(NzbgetSettings settings);
VersionResponse GetVersion(NzbgetSettings settings); String GetVersion(NzbgetSettings settings);
void RemoveFromHistory(string id, NzbgetSettings settings); void RemoveFromHistory(string id, NzbgetSettings settings);
void RetryDownload(string id, NzbgetSettings settings); void RetryDownload(string id, NzbgetSettings settings);
} }
@ -34,7 +36,7 @@ public string DownloadNzb(Stream nzb, string title, string category, int priorit
var parameters = new object[] { title, category, priority, false, Convert.ToBase64String(nzb.ToBytes()) }; var parameters = new object[] { title, category, priority, false, Convert.ToBase64String(nzb.ToBytes()) };
var request = BuildRequest(new JsonRequest("append", parameters)); var request = BuildRequest(new JsonRequest("append", parameters));
var response = Json.Deserialize<NzbgetBooleanResponse>(ProcessRequest(request, settings)); var response = Json.Deserialize<NzbgetResponse<Boolean>>(ProcessRequest(request, settings));
_logger.Debug("Queue Response: [{0}]", response.Result); _logger.Debug("Queue Response: [{0}]", response.Result);
if (!response.Result) if (!response.Result)
@ -61,25 +63,39 @@ public string DownloadNzb(Stream nzb, string title, string category, int priorit
return droneId; return droneId;
} }
public NzbgetGlobalStatus GetGlobalStatus(NzbgetSettings settings)
{
var request = BuildRequest(new JsonRequest("status"));
return Json.Deserialize<NzbgetResponse<NzbgetGlobalStatus>>(ProcessRequest(request, settings)).Result;
}
public List<NzbgetQueueItem> GetQueue(NzbgetSettings settings) public List<NzbgetQueueItem> GetQueue(NzbgetSettings settings)
{ {
var request = BuildRequest(new JsonRequest("listgroups")); var request = BuildRequest(new JsonRequest("listgroups"));
return Json.Deserialize<NzbgetListResponse<NzbgetQueueItem>>(ProcessRequest(request, settings)).QueueItems; return Json.Deserialize<NzbgetResponse<List<NzbgetQueueItem>>>(ProcessRequest(request, settings)).Result;
}
public List<NzbgetPostQueueItem> GetPostQueue(NzbgetSettings settings)
{
var request = BuildRequest(new JsonRequest("postqueue"));
return Json.Deserialize<NzbgetResponse<List<NzbgetPostQueueItem>>>(ProcessRequest(request, settings)).Result;
} }
public List<NzbgetHistoryItem> GetHistory(NzbgetSettings settings) public List<NzbgetHistoryItem> GetHistory(NzbgetSettings settings)
{ {
var request = BuildRequest(new JsonRequest("history")); var request = BuildRequest(new JsonRequest("history"));
return Json.Deserialize<NzbgetListResponse<NzbgetHistoryItem>>(ProcessRequest(request, settings)).QueueItems; return Json.Deserialize<NzbgetResponse<List<NzbgetHistoryItem>>>(ProcessRequest(request, settings)).Result;
} }
public VersionResponse GetVersion(NzbgetSettings settings) public String GetVersion(NzbgetSettings settings)
{ {
var request = BuildRequest(new JsonRequest("version")); var request = BuildRequest(new JsonRequest("version"));
return Json.Deserialize<VersionResponse>(ProcessRequest(request, settings)); return Json.Deserialize<NzbgetResponse<String>>(ProcessRequest(request, settings)).Version;
} }
public void RemoveFromHistory(string id, NzbgetSettings settings) public void RemoveFromHistory(string id, NzbgetSettings settings)
@ -120,7 +136,7 @@ private bool EditQueue(string command, int offset, string editText, int id, Nzbg
{ {
var parameters = new object[] { command, offset, editText, id }; var parameters = new object[] { command, offset, editText, id };
var request = BuildRequest(new JsonRequest("editqueue", parameters)); var request = BuildRequest(new JsonRequest("editqueue", parameters));
var response = Json.Deserialize<NzbgetBooleanResponse>(ProcessRequest(request, settings)); var response = Json.Deserialize<NzbgetResponse<Boolean>>(ProcessRequest(request, settings));
return response.Result; return response.Result;
} }

View File

@ -4,11 +4,11 @@
namespace NzbDrone.Core.Download.Clients.Nzbget namespace NzbDrone.Core.Download.Clients.Nzbget
{ {
public class NzbgetListResponse<T> public class NzbgetResponse<T>
{ {
public String Version { get; set; } public String Version { get; set; }
[JsonProperty(PropertyName = "result")] public T Result { get; set; }
public List<T> QueueItems { get; set; }
} }
} }

View File

@ -1,10 +0,0 @@
using System;
namespace NzbDrone.Core.Download.Clients.Nzbget
{
public class VersionResponse
{
public String Version { get; set; }
public String Result { get; set; }
}
}

View File

@ -83,6 +83,8 @@ public override IEnumerable<DownloadClientItem> GetItems()
else else
{ {
historyItem.Status = DownloadItemStatus.Completed; historyItem.Status = DownloadItemStatus.Completed;
historyItem.RemainingTime = TimeSpan.Zero;
} }
historyItem.RemoteEpisode = GetRemoteEpisode(historyItem.Title); historyItem.RemoteEpisode = GetRemoteEpisode(historyItem.Title);

View File

@ -8,22 +8,23 @@ namespace NzbDrone.Core.Download
{ {
public class DownloadClientItem public class DownloadClientItem
{ {
public string DownloadClient { get; set; } public String DownloadClient { get; set; }
public string DownloadClientId { get; set; } public String DownloadClientId { get; set; }
public string Category { get; set; } public String Category { get; set; }
public string Title { get; set; } public String Title { get; set; }
public long TotalSize { get; set; } public Int64 TotalSize { get; set; }
public long RemainingSize { get; set; } public Int64 RemainingSize { get; set; }
public TimeSpan DownloadTime { get; set; } public TimeSpan DownloadTime { get; set; }
public TimeSpan RemainingTime { get; set; } public TimeSpan RemainingTime { get; set; }
public string OutputPath { get; set; } public String OutputPath { get; set; }
public string Message { get; set; } public String Message { get; set; }
public DownloadItemStatus Status { get; set; } public DownloadItemStatus Status { get; set; }
public bool IsEncrypted { get; set; } public Boolean IsEncrypted { get; set; }
public bool IsReadOnly { get; set; } public Boolean IsReadOnly { get; set; }
public RemoteEpisode RemoteEpisode { get; set; } public RemoteEpisode RemoteEpisode { get; set; }
} }
} }

View File

@ -15,12 +15,12 @@ namespace NzbDrone.Core.Download
{ {
public interface IDownloadTrackingService public interface IDownloadTrackingService
{ {
List<TrackedDownload> GetTrackedDownloads(); TrackedDownload[] GetTrackedDownloads();
List<TrackedDownload> GetCompletedDownloads(); TrackedDownload[] GetCompletedDownloads();
List<TrackedDownload> GetQueuedDownloads(); TrackedDownload[] GetQueuedDownloads();
} }
public class DownloadTrackingService : IDownloadTrackingService, IExecute<CheckForFinishedDownloadCommand>, IHandle<ApplicationStartedEvent> public class DownloadTrackingService : IDownloadTrackingService, IExecute<CheckForFinishedDownloadCommand>, IHandle<ApplicationStartedEvent>, IHandle<EpisodeGrabbedEvent>
{ {
private readonly IProvideDownloadClient _downloadClientProvider; private readonly IProvideDownloadClient _downloadClientProvider;
private readonly IHistoryService _historyService; private readonly IHistoryService _historyService;
@ -30,8 +30,7 @@ public class DownloadTrackingService : IDownloadTrackingService, IExecute<CheckF
private readonly ICompletedDownloadService _completedDownloadService; private readonly ICompletedDownloadService _completedDownloadService;
private readonly Logger _logger; private readonly Logger _logger;
private readonly ICached<TrackedDownload> _trackedDownloads; private readonly ICached<TrackedDownload[]> _trackedDownloadCache;
private readonly ICached<List<TrackedDownload>> _queuedDownloads;
public static string DOWNLOAD_CLIENT = "downloadClient"; public static string DOWNLOAD_CLIENT = "downloadClient";
public static string DOWNLOAD_CLIENT_ID = "downloadClientId"; public static string DOWNLOAD_CLIENT_ID = "downloadClientId";
@ -53,108 +52,105 @@ public DownloadTrackingService(IProvideDownloadClient downloadClientProvider,
_completedDownloadService = completedDownloadService; _completedDownloadService = completedDownloadService;
_logger = logger; _logger = logger;
_trackedDownloads = cacheManager.GetCache<TrackedDownload>(GetType()); _trackedDownloadCache = cacheManager.GetCache<TrackedDownload[]>(GetType());
_queuedDownloads = cacheManager.GetCache<List<TrackedDownload>>(GetType(), "queued");
} }
public List<TrackedDownload> GetTrackedDownloads() public TrackedDownload[] GetTrackedDownloads()
{ {
return _trackedDownloads.Values.ToList(); return _trackedDownloadCache.Get("tracked", () => new TrackedDownload[0]);
} }
public List<TrackedDownload> GetCompletedDownloads() public TrackedDownload[] GetCompletedDownloads()
{ {
return _trackedDownloads.Values.Where(v => v.State == TrackedDownloadState.Downloading && v.DownloadItem.Status == DownloadItemStatus.Completed).ToList(); return GetTrackedDownloads()
.Where(v => v.State == TrackedDownloadState.Downloading && v.DownloadItem.Status == DownloadItemStatus.Completed)
.ToArray();
} }
public List<TrackedDownload> GetQueuedDownloads() public TrackedDownload[] GetQueuedDownloads()
{ {
return _queuedDownloads.Get("queued", () => return _trackedDownloadCache.Get("queued", () =>
{ {
UpdateTrackedDownloads(); UpdateTrackedDownloads();
var enabledFailedDownloadHandling = _configService.EnableFailedDownloadHandling; return FilterQueuedDownloads(GetTrackedDownloads());
var enabledCompletedDownloadHandling = _configService.EnableCompletedDownloadHandling;
return _trackedDownloads.Values
.Where(v => v.State == TrackedDownloadState.Downloading)
.Where(v =>
v.DownloadItem.Status == DownloadItemStatus.Queued ||
v.DownloadItem.Status == DownloadItemStatus.Paused ||
v.DownloadItem.Status == DownloadItemStatus.Downloading ||
v.DownloadItem.Status == DownloadItemStatus.Failed && enabledFailedDownloadHandling ||
v.DownloadItem.Status == DownloadItemStatus.Completed && enabledCompletedDownloadHandling)
.ToList();
}, TimeSpan.FromSeconds(5.0)); }, TimeSpan.FromSeconds(5.0));
} }
private TrackedDownload GetTrackedDownload(IDownloadClient downloadClient, DownloadClientItem queueItem) private TrackedDownload[] FilterQueuedDownloads(IEnumerable<TrackedDownload> trackedDownloads)
{ {
var id = String.Format("{0}-{1}", downloadClient.Definition.Id, queueItem.DownloadClientId); var enabledFailedDownloadHandling = _configService.EnableFailedDownloadHandling;
var trackedDownload = _trackedDownloads.Get(id, () => new TrackedDownload var enabledCompletedDownloadHandling = _configService.EnableCompletedDownloadHandling;
{
TrackingId = id,
DownloadClient = downloadClient.Definition.Id,
StartedTracking = DateTime.UtcNow,
State = TrackedDownloadState.Unknown
});
trackedDownload.DownloadItem = queueItem; return trackedDownloads
.Where(v => v.State == TrackedDownloadState.Downloading)
return trackedDownload; .Where(v =>
v.DownloadItem.Status == DownloadItemStatus.Queued ||
v.DownloadItem.Status == DownloadItemStatus.Paused ||
v.DownloadItem.Status == DownloadItemStatus.Downloading ||
v.DownloadItem.Status == DownloadItemStatus.Failed && enabledFailedDownloadHandling ||
v.DownloadItem.Status == DownloadItemStatus.Completed && enabledCompletedDownloadHandling)
.ToArray();
} }
private List<History.History> GetHistoryItems(List<History.History> grabbedHistory, string downloadClientId) private List<History.History> GetHistoryItems(List<History.History> grabbedHistory, string downloadClientId)
{ {
return grabbedHistory.Where(h => downloadClientId.Equals(h.Data.GetValueOrDefault(DOWNLOAD_CLIENT_ID))) return grabbedHistory.Where(h => downloadClientId.Equals(h.Data.GetValueOrDefault(DOWNLOAD_CLIENT_ID)))
.ToList(); .ToList();
} }
private Boolean UpdateTrackedDownloads() private Boolean UpdateTrackedDownloads()
{ {
var downloadClients = _downloadClientProvider.GetDownloadClients(); var downloadClients = _downloadClientProvider.GetDownloadClients();
var oldTrackedDownloads = new HashSet<TrackedDownload>(_trackedDownloads.Values); var oldTrackedDownloads = GetTrackedDownloads().ToDictionary(v => v.TrackingId);
var newTrackedDownloads = new HashSet<TrackedDownload>(); var newTrackedDownloads = new List<TrackedDownload>();
var stateChanged = false; var stateChanged = false;
foreach (var downloadClient in downloadClients) foreach (var downloadClient in downloadClients)
{ {
var downloadClientHistory = downloadClient.GetItems().Select(v => GetTrackedDownload(downloadClient, v)).ToList(); var downloadClientHistory = downloadClient.GetItems().ToList();
foreach (var trackedDownload in downloadClientHistory) foreach (var downloadItem in downloadClientHistory)
{ {
if (!oldTrackedDownloads.Contains(trackedDownload)) var trackingId = String.Format("{0}-{1}", downloadClient.Definition.Id, downloadItem.DownloadClientId);
TrackedDownload trackedDownload;
if (!oldTrackedDownloads.TryGetValue(trackingId, out trackedDownload))
{ {
_logger.Trace("Started tracking download from history: {0}", trackedDownload.TrackingId); trackedDownload = new TrackedDownload
{
TrackingId = trackingId,
DownloadClient = downloadClient.Definition.Id,
StartedTracking = DateTime.UtcNow,
State = TrackedDownloadState.Unknown
};
_logger.Trace("Started tracking download from history: {0}: {1}", trackedDownload.TrackingId, downloadItem.Title);
stateChanged = true; stateChanged = true;
} }
trackedDownload.DownloadItem = downloadItem;
newTrackedDownloads.Add(trackedDownload); newTrackedDownloads.Add(trackedDownload);
} }
} }
foreach (var item in oldTrackedDownloads.Except(newTrackedDownloads)) foreach (var downloadItem in oldTrackedDownloads.Values.Except(newTrackedDownloads))
{ {
if (item.State != TrackedDownloadState.Removed) if (downloadItem.State != TrackedDownloadState.Removed)
{ {
item.State = TrackedDownloadState.Removed; downloadItem.State = TrackedDownloadState.Removed;
stateChanged = true; stateChanged = true;
_logger.Debug("Item removed from download client by user: {0}", item.TrackingId); _logger.Debug("Item removed from download client by user: {0}: {1}", downloadItem.TrackingId, downloadItem.DownloadItem.Title);
} }
_logger.Trace("Stopped tracking download: {0}: {1}", downloadItem.TrackingId, downloadItem.DownloadItem.Title);
} }
foreach (var item in newTrackedDownloads.Union(oldTrackedDownloads).Where(v => v.State == TrackedDownloadState.Removed)) _trackedDownloadCache.Set("tracked", newTrackedDownloads.ToArray());
{
_trackedDownloads.Remove(item.TrackingId);
_logger.Trace("Stopped tracking download: {0}", item.TrackingId);
}
_queuedDownloads.Clear();
return stateChanged; return stateChanged;
} }
@ -168,7 +164,7 @@ private void ProcessTrackedDownloads()
var stateChanged = UpdateTrackedDownloads(); var stateChanged = UpdateTrackedDownloads();
var downloadClients = _downloadClientProvider.GetDownloadClients(); var downloadClients = _downloadClientProvider.GetDownloadClients();
var trackedDownloads = _trackedDownloads.Values.ToArray(); var trackedDownloads = GetTrackedDownloads();
foreach (var trackedDownload in trackedDownloads) foreach (var trackedDownload in trackedDownloads)
{ {
@ -190,6 +186,8 @@ private void ProcessTrackedDownloads()
} }
} }
_trackedDownloadCache.Set("queued", FilterQueuedDownloads(trackedDownloads), TimeSpan.FromSeconds(5.0));
if (stateChanged) if (stateChanged)
{ {
_eventAggregator.PublishEvent(new UpdateQueueEvent()); _eventAggregator.PublishEvent(new UpdateQueueEvent());
@ -205,5 +203,10 @@ public void Handle(ApplicationStartedEvent message)
{ {
ProcessTrackedDownloads(); ProcessTrackedDownloads();
} }
public void Handle(EpisodeGrabbedEvent message)
{
ProcessTrackedDownloads();
}
} }
} }

View File

@ -239,6 +239,8 @@
<Compile Include="DecisionEngine\Specifications\RssSync\HistorySpecification.cs" /> <Compile Include="DecisionEngine\Specifications\RssSync\HistorySpecification.cs" />
<Compile Include="DiskSpace\DiskSpace.cs" /> <Compile Include="DiskSpace\DiskSpace.cs" />
<Compile Include="DiskSpace\DiskSpaceService.cs" /> <Compile Include="DiskSpace\DiskSpaceService.cs" />
<Compile Include="Download\Clients\Nzbget\NzbgetGlobalStatus.cs" />
<Compile Include="Download\Clients\Nzbget\NzbgetPostQueueItem.cs" />
<Compile Include="Download\Clients\Sabnzbd\SabnzbdDownloadStatus.cs" /> <Compile Include="Download\Clients\Sabnzbd\SabnzbdDownloadStatus.cs" />
<Compile Include="Download\Clients\UsenetBlackhole\UsenetBlackhole.cs" /> <Compile Include="Download\Clients\UsenetBlackhole\UsenetBlackhole.cs" />
<Compile Include="Download\Clients\UsenetBlackhole\TestUsenetBlackholeCommand.cs" /> <Compile Include="Download\Clients\UsenetBlackhole\TestUsenetBlackholeCommand.cs" />
@ -525,13 +527,11 @@
<Compile Include="Instrumentation\LogService.cs" /> <Compile Include="Instrumentation\LogService.cs" />
<Compile Include="Instrumentation\DatabaseTarget.cs" /> <Compile Include="Instrumentation\DatabaseTarget.cs" />
<Compile Include="MediaFiles\MediaInfo\MediaInfoModel.cs" /> <Compile Include="MediaFiles\MediaInfo\MediaInfoModel.cs" />
<Compile Include="Download\Clients\Nzbget\NzbgetBooleanResponse.cs" />
<Compile Include="Download\Clients\Nzbget\ErrorModel.cs" /> <Compile Include="Download\Clients\Nzbget\ErrorModel.cs" />
<Compile Include="Download\Clients\Nzbget\JsonError.cs" /> <Compile Include="Download\Clients\Nzbget\JsonError.cs" />
<Compile Include="Download\Clients\Nzbget\NzbgetListResponse.cs" /> <Compile Include="Download\Clients\Nzbget\NzbgetResponse.cs" />
<Compile Include="Download\Clients\Nzbget\NzbgetQueueItem.cs" /> <Compile Include="Download\Clients\Nzbget\NzbgetQueueItem.cs" />
<Compile Include="Download\Clients\Nzbget\NzbgetPriority.cs" /> <Compile Include="Download\Clients\Nzbget\NzbgetPriority.cs" />
<Compile Include="Download\Clients\Nzbget\VersionResponse.cs" />
<Compile Include="Organizer\NamingConfig.cs" /> <Compile Include="Organizer\NamingConfig.cs" />
<Compile Include="Parser\Language.cs" /> <Compile Include="Parser\Language.cs" />
<Compile Include="Parser\Model\LocalEpisode.cs" /> <Compile Include="Parser\Model\LocalEpisode.cs" />