Skip to content

Instantly share code, notes, and snippets.

@resilience-me
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save resilience-me/93f9fefc562012f0383e to your computer and use it in GitHub Desktop.
Save resilience-me/93f9fefc562012f0383e to your computer and use it in GitHub Desktop.
filter_dividend_pathways_by_dividendRate()
// This first function in swarm_redistribution.js filters out the dividend pathway that is
// closest to the dividendRate of the dividend that is being issued.
// Since nodes can change their dividendRates at any time, you might have multiple
// dividend pathways in different dividendRates.
// filter_dividend_pathways_by_dividendRate() filters out if an account has multiple dividendRates,
// and sorts by the rate closest to the dividendRate of the dividend pathway of the account
// that was passed into the function.
filter_dividend_pathways_by_dividendRate(account, currency, dividendRate_X, function(doc_filtered){console.log(doc_filtered)})
function filter_dividend_pathways_by_dividendRate(account, currency, dividendRate_X, callback){
db.collection(account).find({ type:"dividend_pathway", currency: currency, dividendRate: { $lte: dividendRate_X} },function (err, doc){
var filtered_docs = []
async.each(doc, function(obj, callback){
if(JSON.stringify(filtered_docs).indexOf(obj.account) === -1){
filtered_docs.push(obj)
}
else{
// If the account already has a doc, check if dividendRate is lower than the previous,
// and filter out the highest one.
Array.prototype.filterObjects = function(key, value) {
return this.filter(function(x) { return x[key] === value; })
}
var a = filtered_docs.filterObjects("account", obj.account);
if(a[0].dividendRate > obj.dividendRate){
filtered_docs = filtered_docs.filter(function (el) {
return el.account !== obj.account;
});
filtered_docs.push(obj)
}
}
callback();
}, function(err) {
callback(filtered_docs)
});
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment