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.
82 lines
2.8 KiB
82 lines
2.8 KiB
9 months ago
|
using System.Text.Json;
|
||
|
using System.Text.Json.JsonDiffPatch;
|
||
8 months ago
|
using Recyclarr.ServarrApi.QualityProfile;
|
||
11 months ago
|
|
||
7 months ago
|
namespace Recyclarr.Cli.Pipelines.QualityProfile;
|
||
11 months ago
|
|
||
|
public record ProfileWithStats
|
||
|
{
|
||
|
public required UpdatedQualityProfile Profile { get; set; }
|
||
|
public bool ProfileChanged { get; set; }
|
||
|
public bool ScoresChanged { get; set; }
|
||
|
public bool QualitiesChanged { get; set; }
|
||
|
|
||
|
public bool HasChanges => ProfileChanged || ScoresChanged || QualitiesChanged;
|
||
|
}
|
||
|
|
||
7 months ago
|
public class QualityProfileStatCalculator(ILogger log)
|
||
11 months ago
|
{
|
||
|
public ProfileWithStats Calculate(UpdatedQualityProfile profile)
|
||
|
{
|
||
7 months ago
|
log.Debug("Updates for profile {ProfileName}", profile.ProfileName);
|
||
11 months ago
|
|
||
|
var stats = new ProfileWithStats {Profile = profile};
|
||
10 months ago
|
var oldDto = profile.ProfileDto;
|
||
|
var newDto = profile.BuildUpdatedDto();
|
||
11 months ago
|
|
||
10 months ago
|
ProfileUpdates(stats, oldDto, newDto);
|
||
|
QualityUpdates(stats, oldDto, newDto);
|
||
11 months ago
|
ScoreUpdates(stats, profile.ProfileDto, profile.UpdatedScores);
|
||
|
|
||
|
return stats;
|
||
|
}
|
||
|
|
||
10 months ago
|
private void ProfileUpdates(ProfileWithStats stats, QualityProfileDto oldDto, QualityProfileDto newDto)
|
||
11 months ago
|
{
|
||
10 months ago
|
Log("Upgrade Allowed", oldDto.UpgradeAllowed, newDto.UpgradeAllowed);
|
||
|
Log("Cutoff", oldDto.Items.FindCutoff(oldDto.Cutoff), newDto.Items.FindCutoff(newDto.Cutoff));
|
||
|
Log("Cutoff Score", oldDto.CutoffFormatScore, newDto.CutoffFormatScore);
|
||
|
Log("Minimum Score", oldDto.MinFormatScore, newDto.MinFormatScore);
|
||
10 months ago
|
|
||
10 months ago
|
return;
|
||
11 months ago
|
|
||
|
void Log<T>(string msg, T oldValue, T newValue)
|
||
|
{
|
||
7 months ago
|
log.Debug("{Msg}: {Old} -> {New}", msg, oldValue, newValue);
|
||
11 months ago
|
stats.ProfileChanged |= !EqualityComparer<T>.Default.Equals(oldValue, newValue);
|
||
|
}
|
||
|
}
|
||
|
|
||
10 months ago
|
private static void QualityUpdates(ProfileWithStats stats, QualityProfileDto oldDto, QualityProfileDto newDto)
|
||
11 months ago
|
{
|
||
9 months ago
|
using var oldJson = JsonSerializer.SerializeToDocument(oldDto.Items);
|
||
|
using var newJson = JsonSerializer.SerializeToDocument(newDto.Items);
|
||
|
stats.QualitiesChanged = !oldJson.DeepEquals(newJson);
|
||
11 months ago
|
}
|
||
|
|
||
|
private void ScoreUpdates(
|
||
|
ProfileWithStats stats,
|
||
|
QualityProfileDto profileDto,
|
||
|
IReadOnlyCollection<UpdatedFormatScore> updatedScores)
|
||
|
{
|
||
|
var scores = updatedScores
|
||
|
.Where(y => y.Dto.Score != y.NewScore)
|
||
|
.ToList();
|
||
|
|
||
|
if (scores.Count == 0)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
7 months ago
|
log.Debug("> Scores updated for quality profile: {ProfileName}", profileDto.Name);
|
||
11 months ago
|
|
||
|
foreach (var (dto, newScore, reason) in scores)
|
||
|
{
|
||
7 months ago
|
log.Debug(" - {Format} ({Id}): {OldScore} -> {NewScore} ({Reason})",
|
||
11 months ago
|
dto.Name, dto.Format, dto.Score, newScore, reason);
|
||
|
}
|
||
|
|
||
|
stats.ScoresChanged = true;
|
||
|
}
|
||
|
}
|