From 488b2faf195080c111bf046e66bebbd0f06cd570 Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Sat, 10 Feb 2018 14:33:20 -0600 Subject: [PATCH] finish first pass at Snapshot dedup-ing --- .../SnapshotService/SnapshotDe.cs | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/FilterLists.Services/SnapshotService/SnapshotDe.cs b/src/FilterLists.Services/SnapshotService/SnapshotDe.cs index a9a445fe0..e5c2905ba 100644 --- a/src/FilterLists.Services/SnapshotService/SnapshotDe.cs +++ b/src/FilterLists.Services/SnapshotService/SnapshotDe.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using FilterLists.Data; using FilterLists.Data.Entities; +using FilterLists.Data.Entities.Junctions; using FilterLists.Services.Extensions; namespace FilterLists.Services.SnapshotService @@ -105,24 +106,35 @@ private static async Task SaveSnapshotBatches(IEnumerable snaps await snapshotBatch.SaveSnapshotBatchAsync(); } - //TODO: test private async Task DedupSnapshotRules() { - var currentSnapshotRules = - dbContext.SnapshotRules.Where(x => x.AddedBySnapshot == snapshot).Select(x => x.Rule); + var existingSnapshotRules = GetExistingSnapshotRules(); + UpdateRemovedSnapshotRules(existingSnapshotRules); + RemoveDuplicateSnapshotRules(existingSnapshotRules); + await dbContext.SaveChangesAsync(); + } - var existingSnapshotRules = dbContext.SnapshotRules.Where(x => + private IQueryable GetExistingSnapshotRules() + { + return dbContext.SnapshotRules.Where(x => x.AddedBySnapshot.FilterListId == list.Id && x.AddedBySnapshot != snapshot && x.RemovedBySnapshot == null); + } - var removedSnapshotRules = existingSnapshotRules.Where(x => !currentSnapshotRules.Contains(x.Rule)); - + private void UpdateRemovedSnapshotRules(IQueryable existingSnapshotRules) + { + var newSnapshotRules = dbContext.SnapshotRules.Where(x => x.AddedBySnapshot == snapshot); + var removedSnapshotRules = existingSnapshotRules.Where(x => !newSnapshotRules.Any(y => y.Rule == x.Rule)); removedSnapshotRules.ToList().ForEach(x => x.RemovedBySnapshot = snapshot); + } - //TODO: delete any added SnapshotRules that were already in a previous snapshot and were not marked as removed - - await dbContext.SaveChangesAsync(); + private void RemoveDuplicateSnapshotRules(IQueryable existingSnapshotRules) + { + var duplicateSnapshotRules = dbContext.SnapshotRules.Where(x => + x.AddedBySnapshot == snapshot && + existingSnapshotRules.Any(y => y.Rule == x.Rule)); + dbContext.SnapshotRules.RemoveRange(duplicateSnapshotRules); } } } \ No newline at end of file