You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Lidarr/src/Lidarr.Api.V1/Queue/QueueStatusController.cs

81 lines
2.9 KiB

using System;
using System.Linq;
using Lidarr.Http;
using Lidarr.Http.REST;
using Microsoft.AspNetCore.Mvc;
using NzbDrone.Common.TPL;
using NzbDrone.Core.Datastore.Events;
using NzbDrone.Core.Download.Pending;
using NzbDrone.Core.Download.TrackedDownloads;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Queue;
using NzbDrone.SignalR;
namespace Lidarr.Api.V1.Queue
{
[V1ApiController("queue/status")]
public class QueueStatusController : RestControllerWithSignalR<QueueStatusResource, NzbDrone.Core.Queue.Queue>,
IHandle<QueueUpdatedEvent>, IHandle<PendingReleasesUpdatedEvent>
{
private readonly IQueueService _queueService;
private readonly IPendingReleaseService _pendingReleaseService;
private readonly Debouncer _broadcastDebounce;
public QueueStatusController(IBroadcastSignalRMessage broadcastSignalRMessage, IQueueService queueService, IPendingReleaseService pendingReleaseService)
: base(broadcastSignalRMessage)
{
_queueService = queueService;
_pendingReleaseService = pendingReleaseService;
_broadcastDebounce = new Debouncer(BroadcastChange, TimeSpan.FromSeconds(5));
}
public override QueueStatusResource GetResourceById(int id)
{
throw new NotImplementedException();
}
[HttpGet]
[Produces("application/json")]
public QueueStatusResource GetQueueStatus()
{
_broadcastDebounce.Pause();
var queue = _queueService.GetQueue();
var pending = _pendingReleaseService.GetPendingQueue();
var resource = new QueueStatusResource
{
TotalCount = queue.Count + pending.Count,
Count = queue.Count(q => q.Artist != null) + pending.Count,
UnknownCount = queue.Count(q => q.Artist == null),
Errors = queue.Any(q => q.Artist != null && q.TrackedDownloadStatus == TrackedDownloadStatus.Error),
Warnings = queue.Any(q => q.Artist != null && q.TrackedDownloadStatus == TrackedDownloadStatus.Warning),
UnknownErrors = queue.Any(q => q.Artist == null && q.TrackedDownloadStatus == TrackedDownloadStatus.Error),
UnknownWarnings = queue.Any(q => q.Artist == null && q.TrackedDownloadStatus == TrackedDownloadStatus.Warning)
};
_broadcastDebounce.Resume();
return resource;
}
private void BroadcastChange()
{
BroadcastResourceChange(ModelAction.Updated, GetQueueStatus());
}
[NonAction]
public void Handle(QueueUpdatedEvent message)
{
_broadcastDebounce.Execute();
}
[NonAction]
public void Handle(PendingReleasesUpdatedEvent message)
{
_broadcastDebounce.Execute();
}
}
}