Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Removes dead csv fields that are not used anymore
Browse files Browse the repository at this point in the history
Resolves #11295

Auditors: @bsclifton

Test Plan:
  • Loading branch information
NejcZdovc committed Oct 5, 2017
1 parent d6447e8 commit 353d704
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 226 deletions.
120 changes: 51 additions & 69 deletions app/common/lib/ledgerExportUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,24 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

const base64Encode = require('../../../js/lib/base64').encode
const underscore = require('underscore')
const moment = require('moment')

/**
* Generates a contribution breakdown by publisher as a CSV data URL from an array of one or more transactions
* @param {Object[]} transactions - array of transactions
*/
module.exports.transactionsToCSVDataURL = (transactions) => {
const csvText = module.exports.getTransactionCSVText(transactions, null, true)
return 'data:text/csv;base64,' + base64Encode(csvText)
}

/**
* Filter an array of transactions by an array of viewingIds
* @example
* txUtil.getTransactionsByViewingIds(state.transactions, '0ef3a02d-ffdd-41f1-a074-7a7eb1e8c332')
* // [ { viewingId: '0ef3a02d-ffdd-41f1-a074-7a7eb1e8c332',
* // surveyorId: 'DQfCj8PHdIEJOZp9/L+FZcozgvYoIVSjPSdwqRYQDr0',
* // contribution: { fiat: [Object], rates: [Object], satoshis: 813916, fee: 8858 },
* // contribution: { fiat: [Object], rates: [Object], fee: 8858 },
* // ...
* // }]
*
* @param {Object[]} transactions - array of one or more ledger transactions objects (see `client.state.transactions` entries)
* @param {string[]=} viewingIds - OPTIONAL array of one or more viewingIds to filter transactions (single string viewingId supported too)
* if null or undefined, all transactions are returned
*/
module.exports.getTransactionsByViewingIds = (transactions, viewingIds) => {
const getTransactionsByViewingIds = (transactions, viewingIds) => {
if (!transactions) {
return []
}
Expand Down Expand Up @@ -66,17 +56,16 @@ module.exports.getTransactionsByViewingIds = (transactions, viewingIds) => {
* Gives a contribution summary for an array of one or more transactions
* @example
* txUtil.getTotalContribution(client.state.transactions)
* // { satoshis: 1627832, fiat: { amount: 10, currency: 'USD' }, fee: 19900 }
* // { fiat: { amount: 10, currency: 'USD' }, fee: 19900 }
*
* @param {Object[]} transactions - array of one or more ledger transactions objects (see `client.state.transactions` entries)
* @param {string[]} viewingIds - OPTIONAL array/string containing one or more viewingIds to filter by
* if null or undefined, all transactions are used
*/
module.exports.getTotalContribution = (transactions, viewingIds) => {
const txs = module.exports.getTransactionsByViewingIds(transactions, viewingIds)
const getTotalContribution = (transactions, viewingIds) => {
const txs = getTransactionsByViewingIds(transactions, viewingIds)

const totalContribution = {
satoshis: 0,
fiat: { amount: 0, currency: null },
fee: 0
}
Expand All @@ -85,8 +74,6 @@ module.exports.getTotalContribution = (transactions, viewingIds) => {
const tx = txs[i] || {}
const txContribution = tx.contribution || {}

totalContribution.satoshis += 0 || txContribution.satoshis

if (txContribution.fiat) {
if (!totalContribution.fiat.currency && txContribution.fiat.currency) {
totalContribution.fiat.currency = txContribution.fiat.currency
Expand All @@ -113,38 +100,38 @@ module.exports.getTotalContribution = (transactions, viewingIds) => {
* // 'chronicle.com':
* // { votes: 2,
* // fraction: 0.04081632653061224,
* // contribution: { satoshis: 33221, fiat: 0.2040816326530612, currency: 'USD' } },
* // contribution: { fiat: 0.2040816326530612, currency: 'USD' } },
* // 'waitbutwhy.com':
* // { votes: 3,
* // fraction: 0.061224489795918366,
* // contribution: { satoshis: 49832, fiat: 0.30612244897959184, currency: 'USD' } },
* // contribution: { fiat: 0.30612244897959184, currency: 'USD' } },
* // 'archlinux.org':
* // { votes: 1,
* // fraction: 0.02040816326530612,
* // contribution: { satoshis: 16611, fiat: 0.1020408163265306, currency: 'USD' } },
* // contribution: { fiat: 0.1020408163265306, currency: 'USD' } },
* // /.../
* // }
*
* @param {Object[]} transactions - array of transactions
* @param {string[]=} viewingIds - OPTIONAL array/string with one or more viewingIds to filter transactions by (if empty, uses all tx)
**/
module.exports.getPublisherVoteData = (transactions, viewingIds) => {
transactions = module.exports.getTransactionsByViewingIds(transactions, viewingIds)
const getPublisherVoteData = (transactions, viewingIds) => {
transactions = getTransactionsByViewingIds(transactions, viewingIds)

const publishersWithVotes = {}
let totalVotes = 0

for (let i = transactions.length - 1; i >= 0; i--) {
var tx = transactions[i]
var ballots = tx.ballots
const tx = transactions[i]
const ballots = tx.ballots

if (!ballots) {
continue
}

var publishersOnBallot = underscore.keys(ballots)
const publishersOnBallot = underscore.keys(ballots)

for (var j = publishersOnBallot.length - 1; j >= 0; j--) {
for (let j = publishersOnBallot.length - 1; j >= 0; j--) {
let publisher = publishersOnBallot[j]

let voteDataForPublisher = publishersWithVotes[publisher] || {}
Expand All @@ -158,14 +145,12 @@ module.exports.getPublisherVoteData = (transactions, viewingIds) => {
}
}

var totalContributionAmountSatoshis = null
var totalContributionAmountFiat = null
var currency = null
let totalContributionAmountFiat = null
let currency = null

const totalContribution = module.exports.getTotalContribution(transactions)
const totalContribution = getTotalContribution(transactions)

if (totalContribution) {
totalContributionAmountSatoshis = totalContributionAmountSatoshis || totalContribution.satoshis
totalContributionAmountFiat = totalContributionAmountFiat || (totalContribution.fiat && totalContribution.fiat.amount)
currency = currency || (totalContribution.fiat && totalContribution.fiat.currency)
}
Expand All @@ -175,9 +160,6 @@ module.exports.getPublisherVoteData = (transactions, viewingIds) => {
let fraction = voteDataForPublisher.fraction = voteDataForPublisher.votes / totalVotes

let contribution = voteDataForPublisher.contribution || {}
if (totalContributionAmountSatoshis) {
contribution.satoshis = Math.round(totalContributionAmountSatoshis * fraction)
}
if (totalContributionAmountFiat) {
contribution.fiat = totalContributionAmountFiat * fraction
}
Expand All @@ -197,27 +179,27 @@ module.exports.getPublisherVoteData = (transactions, viewingIds) => {
* Generates a contribution breakdown by publisher in an array of CSV rows from an array of transactions
* @example
* txUtil.getTransactionCSVRows(client.state.transactions)
* // [ ['Publisher,Votes,Fraction,BTC,USD'],
* // ['chronicle.com,2,0.04081632653061224,0.0000033221,0.20 USD'],
* // ['waitbutwhy.com,3,0.061224489795918366,0.0000049832,0.31 USD'],
* // ['archlinux.org,1,0.02040816326530612,0.0000016611,0.10 USD'],
* // [ ['Publisher,Votes,Fraction,USD'],
* // ['chronicle.com,2,0.04081632653061224,0.20 USD'],
* // ['waitbutwhy.com,3,0.061224489795918366,0.31 USD'],
* // ['archlinux.org,1,0.02040816326530612,0.10 USD'],
* // /.../
* // ]
*
* @param {Object[]} transactions - array of transactions
* @param {string[]=} viewingIds - OPTIONAL array/string with one or more viewingIds to filter transactions by (if empty, uses all tx)
* @param {boolean=} addTotalRow - OPTIONAL boolean indicating whether to add a TOTALS row (defaults false)
**/
module.exports.getTransactionCSVRows = (transactions, viewingIds, addTotalRow, sortByContribution) => {
let txContribData = module.exports.getPublisherVoteData(transactions, viewingIds)
var publishers = (underscore.keys(txContribData) || [])
const getTransactionCSVRows = (transactions, viewingIds, addTotalRow, sortByContribution) => {
let txContribData = getPublisherVoteData(transactions, viewingIds)
let publishers = (underscore.keys(txContribData) || [])

let publisherSortFunction

if (sortByContribution) {
// sort publishers by contribution
publisherSortFunction = function (a, b) {
var getVotes = function (pubStr) {
const getVotes = function (pubStr) {
return (pubStr && typeof pubStr === 'string' && txContribData[pubStr] && txContribData[pubStr].votes ? parseInt(txContribData[pubStr].votes) : 0)
}
return (getVotes(a) > getVotes(b) ? -1 : 1)
Expand All @@ -234,25 +216,22 @@ module.exports.getTransactionCSVRows = (transactions, viewingIds, addTotalRow, s

const currency = (publishers.length ? txContribData[publishers[0]].contribution.currency : 'USD')

const headerRow = ['Publisher', 'Votes', 'Fraction', 'BTC', currency].join(',')
const headerRow = ['Publisher', 'Votes', 'Fraction', currency].join(',')

var totalsRow = {
let totalsRow = {
label: 'TOTAL',
votes: 0,
fraction: 0,
btc: 0,
fiat: 0
}

var rows = [headerRow]
let rows = [headerRow]

rows = rows.concat(publishers.map(function (pub) {
var pubRow = txContribData[pub]
const pubRow = txContribData[pub]

let rowBTC = pubRow.contribution.satoshis / Math.pow(10, 10)
totalsRow.votes += pubRow.votes
totalsRow.fraction += pubRow.fraction
totalsRow.btc += rowBTC

if (pubRow.contribution.currency === currency) {
totalsRow.fiat += parseFloat(pubRow.contribution.fiat || '0')
Expand All @@ -264,7 +243,6 @@ module.exports.getTransactionCSVRows = (transactions, viewingIds, addTotalRow, s
pub,
pubRow.votes,
pubRow.fraction,
rowBTC,
pubRow.contribution.fiat.toFixed(2) + ' ' + pubRow.contribution.currency
].join(',')
}))
Expand All @@ -275,30 +253,13 @@ module.exports.getTransactionCSVRows = (transactions, viewingIds, addTotalRow, s
totalsRow.label,
totalsRow.votes,
totalsRow.fraction,
totalsRow.btc,
totalsRow.fiat.toFixed(2) + ' ' + currency
].join(','))
}

return rows
}

/**
* Generates a contribution breakdown by publisher in an array of CSV rows from an array of transactions
* @example
* txUtil.getTransactionCSVText(state.transactions)
* // 'Publisher,Votes,Fraction,BTC,USD\nchronicle.com,2,0.04081632653061224,0.0000033221,0.20 USD\nwaitbutwhy.com,3,0.061224489795918366,0.0000049832,0.31 USD\narchlinux.org,1,0.02040816326530612,0.0000016611,0.10 USD /.../'
*
* @param {Object[]} transactions - array of transactions
* @param {string[]=} viewingIds - OPTIONAL array/string with one or more viewingIds to filter transactions by (if empty, uses all tx)
* @param {boolean=} addTotalRow - OPTIONAL boolean indicating whether to add a TOTALS row (defaults false)
*
* returns a CSV with only a header row if input is empty or invalid
**/
module.exports.getTransactionCSVText = (transactions, viewingIds, addTotalRow) => {
return module.exports.getTransactionCSVRows(transactions, viewingIds, addTotalRow).join('\n')
}

/**
* Adds an `exportFilenamePrefix` field to the provided transaction(s)
* of form `Brave_Payments_${YYYY-MM-DD}`, with "_<n>" added for the nth time a date occurs (n > 1)
Expand All @@ -307,7 +268,7 @@ module.exports.getTransactionCSVText = (transactions, viewingIds, addTotalRow) =
*
* @returns {Object[]} transactions (with each element having an added field `exportFilenamePrefix`)
*/
module.exports.addExportFilenamePrefixToTransactions = (transactions) => {
const addExportFilenamePrefixToTransactions = (transactions) => {
transactions = transactions || []

if (!underscore.isArray(transactions)) {
Expand Down Expand Up @@ -337,3 +298,24 @@ module.exports.addExportFilenamePrefixToTransactions = (transactions) => {
return transaction
})
}

const getMethods = () => {
const publicMethods = {
addExportFilenamePrefixToTransactions,
getTransactionCSVRows
}

let privateMethods = {}

if (process.env.NODE_ENV === 'test') {
privateMethods = {
getPublisherVoteData,
getTotalContribution,
getTransactionsByViewingIds
}
}

return Object.assign({}, publicMethods, privateMethods)
}

module.exports = getMethods()
6 changes: 1 addition & 5 deletions app/renderer/components/preferences/payment/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ class HistoryRow extends ImmutableComponent {
return formattedDateFromTimestamp(timestamp, 'YYYY-MM-DD')
}

get satoshis () {
return this.transaction.getIn(['contribution', 'satoshis'])
}

get currency () {
return this.transaction.getIn(['contribution', 'fiat', 'currency'])
}
Expand All @@ -86,7 +82,7 @@ class HistoryRow extends ImmutableComponent {
render () {
return <tr className={css(styles.flex, styles.rowData)}>
<td className={css(styles.flexAlignCenter, styles.column, styles.leftRow, styles.column__narrow)} data-sort={this.timestamp}>{this.formattedDate}</td>
<td className={css(styles.flexAlignCenter, styles.column, styles.column__amount, styles.column__narrow)} data-sort={this.satoshis}>{this.totalAmountStr}</td>
<td className={css(styles.flexAlignCenter, styles.column, styles.column__amount, styles.column__narrow)} data-sort={this.totalAmountStr}>{this.totalAmountStr}</td>
<td className={css(styles.flexAlignCenter, styles.column, styles.column__wide)}>
<a href={`${aboutContributionsUrl}#${this.viewingId}`} rel='noopener' target='_blank'>{this.receiptFileName}</a>
</td>
Expand Down
6 changes: 1 addition & 5 deletions js/about/contributionStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,6 @@ class ContributionStatement extends React.Component {
return this.state.synopsis
}

get publisherSynopsisMap () {
return this.state.publisherSynopsisMap
}

get timestamp () {
if (!this.transaction) {
return null
Expand Down Expand Up @@ -341,7 +337,7 @@ class ContributionStatement extends React.Component {
let verified = publisherSynopsis.verified
let site = row[0]
let fractionStr = (parseFloat(row[2]) * 100).toFixed(2)
let fiatStr = row[4]
let fiatStr = row[3]

return (
<tr className={css(styles.textAlignRight, styles.table__tr)}>
Expand Down
Loading

0 comments on commit 353d704

Please sign in to comment.