2017-09-04 02:20:56 +00:00
|
|
|
using System;
|
2019-03-21 22:02:16 +00:00
|
|
|
using System.Collections.Generic;
|
2013-03-07 00:19:49 +00:00
|
|
|
using NLog;
|
2015-10-07 05:22:37 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
2015-10-19 18:42:05 +00:00
|
|
|
using NzbDrone.Core.Configuration;
|
2013-03-07 00:19:49 +00:00
|
|
|
using NzbDrone.Core.History;
|
2013-08-07 03:18:05 +00:00
|
|
|
using NzbDrone.Core.IndexerSearch.Definitions;
|
2013-04-15 01:41:39 +00:00
|
|
|
using NzbDrone.Core.Parser.Model;
|
2019-02-23 22:39:11 +00:00
|
|
|
using NzbDrone.Core.Profiles.Releases;
|
2019-03-21 22:02:16 +00:00
|
|
|
using NzbDrone.Core.Qualities;
|
2013-03-07 00:19:49 +00:00
|
|
|
|
2013-08-12 06:00:40 +00:00
|
|
|
namespace NzbDrone.Core.DecisionEngine.Specifications.RssSync
|
2013-03-07 00:19:49 +00:00
|
|
|
{
|
2013-10-24 05:13:04 +00:00
|
|
|
public class HistorySpecification : IDecisionEngineSpecification
|
2013-03-07 00:19:49 +00:00
|
|
|
{
|
|
|
|
private readonly IHistoryService _historyService;
|
2017-09-04 02:20:56 +00:00
|
|
|
private readonly UpgradableSpecification _upgradableSpecification;
|
2015-10-19 18:42:05 +00:00
|
|
|
private readonly IConfigService _configService;
|
2019-02-23 22:39:11 +00:00
|
|
|
private readonly IPreferredWordService _preferredWordServiceCalculator;
|
2013-03-07 00:19:49 +00:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2013-10-24 05:13:04 +00:00
|
|
|
public HistorySpecification(IHistoryService historyService,
|
2017-09-04 02:20:56 +00:00
|
|
|
UpgradableSpecification qualityUpgradableSpecification,
|
2015-10-19 18:42:05 +00:00
|
|
|
IConfigService configService,
|
2019-02-23 22:39:11 +00:00
|
|
|
IPreferredWordService preferredWordServiceCalculator,
|
2013-10-23 06:36:31 +00:00
|
|
|
Logger logger)
|
2013-03-07 00:19:49 +00:00
|
|
|
{
|
|
|
|
_historyService = historyService;
|
2017-09-04 02:20:56 +00:00
|
|
|
_upgradableSpecification = qualityUpgradableSpecification;
|
2015-10-19 18:42:05 +00:00
|
|
|
_configService = configService;
|
2019-02-23 22:39:11 +00:00
|
|
|
_preferredWordServiceCalculator = preferredWordServiceCalculator;
|
2013-03-07 00:19:49 +00:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2017-09-27 03:30:24 +00:00
|
|
|
public SpecificationPriority Priority => SpecificationPriority.Database;
|
2016-12-09 06:54:15 +00:00
|
|
|
public RejectionType Type => RejectionType.Permanent;
|
2014-06-08 08:22:55 +00:00
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
public virtual Decision IsSatisfiedBy(RemoteAlbum subject, SearchCriteriaBase searchCriteria)
|
2013-03-07 00:19:49 +00:00
|
|
|
{
|
2013-08-08 03:30:20 +00:00
|
|
|
if (searchCriteria != null)
|
|
|
|
{
|
2014-03-13 20:12:42 +00:00
|
|
|
_logger.Debug("Skipping history check during search");
|
2014-10-27 05:51:50 +00:00
|
|
|
return Decision.Accept();
|
2013-08-08 03:30:20 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 18:42:05 +00:00
|
|
|
var cdhEnabled = _configService.EnableCompletedDownloadHandling;
|
|
|
|
|
2015-10-07 05:22:37 +00:00
|
|
|
_logger.Debug("Performing history status check on report");
|
2017-08-14 02:58:42 +00:00
|
|
|
foreach (var album in subject.Albums)
|
2013-03-07 00:19:49 +00:00
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
_logger.Debug("Checking current status of album [{0}] in history", album.Id);
|
|
|
|
var mostRecent = _historyService.MostRecentForAlbum(album.Id);
|
2014-10-27 05:51:50 +00:00
|
|
|
|
2021-12-25 01:55:32 +00:00
|
|
|
if (mostRecent != null && mostRecent.EventType == EntityHistoryEventType.Grabbed)
|
2015-10-07 05:22:37 +00:00
|
|
|
{
|
2015-11-02 05:54:49 +00:00
|
|
|
var recent = mostRecent.Date.After(DateTime.UtcNow.AddHours(-12));
|
2019-09-11 03:54:16 +00:00
|
|
|
|
|
|
|
if (!recent && cdhEnabled)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-03-10 02:34:25 +00:00
|
|
|
// The artist will be the same as the one in history since it's the same album.
|
|
|
|
// Instead of fetching the artist from the DB reuse the known artist.
|
2019-06-06 01:54:59 +00:00
|
|
|
var preferredWordScore = _preferredWordServiceCalculator.Calculate(subject.Artist, mostRecent.SourceTitle, subject.Release?.IndexerId ?? 0);
|
2019-02-23 22:39:11 +00:00
|
|
|
|
|
|
|
var cutoffUnmet = _upgradableSpecification.CutoffNotMet(
|
|
|
|
subject.Artist.QualityProfile,
|
2019-03-21 22:02:16 +00:00
|
|
|
new List<QualityModel> { mostRecent.Quality },
|
2019-02-23 22:39:11 +00:00
|
|
|
preferredWordScore,
|
|
|
|
subject.ParsedAlbumInfo.Quality,
|
|
|
|
subject.PreferredWordScore);
|
|
|
|
|
|
|
|
var upgradeable = _upgradableSpecification.IsUpgradable(
|
|
|
|
subject.Artist.QualityProfile,
|
2019-03-21 22:02:16 +00:00
|
|
|
new List<QualityModel> { mostRecent.Quality },
|
2019-02-23 22:39:11 +00:00
|
|
|
preferredWordScore,
|
|
|
|
subject.ParsedAlbumInfo.Quality,
|
|
|
|
subject.PreferredWordScore);
|
2015-10-19 18:42:05 +00:00
|
|
|
|
|
|
|
if (!cutoffUnmet)
|
2014-10-27 05:51:50 +00:00
|
|
|
{
|
2015-11-02 05:54:49 +00:00
|
|
|
if (recent)
|
2015-10-19 18:42:05 +00:00
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
return Decision.Reject("Recent grab event in history already meets cutoff: {0}", mostRecent.Quality);
|
2015-10-19 18:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Decision.Reject("CDH is disabled and grab event in history already meets cutoff: {0}", mostRecent.Quality);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!upgradeable)
|
|
|
|
{
|
2015-11-02 05:54:49 +00:00
|
|
|
if (recent)
|
2015-10-19 18:42:05 +00:00
|
|
|
{
|
|
|
|
return Decision.Reject("Recent grab event in history is of equal or higher quality: {0}", mostRecent.Quality);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Decision.Reject("CDH is disabled and grab event in history is of equal or higher quality: {0}", mostRecent.Quality);
|
2014-10-27 05:51:50 +00:00
|
|
|
}
|
2013-03-07 00:19:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-27 05:51:50 +00:00
|
|
|
return Decision.Accept();
|
2013-03-07 00:19:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|