From bdfc8e1e2032541f817725eceaf1bc621f352274 Mon Sep 17 00:00:00 2001 From: Collin Barrett Date: Tue, 30 Jan 2018 05:01:50 -0600 Subject: [PATCH] minor ScrapeService refactors --- .../IEnumerableExtensions.cs | 14 +++++ .../Services/ScrapeService.cs | 51 ++++++++----------- 2 files changed, 36 insertions(+), 29 deletions(-) create mode 100644 src/FilterLists.Services/IEnumerableExtensions.cs diff --git a/src/FilterLists.Services/IEnumerableExtensions.cs b/src/FilterLists.Services/IEnumerableExtensions.cs new file mode 100644 index 000000000..d36f28a7e --- /dev/null +++ b/src/FilterLists.Services/IEnumerableExtensions.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; + +namespace FilterLists.Services +{ + public static class IEnumerableExtensions + { + public static void ForEach(this IEnumerable items, Action action) + { + foreach (var item in items) + action(item); + } + } +} \ No newline at end of file diff --git a/src/FilterLists.Services/Services/ScrapeService.cs b/src/FilterLists.Services/Services/ScrapeService.cs index d994e667d..2f49e39c2 100644 --- a/src/FilterLists.Services/Services/ScrapeService.cs +++ b/src/FilterLists.Services/Services/ScrapeService.cs @@ -12,24 +12,24 @@ namespace FilterLists.Services.Services { public class ScrapeService { - private readonly FilterListsDbContext filterListsDbContext; + private readonly FilterListsDbContext dbContext; - public ScrapeService(FilterListsDbContext filterListsDbContext) + public ScrapeService(FilterListsDbContext dbContext) { - this.filterListsDbContext = filterListsDbContext; + this.dbContext = dbContext; } //TODO: call via scheduled job public async Task ScrapeAsync(int batchSize) { - var lists = await GetNextFilterListDtosToScrape(batchSize); + var lists = await GetNextFilterListsToScrape(batchSize); var snapshots = await GetSnapshots(lists); - AddOrUpdateRules(snapshots); + snapshots.ForEach(AddOrUpdateRules); } - private async Task> GetNextFilterListDtosToScrape(int batchSize) + private async Task> GetNextFilterListsToScrape(int batchSize) { - return await filterListsDbContext.FilterLists.OrderBy(x => x.ScrapedDateUtc).Take(batchSize) + return await dbContext.FilterLists.OrderBy(x => x.ScrapedDateUtc).Take(batchSize) .ProjectTo().ToListAsync(); } @@ -43,7 +43,7 @@ private static async Task TryGetContent(string url) { try { - return await GetSuccessfulHttpResponseMessageContent(url); + return await GetHttpResponseMessageContent(url); } catch (Exception) { @@ -52,7 +52,7 @@ private static async Task TryGetContent(string url) } } - private static async Task GetSuccessfulHttpResponseMessageContent(string url) + private static async Task GetHttpResponseMessageContent(string url) { using (var httpClient = new HttpClient()) using (var httpResponseMessage = await httpClient.GetAsync(url)) @@ -65,29 +65,22 @@ private static async Task GetSuccessfulHttpResponseMessageContent(string return null; } - private void AddOrUpdateRules(IEnumerable snapshots) - { - foreach (var snapshot in snapshots) - AddOrUpdateRules(snapshot); - } - //TODO: finish and validate private void AddOrUpdateRules(Snapshot snapshot) { // add new Rules - snapshot.ParseRawRules(); - var preExistingSnapshotRules = filterListsDbContext.Rules.Where(x => snapshot.RawRules.Contains(x.Raw)); + var preExistingSnapshotRules = dbContext.Rules.Where(x => snapshot.RawRules.Contains(x.Raw)); var newSnapshotRulesRaw = snapshot.RawRules.Except(preExistingSnapshotRules.Select(x => x.Raw)); var newSnapshotRules = newSnapshotRulesRaw.Select(newSnapshotRuleRaw => new Rule {Raw = newSnapshotRuleRaw}); - filterListsDbContext.Rules.AddRange(newSnapshotRules); + dbContext.Rules.AddRange(newSnapshotRules); // remove deleted FilterListRules var preExistingFilterListRules = - filterListsDbContext.FilterListRules.Where(x => x.FilterListId == snapshot.FilterListId); + dbContext.FilterListRules.Where(x => x.FilterListId == snapshot.FilterListId); var deletedFilterListRules = preExistingFilterListRules.Where(x => !preExistingSnapshotRules.Select(y => y.Id).Contains(x.RuleId)); - filterListsDbContext.FilterListRules.RemoveRange(deletedFilterListRules); + dbContext.FilterListRules.RemoveRange(deletedFilterListRules); // add new FilterListRules @@ -95,14 +88,14 @@ private void AddOrUpdateRules(Snapshot snapshot) // update UpdatedDateUtc if (newSnapshotRulesRaw.Any() || deletedFilterListRules.Any()) { - var list = filterListsDbContext.FilterLists.FindAsync(snapshot.FilterListId).Result; + var list = dbContext.FilterLists.FindAsync(snapshot.FilterListId).Result; list.UpdatedDateUtc = DateTime.UtcNow; - filterListsDbContext.FilterLists.Update(list); + dbContext.FilterLists.Update(list); } //TODO: update FilterList.ScrapedDateUtc - filterListsDbContext.SaveChangesAsync(); + dbContext.SaveChangesAsync(); } private class FilterListViewUrlDto @@ -113,14 +106,14 @@ private class FilterListViewUrlDto private class Snapshot { - public string Content { private get; set; } - public int FilterListId { get; set; } - public string[] RawRules { get; private set; } - - public void ParseRawRules() + public string Content { - RawRules = Content.Split(new[] {"\r\n", "\r", "\n"}, StringSplitOptions.RemoveEmptyEntries); + set => RawRules = value.Split(new[] {"\r\n", "\r", "\n"}, StringSplitOptions.RemoveEmptyEntries); } + + public int FilterListId { get; set; } + + public string[] RawRules { get; private set; } } } } \ No newline at end of file