extract extension method

This commit is contained in:
Collin M. Barrett 2019-06-30 17:59:47 -05:00
parent a9ea461d47
commit a05fcf0e41
3 changed files with 22 additions and 15 deletions

View file

@ -0,0 +1,18 @@
using System.Collections.Generic;
using System.Linq;
using FilterLists.Agent.Core.Entities;
namespace FilterLists.Agent.Extensions
{
public static class ListInfoExtensions
{
public static IEnumerable<ListInfo> DistributeByHost(this IEnumerable<ListInfo> listInfo)
{
return listInfo.GroupBy(l => l.ViewUrl.Host)
.SelectMany((g, gi) => g.Select((l, i) => new {Index = i, GroupIndex = gi, ListInfo = l}))
.OrderBy(a => a.Index)
.ThenBy(a => a.GroupIndex)
.Select(a => a.ListInfo);
}
}
}

View file

@ -8,8 +8,6 @@
using FilterLists.Agent.Infrastructure.Clients;
using MediatR;
//TODO: upsert into MariaDB Rules table https://stackoverflow.com/questions/15271202/mysql-load-data-infile-with-on-duplicate-key-update
namespace FilterLists.Agent.Features.Archiver
{
public static class DownloadList

View file

@ -1,9 +1,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using FilterLists.Agent.Core.Entities;
using FilterLists.Agent.Extensions;
using MediatR;
namespace FilterLists.Agent.Features.Archiver
@ -36,21 +36,12 @@ protected override async Task Handle(Command request, CancellationToken cancella
async l => await _mediator.Send(new DownloadList.Command(l), cancellationToken),
new ExecutionDataflowBlockOptions {MaxDegreeOfParallelism = MaxDegreeOfParallelism}
);
var orderedListInfo = ShardByHost(request.ListInfo);
foreach (var list in orderedListInfo)
await downloader.SendAsync(list, cancellationToken);
var orderedListInfo = request.ListInfo.DistributeByHost();
foreach (var listInfo in orderedListInfo)
await downloader.SendAsync(listInfo, cancellationToken);
downloader.Complete();
await downloader.Completion;
}
private static IEnumerable<ListInfo> ShardByHost(IEnumerable<ListInfo> listInfo)
{
return listInfo.GroupBy(l => l.ViewUrl.Host)
.SelectMany((g, gi) => g.Select((l, i) => new {Index = i, GroupIndex = gi, ListInfo = l}))
.OrderBy(a => a.Index)
.ThenBy(a => a.GroupIndex)
.Select(a => a.ListInfo);
}
}
}
}