mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
finish first pass at Snapshot dedup-ing
This commit is contained in:
parent
1e10bf9264
commit
bf97d1a3b3
1 changed files with 21 additions and 9 deletions
|
|
@ -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<SnapshotBatchDe> 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<SnapshotRule> 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<SnapshotRule> 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<SnapshotRule> existingSnapshotRules)
|
||||
{
|
||||
var duplicateSnapshotRules = dbContext.SnapshotRules.Where(x =>
|
||||
x.AddedBySnapshot == snapshot &&
|
||||
existingSnapshotRules.Any(y => y.Rule == x.Rule));
|
||||
dbContext.SnapshotRules.RemoveRange(duplicateSnapshotRules);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue