-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParallelDependantProcessor.cs
More file actions
59 lines (48 loc) · 1.82 KB
/
ParallelDependantProcessor.cs
File metadata and controls
59 lines (48 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using Microsoft.Extensions.Options;
namespace Test.Services;
public class ParallelDependantProcessor(IDataService dataService, IOptions<ParallelSettings> options) : IReportProcessor
{
public void CreateReport(StringWriter writer)
{
using var parallel = new Parallel(options.Value.Parallelism);
var index = 0;
string? prevSourceAccountId = null;
var subIndex = 0;
writer.WriteLine("index,subIndex,transactionId,at,type,amount,sourceAccountId,sourceName,targetAccountId,targetName");
parallel.ForEachAsync(
dataService.GetTransactions().
OrderBy(item => (item.SourceAccountId, item.At)).
WithContext(),
item =>
{
(var transaction, prevSourceAccountId) = (item.current, item.prev?.SourceAccountId);
if (transaction.SourceAccountId != prevSourceAccountId)
{
parallel.PostSync(() =>
{
subIndex = 0;
});
}
var sourceAccount = dataService.GetAccount(transaction.SourceAccountId);
var targetAccount = transaction.TargetAccountId != null ?
dataService.GetAccount(transaction.TargetAccountId) : null;
parallel.PostSync(
(transaction, sourceAccount, targetAccount),
data =>
{
var (transaction, sourceAccount, targetAccount) = data;
++index;
++subIndex;
if (index % 100 == 0)
{
Console.WriteLine(index);
}
writer.WriteLine($"{index},{subIndex},{transaction.Id},{
transaction.At},{transaction.Type},{transaction.Amount},{
transaction.SourceAccountId},{sourceAccount?.Name},{
transaction.TargetAccountId},{targetAccount?.Name}");
prevSourceAccountId = transaction.SourceAccountId;
});
});
}
}