You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.7 KiB
79 lines
2.7 KiB
using Recyclarr.Cli.Pipelines.QualityProfile.Api;
|
|
using Recyclarr.TrashLib.Config.Services;
|
|
|
|
namespace Recyclarr.Cli.Pipelines.QualityProfile.PipelinePhases;
|
|
|
|
public class QualityProfileApiPersistencePhase
|
|
{
|
|
private readonly ILogger _log;
|
|
private readonly IQualityProfileService _api;
|
|
private readonly QualityProfileStatCalculator _statCalculator;
|
|
|
|
public QualityProfileApiPersistencePhase(
|
|
ILogger log,
|
|
IQualityProfileService api,
|
|
QualityProfileStatCalculator statCalculator)
|
|
{
|
|
_log = log;
|
|
_api = api;
|
|
_statCalculator = statCalculator;
|
|
}
|
|
|
|
public async Task Execute(IServiceConfiguration config, QualityProfileTransactionData transactions)
|
|
{
|
|
var profilesWithStats = transactions.UpdatedProfiles
|
|
.Select(x => _statCalculator.Calculate(x))
|
|
.ToLookup(x => x.HasChanges);
|
|
|
|
// Profiles without changes (false) get logged
|
|
var unchangedProfiles = profilesWithStats[false].ToList();
|
|
if (unchangedProfiles.Any())
|
|
{
|
|
_log.Debug("These profiles have no changes and will not be persisted: {Profiles}",
|
|
unchangedProfiles.Select(x => x.Profile.ProfileName));
|
|
}
|
|
|
|
// Profiles with changes (true) get sent to the service
|
|
var changedProfiles = profilesWithStats[true].ToList();
|
|
foreach (var profile in changedProfiles.Select(x => x.Profile))
|
|
{
|
|
var dto = profile.BuildUpdatedDto();
|
|
|
|
switch (profile.UpdateReason)
|
|
{
|
|
case QualityProfileUpdateReason.New:
|
|
await _api.CreateQualityProfile(config, dto);
|
|
break;
|
|
|
|
case QualityProfileUpdateReason.Changed:
|
|
await _api.UpdateQualityProfile(config, dto);
|
|
break;
|
|
|
|
default:
|
|
throw new InvalidOperationException($"Unsupported UpdateReason: {profile.UpdateReason}");
|
|
}
|
|
}
|
|
|
|
LogUpdates(changedProfiles);
|
|
}
|
|
|
|
private void LogUpdates(IReadOnlyCollection<ProfileWithStats> changedProfiles)
|
|
{
|
|
if (changedProfiles.Count > 0)
|
|
{
|
|
var numProfiles = changedProfiles.Count;
|
|
var numQuality = changedProfiles.Count(x => x.QualitiesChanged);
|
|
var numScores = changedProfiles.Count(x => x.ScoresChanged);
|
|
|
|
_log.Information(
|
|
"A total of {NumProfiles} profiles changed: {NumQuality} contain quality changes; " +
|
|
"{NumScores} contain updated scores",
|
|
numProfiles, numQuality, numScores);
|
|
}
|
|
else
|
|
{
|
|
_log.Information("All quality profiles are up to date!");
|
|
}
|
|
}
|
|
}
|