Fixed: Cutoff will be respected when release is still in queue

pull/4/head
Mark McDowall 10 years ago
parent 03335b2a71
commit 72993f3c3c

@ -3,6 +3,7 @@ using System.Linq;
using FizzWare.NBuilder;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.DecisionEngine;
using NzbDrone.Core.DecisionEngine.Specifications;
using NzbDrone.Core.Download.TrackedDownloads;
using NzbDrone.Core.Parser.Model;
@ -15,7 +16,7 @@ using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.DecisionEngineTests
{
[TestFixture]
public class NotInQueueSpecificationFixture : CoreTest<NotInQueueSpecification>
public class QueueSpecificationFixture : CoreTest<QueueSpecification>
{
private Series _series;
private Episode _episode;
@ -27,6 +28,8 @@ namespace NzbDrone.Core.Test.DecisionEngineTests
[SetUp]
public void Setup()
{
Mocker.Resolve<QualityUpgradableSpecification>();
_series = Builder<Series>.CreateNew()
.With(e => e.Profile = new Profile { Items = Qualities.QualityFixture.GetDefaultQualities() })
.Build();
@ -91,10 +94,11 @@ namespace NzbDrone.Core.Test.DecisionEngineTests
Subject.IsSatisfiedBy(_remoteEpisode, null).Accepted.Should().BeTrue();
}
[Test]
public void should_return_true_when_quality_in_queue_is_lower()
{
_series.Profile.Value.Cutoff = Quality.Bluray1080p;
var remoteEpisode = Builder<RemoteEpisode>.CreateNew()
.With(r => r.Series = _series)
.With(r => r.Episodes = new List<Episode> { _episode })
@ -143,6 +147,8 @@ namespace NzbDrone.Core.Test.DecisionEngineTests
[Test]
public void should_return_false_when_quality_in_queue_is_better()
{
_series.Profile.Value.Cutoff = Quality.Bluray1080p;
var remoteEpisode = Builder<RemoteEpisode>.CreateNew()
.With(r => r.Series = _series)
.With(r => r.Episodes = new List<Episode> { _episode })
@ -230,5 +236,24 @@ namespace NzbDrone.Core.Test.DecisionEngineTests
GivenQueue(remoteEpisodes);
Subject.IsSatisfiedBy(_remoteEpisode, null).Accepted.Should().BeFalse();
}
[Test]
public void should_return_false_if_quality_in_queue_meets_cutoff()
{
_series.Profile.Value.Cutoff = _remoteEpisode.ParsedEpisodeInfo.Quality.Quality;
var remoteEpisode = Builder<RemoteEpisode>.CreateNew()
.With(r => r.Series = _series)
.With(r => r.Episodes = new List<Episode> { _episode })
.With(r => r.ParsedEpisodeInfo = new ParsedEpisodeInfo
{
Quality = new QualityModel(Quality.HDTV720p)
})
.Build();
GivenQueue(new List<RemoteEpisode> { remoteEpisode });
Subject.IsSatisfiedBy(_remoteEpisode, null).Accepted.Should().BeFalse();
}
}
}

@ -136,7 +136,7 @@
<Compile Include="DecisionEngineTests\HistorySpecificationFixture.cs" />
<Compile Include="DecisionEngineTests\LanguageSpecificationFixture.cs" />
<Compile Include="DecisionEngineTests\MonitoredEpisodeSpecificationFixture.cs" />
<Compile Include="DecisionEngineTests\NotInQueueSpecificationFixture.cs" />
<Compile Include="DecisionEngineTests\QueueSpecificationFixture.cs" />
<Compile Include="DecisionEngineTests\ReleaseRestrictionsSpecificationFixture.cs" />
<Compile Include="DecisionEngineTests\PrioritizeDownloadDecisionFixture.cs" />
<Compile Include="DecisionEngineTests\QualityAllowedByProfileSpecificationFixture.cs" />

@ -1,46 +0,0 @@
using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Core.IndexerSearch.Definitions;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Qualities;
using NzbDrone.Core.Queue;
namespace NzbDrone.Core.DecisionEngine.Specifications
{
public class NotInQueueSpecification : IDecisionEngineSpecification
{
private readonly IQueueService _queueService;
private readonly Logger _logger;
public NotInQueueSpecification(IQueueService queueService, Logger logger)
{
_queueService = queueService;
_logger = logger;
}
public RejectionType Type { get { return RejectionType.Permanent; } }
public Decision IsSatisfiedBy(RemoteEpisode subject, SearchCriteriaBase searchCriteria)
{
var queue = _queueService.GetQueue()
.Select(q => q.RemoteEpisode).ToList();
if (IsInQueue(subject, queue))
{
_logger.Debug("Already in queue, rejecting.");
return Decision.Reject("Already in download queue");
}
return Decision.Accept();
}
private bool IsInQueue(RemoteEpisode newEpisode, IEnumerable<RemoteEpisode> episodesInQueue)
{
var matchingSeries = episodesInQueue.Where(q => q.Series.Id == newEpisode.Series.Id);
var matchingSeriesAndQuality = matchingSeries.Where(q => new QualityModelComparer(q.Series.Profile).Compare(q.ParsedEpisodeInfo.Quality, newEpisode.ParsedEpisodeInfo.Quality) >= 0);
return matchingSeriesAndQuality.Any(q => q.Episodes.Select(e => e.Id).Intersect(newEpisode.Episodes.Select(e => e.Id)).Any());
}
}
}

@ -0,0 +1,56 @@
using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Core.IndexerSearch.Definitions;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Qualities;
using NzbDrone.Core.Queue;
namespace NzbDrone.Core.DecisionEngine.Specifications
{
public class QueueSpecification : IDecisionEngineSpecification
{
private readonly IQueueService _queueService;
private readonly QualityUpgradableSpecification _qualityUpgradableSpecification;
private readonly Logger _logger;
public QueueSpecification(IQueueService queueService,
QualityUpgradableSpecification qualityUpgradableSpecification,
Logger logger)
{
_queueService = queueService;
_qualityUpgradableSpecification = qualityUpgradableSpecification;
_logger = logger;
}
public RejectionType Type { get { return RejectionType.Permanent; } }
public Decision IsSatisfiedBy(RemoteEpisode subject, SearchCriteriaBase searchCriteria)
{
var queue = _queueService.GetQueue()
.Select(q => q.RemoteEpisode).ToList();
var matchingSeries = queue.Where(q => q.Series.Id == subject.Series.Id);
var matchingEpisode = matchingSeries.Where(q => q.Episodes.Select(e => e.Id).Intersect(subject.Episodes.Select(e => e.Id)).Any());
foreach (var remoteEpisode in matchingEpisode)
{
_logger.Debug("Checking if existing release in queue meets cutoff. Queued quality is: {0}", remoteEpisode.ParsedEpisodeInfo.Quality);
if (!_qualityUpgradableSpecification.CutoffNotMet(subject.Series.Profile, remoteEpisode.ParsedEpisodeInfo.Quality, subject.ParsedEpisodeInfo.Quality))
{
return Decision.Reject("Quality for release in queue alerady meets cutoff: {0}", remoteEpisode.ParsedEpisodeInfo.Quality);
}
_logger.Debug("Checking if release is higher quality than queued release. Queued quality is: {0}", remoteEpisode.ParsedEpisodeInfo.Quality);
if (!_qualityUpgradableSpecification.IsUpgradable(subject.Series.Profile, remoteEpisode.ParsedEpisodeInfo.Quality, subject.ParsedEpisodeInfo.Quality))
{
return Decision.Reject("Quality for release in queue is of equal or higher preference: {0}", remoteEpisode.ParsedEpisodeInfo.Quality);
}
}
return Decision.Accept();
}
}
}

@ -280,7 +280,7 @@
<Compile Include="DecisionEngine\Specifications\CutoffSpecification.cs" />
<Compile Include="DecisionEngine\Specifications\ProtocolSpecification.cs" />
<Compile Include="DecisionEngine\Specifications\LanguageSpecification.cs" />
<Compile Include="DecisionEngine\Specifications\NotInQueueSpecification.cs" />
<Compile Include="DecisionEngine\Specifications\QueueSpecification.cs" />
<Compile Include="DecisionEngine\Specifications\ReleaseRestrictionsSpecification.cs" />
<Compile Include="DecisionEngine\Specifications\NotSampleSpecification.cs" />
<Compile Include="DecisionEngine\Specifications\QualityAllowedByProfileSpecification.cs" />

Loading…
Cancel
Save