Moved IsFirstOrLastEpisodeOfSeason logic to AcceptableSize specification so we can reuse the seasonsearchcriteria to reduce the number of database calls.

pull/4/head
Taloth Saldono 11 years ago
parent 0187df38ec
commit 7b607ce895

@ -48,7 +48,7 @@ namespace NzbDrone.Core.Test.DecisionEngineTests
Series = series, Series = series,
Release = new ReleaseInfo(), Release = new ReleaseInfo(),
ParsedEpisodeInfo = new ParsedEpisodeInfo { Quality = new QualityModel(Quality.SDTV, true) }, ParsedEpisodeInfo = new ParsedEpisodeInfo { Quality = new QualityModel(Quality.SDTV, true) },
Episodes = new List<Episode> { new Episode() } Episodes = new List<Episode> { new Episode() { Id = 2 } }
}; };
@ -59,13 +59,21 @@ namespace NzbDrone.Core.Test.DecisionEngineTests
.Build(); .Build();
Mocker.GetMock<IQualityDefinitionService>().Setup(s => s.Get(Quality.SDTV)).Returns(qualityType); Mocker.GetMock<IQualityDefinitionService>().Setup(s => s.Get(Quality.SDTV)).Returns(qualityType);
Mocker.GetMock<IEpisodeService>().Setup(
s => s.GetEpisodesBySeason(It.IsAny<int>(), It.IsAny<int>()))
.Returns(new List<Episode>() {
new Episode(), new Episode(), new Episode(), new Episode(), new Episode(),
new Episode(), new Episode(), new Episode(), new Episode() { Id = 2 }, new Episode() });
} }
private void GivenLastEpisode() private void GivenLastEpisode()
{ {
Mocker.GetMock<IEpisodeService>().Setup( Mocker.GetMock<IEpisodeService>().Setup(
s => s.IsFirstOrLastEpisodeOfSeason(It.IsAny<int>())) s => s.GetEpisodesBySeason(It.IsAny<int>(), It.IsAny<int>()))
.Returns(true); .Returns(new List<Episode>() {
new Episode(), new Episode(), new Episode(), new Episode(), new Episode(),
new Episode(), new Episode(), new Episode(), new Episode(), new Episode() { Id = 2 } });
} }
[TestCase(30, 50, false)] [TestCase(30, 50, false)]
@ -110,10 +118,6 @@ namespace NzbDrone.Core.Test.DecisionEngineTests
parseResultMulti.Series = series; parseResultMulti.Series = series;
parseResultMulti.Release.Size = sizeInMegaBytes.Megabytes(); parseResultMulti.Release.Size = sizeInMegaBytes.Megabytes();
Mocker.GetMock<IEpisodeService>().Setup(
s => s.IsFirstOrLastEpisodeOfSeason(It.IsAny<int>()))
.Returns(false);
Subject.IsSatisfiedBy(parseResultMulti, null).Should().Be(expectedResult); Subject.IsSatisfiedBy(parseResultMulti, null).Should().Be(expectedResult);
} }
@ -129,10 +133,6 @@ namespace NzbDrone.Core.Test.DecisionEngineTests
parseResultMultiSet.Series = series; parseResultMultiSet.Series = series;
parseResultMultiSet.Release.Size = sizeInMegaBytes.Megabytes(); parseResultMultiSet.Release.Size = sizeInMegaBytes.Megabytes();
Mocker.GetMock<IEpisodeService>().Setup(
s => s.IsFirstOrLastEpisodeOfSeason(It.IsAny<int>()))
.Returns(false);
Subject.IsSatisfiedBy(parseResultMultiSet, null).Should().Be(expectedResult); Subject.IsSatisfiedBy(parseResultMultiSet, null).Should().Be(expectedResult);
} }

@ -4,6 +4,7 @@ using NzbDrone.Core.IndexerSearch.Definitions;
using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Qualities; using NzbDrone.Core.Qualities;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
using System.Collections.Generic;
namespace NzbDrone.Core.DecisionEngine.Specifications namespace NzbDrone.Core.DecisionEngine.Specifications
{ {
@ -66,11 +67,28 @@ namespace NzbDrone.Core.DecisionEngine.Specifications
//Multiply maxSize by Series.Runtime //Multiply maxSize by Series.Runtime
maxSize = maxSize * subject.Series.Runtime * subject.Episodes.Count; maxSize = maxSize * subject.Series.Runtime * subject.Episodes.Count;
//Check if there was only one episode parsed and it is the first if (subject.Episodes.Count == 1)
if (subject.Episodes.Count == 1 && _episodeService.IsFirstOrLastEpisodeOfSeason(subject.Episodes.First().Id)) {
Episode episode = subject.Episodes.First();
List<Episode> seasonEpisodes;
var seasonSearchCriteria = searchCriteria as SeasonSearchCriteria;
if (seasonSearchCriteria != null && !seasonSearchCriteria.Series.UseSceneNumbering && seasonSearchCriteria.Episodes.Any(v => v.Id == episode.Id))
{
seasonEpisodes = (searchCriteria as SeasonSearchCriteria).Episodes;
}
else
{
seasonEpisodes = _episodeService.GetEpisodesBySeason(episode.SeriesId, episode.SeasonNumber);
}
//Ensure that this is either the first episode
//or is the last episode in a season that has 10 or more episodes
if (seasonEpisodes.First().Id == episode.Id || (seasonEpisodes.Count() >= 10 && seasonEpisodes.Last().Id == episode.Id))
{ {
maxSize = maxSize * 2; maxSize = maxSize * 2;
} }
}
//If the parsed size is greater than maxSize we don't want it //If the parsed size is greater than maxSize we don't want it
if (subject.Release.Size > maxSize) if (subject.Release.Size > maxSize)

@ -27,7 +27,6 @@ namespace NzbDrone.Core.Tv
List<Episode> GetEpisodesByFileId(int episodeFileId); List<Episode> GetEpisodesByFileId(int episodeFileId);
void UpdateEpisode(Episode episode); void UpdateEpisode(Episode episode);
void SetEpisodeMonitored(int episodeId, bool monitored); void SetEpisodeMonitored(int episodeId, bool monitored);
bool IsFirstOrLastEpisodeOfSeason(int episodeId);
void UpdateEpisodes(List<Episode> episodes); void UpdateEpisodes(List<Episode> episodes);
List<Episode> EpisodesBetweenDates(DateTime start, DateTime end); List<Episode> EpisodesBetweenDates(DateTime start, DateTime end);
void InsertMany(List<Episode> episodes); void InsertMany(List<Episode> episodes);
@ -141,19 +140,6 @@ namespace NzbDrone.Core.Tv
_episodeRepository.SetMonitoredBySeason(seriesId, seasonNumber, monitored); _episodeRepository.SetMonitoredBySeason(seriesId, seasonNumber, monitored);
} }
public bool IsFirstOrLastEpisodeOfSeason(int episodeId)
{
var episode = GetEpisode(episodeId);
var seasonEpisodes = GetEpisodesBySeason(episode.SeriesId, episode.SeasonNumber);
//Ensure that this is either the first episode
//or is the last episode in a season that has 10 or more episodes
if (seasonEpisodes.First().EpisodeNumber == episode.EpisodeNumber || (seasonEpisodes.Count() >= 10 && seasonEpisodes.Last().EpisodeNumber == episode.EpisodeNumber))
return true;
return false;
}
public void UpdateEpisodes(List<Episode> episodes) public void UpdateEpisodes(List<Episode> episodes)
{ {
_episodeRepository.UpdateMany(episodes); _episodeRepository.UpdateMany(episodes);

Loading…
Cancel
Save