mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
restore rest of cleanup from 46dffec60a
This commit is contained in:
parent
9fe4fb7edd
commit
aedbd3f214
3 changed files with 25 additions and 22 deletions
|
|
@ -7,7 +7,6 @@
|
|||
using System.Threading.Tasks;
|
||||
using FilterLists.Data;
|
||||
using FilterLists.Data.Entities.Junctions;
|
||||
using FilterLists.Services.Extensions;
|
||||
using FilterLists.Services.Snapshot.Models;
|
||||
using MoreLinq;
|
||||
|
||||
|
|
@ -29,10 +28,14 @@ public SnapshotDe(FilterListsDbContext dbContext, EmailService emailService, Fil
|
|||
this.dbContext = dbContext;
|
||||
this.emailService = emailService;
|
||||
this.list = list;
|
||||
snapshot = new Data.Entities.Snapshot {FilterListId = list.Id};
|
||||
snapshot = new Data.Entities.Snapshot
|
||||
{
|
||||
FilterListId = list.Id,
|
||||
AddedSnapshotRules = new List<SnapshotRule>()
|
||||
};
|
||||
}
|
||||
|
||||
public async Task SaveSnapshotAsync()
|
||||
public async Task SaveAsync()
|
||||
{
|
||||
using (var transaction = dbContext.Database.BeginTransaction())
|
||||
{
|
||||
|
|
@ -122,12 +125,12 @@ private async Task SendExceptionEmail(Exception e)
|
|||
|
||||
private async Task SaveSnapshotInBatches(string content)
|
||||
{
|
||||
var rawRules = GetRawRules(content);
|
||||
var snapshotBatches = GetSnapshotBatches(rawRules);
|
||||
var rawRules = ParseRawRules(content);
|
||||
var snapshotBatches = CreateSnapshotBatches(rawRules);
|
||||
await SaveSnapshotBatches(snapshotBatches);
|
||||
}
|
||||
|
||||
private static IEnumerable<string> GetRawRules(string content)
|
||||
private static IEnumerable<string> ParseRawRules(string content)
|
||||
{
|
||||
var rawRules = content.Split(new[] {"\r\n", "\r", "\n"}, StringSplitOptions.RemoveEmptyEntries);
|
||||
for (var i = 0; i < rawRules.Length; i++)
|
||||
|
|
@ -135,13 +138,13 @@ private static IEnumerable<string> GetRawRules(string content)
|
|||
return new HashSet<string>(rawRules.Where(r => r != null));
|
||||
}
|
||||
|
||||
private IEnumerable<SnapshotBatchDe> GetSnapshotBatches(IEnumerable<string> rawRules) =>
|
||||
rawRules.Batch(BatchSize).Select(b => new SnapshotBatchDe(dbContext, snapshot, b));
|
||||
private IEnumerable<SnapshotDeBatch> CreateSnapshotBatches(IEnumerable<string> rawRules) =>
|
||||
rawRules.Batch(BatchSize).Select(b => new SnapshotDeBatch(dbContext, snapshot, b));
|
||||
|
||||
private static async Task SaveSnapshotBatches(IEnumerable<SnapshotBatchDe> snapshotBatches)
|
||||
private static async Task SaveSnapshotBatches(IEnumerable<SnapshotDeBatch> snapshotBatches)
|
||||
{
|
||||
foreach (var snapshotBatch in snapshotBatches)
|
||||
await snapshotBatch.SaveSnapshotBatchAsync();
|
||||
await snapshotBatch.SaveAsync();
|
||||
}
|
||||
|
||||
private async Task DedupSnapshotRules()
|
||||
|
|
|
|||
|
|
@ -8,14 +8,14 @@
|
|||
|
||||
namespace FilterLists.Services.Snapshot
|
||||
{
|
||||
public class SnapshotBatchDe
|
||||
public class SnapshotDeBatch
|
||||
{
|
||||
private readonly FilterListsDbContext dbContext;
|
||||
private readonly IEnumerable<string> rawRules;
|
||||
private readonly Data.Entities.Snapshot snapshot;
|
||||
private IQueryable<Rule> rules;
|
||||
|
||||
public SnapshotBatchDe(FilterListsDbContext dbContext, Data.Entities.Snapshot snapshot,
|
||||
public SnapshotDeBatch(FilterListsDbContext dbContext, Data.Entities.Snapshot snapshot,
|
||||
IEnumerable<string> rawRules)
|
||||
{
|
||||
this.dbContext = dbContext;
|
||||
|
|
@ -23,7 +23,7 @@ public SnapshotBatchDe(FilterListsDbContext dbContext, Data.Entities.Snapshot sn
|
|||
this.rawRules = rawRules;
|
||||
}
|
||||
|
||||
public async Task SaveSnapshotBatchAsync()
|
||||
public async Task SaveAsync()
|
||||
{
|
||||
await AddRules();
|
||||
AddSnapshotRules();
|
||||
|
|
@ -32,18 +32,18 @@ public async Task SaveSnapshotBatchAsync()
|
|||
|
||||
private async Task AddRules()
|
||||
{
|
||||
var existingRules = dbContext.Rules.Where(r => rawRules.Contains(r.Raw));
|
||||
var newRawRules = rawRules.Except(existingRules.Select(r => r.Raw));
|
||||
var newRules = newRawRules.Select(r => new Rule {Raw = r}).ToList();
|
||||
var existingRules = GetExistingRules();
|
||||
var newRules = CreateNewRules(existingRules);
|
||||
rules = existingRules.Concat(newRules);
|
||||
await dbContext.Rules.AddRangeAsync(newRules);
|
||||
}
|
||||
|
||||
private void AddSnapshotRules()
|
||||
{
|
||||
if (snapshot.AddedSnapshotRules == null)
|
||||
snapshot.AddedSnapshotRules = new List<SnapshotRule>();
|
||||
private IQueryable<Rule> GetExistingRules() => dbContext.Rules.Where(r => rawRules.Contains(r.Raw));
|
||||
|
||||
private List<Rule> CreateNewRules(IQueryable<Rule> existingRules) =>
|
||||
rawRules.Except(existingRules.Select(r => r.Raw)).Select(r => new Rule {Raw = r}).ToList();
|
||||
|
||||
private void AddSnapshotRules() =>
|
||||
snapshot.AddedSnapshotRules.AddRange(rules.Select(r => new SnapshotRule {Rule = r}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -69,7 +69,7 @@ private IEnumerable<SnapshotDe> CreateSnapshots(IEnumerable<FilterListViewUrlDto
|
|||
private static async Task SaveSnapshots(IEnumerable<SnapshotDe> snapshots)
|
||||
{
|
||||
foreach (var snapshot in snapshots)
|
||||
await snapshot.SaveSnapshotAsync();
|
||||
await snapshot.SaveAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue