New: Missing/Cutoff Unmet searches will search for episodes that haven't been searched recently first

Closes #3067
pull/5329/head
Mark McDowall 2 years ago
parent 2491da0678
commit f504dfcbab

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.IndexerSearch
{
public class EpisodeSearchGroup
{
public int SeriesId { get; set; }
public int SeasonNumber { get; set; }
public List<Episode> Episodes { get; set; }
}
}

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using NLog;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Instrumentation.Extensions;
@ -40,46 +39,63 @@ namespace NzbDrone.Core.IndexerSearch
_logger = logger;
}
private void SearchForEpisodes(List<Episode> episodes, bool monitoredOnly, bool userInvokedSearch)
private void SearchForBulkEpisodes(List<Episode> episodes, bool monitoredOnly, bool userInvokedSearch)
{
_logger.ProgressInfo("Performing search for {0} episodes", episodes.Count);
var downloadedCount = 0;
var groups = new List<EpisodeSearchGroup>();
foreach (var series in episodes.GroupBy(e => e.SeriesId))
{
foreach (var season in series.Select(e => e).GroupBy(e => e.SeasonNumber))
{
List<DownloadDecision> decisions;
groups.Add(new EpisodeSearchGroup
{
SeriesId = series.Key,
SeasonNumber = season.Key,
Episodes = season.ToList()
});
}
}
foreach (var group in groups.OrderBy(g => g.Episodes.Min(e => e.LastSearchTime ?? DateTime.MinValue)))
{
List<DownloadDecision> decisions;
if (season.Count() > 1)
var seriesId = group.SeriesId;
var seasonNumber = group.SeasonNumber;
var groupEpisodes = group.Episodes;
if (groupEpisodes.Count > 1)
{
try
{
try
{
decisions = _releaseSearchService.SeasonSearch(series.Key, season.Key, season.ToList(), monitoredOnly, userInvokedSearch, false);
}
catch (Exception ex)
{
_logger.Error(ex, "Unable to search for episodes in season {0} of [{1}]", season.Key, series.Key);
continue;
}
decisions = _releaseSearchService.SeasonSearch(seriesId, seasonNumber, groupEpisodes, monitoredOnly, userInvokedSearch, false);
}
else
catch (Exception ex)
{
try
{
decisions = _releaseSearchService.EpisodeSearch(season.First(), userInvokedSearch, false);
}
catch (Exception ex)
{
_logger.Error(ex, "Unable to search for episode: [{0}]", season.First());
continue;
}
_logger.Error(ex, "Unable to search for episodes in season {0} of [{1}]", seasonNumber, seriesId);
continue;
}
}
else
{
var episode = groupEpisodes.First();
var processed = _processDownloadDecisions.ProcessDecisions(decisions);
downloadedCount += processed.Grabbed.Count;
try
{
decisions = _releaseSearchService.EpisodeSearch(episode, userInvokedSearch, false);
}
catch (Exception ex)
{
_logger.Error(ex, "Unable to search for episode: [{0}]", episode);
continue;
}
}
var processed = _processDownloadDecisions.ProcessDecisions(decisions);
downloadedCount += processed.Grabbed.Count;
}
_logger.ProgressInfo("Completed search for {0} episodes. {1} reports downloaded.", episodes.Count, downloadedCount);
@ -120,7 +136,7 @@ namespace NzbDrone.Core.IndexerSearch
var pagingSpec = new PagingSpec<Episode>
{
Page = 1,
PageSize = 100000,
PageSize = 1000000,
SortDirection = SortDirection.Ascending,
SortKey = "Id"
};
@ -140,7 +156,7 @@ namespace NzbDrone.Core.IndexerSearch
var queue = _queueService.GetQueue().Where(q => q.Episode != null).Select(q => q.Episode.Id);
var missing = episodes.Where(e => !queue.Contains(e.Id)).ToList();
SearchForEpisodes(missing, monitored, message.Trigger == CommandTrigger.Manual);
SearchForBulkEpisodes(missing, monitored, message.Trigger == CommandTrigger.Manual);
}
public void Execute(CutoffUnmetEpisodeSearchCommand message)
@ -174,7 +190,7 @@ namespace NzbDrone.Core.IndexerSearch
var queue = _queueService.GetQueue().Where(q => q.Episode != null).Select(q => q.Episode.Id);
var cutoffUnmet = episodes.Where(e => !queue.Contains(e.Id)).ToList();
SearchForEpisodes(cutoffUnmet, monitored, message.Trigger == CommandTrigger.Manual);
SearchForBulkEpisodes(cutoffUnmet, monitored, message.Trigger == CommandTrigger.Manual);
}
}
}

@ -540,7 +540,7 @@ namespace NzbDrone.Core.IndexerSearch
_logger.Debug("Setting last search time to: {0}", lastSearchTime);
criteriaBase.Episodes.ForEach(e => e.LastSearchTime = lastSearchTime);
_episodeService.UpdateEpisodes(criteriaBase.Episodes);
_episodeService.UpdateLastSearchTime(criteriaBase.Episodes);
}
return _makeDownloadDecision.GetSearchDecision(reports, criteriaBase).ToList();

@ -31,6 +31,7 @@ namespace NzbDrone.Core.Tv
void SetEpisodeMonitored(int episodeId, bool monitored);
void SetMonitored(IEnumerable<int> ids, bool monitored);
void UpdateEpisodes(List<Episode> episodes);
void UpdateLastSearchTime(List<Episode> episodes);
List<Episode> EpisodesBetweenDates(DateTime start, DateTime end, bool includeUnmonitored);
void InsertMany(List<Episode> episodes);
void UpdateMany(List<Episode> episodes);
@ -187,6 +188,11 @@ namespace NzbDrone.Core.Tv
_episodeRepository.UpdateMany(episodes);
}
public void UpdateLastSearchTime(List<Episode> episodes)
{
_episodeRepository.SetFields(episodes, e => e.LastSearchTime);
}
public List<Episode> EpisodesBetweenDates(DateTime start, DateTime end, bool includeUnmonitored)
{
var episodes = _episodeRepository.EpisodesBetweenDates(start.ToUniversalTime(), end.ToUniversalTime(), includeUnmonitored);

Loading…
Cancel
Save