Skip to content

Instantly share code, notes, and snippets.

@ameensol
Last active August 29, 2015 14:17
Show Gist options
  • Save ameensol/26ede58e398d2148912f to your computer and use it in GitHub Desktop.
Save ameensol/26ede58e398d2148912f to your computer and use it in GitHub Desktop.
/* 3/13/2015 NH2
* topTweeters.js
* For a given topic, finds and sets the top (size) tweeters
*/
var stream = require('readable-stream')
var util = require('util')
var TopTweeters = module.exports = function (options) {
options || (options = {})
var hwm = options.hwm || 16
stream.Transform.call(this, {objectMode: true, highWaterMark: hwm})
this.size = options.size || 1000
}
util.inherits(TopTweeters, stream.Transform)
TopTweeters.prototype._transform = function (topic, encoding, callback) {
var lists = topic.Lists
var authorities = lists.map(function (list) {
return list.Authorities
}).reduce(function (bigArray, smallArray) {
return bigArray.concat(smallArray)
})
var rankedAuthorities = authorities.reduce(function (ranked, authority) {
if (ranked[authority.id]) {
ranked[authority.id].rank += 1
} else {
authority.rank = 1
ranked[authority.id] = authority
}
return ranked
}, {})
var sortedAuthorities = Object.keys(rankedAuthorities).map(function (key) {
return rankedAuthorities[key]
}).sort(function (a, b) {
return b.rank - a.rank
})
var topAuthorities = sortedAuthorities.splice(0, this.size)
var topTweeterIds = topAuthorities.map(function (authority) {
return authority.id
})
topic.setTopTweeters(topTweeterIds).then(function () {
callback(null, topic);
})
};
TopTweeters.prototype._flush = function (callback) {
callback();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment