Published
Edited
Sep 23, 2021
1 star
Insert cell
Insert cell
Insert cell
sc = require("sourcecred@latest")
// In other environments, you might need to use:
// sc = require("sourcecred@latest").sourcecred
Insert cell
Insert cell
// Use this URL as a template and simply replace "sourcecred/cred" with your own instance's repo.
instance = sc.instance.readInstance.getNetworkReadInstance(
"https://raw.githubusercontent.com/sourcecred/cred/gh-pages/"
)
Insert cell
Insert cell
credGraph = await instance.readCredGraph()
Insert cell
ledger = await instance.readLedger()
Insert cell
credGrainView = await instance.readCredGrainView()
Insert cell
credrankInput = await instance.readCredrankInput()
Insert cell
Insert cell
// wrapping in a function so that it doesn't add latency
async () => {
// Substitute any sort of file loading you want here
// Note that you'll need the non-compressed json. To get that, run
// yarn sourcecred credrank --no-zip
const ledgerRaw = await (await fetch(
`https://raw.githubusercontent.com/sourcecred/cred/gh-pages/data/ledger.json`
)).text();
// These two will not work, because we do not upload our un-compressed files to github
const credGrainViewRaw = await (await fetch(
`https://raw.githubusercontent.com/sourcecred/cred/gh-pages/output/credGrainView.json`
)).text();
const credGraphRaw = await (await fetch(
`https://raw.githubusercontent.com/sourcecred/cred/gh-pages/output/credGraph.json`
)).text();

// Once you have the file in memory, use our builders:
const parsedLedger = sc.ledger.ledger.Ledger.parse(ledgerRaw);
const parseCredGrainView = sc.core.CredGrainView.fromJSON(
JSON.parse(credGrainViewRaw)
);
const parsedCredGraph = sc.core.credGraph.CredGraph.fromJSON(
JSON.parse(credGraphRaw)
);
}
Insert cell
Insert cell
Insert cell
thenaCred = {
const credPerInterval = credGrainView
.participants()
.find(x => x.identity.name === "Thena").credPerInterval;
const intervalIndex = credGrainView
.intervals()
.findIndex(x => x.endTimeMs > new Date("8/15/2021"));
return credPerInterval[intervalIndex];
}
Insert cell
Insert cell
contributionNodes = {
const thenaId = credGrainView
.participants()
.find(x => x.identity.name === "Thena").identity.id;
const intervalStartTime = credGrainView
.intervals()
.find(x => x.endTimeMs > new Date("8/15/2021")).startTimeMs;
const userWeekNode = Array.from(credGraph.nodes()).find(
node =>
node.address.includes(thenaId) && node.address.includes(intervalStartTime)
);
return Array.from(credGraph.inNeighbors(userWeekNode.address)).map(edge =>
credGraph.node(edge.src)
);
}
Insert cell
Insert cell
credrankOutput = sc.api.credrank.credrank(credrankInput)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more