2017-09-04 02:20:56 +00:00
|
|
|
using System;
|
2014-02-21 02:30:30 +00:00
|
|
|
using System.Collections.Generic;
|
2014-12-26 13:55:35 +00:00
|
|
|
using System.Linq;
|
|
|
|
using NzbDrone.Common.Crypto;
|
2014-12-19 00:26:42 +00:00
|
|
|
using NzbDrone.Core.Download.TrackedDownloads;
|
2018-11-11 05:38:38 +00:00
|
|
|
using NzbDrone.Core.History;
|
2014-12-19 00:26:42 +00:00
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2017-08-14 02:58:42 +00:00
|
|
|
using NzbDrone.Core.Music;
|
2020-01-03 12:49:24 +00:00
|
|
|
using NzbDrone.Core.Qualities;
|
2013-10-02 05:20:30 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Queue
|
|
|
|
{
|
|
|
|
public interface IQueueService
|
|
|
|
{
|
|
|
|
List<Queue> GetQueue();
|
2014-12-03 00:47:45 +00:00
|
|
|
Queue Find(int id);
|
2017-09-04 02:20:56 +00:00
|
|
|
void Remove(int id);
|
2013-10-02 05:20:30 +00:00
|
|
|
}
|
|
|
|
|
2014-12-19 00:26:42 +00:00
|
|
|
public class QueueService : IQueueService, IHandle<TrackedDownloadRefreshedEvent>
|
2013-10-02 05:20:30 +00:00
|
|
|
{
|
2014-12-19 00:26:42 +00:00
|
|
|
private readonly IEventAggregator _eventAggregator;
|
2024-06-30 17:47:00 +00:00
|
|
|
private static List<Queue> _queue = new ();
|
2018-11-11 05:38:38 +00:00
|
|
|
private readonly IHistoryService _historyService;
|
2013-10-02 05:20:30 +00:00
|
|
|
|
2018-11-11 05:38:38 +00:00
|
|
|
public QueueService(IEventAggregator eventAggregator,
|
|
|
|
IHistoryService historyService)
|
2013-10-02 05:20:30 +00:00
|
|
|
{
|
2014-12-19 00:26:42 +00:00
|
|
|
_eventAggregator = eventAggregator;
|
2018-11-11 05:38:38 +00:00
|
|
|
_historyService = historyService;
|
2013-10-02 05:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<Queue> GetQueue()
|
|
|
|
{
|
2014-12-19 00:26:42 +00:00
|
|
|
return _queue;
|
2013-10-02 05:20:30 +00:00
|
|
|
}
|
|
|
|
|
2014-12-03 00:47:45 +00:00
|
|
|
public Queue Find(int id)
|
|
|
|
{
|
2014-12-19 00:26:42 +00:00
|
|
|
return _queue.SingleOrDefault(q => q.Id == id);
|
2014-12-03 00:47:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 02:20:56 +00:00
|
|
|
public void Remove(int id)
|
|
|
|
{
|
|
|
|
_queue.Remove(Find(id));
|
|
|
|
}
|
|
|
|
|
2014-12-26 13:55:35 +00:00
|
|
|
private IEnumerable<Queue> MapQueue(TrackedDownload trackedDownload)
|
2014-12-19 00:26:42 +00:00
|
|
|
{
|
2019-02-23 22:39:11 +00:00
|
|
|
if (trackedDownload.RemoteAlbum?.Albums != null && trackedDownload.RemoteAlbum.Albums.Any())
|
2013-10-02 05:20:30 +00:00
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
foreach (var album in trackedDownload.RemoteAlbum.Albums)
|
2013-10-02 05:20:30 +00:00
|
|
|
{
|
2019-02-23 22:39:11 +00:00
|
|
|
yield return MapQueueItem(trackedDownload, album);
|
2013-10-02 05:20:30 +00:00
|
|
|
}
|
2015-07-11 20:49:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-02-23 22:39:11 +00:00
|
|
|
yield return MapQueueItem(trackedDownload, null);
|
2015-07-11 20:49:38 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-19 00:26:42 +00:00
|
|
|
|
2019-02-23 22:39:11 +00:00
|
|
|
private Queue MapQueueItem(TrackedDownload trackedDownload, Album album)
|
2015-07-11 20:49:38 +00:00
|
|
|
{
|
2023-05-23 10:52:39 +00:00
|
|
|
var downloadForced = false;
|
2021-12-25 01:55:32 +00:00
|
|
|
var history = _historyService.Find(trackedDownload.DownloadItem.DownloadId, EntityHistoryEventType.Grabbed).FirstOrDefault();
|
2018-11-11 05:38:38 +00:00
|
|
|
if (history != null && history.Data.ContainsKey("downloadForced"))
|
|
|
|
{
|
|
|
|
downloadForced = bool.Parse(history.Data["downloadForced"]);
|
|
|
|
}
|
2020-01-03 12:49:24 +00:00
|
|
|
|
2015-07-11 20:49:38 +00:00
|
|
|
var queue = new Queue
|
|
|
|
{
|
2019-03-02 02:52:05 +00:00
|
|
|
Artist = trackedDownload.RemoteAlbum?.Artist,
|
2017-08-14 02:58:42 +00:00
|
|
|
Album = album,
|
2019-02-23 22:39:11 +00:00
|
|
|
Quality = trackedDownload.RemoteAlbum?.ParsedAlbumInfo.Quality ?? new QualityModel(Quality.Unknown),
|
|
|
|
Title = Parser.Parser.RemoveFileExtension(trackedDownload.DownloadItem.Title),
|
2015-07-11 20:49:38 +00:00
|
|
|
Size = trackedDownload.DownloadItem.TotalSize,
|
|
|
|
Sizeleft = trackedDownload.DownloadItem.RemainingSize,
|
|
|
|
Timeleft = trackedDownload.DownloadItem.RemainingTime,
|
|
|
|
Status = trackedDownload.DownloadItem.Status.ToString(),
|
2020-07-26 20:27:30 +00:00
|
|
|
TrackedDownloadStatus = trackedDownload.Status,
|
|
|
|
TrackedDownloadState = trackedDownload.State,
|
2015-07-11 20:49:38 +00:00
|
|
|
StatusMessages = trackedDownload.StatusMessages.ToList(),
|
2017-09-04 02:20:56 +00:00
|
|
|
ErrorMessage = trackedDownload.DownloadItem.Message,
|
2017-08-14 02:58:42 +00:00
|
|
|
RemoteAlbum = trackedDownload.RemoteAlbum,
|
2015-07-11 20:49:38 +00:00
|
|
|
DownloadId = trackedDownload.DownloadItem.DownloadId,
|
2017-09-04 02:20:56 +00:00
|
|
|
Protocol = trackedDownload.Protocol,
|
2020-11-16 21:34:49 +00:00
|
|
|
DownloadClient = trackedDownload.DownloadItem.DownloadClientInfo.Name,
|
2018-11-11 05:38:38 +00:00
|
|
|
Indexer = trackedDownload.Indexer,
|
2019-04-28 04:32:38 +00:00
|
|
|
OutputPath = trackedDownload.DownloadItem.OutputPath.ToString(),
|
2024-01-16 05:28:28 +00:00
|
|
|
DownloadForced = downloadForced,
|
2024-01-23 04:56:35 +00:00
|
|
|
Added = trackedDownload.Added,
|
|
|
|
DownloadClientHasPostImportCategory = trackedDownload.DownloadItem.DownloadClientInfo.HasPostImportCategory
|
2015-07-11 20:49:38 +00:00
|
|
|
};
|
|
|
|
|
2021-12-24 22:46:44 +00:00
|
|
|
queue.Id = HashConverter.GetHashInt31($"trackedDownload-{trackedDownload.DownloadClient}-{trackedDownload.DownloadItem.DownloadId}-album{album?.Id ?? 0}");
|
2019-02-23 22:39:11 +00:00
|
|
|
|
2015-07-11 20:49:38 +00:00
|
|
|
if (queue.Timeleft.HasValue)
|
|
|
|
{
|
|
|
|
queue.EstimatedCompletionTime = DateTime.UtcNow.Add(queue.Timeleft.Value);
|
2013-10-02 05:20:30 +00:00
|
|
|
}
|
|
|
|
|
2015-07-11 20:49:38 +00:00
|
|
|
return queue;
|
2013-10-02 05:20:30 +00:00
|
|
|
}
|
2019-02-23 22:39:11 +00:00
|
|
|
|
|
|
|
public void Handle(TrackedDownloadRefreshedEvent message)
|
|
|
|
{
|
2024-06-30 17:47:00 +00:00
|
|
|
_queue = message.TrackedDownloads
|
|
|
|
.Where(t => t.IsTrackable)
|
|
|
|
.OrderBy(c => c.DownloadItem.RemainingTime)
|
|
|
|
.SelectMany(MapQueue)
|
|
|
|
.ToList();
|
2019-02-23 22:39:11 +00:00
|
|
|
|
|
|
|
_eventAggregator.PublishEvent(new QueueUpdatedEvent());
|
|
|
|
}
|
2013-10-02 05:20:30 +00:00
|
|
|
}
|
|
|
|
}
|