From 514a03e9cfa675485a78382956e48fb1617cd22d Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Sat, 29 Sep 2018 12:58:18 -0500 Subject: [PATCH] cleanup orphaned rules, reduce RulesCount complexity --- src/FilterLists.Services/RuleService.cs | 5 ++--- .../Snapshot/SnapshotService.cs | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/FilterLists.Services/RuleService.cs b/src/FilterLists.Services/RuleService.cs index a194623ae..887f0116b 100644 --- a/src/FilterLists.Services/RuleService.cs +++ b/src/FilterLists.Services/RuleService.cs @@ -1,5 +1,4 @@ -using System.Linq; -using System.Threading.Tasks; +using System.Threading.Tasks; using FilterLists.Data; using JetBrains.Annotations; using Microsoft.EntityFrameworkCore; @@ -14,6 +13,6 @@ public RuleService(FilterListsDbContext dbContext) : base(dbContext) } public async Task GetCountAll() => - await DbContext.Rules.AsNoTracking().Where(r => r.SnapshotRules.Any()).CountAsync(); + await DbContext.Rules.AsNoTracking().CountAsync(); } } \ No newline at end of file diff --git a/src/FilterLists.Services/Snapshot/SnapshotService.cs b/src/FilterLists.Services/Snapshot/SnapshotService.cs index cd2c9a913..afe128e2d 100644 --- a/src/FilterLists.Services/Snapshot/SnapshotService.cs +++ b/src/FilterLists.Services/Snapshot/SnapshotService.cs @@ -36,6 +36,12 @@ public async Task CaptureAsync(int batchSize) } private async Task CleanupFailedSnapshots() + { + await CleanupOrphanedSnapshotsRules(); + await CleanupOrphanedRules(); + } + + private async Task CleanupOrphanedSnapshotsRules() { const string command = @"DELETE snapshots_rules @@ -44,6 +50,16 @@ FROM snapshots_rules WHERE snapshots.WasSuccessful = 0;"; await DbContext.Database.ExecuteSqlCommandAsync(command); } + + private async Task CleanupOrphanedRules() + { + const string command = + @"DELETE rules + FROM rules + LEFT JOIN snapshots_rules ON rules.Id = snapshots_rules.RuleId + WHERE snapshots_rules.RuleId IS NULL;"; + await DbContext.Database.ExecuteSqlCommandAsync(command); + } private async Task> GetListsToCapture(int batchSize) => await DbContext