|
|
@ -18,32 +18,32 @@ namespace NzbDrone.Core.Qualities
|
|
|
|
|
|
|
|
|
|
|
|
public class QualityDefinitionService : IQualityDefinitionService, IHandle<ApplicationStartedEvent>
|
|
|
|
public class QualityDefinitionService : IQualityDefinitionService, IHandle<ApplicationStartedEvent>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private readonly IQualityDefinitionRepository _qualityDefinitionRepository;
|
|
|
|
private readonly IQualityDefinitionRepository _repo;
|
|
|
|
private readonly ICached<Dictionary<Quality, QualityDefinition>> _cache;
|
|
|
|
private readonly ICached<Dictionary<Quality, QualityDefinition>> _cache;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
|
|
|
|
public QualityDefinitionService(IQualityDefinitionRepository qualityDefinitionRepository, ICacheManager cacheManager, Logger logger)
|
|
|
|
public QualityDefinitionService(IQualityDefinitionRepository repo, ICacheManager cacheManager, Logger logger)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_qualityDefinitionRepository = qualityDefinitionRepository;
|
|
|
|
_repo = repo;
|
|
|
|
_cache = cacheManager.GetCache<Dictionary<Quality, QualityDefinition>>(this.GetType());
|
|
|
|
_cache = cacheManager.GetCache<Dictionary<Quality, QualityDefinition>>(this.GetType());
|
|
|
|
_logger = logger;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Dictionary<Quality, QualityDefinition> GetAll()
|
|
|
|
private Dictionary<Quality, QualityDefinition> GetAll()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return _cache.Get("all", () => _qualityDefinitionRepository.All().ToDictionary(v => v.Quality), TimeSpan.FromSeconds(5.0));
|
|
|
|
return _cache.Get("all", () => _repo.All().Select(WithWeight).ToDictionary(v => v.Quality), TimeSpan.FromSeconds(5.0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Update(QualityDefinition qualityDefinition)
|
|
|
|
public void Update(QualityDefinition qualityDefinition)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_qualityDefinitionRepository.Update(qualityDefinition);
|
|
|
|
_repo.Update(qualityDefinition);
|
|
|
|
|
|
|
|
|
|
|
|
_cache.Clear();
|
|
|
|
_cache.Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<QualityDefinition> All()
|
|
|
|
public List<QualityDefinition> All()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return GetAll().Values.ToList();
|
|
|
|
return GetAll().Values.OrderBy(d => d.Weight).ToList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public QualityDefinition GetById(Int32 id)
|
|
|
|
public QualityDefinition GetById(Int32 id)
|
|
|
@ -53,67 +53,52 @@ namespace NzbDrone.Core.Qualities
|
|
|
|
|
|
|
|
|
|
|
|
public QualityDefinition Get(Quality quality)
|
|
|
|
public QualityDefinition Get(Quality quality)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (quality == Quality.Unknown)
|
|
|
|
|
|
|
|
return new QualityDefinition(Quality.Unknown);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return GetAll()[quality];
|
|
|
|
return GetAll()[quality];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void InsertMissingDefinitions(List<QualityDefinition> allDefinitions)
|
|
|
|
private void InsertMissingDefinitions()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
allDefinitions.OrderBy(v => v.Weight).ToList();
|
|
|
|
List<QualityDefinition> insertList = new List<QualityDefinition>();
|
|
|
|
var existingDefinitions = _qualityDefinitionRepository.All().OrderBy(v => v.Weight).ToList();
|
|
|
|
List<QualityDefinition> updateList = new List<QualityDefinition>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var allDefinitions = Quality.DefaultQualityDefinitions.OrderBy(d => d.Weight).ToList();
|
|
|
|
|
|
|
|
var existingDefinitions = _repo.All().ToList();
|
|
|
|
|
|
|
|
|
|
|
|
// Try insert each item intelligently to merge the lists preserving the Weight the user set.
|
|
|
|
foreach (var definition in allDefinitions)
|
|
|
|
for (int i = 0; i < allDefinitions.Count;i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Skip if this definition isn't missing.
|
|
|
|
var existing = existingDefinitions.SingleOrDefault(d => d.Quality == definition.Quality);
|
|
|
|
if (existingDefinitions.Any(v => v.Quality == allDefinitions[i].Quality))
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int targetIndexMinimum = 0;
|
|
|
|
if (existing == null)
|
|
|
|
for (int j = 0; j < i; j++)
|
|
|
|
{
|
|
|
|
targetIndexMinimum = Math.Max(targetIndexMinimum, existingDefinitions.FindIndex(v => v.Quality == allDefinitions[j].Quality) + 1);
|
|
|
|
insertList.Add(definition);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int targetIndexMaximum = existingDefinitions.Count;
|
|
|
|
else
|
|
|
|
for (int j = i + 1; j < allDefinitions.Count; j++)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var index = existingDefinitions.FindIndex(v => v.Quality == allDefinitions[j].Quality);
|
|
|
|
updateList.Add(existing);
|
|
|
|
if (index != -1)
|
|
|
|
existingDefinitions.Remove(existing);
|
|
|
|
targetIndexMaximum = Math.Min(targetIndexMaximum, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Rounded down average sounds reasonable.
|
|
|
|
_repo.InsertMany(insertList);
|
|
|
|
int targetIndex = (targetIndexMinimum + targetIndexMaximum) / 2;
|
|
|
|
_repo.UpdateMany(updateList);
|
|
|
|
|
|
|
|
_repo.DeleteMany(existingDefinitions);
|
|
|
|
|
|
|
|
|
|
|
|
existingDefinitions.Insert(targetIndex, allDefinitions[i]);
|
|
|
|
_cache.Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Update all Weights.
|
|
|
|
private static QualityDefinition WithWeight(QualityDefinition definition)
|
|
|
|
List<QualityDefinition> insertList = new List<QualityDefinition>();
|
|
|
|
|
|
|
|
List<QualityDefinition> updateList = new List<QualityDefinition>();
|
|
|
|
|
|
|
|
for (int i = 0; i < existingDefinitions.Count; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (existingDefinitions[i].Id == 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
existingDefinitions[i].Weight = i + 1;
|
|
|
|
|
|
|
|
_qualityDefinitionRepository.Insert(existingDefinitions[i]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (existingDefinitions[i].Weight != i + 1)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
existingDefinitions[i].Weight = i + 1;
|
|
|
|
definition.Weight = Quality.DefaultQualityDefinitions.Single(d => d.Quality == definition.Quality).Weight;
|
|
|
|
_qualityDefinitionRepository.Update(existingDefinitions[i]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_cache.Clear();
|
|
|
|
return definition;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Handle(ApplicationStartedEvent message)
|
|
|
|
public void Handle(ApplicationStartedEvent message)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_logger.Debug("Setting up default quality config");
|
|
|
|
_logger.Debug("Setting up default quality config");
|
|
|
|
|
|
|
|
|
|
|
|
InsertMissingDefinitions(Quality.DefaultQualityDefinitions.ToList());
|
|
|
|
InsertMissingDefinitions();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|