-
-
Save Beanow/f1a6bf5b83b5791d129f64871e0f968f to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env node | |
/* | |
Usage: | |
./compare-scores.js scores-before.json scores-after.json | |
Expecting v0.2.0 sourcecred/cli/scores JSON files. | |
*/ | |
const { resolve } = require("path"); | |
console.log("comparing", process.argv[2], process.argv[3]); | |
const before = require(resolve(process.argv[2])); | |
const after = require(resolve(process.argv[3])); | |
function toBinary(address) { | |
return address.join("\u0000"); | |
} | |
function fromBinary(address) { | |
return address.split("\u0000"); | |
} | |
function sortBy(arr, ...pluckArgs) { | |
const plucks = pluckArgs.length === 0 ? [x => x] : pluckArgs; | |
function sortByCompare(a, b) { | |
for (const pluck of plucks) { | |
const valA = pluck(a); | |
const valB = pluck(b); | |
if (valA > valB) return 1; | |
if (valA < valB) return -1; | |
} | |
return 0; | |
} | |
return [...arr].sort(sortByCompare); | |
} | |
function toMap(input) { | |
return new Map( | |
input[1].users.map(({ address, totalCred }) => [ | |
toBinary(address), | |
totalCred | |
]) | |
); | |
} | |
const beforeMap = toMap(before); | |
const afterMap = toMap(after); | |
const userSet = new Set([...beforeMap.keys(), ...afterMap.keys()]); | |
const finalList = Array.from(userSet).map(address => { | |
const before = beforeMap.get(address) || 0; | |
const after = afterMap.get(address) || 0; | |
const diff = after - before; | |
return { | |
address: fromBinary(address), | |
before: Math.floor(before), | |
after: Math.floor(after), | |
diff: Math.round(diff * 10) / 10 | |
}; | |
}); | |
const withChanges = finalList.filter(({ diff }) => Math.abs(diff) > 1); | |
const byDiff = sortBy(withChanges, ({ diff }) => diff).reverse(); | |
const asMap = new Map( | |
byDiff.map(({ address, diff }) => [ | |
`${address[1]}/${address[address.length - 1]}`, | |
diff | |
]) | |
); | |
function padRight(str, len) { | |
return (String(str) + " ".repeat(len)).slice(0, len); | |
} | |
function padLeft(str, len) { | |
const buffer = " ".repeat(len) + String(str); | |
return buffer.slice(buffer.length - len); | |
} | |
console.log(` | |
| Contributor | Before | After | Change | | |
|---|---:|---:|---:| | |
${byDiff | |
.map( | |
({ address, before, after, diff }) => | |
`|${padRight(address[1] + "/" + address[address.length - 1], 31)}|` + | |
`${padLeft(before, 6)}|` + | |
`${padLeft(after, 6)}|` + | |
`${padLeft(diff, 7)}|` | |
) | |
.join("\n")} | |
`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment