From 87735b5805bd92ee188a1a310d87a6abee27a7d0 Mon Sep 17 00:00:00 2001 From: sillock1 Date: Sun, 28 Apr 2024 15:18:24 +0100 Subject: [PATCH 1/2] New: config flag to disable log database --- .../ServiceFactoryFixture.cs | 1 + src/NzbDrone.Common/Options/LogOptions.cs | 1 + .../Configuration/ConfigFileProvider.cs | 3 ++- .../Extensions/CompositionExtensions.cs | 12 ++++++++++ .../Update/History/UpdateHistoryService.cs | 7 ++++-- src/NzbDrone.Host/Bootstrap.cs | 17 ++++++++++++++ src/NzbDrone.Host/Startup.cs | 7 ++++-- src/Sonarr.Api.V3/Logs/LogController.cs | 3 ++- src/Sonarr.Api.V3/Update/UpdateController.cs | 2 ++ src/Sonarr.Http/PagingResource.cs | 2 +- ...ogDatabaseDisabledActionFilterAttribute.cs | 23 +++++++++++++++++++ 11 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 src/Sonarr.Http/REST/Filters/LogDatabaseDisabledActionFilterAttribute.cs diff --git a/src/NzbDrone.Common.Test/ServiceFactoryFixture.cs b/src/NzbDrone.Common.Test/ServiceFactoryFixture.cs index 6749006e5..cc2524248 100644 --- a/src/NzbDrone.Common.Test/ServiceFactoryFixture.cs +++ b/src/NzbDrone.Common.Test/ServiceFactoryFixture.cs @@ -30,6 +30,7 @@ namespace NzbDrone.Common.Test .AddNzbDroneLogger() .AutoAddServices(Bootstrap.ASSEMBLIES) .AddDummyDatabase() + .AddDummyLogDatabase() .AddStartupContext(new StartupContext("first", "second")); container.RegisterInstance(new Mock().Object); diff --git a/src/NzbDrone.Common/Options/LogOptions.cs b/src/NzbDrone.Common/Options/LogOptions.cs index c36533cb4..1529bb1d0 100644 --- a/src/NzbDrone.Common/Options/LogOptions.cs +++ b/src/NzbDrone.Common/Options/LogOptions.cs @@ -11,4 +11,5 @@ public class LogOptions public string SyslogServer { get; set; } public int? SyslogPort { get; set; } public string SyslogLevel { get; set; } + public bool? DbEnabled { get; set; } } diff --git a/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs b/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs index e1168c2fb..4dc2e1fc2 100644 --- a/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs +++ b/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs @@ -54,6 +54,7 @@ namespace NzbDrone.Core.Configuration string SyslogServer { get; } int SyslogPort { get; } string SyslogLevel { get; } + bool LogDbEnabled { get; } string Theme { get; } string PostgresHost { get; } int PostgresPort { get; } @@ -230,7 +231,7 @@ namespace NzbDrone.Core.Configuration public string PostgresMainDb => _postgresOptions?.MainDb ?? GetValue("PostgresMainDb", "sonarr-main", persist: false); public string PostgresLogDb => _postgresOptions?.LogDb ?? GetValue("PostgresLogDb", "sonarr-log", persist: false); public int PostgresPort => (_postgresOptions?.Port ?? 0) != 0 ? _postgresOptions.Port : GetValueInt("PostgresPort", 5432, persist: false); - + public bool LogDbEnabled => _logOptions.DbEnabled ?? GetValueBoolean("LogDbEnabled", true, persist: false); public bool LogSql => _logOptions.Sql ?? GetValueBoolean("LogSql", false, persist: false); public int LogRotate => _logOptions.Rotate ?? GetValueInt("LogRotate", 50, persist: false); public bool FilterSentryEvents => _logOptions.FilterSentryEvents ?? GetValueBoolean("FilterSentryEvents", true, persist: false); diff --git a/src/NzbDrone.Core/Datastore/Extensions/CompositionExtensions.cs b/src/NzbDrone.Core/Datastore/Extensions/CompositionExtensions.cs index c5e31f92c..67e251805 100644 --- a/src/NzbDrone.Core/Datastore/Extensions/CompositionExtensions.cs +++ b/src/NzbDrone.Core/Datastore/Extensions/CompositionExtensions.cs @@ -8,6 +8,12 @@ namespace NzbDrone.Core.Datastore.Extensions public static IContainer AddDatabase(this IContainer container) { container.RegisterDelegate(f => new MainDatabase(f.Create()), Reuse.Singleton); + + return container; + } + + public static IContainer AddLogDatabase(this IContainer container) + { container.RegisterDelegate(f => new LogDatabase(f.Create(MigrationType.Log)), Reuse.Singleton); return container; @@ -16,6 +22,12 @@ namespace NzbDrone.Core.Datastore.Extensions public static IContainer AddDummyDatabase(this IContainer container) { container.RegisterInstance(new MainDatabase(null)); + + return container; + } + + public static IContainer AddDummyLogDatabase(this IContainer container) + { container.RegisterInstance(new LogDatabase(null)); return container; diff --git a/src/NzbDrone.Core/Update/History/UpdateHistoryService.cs b/src/NzbDrone.Core/Update/History/UpdateHistoryService.cs index 09cf70602..df4642d81 100644 --- a/src/NzbDrone.Core/Update/History/UpdateHistoryService.cs +++ b/src/NzbDrone.Core/Update/History/UpdateHistoryService.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using NLog; using NzbDrone.Common.EnvironmentInfo; +using NzbDrone.Core.Configuration; using NzbDrone.Core.Lifecycle; using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Update.History.Events; @@ -19,13 +20,15 @@ namespace NzbDrone.Core.Update.History private readonly IUpdateHistoryRepository _repository; private readonly IEventAggregator _eventAggregator; private readonly Logger _logger; + private readonly IConfigFileProvider _configFileProvider; private Version _prevVersion; - public UpdateHistoryService(IUpdateHistoryRepository repository, IEventAggregator eventAggregator, Logger logger) + public UpdateHistoryService(IUpdateHistoryRepository repository, IEventAggregator eventAggregator, Logger logger, IConfigFileProvider configFileProvider) { _repository = repository; _eventAggregator = eventAggregator; _logger = logger; + _configFileProvider = configFileProvider; } public Version PreviouslyInstalled() @@ -58,7 +61,7 @@ namespace NzbDrone.Core.Update.History public void Handle(ApplicationStartedEvent message) { - if (BuildInfo.Version.Major == 10) + if (BuildInfo.Version.Major == 10 || !_configFileProvider.LogDbEnabled) { // Don't save dev versions, they change constantly return; diff --git a/src/NzbDrone.Host/Bootstrap.cs b/src/NzbDrone.Host/Bootstrap.cs index 2073eb622..562e6eefe 100644 --- a/src/NzbDrone.Host/Bootstrap.cs +++ b/src/NzbDrone.Host/Bootstrap.cs @@ -95,6 +95,14 @@ namespace NzbDrone.Host .AddStartupContext(startupContext) .Resolve() .Route(appMode); + if (config.GetValue(nameof(ConfigFileProvider.LogDbEnabled), true)) + { + c.AddLogDatabase(); + } + else + { + c.AddDummyLogDatabase(); + } }) .ConfigureServices(services => { @@ -131,6 +139,7 @@ namespace NzbDrone.Host var enableSsl = config.GetValue($"Sonarr:Server:{nameof(ServerOptions.EnableSsl)}") ?? config.GetValue(nameof(ConfigFileProvider.EnableSsl), false); var sslCertPath = config.GetValue($"Sonarr:Server:{nameof(ServerOptions.SslCertPath)}") ?? config.GetValue(nameof(ConfigFileProvider.SslCertPath)); var sslCertPassword = config.GetValue($"Sonarr:Server:{nameof(ServerOptions.SslCertPassword)}") ?? config.GetValue(nameof(ConfigFileProvider.SslCertPassword)); + var logDbEnabled = config.GetValue($"Sonarr:Log:{nameof(LogOptions.DbEnabled)}") ?? config.GetValue(nameof(ConfigFileProvider.LogDbEnabled), true); var urls = new List { BuildUrl("http", bindAddress, port) }; @@ -152,6 +161,14 @@ namespace NzbDrone.Host .AddNzbDroneLogger() .AddDatabase() .AddStartupContext(context); + if (logDbEnabled) + { + c.AddLogDatabase(); + } + else + { + c.AddDummyLogDatabase(); + } SchemaBuilder.Initialize(c); }) diff --git a/src/NzbDrone.Host/Startup.cs b/src/NzbDrone.Host/Startup.cs index eb18394fc..ca97ee60c 100644 --- a/src/NzbDrone.Host/Startup.cs +++ b/src/NzbDrone.Host/Startup.cs @@ -220,9 +220,12 @@ namespace NzbDrone.Host // instantiate the databases to initialize/migrate them _ = mainDatabaseFactory.Value; - _ = logDatabaseFactory.Value; - dbTarget.Register(); + if (configFileProvider.LogDbEnabled) + { + _ = logDatabaseFactory.Value; + dbTarget.Register(); + } if (OsInfo.IsNotWindows) { diff --git a/src/Sonarr.Api.V3/Logs/LogController.cs b/src/Sonarr.Api.V3/Logs/LogController.cs index e838bc7e6..80a8512c2 100644 --- a/src/Sonarr.Api.V3/Logs/LogController.cs +++ b/src/Sonarr.Api.V3/Logs/LogController.cs @@ -3,10 +3,11 @@ using NzbDrone.Common.Extensions; using NzbDrone.Core.Instrumentation; using Sonarr.Http; using Sonarr.Http.Extensions; - +using Sonarr.Http.REST.Filters; namespace Sonarr.Api.V3.Logs { [V3ApiController] + [TypeFilter(typeof(LogDatabaseDisabledActionFilterAttribute>))] public class LogController : Controller { private readonly ILogService _logService; diff --git a/src/Sonarr.Api.V3/Update/UpdateController.cs b/src/Sonarr.Api.V3/Update/UpdateController.cs index a6e22f33e..c6efbfe4f 100644 --- a/src/Sonarr.Api.V3/Update/UpdateController.cs +++ b/src/Sonarr.Api.V3/Update/UpdateController.cs @@ -5,10 +5,12 @@ using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Core.Update; using NzbDrone.Core.Update.History; using Sonarr.Http; +using Sonarr.Http.REST.Filters; namespace Sonarr.Api.V3.Update { [V3ApiController] + [TypeFilter(typeof(LogDatabaseDisabledActionFilterAttribute>))] public class UpdateController : Controller { private readonly IRecentUpdateProvider _recentUpdateProvider; diff --git a/src/Sonarr.Http/PagingResource.cs b/src/Sonarr.Http/PagingResource.cs index 6559d80ab..64123e66a 100644 --- a/src/Sonarr.Http/PagingResource.cs +++ b/src/Sonarr.Http/PagingResource.cs @@ -21,7 +21,7 @@ namespace Sonarr.Http public string SortKey { get; set; } public SortDirection SortDirection { get; set; } public int TotalRecords { get; set; } - public List Records { get; set; } + public List Records { get; set; } = new (); public PagingResource() { diff --git a/src/Sonarr.Http/REST/Filters/LogDatabaseDisabledActionFilterAttribute.cs b/src/Sonarr.Http/REST/Filters/LogDatabaseDisabledActionFilterAttribute.cs new file mode 100644 index 000000000..66afc6b21 --- /dev/null +++ b/src/Sonarr.Http/REST/Filters/LogDatabaseDisabledActionFilterAttribute.cs @@ -0,0 +1,23 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.Extensions.DependencyInjection; +using NzbDrone.Core.Configuration; + +namespace Sonarr.Http.REST.Filters; + +public class LogDatabaseDisabledActionFilterAttribute : IActionFilter + where TResult : class, new() +{ + public void OnActionExecuting(ActionExecutingContext context) + { + var configFileProvider = context.HttpContext.RequestServices.GetService(); + if (!configFileProvider.LogDbEnabled) + { + context.Result = new OkObjectResult(new TResult()); + } + } + + public void OnActionExecuted(ActionExecutedContext context) + { + } +} From 7f0eb0f4f63556331dca29ee7cd400fbb001ac29 Mon Sep 17 00:00:00 2001 From: sillock1 Date: Sun, 28 Apr 2024 18:01:34 +0100 Subject: [PATCH 2/2] refactor: tidied up code with newlines and removed action filter for checks in the functions --- .../Update/History/UpdateHistoryService.cs | 6 ++--- .../Update/RecentUpdateProvider.cs | 2 +- src/NzbDrone.Host/Bootstrap.cs | 2 ++ src/Sonarr.Api.V3/Logs/LogController.cs | 13 ++++++++--- src/Sonarr.Api.V3/Update/UpdateController.cs | 13 +++++++---- ...ogDatabaseDisabledActionFilterAttribute.cs | 23 ------------------- 6 files changed, 25 insertions(+), 34 deletions(-) delete mode 100644 src/Sonarr.Http/REST/Filters/LogDatabaseDisabledActionFilterAttribute.cs diff --git a/src/NzbDrone.Core/Update/History/UpdateHistoryService.cs b/src/NzbDrone.Core/Update/History/UpdateHistoryService.cs index df4642d81..7be7349e1 100644 --- a/src/NzbDrone.Core/Update/History/UpdateHistoryService.cs +++ b/src/NzbDrone.Core/Update/History/UpdateHistoryService.cs @@ -19,16 +19,16 @@ namespace NzbDrone.Core.Update.History { private readonly IUpdateHistoryRepository _repository; private readonly IEventAggregator _eventAggregator; - private readonly Logger _logger; private readonly IConfigFileProvider _configFileProvider; + private readonly Logger _logger; private Version _prevVersion; - public UpdateHistoryService(IUpdateHistoryRepository repository, IEventAggregator eventAggregator, Logger logger, IConfigFileProvider configFileProvider) + public UpdateHistoryService(IUpdateHistoryRepository repository, IEventAggregator eventAggregator, IConfigFileProvider configFileProvider, Logger logger) { _repository = repository; _eventAggregator = eventAggregator; - _logger = logger; _configFileProvider = configFileProvider; + _logger = logger; } public Version PreviouslyInstalled() diff --git a/src/NzbDrone.Core/Update/RecentUpdateProvider.cs b/src/NzbDrone.Core/Update/RecentUpdateProvider.cs index a3264300d..08cc39865 100644 --- a/src/NzbDrone.Core/Update/RecentUpdateProvider.cs +++ b/src/NzbDrone.Core/Update/RecentUpdateProvider.cs @@ -29,7 +29,7 @@ namespace NzbDrone.Core.Update { var branch = _configFileProvider.Branch; var version = BuildInfo.Version; - var prevVersion = _updateHistoryService.PreviouslyInstalled(); + var prevVersion = _configFileProvider.LogDbEnabled ? _updateHistoryService.PreviouslyInstalled() : null; return _updatePackageProvider.GetRecentUpdates(branch, version, prevVersion); } } diff --git a/src/NzbDrone.Host/Bootstrap.cs b/src/NzbDrone.Host/Bootstrap.cs index 562e6eefe..7462a47d0 100644 --- a/src/NzbDrone.Host/Bootstrap.cs +++ b/src/NzbDrone.Host/Bootstrap.cs @@ -95,6 +95,7 @@ namespace NzbDrone.Host .AddStartupContext(startupContext) .Resolve() .Route(appMode); + if (config.GetValue(nameof(ConfigFileProvider.LogDbEnabled), true)) { c.AddLogDatabase(); @@ -161,6 +162,7 @@ namespace NzbDrone.Host .AddNzbDroneLogger() .AddDatabase() .AddStartupContext(context); + if (logDbEnabled) { c.AddLogDatabase(); diff --git a/src/Sonarr.Api.V3/Logs/LogController.cs b/src/Sonarr.Api.V3/Logs/LogController.cs index 80a8512c2..ba8021a42 100644 --- a/src/Sonarr.Api.V3/Logs/LogController.cs +++ b/src/Sonarr.Api.V3/Logs/LogController.cs @@ -1,26 +1,33 @@ using Microsoft.AspNetCore.Mvc; using NzbDrone.Common.Extensions; +using NzbDrone.Core.Configuration; using NzbDrone.Core.Instrumentation; using Sonarr.Http; using Sonarr.Http.Extensions; -using Sonarr.Http.REST.Filters; + namespace Sonarr.Api.V3.Logs { [V3ApiController] - [TypeFilter(typeof(LogDatabaseDisabledActionFilterAttribute>))] public class LogController : Controller { private readonly ILogService _logService; + private readonly IConfigFileProvider _configFileProvider; - public LogController(ILogService logService) + public LogController(ILogService logService, IConfigFileProvider configFileProvider) { _logService = logService; + _configFileProvider = configFileProvider; } [HttpGet] [Produces("application/json")] public PagingResource GetLogs([FromQuery] PagingRequestResource paging, string level) { + if (!_configFileProvider.LogDbEnabled) + { + return new PagingResource(); + } + var pagingResource = new PagingResource(paging); var pageSpec = pagingResource.MapToPagingSpec(); diff --git a/src/Sonarr.Api.V3/Update/UpdateController.cs b/src/Sonarr.Api.V3/Update/UpdateController.cs index c6efbfe4f..8c26b106c 100644 --- a/src/Sonarr.Api.V3/Update/UpdateController.cs +++ b/src/Sonarr.Api.V3/Update/UpdateController.cs @@ -2,24 +2,25 @@ using System.Collections.Generic; using System.Linq; using Microsoft.AspNetCore.Mvc; using NzbDrone.Common.EnvironmentInfo; +using NzbDrone.Core.Configuration; using NzbDrone.Core.Update; using NzbDrone.Core.Update.History; using Sonarr.Http; -using Sonarr.Http.REST.Filters; namespace Sonarr.Api.V3.Update { [V3ApiController] - [TypeFilter(typeof(LogDatabaseDisabledActionFilterAttribute>))] public class UpdateController : Controller { private readonly IRecentUpdateProvider _recentUpdateProvider; private readonly IUpdateHistoryService _updateHistoryService; + private readonly IConfigFileProvider _configFileProvider; - public UpdateController(IRecentUpdateProvider recentUpdateProvider, IUpdateHistoryService updateHistoryService) + public UpdateController(IRecentUpdateProvider recentUpdateProvider, IUpdateHistoryService updateHistoryService, IConfigFileProvider configFileProvider) { _recentUpdateProvider = recentUpdateProvider; _updateHistoryService = updateHistoryService; + _configFileProvider = configFileProvider; } [HttpGet] @@ -47,7 +48,11 @@ namespace Sonarr.Api.V3.Update installed.Installed = true; } - var installDates = _updateHistoryService.InstalledSince(resources.Last().ReleaseDate) + var updateHistory = _configFileProvider.LogDbEnabled + ? _updateHistoryService.InstalledSince(resources.Last().ReleaseDate) + : new List(); + + var installDates = updateHistory .DistinctBy(v => v.Version) .ToDictionary(v => v.Version); diff --git a/src/Sonarr.Http/REST/Filters/LogDatabaseDisabledActionFilterAttribute.cs b/src/Sonarr.Http/REST/Filters/LogDatabaseDisabledActionFilterAttribute.cs deleted file mode 100644 index 66afc6b21..000000000 --- a/src/Sonarr.Http/REST/Filters/LogDatabaseDisabledActionFilterAttribute.cs +++ /dev/null @@ -1,23 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Filters; -using Microsoft.Extensions.DependencyInjection; -using NzbDrone.Core.Configuration; - -namespace Sonarr.Http.REST.Filters; - -public class LogDatabaseDisabledActionFilterAttribute : IActionFilter - where TResult : class, new() -{ - public void OnActionExecuting(ActionExecutingContext context) - { - var configFileProvider = context.HttpContext.RequestServices.GetService(); - if (!configFileProvider.LogDbEnabled) - { - context.Result = new OkObjectResult(new TResult()); - } - } - - public void OnActionExecuted(ActionExecutedContext context) - { - } -}