Created
May 11, 2020 14:52
-
-
Save ayende/43414c084e74739efd51c8eacb46fb00 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// map | |
from p in docs.Payments | |
let fraud = p.isFraud != 0 || p.isFlaggedFraud != 0 | |
select new { | |
Count = 1, | |
Customer = p.nameOrig, | |
Transactions = new []{ | |
new { | |
Type = p.type, | |
To = p.nameDest, | |
Amount = p.amount, | |
MaxAmount = p.amount, | |
Date = p.step, | |
Fraud = fraud, | |
Count = 1, | |
} | |
}, | |
MaxAmount = p.amount, | |
GoodDestinations = fraud ? new object[0] : new object []{p.nameDest}, | |
BadDestinations = !fraud ? new object[0] : new object []{p.nameDest}, | |
} | |
// reduce | |
from result in results | |
group result by result.Customer into g | |
select new { | |
Count = g.Sum(x=>x.Count), | |
MaxAmount = g.Sum(x=>x.MaxAmount), | |
Customer = g.Key, | |
GoodDestinations = g.SelectMany(x=>x.GoodDestinations).Distinct(), | |
BadDestinations = g.SelectMany(x=>x.BadDestinations).Distinct(), | |
Transactions = g.SelectMany(x=>x.Transactions).GroupBy(x=>new { x.To, x.Type }) | |
.Select(gx => new { | |
gx.Key.Type, | |
gx.Key.To, | |
Amount = gx.Sum(x=>x.Amount), | |
Date = gx.OrderBy(x=>x.Date).Last(), | |
Fraud = gx.Any(x=>x.Fraud), | |
Count = gx.Sum(x=>x.Count), | |
MaxAmount = gx.Sum(x=>x.MaxAmount), | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment