Enable score command to load weights (#1913)

Now, if a `weights.json` file is present in the `config/` folder, its
weights will be loaded and used in computing the credResult.

Test plan: We don't yet have proper unit testing for the CLI, so I added
weights to the snapshot. The snapshot weights are pretty silly; 32x for
Discourse posts and 32x for GitHub bots. Load the UI via `yarn start`,
and observe that these weights were persisted in the CredResult.
This commit is contained in:
Dandelion Mané 2020-07-01 11:43:14 -07:00 committed by GitHub
parent f0aeb9425e
commit ba8313e4b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 3 deletions

View File

@ -0,0 +1 @@
[{"type":"sourcecred/weights","version":"0.2.0"},{"edgeWeights":{"E\u0000sourcecred\u0000discourse\u0000authors\u0000post\u0000":{"backwards":1,"forwards":0},"E\u0000sourcecred\u0000discourse\u0000authors\u0000topic\u0000":{"backwards":1,"forwards":0},"E\u0000sourcecred\u0000discourse\u0000createsLike\u0000":{"backwards":0,"forwards":1},"E\u0000sourcecred\u0000discourse\u0000likes\u0000":{"backwards":0,"forwards":1},"E\u0000sourcecred\u0000discourse\u0000references\u0000post\u0000":{"backwards":0,"forwards":0.5},"E\u0000sourcecred\u0000discourse\u0000references\u0000topic\u0000":{"backwards":0,"forwards":0.5},"E\u0000sourcecred\u0000discourse\u0000references\u0000user\u0000":{"backwards":0,"forwards":0.25},"E\u0000sourcecred\u0000discourse\u0000replyTo\u0000":{"backwards":0,"forwards":0.5},"E\u0000sourcecred\u0000discourse\u0000topicContainsPost\u0000":{"backwards":0.125,"forwards":0},"E\u0000sourcecred\u0000github\u0000AUTHORS\u0000":{"backwards":1,"forwards":0.5},"E\u0000sourcecred\u0000github\u0000CORRESPONDS_TO_COMMIT_TYPE\u0000":{"backwards":1,"forwards":1},"E\u0000sourcecred\u0000github\u0000HAS_PARENT\u0000":{"backwards":0.25,"forwards":1},"E\u0000sourcecred\u0000github\u0000MERGED_AS\u0000":{"backwards":1,"forwards":0.5},"E\u0000sourcecred\u0000github\u0000REACTS\u0000HEART\u0000":{"backwards":0,"forwards":2},"E\u0000sourcecred\u0000github\u0000REACTS\u0000HOORAY\u0000":{"backwards":0,"forwards":4},"E\u0000sourcecred\u0000github\u0000REACTS\u0000ROCKET\u0000":{"backwards":0,"forwards":1},"E\u0000sourcecred\u0000github\u0000REACTS\u0000THUMBS_UP\u0000":{"backwards":0,"forwards":1},"E\u0000sourcecred\u0000github\u0000REFERENCES\u0000":{"backwards":0,"forwards":1}},"nodeWeights":{"N\u0000sourcecred\u0000discourse\u0000like\u0000":4,"N\u0000sourcecred\u0000discourse\u0000post\u0000":32,"N\u0000sourcecred\u0000discourse\u0000topic\u0000":0,"N\u0000sourcecred\u0000discourse\u0000user\u0000":0,"N\u0000sourcecred\u0000github\u0000COMMENT\u0000":1,"N\u0000sourcecred\u0000github\u0000COMMIT\u0000":1,"N\u0000sourcecred\u0000github\u0000ISSUE\u0000":2,"N\u0000sourcecred\u0000github\u0000PULL\u0000":4,"N\u0000sourcecred\u0000github\u0000REPO\u0000":4,"N\u0000sourcecred\u0000github\u0000REVIEW\u0000":1,"N\u0000sourcecred\u0000github\u0000USERLIKE\u0000BOT\u0000":32,"N\u0000sourcecred\u0000github\u0000USERLIKE\u0000USER\u0000":0}}]

File diff suppressed because one or more lines are too long

View File

@ -7,7 +7,12 @@ import {join as pathJoin} from "path";
import type {Command} from "./command";
import {makePluginDir, loadInstanceConfig, loadJsonWithDefault} from "./common";
import {fromJSON as weightedGraphFromJSON} from "../core/weightedGraph";
import {type WeightedGraph, merge} from "../core/weightedGraph";
import {
type WeightedGraph,
merge,
overrideWeights,
} from "../core/weightedGraph";
import * as Weights from "../core/weights";
import {LoggingTaskReporter} from "../util/taskReporter";
import {
compute,
@ -47,6 +52,15 @@ const scoreCommand: Command = async (args, std) => {
const graphs = await Promise.all(pluginNames.map(loadGraph));
const combinedGraph = merge(graphs);
// TODO(@decentralion): This is snapshot tested, add unit tests?
const weightsPath = pathJoin(baseDir, "config", "weights.json");
const weights = await loadJsonWithDefault(
weightsPath,
Weights.parser,
Weights.empty
);
const weightedGraph = overrideWeights(combinedGraph, weights);
const plugins = Array.from(config.bundledPlugins.values());
const declarations = plugins.map((x) => x.declaration());
@ -58,7 +72,7 @@ const scoreCommand: Command = async (args, std) => {
Params.defaultParams
);
const credResult = await compute(combinedGraph, params, declarations);
const credResult = await compute(weightedGraph, params, declarations);
const compressed = compressByThreshold(credResult, CRED_THRESHOLD);
const credJSON = stringify(credResultToJSON(compressed));
const outputPath = pathJoin(baseDir, "output", "credResult.json");