Skip to content

Commit

Permalink
Payments features for 1.0
Browse files Browse the repository at this point in the history
Resolves brave#7347

Auditors: @bsclifton @bradleyrichter @mrose17

Test plan:
  • Loading branch information
NejcZdovc committed Mar 20, 2017
1 parent bee2993 commit 8c189cc
Show file tree
Hide file tree
Showing 9 changed files with 416 additions and 152 deletions.
14 changes: 14 additions & 0 deletions app/extensions/brave/img/ledger/icon_pin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions app/extensions/brave/img/ledger/icon_remove.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions app/extensions/brave/locales/en-US/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ site=Site
views=Views
timeSpent=Time Spent
include=Include
actions=Actions
percentage=%
remove=Remove
bravery=Bravery
Expand Down
140 changes: 79 additions & 61 deletions app/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ const doAction = (action) => {
if (publisherInfo._internal.verboseP) console.log('\nupdating ' + publisher + ' stickyP=' + action.value)
updatePublisherInfo()
verifiedP(publisher)
} else if (action.key === 'ledgerPinPercentage') {
if (!synopsis.publishers[publisher]) break
// TODO save to appState if changed
synopsis.publishers[publisher].pinPercentage = action.value
updatePublisherInfo()
}
break

Expand Down Expand Up @@ -1000,7 +1005,7 @@ var stickyP = (publisher) => {
delete synopsis.publishers[publisher].options.stickyP
}

return (result || false)
return (result === undefined || result)
}

