diff options
| author | schneefux <schneefux+commit@schneefux.xyz> | 2018-05-07 09:06:04 +0200 |
|---|---|---|
| committer | schneefux <schneefux+commit@schneefux.xyz> | 2018-05-07 09:06:04 +0200 |
| commit | 5caf4e48174533710243e8eeaf52567795ec9d01 (patch) | |
| tree | 1396f09bd14c3c4e1af3164ef924f2190b3f949f /backend/reduce.js | |
| parent | ded191658af266a764332b9a1b72d628de3d5b6e (diff) | |
| download | brokentalents-5caf4e48174533710243e8eeaf52567795ec9d01.tar.gz brokentalents-5caf4e48174533710243e8eeaf52567795ec9d01.zip | |
move files to backend subdir, push data in submodule
Diffstat (limited to 'backend/reduce.js')
| -rw-r--r-- | backend/reduce.js | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/backend/reduce.js b/backend/reduce.js new file mode 100644 index 0000000..3ac9da2 --- /dev/null +++ b/backend/reduce.js @@ -0,0 +1,51 @@ +#!/usr/bin/node + +const R = require('ramda'), + utils = require('./utils'); + +function aggregatePayloads(config) { + // Ramda group() only groups neighbors, so sort beforehand + const sorter = R.props(config.group); + const grouper = utils.eqPropses(config.group); + + // sum & count + const aggregator = R.pipe( + R.sortBy(sorter), + R.groupWith(grouper), + R.map( + R.converge(utils.mergeAllArgs, [ + R.pipe( + R.project(config.group), + R.prop(0), + ), + R.pipe( + R.project(R.concat(config.sum, R.of('Count'))), + utils.mergeAllWith(R.add), + utils.unlist, + ), + ]), + ), + ); + + return aggregator; +} + +function deriveStatistics(config) { + const sorter = R.compose(R.defaultTo(0), R.prop('Winner')); + + // divide sums by counts = averages + const transformer = R.map( + R.converge(utils.mergeAllArgs, [ + utils.projectProps(config.group), + utils.divPropsByProp('Count', config.sum), + utils.projectProps(['Count']), + ]) + ); + + return R.compose(transformer, R.sortBy(sorter)); +} + +module.exports = { + aggregatePayloads, + deriveStatistics, +}; |
