-
-
Save ayende/51a67efff7e625f671d3c357d51d063e to your computer and use it in GitHub Desktop.
This file contains 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 | |
var monthNames = ["January", "February", "March", "April", "May", "June", | |
"July", "August", "September", "October", "November", "December" | |
]; | |
map("Orders", o => { | |
var results = []; | |
for(var i = 0; i < o.Lines.length; i++){ | |
var cur = { | |
Product: o.Lines[i].Product, | |
Year: new Date(o.OrderedAt).getFullYear(), | |
}; | |
var month = monthNames[new Date(o.OrderedAt).getMonth()]; | |
cur[month + " Sales"] = o.Lines[i].Quantity; | |
cur[month + " Totals"] = o.Lines[i].Quantity * o.Lines[i].PricePerUnit * (1-o.Lines[i].Discount) | |
results.push(cur); | |
} | |
return results; | |
}) | |
// reduce | |
groupBy(x => ({Product: x.Product, Year: x.Year})) | |
.aggregate(g=>{ | |
var final = {}; | |
for(var i = 0; i < g.values.length; i++) { | |
var keys = Object.keys(g.values[i]); | |
for(var j = 0; j < keys.length; j++) { | |
if (keys[j].endsWith("Sales") == false && keys[j].endsWith("Totals") == false) | |
continue; | |
final[keys[j]] = (final[keys[j]] || 0) + g.values[i][keys[j]]; | |
} | |
} | |
final.Product = g.key.Product; | |
final.Year = g.key.Year; | |
return final; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment