1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-10-29 23:12:39 +01:00

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

Closes #3067
This commit is contained in:
Mark McDowall 2022-12-27 23:27:31 -08:00
parent 2491da0678
commit f504dfcbab
4 changed files with 71 additions and 35 deletions

View File

@ -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; }
}
}

View File

@ -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,48 +39,65 @@ 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;
if (season.Count() > 1)
groups.Add(new EpisodeSearchGroup
{
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;
}
}
else
{
try
{
decisions = _releaseSearchService.EpisodeSearch(season.First(), userInvokedSearch, false);
}
catch (Exception ex)
{
_logger.Error(ex, "Unable to search for episode: [{0}]", season.First());
continue;
}
}
var processed = _processDownloadDecisions.ProcessDecisions(decisions);
downloadedCount += processed.Grabbed.Count;
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;
var seriesId = group.SeriesId;
var seasonNumber = group.SeasonNumber;
var groupEpisodes = group.Episodes;
if (groupEpisodes.Count > 1)
{
try
{
decisions = _releaseSearchService.SeasonSearch(seriesId, seasonNumber, groupEpisodes, monitoredOnly, userInvokedSearch, false);
}
catch (Exception ex)
{
_logger.Error(ex, "Unable to search for episodes in season {0} of [{1}]", seasonNumber, seriesId);
continue;
}
}
else
{
var episode = groupEpisodes.First();
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);
}
}
}

View File

@ -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();

View File

@ -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);