cleaned up history lookup.

This commit is contained in:
kay.one 2013-03-24 18:38:11 -07:00
parent cd6f0fc55c
commit b9fac94eca
7 changed files with 24 additions and 18 deletions

View File

@ -67,9 +67,9 @@ namespace NzbDrone.Core.Test.DecisionEngineTests
firstQuality = new QualityModel(Quality.Bluray1080p, true); firstQuality = new QualityModel(Quality.Bluray1080p, true);
secondQuality = new QualityModel(Quality.Bluray1080p, true); secondQuality = new QualityModel(Quality.Bluray1080p, true);
Mocker.GetMock<IHistoryService>().Setup(c => c.GetBestQualityInHistory(fakeSeries.Id, 12, 3)).Returns(firstQuality); Mocker.GetMock<IHistoryService>().Setup(c => c.GetBestQualityInHistory(1)).Returns(firstQuality);
Mocker.GetMock<IHistoryService>().Setup(c => c.GetBestQualityInHistory(fakeSeries.Id, 12, 4)).Returns(secondQuality); Mocker.GetMock<IHistoryService>().Setup(c => c.GetBestQualityInHistory(2)).Returns(secondQuality);
Mocker.GetMock<IHistoryService>().Setup(c => c.GetBestQualityInHistory(fakeSeries.Id, 12, 5)).Returns<QualityModel>(null); Mocker.GetMock<IHistoryService>().Setup(c => c.GetBestQualityInHistory(3)).Returns<QualityModel>(null);
} }
private void WithFirstReportUpgradable() private void WithFirstReportUpgradable()
@ -125,7 +125,7 @@ namespace NzbDrone.Core.Test.DecisionEngineTests
parseResultSingle.Quality = new QualityModel(Quality.WEBDL1080p, false); parseResultSingle.Quality = new QualityModel(Quality.WEBDL1080p, false);
firstQuality = new QualityModel(Quality.WEBDL1080p, false); firstQuality = new QualityModel(Quality.WEBDL1080p, false);
Mocker.GetMock<IHistoryService>().Setup(c => c.GetBestQualityInHistory(fakeSeries.Id, 12, 3)).Returns(firstQuality); Mocker.GetMock<IHistoryService>().Setup(c => c.GetBestQualityInHistory(1)).Returns(firstQuality);
_upgradeHistory.IsSatisfiedBy(parseResultSingle).Should().BeFalse(); _upgradeHistory.IsSatisfiedBy(parseResultSingle).Should().BeFalse();
} }

View File

@ -37,7 +37,7 @@ namespace NzbDrone.Core.Test.HistoryTests
[Test] [Test]
public void GetBestQualityInHistory_no_result() public void GetBestQualityInHistory_no_result()
{ {
Subject.GetBestQualityInHistory(12, 12, 12).Should().Be(null); Subject.GetBestQualityInHistory(12).Should().Be(null);
} }
[Test] [Test]
@ -54,12 +54,12 @@ namespace NzbDrone.Core.Test.HistoryTests
var history = Builder<History.History>.CreateNew() var history = Builder<History.History>.CreateNew()
.With(c => c.Id = 0) .With(c => c.Id = 0)
.With(h => h.Quality = new QualityModel(Quality.Bluray720p, true)) .With(h => h.Quality = new QualityModel(Quality.Bluray720p, true))
.With(h => h.Episode = episode) .With(h => h.EpisodeId = episode.Id)
.Build(); .Build();
Db.Insert(history); Db.Insert(history);
var result = Subject.GetBestQualityInHistory(episode.SeriesId, episode.SeasonNumber, episode.EpisodeNumber); var result = Subject.GetBestQualityInHistory(episode.Id);
result.Should().NotBeNull(); result.Should().NotBeNull();
result.Quality.Should().Be(Quality.Bluray720p); result.Quality.Should().Be(Quality.Bluray720p);
@ -81,7 +81,7 @@ namespace NzbDrone.Core.Test.HistoryTests
.CreateListOfSize(5) .CreateListOfSize(5)
.All() .All()
.With(c => c.Id = 0) .With(c => c.Id = 0)
.With(h => h.Episode = episode) .With(h => h.EpisodeId = episode.Id)
.TheFirst(1) .TheFirst(1)
.With(h => h.Quality = new QualityModel(Quality.DVD, true)) .With(h => h.Quality = new QualityModel(Quality.DVD, true))
.TheNext(1) .TheNext(1)
@ -96,7 +96,7 @@ namespace NzbDrone.Core.Test.HistoryTests
Db.InsertMany(history); Db.InsertMany(history);
var result = Subject.GetBestQualityInHistory(episode.SeriesId, episode.SeasonNumber, episode.EpisodeNumber); var result = Subject.GetBestQualityInHistory(episode.Id);
result.Should().NotBeNull(); result.Should().NotBeNull();
result.Quality.Should().Be(Quality.Bluray720p); result.Quality.Should().Be(Quality.Bluray720p);

View File

