From bca4baffd4f39c2d080563fecf7cc3ae2e498e92 Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Sat, 10 Feb 2018 20:01:08 -0600 Subject: [PATCH] minor refactors --- .../SnapshotService/SnapshotBatchDe.cs | 2 +- .../SnapshotService/SnapshotDe.cs | 23 +++++----- .../SnapshotService/SnapshotService.cs | 42 ++++++++++--------- 3 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/FilterLists.Services/SnapshotService/SnapshotBatchDe.cs b/src/FilterLists.Services/SnapshotService/SnapshotBatchDe.cs index bc69b55b5..5537bb793 100644 --- a/src/FilterLists.Services/SnapshotService/SnapshotBatchDe.cs +++ b/src/FilterLists.Services/SnapshotService/SnapshotBatchDe.cs @@ -32,7 +32,7 @@ public async Task SaveSnapshotBatchAsync() private void AddRules() { var existingRules = dbContext.Rules.Where(rule => rawRules.Contains(rule.Raw)); - var newRawRules = rawRules.Except(existingRules.Select(x => x.Raw)); + var newRawRules = rawRules.Except(existingRules.Select(r => r.Raw)); var newRules = newRawRules.Select(newRawRule => new Rule {Raw = newRawRule}).ToList(); dbContext.Rules.AddRange(newRules); rules = existingRules.Concat(newRules); diff --git a/src/FilterLists.Services/SnapshotService/SnapshotDe.cs b/src/FilterLists.Services/SnapshotService/SnapshotDe.cs index bacb4e70b..1364f7b91 100644 --- a/src/FilterLists.Services/SnapshotService/SnapshotDe.cs +++ b/src/FilterLists.Services/SnapshotService/SnapshotDe.cs @@ -93,7 +93,7 @@ private static IEnumerable GetRawRules(string content) var rawRules = content.Split(new[] {"\r\n", "\r", "\n"}, StringSplitOptions.RemoveEmptyEntries); for (var i = 0; i < rawRules.Length; i++) rawRules[i] = rawRules[i].LintRawRule(); - return new HashSet(rawRules.Where(x => x != null)); + return new HashSet(rawRules.Where(rr => rr != null)); } private IEnumerable GetSnapshotBatches(IEnumerable rawRules) @@ -118,24 +118,25 @@ private async Task DedupSnapshotRules() private IQueryable GetExistingSnapshotRules() { - return dbContext.SnapshotRules.Where(x => - x.AddedBySnapshot.FilterListId == list.Id && - x.AddedBySnapshot != snapshot && - x.RemovedBySnapshot == null); + return dbContext.SnapshotRules.Where(sr => + sr.AddedBySnapshot.FilterListId == list.Id && + sr.AddedBySnapshot != snapshot && + sr.RemovedBySnapshot == null); } 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); + var newSnapshotRules = dbContext.SnapshotRules.Where(sr => sr.AddedBySnapshot == snapshot); + var removedSnapshotRules = + existingSnapshotRules.Where(sr => !newSnapshotRules.Any(nsr => nsr.Rule == sr.Rule)); + removedSnapshotRules.ToList().ForEach(sr => sr.RemovedBySnapshot = snapshot); } private void RemoveDuplicateSnapshotRules(IQueryable existingSnapshotRules) { - var duplicateSnapshotRules = dbContext.SnapshotRules.Where(x => - x.AddedBySnapshot == snapshot && - existingSnapshotRules.Any(y => y.Rule == x.Rule)); + var duplicateSnapshotRules = dbContext.SnapshotRules.Where(sr => + sr.AddedBySnapshot == snapshot && + existingSnapshotRules.Any(esr => esr.Rule == sr.Rule)); dbContext.SnapshotRules.RemoveRange(duplicateSnapshotRules); } diff --git a/src/FilterLists.Services/SnapshotService/SnapshotService.cs b/src/FilterLists.Services/SnapshotService/SnapshotService.cs index 0ceb038ac..ce65ba3f4 100644 --- a/src/FilterLists.Services/SnapshotService/SnapshotService.cs +++ b/src/FilterLists.Services/SnapshotService/SnapshotService.cs @@ -19,35 +19,39 @@ public SnapshotService(FilterListsDbContext dbContext) public async Task CaptureAsync(int batchSize) { - RollbackIncomplete(); - var lists = await GetLeastRecentlyCapturedLists(batchSize); + RollbackIncompletedSnapshot(); + var lists = await GetListsToCapture(batchSize); var snapshots = GetSnapshots(lists); await SaveSnapshots(snapshots); } - private void RollbackIncomplete() + private void RollbackIncompletedSnapshot() { - var incompleteSnapshots = dbContext.Snapshots.Where(x => x.IsCompleted == false); + var incompleteSnapshots = dbContext.Snapshots.Where(ss => ss.IsCompleted == false); dbContext.Snapshots.RemoveRange(incompleteSnapshots); //TODO: don't assume that SnapshotDe.DedupSnapshotRules() didn't partially complete } - private async Task> GetLeastRecentlyCapturedLists(int batchSize) + private async Task> GetListsToCapture(int batchSize) { - return await dbContext.FilterLists.OrderBy(x => x.Snapshots.Any()) - .ThenBy(x => - x.Snapshots.Select(y => y.CreatedDateUtc) - .OrderByDescending(y => y) - .FirstOrDefault()) - .Where(x => !x.Snapshots.Any() || - x.Snapshots.Select(y => y.CreatedDateUtc) - .OrderByDescending(y => y) - .FirstOrDefault() < - DateTime.UtcNow.AddDays(-1)) - .Take(batchSize) - .AsNoTracking() - .ProjectTo() - .ToListAsync(); + return await dbContext + .FilterLists + .Where(list => + !list.Snapshots.Any() || + list.Snapshots + .Select(ss => ss.CreatedDateUtc) + .OrderByDescending(sscd => sscd) + .FirstOrDefault() < DateTime.UtcNow.AddDays(-1)) + .OrderBy(list => list.Snapshots.Any()) + .ThenBy(list => + list.Snapshots + .Select(ss => ss.CreatedDateUtc) + .OrderByDescending(sscd => sscd) + .FirstOrDefault()) + .Take(batchSize) + .AsNoTracking() + .ProjectTo() + .ToListAsync(); } private IEnumerable GetSnapshots(IEnumerable lists)