This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Helper methods for summarizing contributions and CSV export (browser-laptop#3477) #15
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -275,6 +275,160 @@ Client.prototype.ballots = function (viewingId) { | |
return count | ||
} | ||
|
||
Client.prototype._totalContribution = function (viewingIds) { | ||
var txs = (viewingIds && viewingIds.length | ||
? this.state.transactions.filter(function (tx) { | ||
return (tx && tx.viewingId && viewingIds.indexOf(tx.viewingId) > -1) | ||
}) | ||
: this.state.transactions | ||
) | ||
|
||
var totalContribution = { | ||
satoshis: 0, | ||
fiat: { amount: 0, currency: null }, | ||
fee: 0 | ||
} | ||
|
||
for (var i = txs.length - 1; i >= 0; i--) { | ||
var tx = txs[i] || {} | ||
var txContribution = tx.contribution || {} | ||
|
||
totalContribution.satoshis += 0 || txContribution.satoshis | ||
|
||
if (txContribution.fiat) { | ||
if (!totalContribution.fiat.currency && txContribution.fiat.currency) { | ||
totalContribution.fiat.currency = txContribution.fiat.currency | ||
} | ||
|
||
if (totalContribution.fiat.currency === txContribution.fiat.currency) { | ||
totalContribution.fiat.amount += 0 || (txContribution.fiat && txContribution.fiat.amount) | ||
} else { | ||
throw new Error('Client#_totalContribution cannot handle multiple fiat currencies') | ||
} | ||
} | ||
|
||
totalContribution.fee += 0 || txContribution.fee | ||
} | ||
|
||
return totalContribution | ||
} | ||
|
||
Client.prototype._publisherVoteData = function (viewingIds) { | ||
if (viewingIds && typeof (viewingIds) === 'string') { | ||
viewingIds = [viewingIds] | ||
} | ||
if (viewingIds && !viewingIds.length) { | ||
viewingIds = null | ||
} | ||
|
||
var transactions = ( | ||
viewingIds | ||
? this.state.transactions.filter(function (tx) { | ||
|
||
return tx && tx.viewingId && tx.ballots && (viewingIds.indexOf(tx.viewingId) > -1) | ||
}) | ||
|
||
: this.state.transactions | ||
) | ||
|
||
var publishersWithVotes = {} | ||
var totalVotes = 0 | ||
|
||
for (var i = transactions.length - 1; i >= 0; i--) { | ||
var tx = transactions[i] | ||
var ballots = tx.ballots | ||
|
||
var publishersOnBallot = underscore.keys(ballots) | ||
|
||
for (var j = publishersOnBallot.length -1; j >= 0; j--) { | ||
var publisher = publishersOnBallot[j] | ||
|
||
var voteDataForPublisher = publishersWithVotes[publisher] || {} | ||
|
||
var voteCount = ballots[publisher] | ||
var publisherVotes = (voteDataForPublisher.votes || 0) + voteCount | ||
totalVotes += voteCount | ||
|
||
voteDataForPublisher.votes = publisherVotes | ||
publishersWithVotes[publisher] = voteDataForPublisher | ||
} | ||
|
||
} | ||
|
||
var totalContributionAmountSatoshis = null | ||
var totalContributionAmountFiat = null | ||
var currency = null | ||
|
||
var totalContribution = this._totalContribution(viewingIds) | ||
|
||
if (totalContribution) { | ||
totalContributionAmountSatoshis = totalContributionAmountSatoshis || totalContribution.satoshis | ||
totalContributionAmountFiat = totalContributionAmountFiat || (totalContribution.fiat && totalContribution.fiat.amount) | ||
currency = currency || (totalContribution.fiat && totalContribution.fiat.currency) | ||
} | ||
|
||
for (var publisher in publishersWithVotes) { | ||
var voteDataForPublisher = publishersWithVotes[publisher] | ||
var fraction = voteDataForPublisher.fraction = voteDataForPublisher.votes / totalVotes | ||
|
||
var contribution = voteDataForPublisher.contribution || {} | ||
if (totalContributionAmountSatoshis) { | ||
contribution.satoshis = Math.round(totalContributionAmountSatoshis * fraction) | ||
} | ||
if (totalContributionAmountFiat) { | ||
contribution.fiat = totalContributionAmountFiat * fraction | ||
} | ||
if (currency) { | ||
contribution.currency = currency | ||
} | ||
|
||
voteDataForPublisher.contribution = contribution | ||
|
||
publishersWithVotes[publisher] = voteDataForPublisher | ||
} | ||
|
||
return publishersWithVotes | ||
} | ||
|
||
Client.prototype._transactionByViewingId = function (viewingId) { | ||
if (viewingId) { | ||
for (var i = this.state.transactions.length - 1; i >= 0; i--) { | ||
var curViewingId = this.state.transactions[i] && this.state.transactions[i].viewingId | ||
if (curViewingId && curViewingId === viewingId) { | ||
return this.state.transactions[i] | ||
} | ||
} | ||
} | ||
|
||
return null | ||
} | ||
|
||
Client.prototype._getTransactionCSVText = function (viewingIds) { | ||
return this._getTransactionCSVRows(viewingIds).join('\n') | ||
} | ||
|
||
Client.prototype._getTransactionCSVRows = function (viewingIds) { | ||
let txContribData = this._publisherVoteData(viewingIds) | ||
var publishers = underscore.keys(txContribData) | ||
|
||
var currency = txContribData[publishers[0]].contribution.currency | ||
var headerRow = ['Publisher','Votes','Fraction','BTC', currency].join(',') | ||
|
||
var rows = [headerRow] | ||
|
||
rows = rows.concat(publishers.map(function (pub) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't there be some kind of CSV quoting for the publisher name? |
||
var pubRow = txContribData[pub] | ||
return [pub, | ||
pubRow.votes, | ||
pubRow.fraction, | ||
pubRow.contribution.satoshis / Math.pow(10, 10), | ||
pubRow.contribution.fiat.toFixed(2) + ' ' + pubRow.contribution.currency | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should add a comment such as
|
||
].join(',') | ||
})) | ||
|
||
return rows | ||
} | ||
|
||
Client.prototype.vote = function (publisher, viewingId) { | ||
var i, transaction | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the entry point that users of this module are going to access? it should be the function that doesn't start with a "_" and is defined between
ballots
andvote
, the other functions should be defined between_updatedRules
and_roundtrip
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's
_getTransactionCSVText
, which shouldn't begin with an underscore