1
0
mirror of https://github.com/Radarr/Radarr.git synced 2026-04-21 22:05:43 -04:00

New: Calculate custom formats on demand

This commit is contained in:
ta264
2020-01-22 21:47:33 +00:00
parent 13701498ce
commit df101258c5
103 changed files with 1901 additions and 1346 deletions
@@ -1,14 +1,9 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Common.Cache;
using NzbDrone.Common.Composition;
using NzbDrone.Core.Blacklisting;
using NzbDrone.Core.CustomFormats.Events;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.History;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.Profiles;
using NzbDrone.Core.Messaging.Events;
namespace NzbDrone.Core.CustomFormats
{
@@ -24,145 +19,21 @@ namespace NzbDrone.Core.CustomFormats
public class CustomFormatService : ICustomFormatService
{
private readonly ICustomFormatRepository _formatRepository;
private readonly IHistoryService _historyService;
private IProfileService _profileService;
public IProfileService ProfileService
{
get
{
if (_profileService == null)
{
_profileService = _container.Resolve<IProfileService>();
}
return _profileService;
}
}
private readonly IContainer _container;
private readonly IEventAggregator _eventAggregator;
private readonly ICached<Dictionary<int, CustomFormat>> _cache;
private readonly Logger _logger;
public static Dictionary<int, CustomFormat> AllCustomFormats;
public CustomFormatService(ICustomFormatRepository formatRepository,
ICacheManager cacheManager,
IContainer container,
IHistoryService historyService,
Logger logger)
ICacheManager cacheManager,
IEventAggregator eventAggregator)
{
_formatRepository = formatRepository;
_container = container;
_eventAggregator = eventAggregator;
_cache = cacheManager.GetCache<Dictionary<int, CustomFormat>>(typeof(CustomFormat), "formats");
_historyService = historyService;
_logger = logger;
// Fill up the cache for subsequent DB lookups
All();
}
public void Update(CustomFormat customFormat)
{
_formatRepository.Update(customFormat);
_cache.Clear();
}
public CustomFormat Insert(CustomFormat customFormat)
{
var ret = _formatRepository.Insert(customFormat);
try
{
ProfileService.AddCustomFormat(ret);
}
catch (Exception e)
{
_logger.Error(e, "Failure while trying to add the new custom format to all profiles. Deleting again!");
_formatRepository.Delete(ret);
throw;
}
_cache.Clear();
return ret;
}
public void Delete(int id)
{
_cache.Clear();
try
{
//First history:
var historyRepo = _container.Resolve<IHistoryRepository>();
DeleteInRepo(historyRepo,
h => h.Quality.CustomFormats,
(h, f) =>
{
h.Quality.CustomFormats = f;
return h;
},
id);
//Then Blacklist:
var blacklistRepo = _container.Resolve<IBlacklistRepository>();
DeleteInRepo(blacklistRepo,
h => h.Quality.CustomFormats,
(h, f) =>
{
h.Quality.CustomFormats = f;
return h;
},
id);
//Then MovieFiles:
var moviefileRepo = _container.Resolve<IMediaFileRepository>();
DeleteInRepo(moviefileRepo,
h => h.Quality.CustomFormats,
(h, f) =>
{
h.Quality.CustomFormats = f;
return h;
},
id);
//Then Profiles
ProfileService.DeleteCustomFormat(id);
}
catch (Exception e)
{
_logger.Error(e, "Failed to delete format with id {} from other repositories! Format will not be deleted!", id);
throw;
}
//Finally delete the format for real!
_formatRepository.Delete(id);
_cache.Clear();
}
private void DeleteInRepo<TModel>(IBasicRepository<TModel> repository,
Func<TModel, List<CustomFormat>> queryFunc,
Func<TModel, List<CustomFormat>, TModel> updateFunc,
int customFormatId)
where TModel : ModelBase, new()
{
var allItems = repository.All();
var toUpdate = allItems.Where(r => queryFunc(r).Exists(c => c.Id == customFormatId)).Select(r =>
{
return updateFunc(r, queryFunc(r).Where(c => c.Id != customFormatId).ToList());
});
repository.UpdateMany(toUpdate.ToList());
}
private Dictionary<int, CustomFormat> AllDictionary()
{
return _cache.Get("all", () =>
{
var all = _formatRepository.All().Select(x => (CustomFormat)x).ToDictionary(m => m.Id);
AllCustomFormats = all;
return all;
});
return _cache.Get("all", () => _formatRepository.All().ToDictionary(m => m.Id));
}
public List<CustomFormat> All()
@@ -175,11 +46,35 @@ namespace NzbDrone.Core.CustomFormats
return AllDictionary()[id];
}
public static Dictionary<string, List<CustomFormat>> Templates
public void Update(CustomFormat customFormat)
{
get
{
return new Dictionary<string, List<CustomFormat>>
_formatRepository.Update(customFormat);
_cache.Clear();
}
public CustomFormat Insert(CustomFormat customFormat)
{
// Add to DB then insert into profiles
var result = _formatRepository.Insert(customFormat);
_cache.Clear();
_eventAggregator.PublishEvent(new CustomFormatAddedEvent(result));
return result;
}
public void Delete(int id)
{
var format = _formatRepository.Get(id);
// Remove from profiles before removing from DB
_eventAggregator.PublishEvent(new CustomFormatDeletedEvent(format));
_formatRepository.Delete(id);
_cache.Clear();
}
public static Dictionary<string, List<CustomFormat>> Templates => new Dictionary<string, List<CustomFormat>>
{
{
"Easy", new List<CustomFormat>
@@ -207,7 +102,5 @@ namespace NzbDrone.Core.CustomFormats
}
}
};
}
}
}
}