@ -29,6 +29,7 @@ namespace NzbDrone.Core.Datastore
bool HasItems(); bool HasItems();
void DeleteMany(IEnumerable<int> ids); void DeleteMany(IEnumerable<int> ids);
void UpdateFields<TKey>(TModel model, Expression<Func<TModel, TKey>> onlyFields); void UpdateFields<TKey>(TModel model, Expression<Func<TModel, TKey>> onlyFields);
List<TModel> Where(SqlExpressionVisitor<TModel> expression);
} }
public class BasicRepository<TModel> : IBasicRepository<TModel> where TModel : ModelBase, new() public class BasicRepository<TModel> : IBasicRepository<TModel> where TModel : ModelBase, new()
@ -93,6 +94,11 @@ namespace NzbDrone.Core.Datastore
return _database.Select(predicate); return _database.Select(predicate);
} }
public List<TModel> Where(SqlExpressionVisitor<TModel> expression)
{
return _database.Select(expression);
}
public TModel Insert(TModel model) public TModel Insert(TModel model)
{ {
if (model.Id != 0) if (model.Id != 0)

View File

@ -29,7 +29,7 @@ namespace NzbDrone.Core.DecisionEngine.Specifications
{ {
foreach (var episode in subject.Episodes) foreach (var episode in subject.Episodes)
{ {
var bestQualityInHistory = _historyService.GetBestQualityInHistory(subject.Series.Id, episode.SeasonNumber, episode.EpisodeNumber); var bestQualityInHistory = _historyService.GetBestQualityInHistory(episode.Id);
if (bestQualityInHistory != null) if (bestQualityInHistory != null)
{ {
_logger.Trace("Comparing history quality with report. History is {0}", bestQualityInHistory); _logger.Trace("Comparing history quality with report. History is {0}", bestQualityInHistory);

View File

@ -7,6 +7,7 @@ namespace NzbDrone.Core.History
{ {
public class History : ModelBase public class History : ModelBase
{ {
public int EpisodeId { get; set; }
public string NzbTitle { get; set; } public string NzbTitle { get; set; }
public QualityModel Quality { get; set; } public QualityModel Quality { get; set; }
public DateTime Date { get; set; } public DateTime Date { get; set; }
@ -14,6 +15,5 @@ namespace NzbDrone.Core.History
public string NzbInfoUrl { get; set; } public string NzbInfoUrl { get; set; }
public string ReleaseGroup { get; set; } public string ReleaseGroup { get; set; }
public Episode Episode { get; set; }
} }
} }

View File

@ -9,7 +9,7 @@ namespace NzbDrone.Core.History
public interface IHistoryRepository : IBasicRepository<History> public interface IHistoryRepository : IBasicRepository<History>
{ {
void Trim(); void Trim();
QualityModel GetBestQualityInHistory(int seriesId, int seasonNumber, int episodeNumber); QualityModel GetBestQualityInHistory(int episodeId);
} }
public class HistoryRepository : BasicRepository<History>, IHistoryRepository public class HistoryRepository : BasicRepository<History>, IHistoryRepository
@ -26,9 +26,9 @@ namespace NzbDrone.Core.History
} }
public QualityModel GetBestQualityInHistory(int seriesId, int seasonNumber, int episodeNumber) public QualityModel GetBestQualityInHistory(int episodeId)
{ {
var history = Where(c => c.Episode.Series.Id == seriesId && c.Episode.SeasonNumber == seasonNumber && c.Episode.EpisodeNumber == episodeNumber) var history = Where(c => c.EpisodeId == episodeId)
.OrderByDescending(c => c.Quality).FirstOrDefault(); .OrderByDescending(c => c.Quality).FirstOrDefault();
if (history != null) if (history != null)

View File

@ -13,7 +13,7 @@ namespace NzbDrone.Core.History
List<History> All(); List<History> All();
void Purge(); void Purge();
void Trim(); void Trim();
QualityModel GetBestQualityInHistory(int seriesId, int seasonNumber, int episodeNumber); QualityModel GetBestQualityInHistory(int episodeId);
} }
public class HistoryService : IHistoryService, IHandle<EpisodeGrabbedEvent> public class HistoryService : IHistoryService, IHandle<EpisodeGrabbedEvent>
@ -43,9 +43,9 @@ namespace NzbDrone.Core.History
_historyRepository.Trim(); _historyRepository.Trim();
} }
public virtual QualityModel GetBestQualityInHistory(int seriesId, int seasonNumber, int episodeNumber) public virtual QualityModel GetBestQualityInHistory(int episodeId)
{ {
return _historyRepository.GetBestQualityInHistory(seriesId, seasonNumber, episodeNumber); return _historyRepository.GetBestQualityInHistory(episodeId);
} }
public void Handle(EpisodeGrabbedEvent message) public void Handle(EpisodeGrabbedEvent message)
@ -58,7 +58,7 @@ namespace NzbDrone.Core.History
Indexer = message.ParseResult.Indexer, Indexer = message.ParseResult.Indexer,
Quality = message.ParseResult.Quality, Quality = message.ParseResult.Quality,
NzbTitle = message.ParseResult.OriginalString, NzbTitle = message.ParseResult.OriginalString,
Episode = episode, EpisodeId = episode.Id,
NzbInfoUrl = message.ParseResult.NzbInfoUrl, NzbInfoUrl = message.ParseResult.NzbInfoUrl,
ReleaseGroup = message.ParseResult.ReleaseGroup, ReleaseGroup = message.ParseResult.ReleaseGroup,
}; };