From 73f34592649fedc20903e7d3e1d2694a84422d53 Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Tue, 18 Jun 2013 18:01:08 -0700 Subject: [PATCH] added log trim command --- NzbDrone.Core/Datastore/IReadModels.cs | 8 ------- NzbDrone.Core/Datastore/IWriteModels.cs | 9 ------- .../Migration/006_add_index_to_log_time.cs | 24 +++++++++++++++++++ .../Migration/Framework/MigrationLogger.cs | 1 - .../Framework/NzbDroneMigrationBase.cs | 22 ++++++++--------- NzbDrone.Core/Datastore/MigrationType.cs | 7 +----- NzbDrone.Core/Datastore/ModelBase.cs | 4 +--- NzbDrone.Core/Datastore/PagingSpec.cs | 5 +--- .../Datastore/PagingSpecExtensions.cs | 4 ---- NzbDrone.Core/Datastore/TableMapping.cs | 2 -- .../Commands/TrimLogCommand.cs | 8 +++++++ .../Instrumentation/LogRepository.cs | 6 ++--- NzbDrone.Core/Instrumentation/LogService.cs | 22 +++++++---------- NzbDrone.Core/Jobs/TaskManager.cs | 4 +++- NzbDrone.Core/NzbDrone.Core.csproj | 4 ++-- NzbDrone.ncrunchsolution | 1 + 16 files changed, 62 insertions(+), 69 deletions(-) delete mode 100644 NzbDrone.Core/Datastore/IReadModels.cs delete mode 100644 NzbDrone.Core/Datastore/IWriteModels.cs create mode 100644 NzbDrone.Core/Datastore/Migration/006_add_index_to_log_time.cs create mode 100644 NzbDrone.Core/Instrumentation/Commands/TrimLogCommand.cs diff --git a/NzbDrone.Core/Datastore/IReadModels.cs b/NzbDrone.Core/Datastore/IReadModels.cs deleted file mode 100644 index 6a8a6023d..000000000 --- a/NzbDrone.Core/Datastore/IReadModels.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace NzbDrone.Core.Datastore -{ - public interface IReadModels where T : ModelBase - { - T All(); - T Get(int id); - } -} \ No newline at end of file diff --git a/NzbDrone.Core/Datastore/IWriteModels.cs b/NzbDrone.Core/Datastore/IWriteModels.cs deleted file mode 100644 index f3608f16a..000000000 --- a/NzbDrone.Core/Datastore/IWriteModels.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace NzbDrone.Core.Datastore -{ - public interface IWriteModels where T : ModelBase - { - T Add(T model); - T Update(T model); - void Delete(int id); - } -} \ No newline at end of file diff --git a/NzbDrone.Core/Datastore/Migration/006_add_index_to_log_time.cs b/NzbDrone.Core/Datastore/Migration/006_add_index_to_log_time.cs new file mode 100644 index 000000000..89d3d39bc --- /dev/null +++ b/NzbDrone.Core/Datastore/Migration/006_add_index_to_log_time.cs @@ -0,0 +1,24 @@ +using FluentMigrator; +using NzbDrone.Core.Datastore.Migration.Framework; + +namespace NzbDrone.Core.Datastore.Migration +{ + [Tags("")] + [Migration(6)] + public class add_index_to_log_time : NzbDroneMigrationBase + { + protected override void LogDbUpgrade() + { + Delete.Table("Logs"); + + Create.TableForModel("Logs") + .WithColumn("Message").AsString() + .WithColumn("Time").AsDateTime().Indexed() + .WithColumn("Logger").AsString() + .WithColumn("Method").AsString().Nullable() + .WithColumn("Exception").AsString().Nullable() + .WithColumn("ExceptionType").AsString().Nullable() + .WithColumn("Level").AsString(); + } + } +} diff --git a/NzbDrone.Core/Datastore/Migration/Framework/MigrationLogger.cs b/NzbDrone.Core/Datastore/Migration/Framework/MigrationLogger.cs index a1761c830..cab90b90c 100644 --- a/NzbDrone.Core/Datastore/Migration/Framework/MigrationLogger.cs +++ b/NzbDrone.Core/Datastore/Migration/Framework/MigrationLogger.cs @@ -1,7 +1,6 @@ using System; using FluentMigrator.Runner; using NLog; -using NzbDrone.Common.Composition; namespace NzbDrone.Core.Datastore.Migration.Framework { diff --git a/NzbDrone.Core/Datastore/Migration/Framework/NzbDroneMigrationBase.cs b/NzbDrone.Core/Datastore/Migration/Framework/NzbDroneMigrationBase.cs index 5bb072233..02b019439 100644 --- a/NzbDrone.Core/Datastore/Migration/Framework/NzbDroneMigrationBase.cs +++ b/NzbDrone.Core/Datastore/Migration/Framework/NzbDroneMigrationBase.cs @@ -14,18 +14,18 @@ protected virtual void LogDbUpgrade() public override void Up() { - if ((MigrationType)ApplicationContext == MigrationType.Main) + switch ((MigrationType)ApplicationContext) { - MainDbUpgrade(); - } - else if ((MigrationType)ApplicationContext == MigrationType.Log) - { - LogDbUpgrade(); - } - else - { - LogDbUpgrade(); - MainDbUpgrade(); + case MigrationType.Main: + MainDbUpgrade(); + return; + case MigrationType.Log: + LogDbUpgrade(); + return; + default: + LogDbUpgrade(); + MainDbUpgrade(); + return; } } diff --git a/NzbDrone.Core/Datastore/MigrationType.cs b/NzbDrone.Core/Datastore/MigrationType.cs index b9bd958ce..b572755cc 100644 --- a/NzbDrone.Core/Datastore/MigrationType.cs +++ b/NzbDrone.Core/Datastore/MigrationType.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace NzbDrone.Core.Datastore +namespace NzbDrone.Core.Datastore { public enum MigrationType { diff --git a/NzbDrone.Core/Datastore/ModelBase.cs b/NzbDrone.Core/Datastore/ModelBase.cs index 5176ab4ad..4c2c9a43c 100644 --- a/NzbDrone.Core/Datastore/ModelBase.cs +++ b/NzbDrone.Core/Datastore/ModelBase.cs @@ -1,6 +1,4 @@ -using System.Data; -using System.Diagnostics; -using Marr.Data; +using System.Diagnostics; namespace NzbDrone.Core.Datastore { diff --git a/NzbDrone.Core/Datastore/PagingSpec.cs b/NzbDrone.Core/Datastore/PagingSpec.cs index b763c02e2..c1b7eb8a7 100644 --- a/NzbDrone.Core/Datastore/PagingSpec.cs +++ b/NzbDrone.Core/Datastore/PagingSpec.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Collections.Generic; namespace NzbDrone.Core.Datastore { diff --git a/NzbDrone.Core/Datastore/PagingSpecExtensions.cs b/NzbDrone.Core/Datastore/PagingSpecExtensions.cs index ed7ba739f..15f09c625 100644 --- a/NzbDrone.Core/Datastore/PagingSpecExtensions.cs +++ b/NzbDrone.Core/Datastore/PagingSpecExtensions.cs @@ -1,10 +1,6 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; using System.Linq; using System.Linq.Expressions; -using System.Reflection; -using System.Text; namespace NzbDrone.Core.Datastore { diff --git a/NzbDrone.Core/Datastore/TableMapping.cs b/NzbDrone.Core/Datastore/TableMapping.cs index b9cdfa1b5..37ec92fa8 100644 --- a/NzbDrone.Core/Datastore/TableMapping.cs +++ b/NzbDrone.Core/Datastore/TableMapping.cs @@ -1,8 +1,6 @@ using System; -using System.Collections; using System.Collections.Generic; using System.Linq; -using System.Reflection; using Marr.Data; using Marr.Data.Mapping; using NzbDrone.Core.Configuration; diff --git a/NzbDrone.Core/Instrumentation/Commands/TrimLogCommand.cs b/NzbDrone.Core/Instrumentation/Commands/TrimLogCommand.cs new file mode 100644 index 000000000..c00b27020 --- /dev/null +++ b/NzbDrone.Core/Instrumentation/Commands/TrimLogCommand.cs @@ -0,0 +1,8 @@ +using NzbDrone.Common.Messaging; + +namespace NzbDrone.Core.Instrumentation.Commands +{ + public class TrimLogCommand : ICommand + { + } +} \ No newline at end of file diff --git a/NzbDrone.Core/Instrumentation/LogRepository.cs b/NzbDrone.Core/Instrumentation/LogRepository.cs index 29cc4ed7f..5fe755580 100644 --- a/NzbDrone.Core/Instrumentation/LogRepository.cs +++ b/NzbDrone.Core/Instrumentation/LogRepository.cs @@ -1,6 +1,4 @@ using System; -using System.Data; -using System.Linq; using NzbDrone.Common.Messaging; using NzbDrone.Core.Datastore; @@ -20,8 +18,8 @@ public LogRepository(IDatabase database, IMessageAggregator messageAggregator) public void Trim() { - var oldIds = Query.Where(c => c.Time < DateTime.UtcNow.AddDays(-30).Date).Select(c => c.Id); - DeleteMany(oldIds); + var trimDate = DateTime.UtcNow.AddDays(-15).Date; + Delete(c => c.Time <= trimDate); } } } \ No newline at end of file diff --git a/NzbDrone.Core/Instrumentation/LogService.cs b/NzbDrone.Core/Instrumentation/LogService.cs index d214166dd..56f38a345 100644 --- a/NzbDrone.Core/Instrumentation/LogService.cs +++ b/NzbDrone.Core/Instrumentation/LogService.cs @@ -1,16 +1,15 @@ -using System.Linq; +using NzbDrone.Common.Messaging; using NzbDrone.Core.Datastore; +using NzbDrone.Core.Instrumentation.Commands; namespace NzbDrone.Core.Instrumentation { public interface ILogService { - void DeleteAll(); - void Trim(); PagingSpec Paged(PagingSpec pagingSpec); } - public class LogService : ILogService + public class LogService : ILogService, IExecute { private readonly ILogRepository _logRepository; @@ -19,19 +18,14 @@ public LogService(ILogRepository logRepository) _logRepository = logRepository; } - public void DeleteAll() - { - _logRepository.Purge(); - } - - public void Trim() - { - _logRepository.Trim(); - } - public PagingSpec Paged(PagingSpec pagingSpec) { return _logRepository.GetPaged(pagingSpec); } + + public void Execute(TrimLogCommand message) + { + _logRepository.Trim(); + } } } \ No newline at end of file diff --git a/NzbDrone.Core/Jobs/TaskManager.cs b/NzbDrone.Core/Jobs/TaskManager.cs index 54238170c..50fbc3f3c 100644 --- a/NzbDrone.Core/Jobs/TaskManager.cs +++ b/NzbDrone.Core/Jobs/TaskManager.cs @@ -4,6 +4,7 @@ using NLog; using NzbDrone.Common.Messaging; using NzbDrone.Core.Indexers; +using NzbDrone.Core.Instrumentation.Commands; using NzbDrone.Core.Lifecycle; using NzbDrone.Core.MediaFiles.Commands; using NzbDrone.Core.Providers; @@ -42,7 +43,8 @@ public void Handle(ApplicationStartedEvent message) new ScheduledTask{ Interval = 12*60, TypeName = typeof(UpdateXemMappings).FullName}, new ScheduledTask{ Interval = 6*60, TypeName = typeof(RefreshSeriesCommand).FullName}, new ScheduledTask{ Interval = 1, TypeName = typeof(DownloadedEpisodesScanCommand).FullName}, - new ScheduledTask{ Interval = 5, TypeName = typeof(ApplicationUpdateCommand).FullName} + new ScheduledTask{ Interval = 5, TypeName = typeof(ApplicationUpdateCommand).FullName}, + new ScheduledTask{ Interval = 1*60, TypeName = typeof(TrimLogCommand).FullName} }; var currentTasks = _scheduledTaskRepository.All(); diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 0c62441ed..eb3227472 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -204,14 +204,13 @@ - - + @@ -255,6 +254,7 @@ + diff --git a/NzbDrone.ncrunchsolution b/NzbDrone.ncrunchsolution index 6cb47a29a..444b34b1d 100644 --- a/NzbDrone.ncrunchsolution +++ b/NzbDrone.ncrunchsolution @@ -2,6 +2,7 @@ 1 False true + true UseDynamicAnalysis Disabled Disabled