var eligibleP = (publisher) => {
Expand All @@ -1022,92 +1027,105 @@ var contributeP = (publisher) => {
}

var synopsisNormalizer = () => {
var i, duration, minP, n, pct, publisher, results, total
var data = []
var scorekeeper = synopsis.options.scorekeeper
// courtesy of https://stackoverflow.com/questions/13483430/how-to-make-rounded-percentages-add-up-to-100#13485888
const roundToTarget = (l, target, property) => {
let off = target - underscore.reduce(l, (acc, x) => { return acc + Math.round(x[property]) }, 0)

results = []
underscore.keys(synopsis.publishers).forEach((publisher) => {
if (!visibleP(publisher)) return
return underscore.sortBy(l, (x) => Math.round(x[property]) - x[property])
.map((x, i) => {
x[property] = Math.round(x[property]) + (off > i) - (i >= (l.length + off))
return x
})
}
const getPublisherData = (result) => {
let duration = result.duration

results.push(underscore.extend({ publisher: publisher }, underscore.omit(synopsis.publishers[publisher], 'window')))
}, synopsis)
results = underscore.sortBy(results, (entry) => { return -entry.scores[scorekeeper] })
n = results.length

total = 0
for (i = 0; i < n; i++) { total += results[i].scores[scorekeeper] }
if (total === 0) return data

pct = []
for (i = 0; i < n; i++) {
publisher = synopsis.publishers[results[i].publisher]
duration = results[i].duration

data[i] = {
rank: i + 1,
verified: results[i].options.verified || false,
site: results[i].publisher,
views: results[i].visits,
let data = {
verified: result.options.verified || false,
site: result.publisher,
views: result.visits,
duration: duration,
daysSpent: 0,
hoursSpent: 0,
minutesSpent: 0,
secondsSpent: 0,
faviconURL: publisher.faviconURL,
score: results[i].scores[scorekeeper]
faviconURL: result.faviconURL,
score: result.scores[scorekeeper],
pinPercentage: result.pinPercentage
}
// HACK: Protocol is sometimes blank here, so default to http:// so we can
// still generate publisherURL.
data[i].publisherURL = (results[i].protocol || 'http:') + '//' + results[i].publisher

pct[i] = Math.round((results[i].scores[scorekeeper] * 100) / total)
data.publisherURL = (result.protocol || 'http:') + '//' + result.publisher

if (duration >= msecs.day) {
data[i].daysSpent = Math.max(Math.round(duration / msecs.day), 1)
data.daysSpent = Math.max(Math.round(duration / msecs.day), 1)
} else if (duration >= msecs.hour) {
data[i].hoursSpent = Math.max(Math.floor(duration / msecs.hour), 1)
data[i].minutesSpent = Math.round((duration % msecs.hour) / msecs.minute)
data.hoursSpent = Math.max(Math.floor(duration / msecs.hour), 1)
data.minutesSpent = Math.round((duration % msecs.hour) / msecs.minute)
} else if (duration >= msecs.minute) {
data[i].minutesSpent = Math.max(Math.round(duration / msecs.minute), 1)
data[i].secondsSpent = Math.round((duration % msecs.minute) / msecs.second)
data.minutesSpent = Math.max(Math.round(duration / msecs.minute), 1)
data.secondsSpent = Math.round((duration % msecs.minute) / msecs.second)
} else {
data[i].secondsSpent = Math.max(Math.round(duration / msecs.second), 1)
data.secondsSpent = Math.max(Math.round(duration / msecs.second), 1)
}

return data
}

// courtesy of https://stackoverflow.com/questions/13483430/how-to-make-rounded-percentages-add-up-to-100#13485888
var foo = (l, target) => {
var off = target - underscore.reduce(l, (acc, x) => { return acc + Math.round(x) }, 0)
let results
let dataPinned = []
let dataUnPinned = []
let dataExcluded = []
let pinnedTotal = 0
let unPinnedTotal = 0
const scorekeeper = synopsis.options.scorekeeper

return underscore.chain(l)
.sortBy((x) => { return Math.round(x) - x })
.map((x, i) => { return Math.round(x) + (off > i) - (i >= (l.length + off)) })
.value()
}
results = []
underscore.keys(synopsis.publishers).forEach((publisher) => {
if (!visibleP(publisher)) return

minP = getSetting(settings.MINIMUM_PERCENTAGE)
pct = foo(pct, 100)
total = 0
for (i = 0; i < n; i++) {
if (pct[i] < 0) pct[i] = 0
if ((minP) && (pct[i] < 1)) {
data = data.slice(0, i)
break
results.push(underscore.extend({ publisher: publisher }, underscore.omit(synopsis.publishers[publisher], 'window')))
}, synopsis)
results = underscore.sortBy(results, (entry) => { return -entry.scores[scorekeeper] })

// move publisher to the correct array and get totals
results.forEach((result) => {
if (result.pinPercentage && result.pinPercentage > 0) {
// pinned
pinnedTotal += result.pinPercentage
dataPinned.push(getPublisherData(result))
} else if (stickyP(result.publisher)) {
// unpinned
unPinnedTotal += result.scores[scorekeeper]
dataUnPinned.push(result)
} else {
// excluded
let publisher = getPublisherData(result)
publisher.percentage = 0
dataExcluded.push(publisher)
}
})

data[i].percentage = pct[i]
total += pct[i]
// round if over 100% of pinned publishers
if (pinnedTotal > 100) {
// dataPinned = roundToTarget(dataPinned, 100) TODO Normalize
dataUnPinned = dataUnPinned.map((publisher) => {
publisher.percentage = 0
return publisher
})
}

for (i = data.length - 1; (total > 100) && (i >= 0); i--) {
if (data[i].percentage < 2) continue
// unpinned publishers
dataUnPinned = dataUnPinned.map((result) => {
let publisher = getPublisherData(result)
publisher.percentage = Math.round((publisher.score / unPinnedTotal) * (100 - pinnedTotal))
return publisher
})

data[i].percentage--
total--
}
// normalize unpinned values
dataUnPinned = roundToTarget(dataUnPinned, (100 - pinnedTotal), 'percentage')

return data
return dataPinned.concat(dataUnPinned, dataExcluded)
}

/*
Expand Down
Loading

0 comments on commit 8c189cc

Please sign in to comment.