Skip to content

Commit

Permalink
localinstance extends readinstance (code dedup) (#3088)
Browse files Browse the repository at this point in the history
* localinstance extends readinstance (code dedup)

* Removed no-longer-needed includes and prettified.
  • Loading branch information
bensodenkamp committed Jun 3, 2021
1 parent ec13926 commit f44ae63
Showing 1 changed file with 29 additions and 223 deletions.
252 changes: 29 additions & 223 deletions packages/sourcecred/src/api/instance/localInstance.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,36 @@
// @flow

import {Instance} from "./instance";
import {type CredrankInput, type CredrankOutput} from "../main/credrank";
import {ReadInstance} from "./readInstance";
import {type CredrankOutput} from "../main/credrank";
import {type GraphInput, type GraphOutput} from "../main/graph";
import {type GrainInput} from "../main/grain";
import {type AnalysisInput, type AnalysisOutput} from "../main/analysis";
import {
type WeightsT,
parser as weightsParser,
empty as emptyWeights,
} from "../../core/weights";
import {type AnalysisOutput} from "../main/analysis";
import {join as pathJoin} from "path";
import stringify from "json-stable-stringify";
import {
type WeightedGraph,
type WeightedGraphJSON,
fromJSON as weightedGraphFromJSON,
toJSON as weightedGraphToJSON,
} from "../../core/weightedGraph";
import {
loadJson,
loadFileWithDefault,
loadJsonWithDefault,
} from "../../util/storage";
import {loadJson} from "../../util/storage";
import {mkdirx} from "../../util/disk";
import {parser as configParser, type InstanceConfig} from "../instanceConfig";
import {Ledger} from "../../core/ledger/ledger";
import {
parser as dependenciesParser,
type DependenciesConfig,
} from "../dependenciesConfig";
import {type Budget} from "../../core/mintBudget";
import {parser as pluginBudgetParser} from "../pluginBudgetConfig";
import {
CredGraph,
parser as credGraphParser,
} from "../../core/credrank/credGraph";
import {CredGrainView, credGrainViewParser} from "../../core/credGrainView";
import {type DependenciesConfig} from "../dependenciesConfig";
import {CredGraph} from "../../core/credrank/credGraph";
import {CredGrainView} from "../../core/credGrainView";
import {DiskStorage} from "../../core/storage/disk";
import {WritableZipStorage} from "../../core/storage/zip";
import {encode} from "../../core/storage/textEncoding";
import * as Combo from "../../util/combo";
import type {PluginDirectoryContext} from "../plugin";
import {type GrainConfig, parser as grainConfigParser} from "../grainConfig";
import {
parser as currencyConfigParser,
type CurrencyDetails,
} from "../currencyConfig";
import {defaultCurrencyConfig} from "../currencyConfig";
import {type CredAccountData} from "../../core/ledger/credAccounts";
import {
type PersonalAttributionsConfig,
personalAttributionsConfigParser,
} from "../config/personalAttributionsConfig";
import {type PersonalAttributionsConfig} from "../config/personalAttributionsConfig";
import {WritableDataStorage} from "../../core/storage";

const DEPENDENCIES_PATH: $ReadOnlyArray<string> = [
"config",
"dependencies.json",
];
const WEIGHT_OVERRIDES_PATH: $ReadOnlyArray<string> = [
"config",
"weights.json",
];
const BUDGET_PATH: $ReadOnlyArray<string> = ["config", "pluginBudgets.json"];
const GRAIN_PATH: $ReadOnlyArray<string> = ["config", "grain.json"];
const CURRENCY_PATH: $ReadOnlyArray<string> = [
"config",
"currencyDetails.json",
];

const PERSONAL_ATTRIBUTIONS_PATH: $ReadOnlyArray<string> = [
"config",
"personalAttributions.json",
Expand All @@ -78,7 +41,6 @@ const CREDGRAPH_PATH: $ReadOnlyArray<string> = [
"credGraph.json.gzip",
];
const CREDGRAINVIEW_PATH: $ReadOnlyArray<string> = ["output", "credGrainView"];
const GRAPHS_DIRECTORY: $ReadOnlyArray<string> = ["output", "graphs"];
const GRAPHS_PATH: $ReadOnlyArray<string> = ["graph.json.gzip"];
const LEDGER_PATH: $ReadOnlyArray<string> = ["data", "ledger.json"];
const ACCOUNTS_PATH: $ReadOnlyArray<string> = ["output", "accounts.json"];
Expand All @@ -87,15 +49,19 @@ const ACCOUNTS_PATH: $ReadOnlyArray<string> = ["output", "accounts.json"];
This is an Instance implementation that reads and writes using relative paths
on the local disk.
*/
export class LocalInstance implements Instance {
export class LocalInstance extends ReadInstance implements Instance {
_baseDirectory: string;
_storage: DiskStorage;
_zipStorage: WritableZipStorage;

_writableStorage: WritableDataStorage;
_writableZipStorage: WritableZipStorage;

constructor(baseDirectory: string) {
const storage = new DiskStorage(baseDirectory);

super(storage);
this._writableStorage = storage;
this._baseDirectory = baseDirectory;
this._storage = new DiskStorage(baseDirectory);
this._zipStorage = new WritableZipStorage(this._storage);
this._writableZipStorage = new WritableZipStorage(storage);
}

//////////////////////////////
Expand All @@ -120,93 +86,6 @@ export class LocalInstance implements Instance {
};
}

async readCredrankInput(): Promise<CredrankInput> {
const [
pluginGraphs,
ledger,
weightOverrides,
dependencies,
pluginsBudget,
personalAttributions,
] = await Promise.all([
this.readPluginGraphs(),
this.readLedger(),
this.readWeightOverrides(),
this.readDependencies(),
this.readPluginsBudget(),
this.readPersonalAttributions(),
]);
return {
pluginGraphs,
ledger,
weightOverrides,
dependencies,
pluginsBudget,
personalAttributions,
};
}

async readGrainInput(): Promise<GrainInput> {
const [
credGraph,
ledger,
grainConfig,
currencyDetails,
] = await Promise.all([
this.readCredGraph(),
this.readLedger(),
this.readGrainConfig(),
this.readCurrencyDetails(),
]);
return {
credGraph,
ledger,
grainConfig,
currencyDetails,
};
}

async readAnalysisInput(): Promise<AnalysisInput> {
const [credGraph, ledger] = await Promise.all([
this.readCredGraph(),
this.readLedger(),
]);
return {
credGraph,
ledger,
};
}

async readWeightedGraphForPlugin(pluginId: string): Promise<WeightedGraph> {
const outputPath = pathJoin(
this.createPluginGraphDirectory(pluginId),
...GRAPHS_PATH
);
const graphJSON = await loadJson(
this._zipStorage,
outputPath,
((Combo.raw: any): Combo.Parser<WeightedGraphJSON>)
);
return weightedGraphFromJSON(graphJSON);
}

async readCredGraph(): Promise<CredGraph> {
const path = pathJoin(...CREDGRAPH_PATH);
return loadJson(this._zipStorage, path, credGraphParser);
}

async readCredGrainView(): Promise<CredGrainView> {
const path = pathJoin(...CREDGRAPH_PATH);
return loadJson(this._zipStorage, path, credGrainViewParser);
}

async readLedger(): Promise<Ledger> {
const path = pathJoin(...LEDGER_PATH);
return loadFileWithDefault(this._storage, path, () =>
new Ledger().serialize()
).then((result) => Ledger.parse(result));
}

async writeGraphOutput(graphOutput: GraphOutput): Promise<void> {
await Promise.all([
this.writeLedger(graphOutput.ledger),
Expand All @@ -232,7 +111,7 @@ export class LocalInstance implements Instance {

async writeLedger(ledger: Ledger): Promise<void> {
const ledgerPath = pathJoin(...LEDGER_PATH);
return this._storage.set(ledgerPath, encode(ledger.serialize()));
return this._writableStorage.set(ledgerPath, encode(ledger.serialize()));
}

//////////////////////////////
Expand All @@ -244,78 +123,6 @@ export class LocalInstance implements Instance {
return loadJson(this._storage, pluginsConfigPath, configParser);
}

async readPluginGraphs(): Promise<Array<WeightedGraph>> {
const instanceConfig = await this.readInstanceConfig();
const pluginNames = Array.from(instanceConfig.bundledPlugins.keys());
return await Promise.all(
pluginNames.map(async (name) => {
const outputDir = this.createPluginDirectory(GRAPHS_DIRECTORY, name);
const outputPath = pathJoin(outputDir, ...GRAPHS_PATH);
const graphJSON = await loadJson(
this._zipStorage,
outputPath,
((Combo.raw: any): Combo.Parser<WeightedGraphJSON>)
);
return weightedGraphFromJSON(graphJSON);
})
);
}

async readWeightOverrides(): Promise<WeightsT> {
const weightsPath = pathJoin(...WEIGHT_OVERRIDES_PATH);
return loadJsonWithDefault(
this._storage,
weightsPath,
weightsParser,
emptyWeights
);
}

async readDependencies(): Promise<DependenciesConfig> {
const dependenciesPath = pathJoin(...DEPENDENCIES_PATH);
return loadJsonWithDefault(
this._storage,
dependenciesPath,
dependenciesParser,
() => []
);
}

async readPluginsBudget(): Promise<Budget | null> {
const budgetPath = pathJoin(...BUDGET_PATH);
return loadJsonWithDefault(
this._storage,
budgetPath,
pluginBudgetParser,
() => null
);
}

async readGrainConfig(): Promise<GrainConfig> {
const grainConfigPath = pathJoin(...GRAIN_PATH);
return loadJson(this._storage, grainConfigPath, grainConfigParser);
}

async readCurrencyDetails(): Promise<CurrencyDetails> {
const currencyDetailsPath = pathJoin(...CURRENCY_PATH);
return loadJsonWithDefault(
this._storage,
currencyDetailsPath,
currencyConfigParser,
defaultCurrencyConfig
);
}

async readPersonalAttributions(): Promise<PersonalAttributionsConfig> {
const path = pathJoin(...PERSONAL_ATTRIBUTIONS_PATH);
return loadJsonWithDefault(
this._storage,
path,
personalAttributionsConfigParser,
() => []
);
}

createPluginDirectory(
components: $ReadOnlyArray<string>,
pluginId: string
Expand All @@ -334,10 +141,6 @@ export class LocalInstance implements Instance {
return pathJoin(...pathComponents);
}

createPluginGraphDirectory(pluginId: string): string {
return this.createPluginDirectory(GRAPHS_DIRECTORY, pluginId);
}

pluginDirectoryContext(pluginName: string): PluginDirectoryContext {
const cacheDir = this.createPluginDirectory(["cache"], pluginName);
const configDir = this.createPluginDirectory(
Expand All @@ -357,13 +160,13 @@ export class LocalInstance implements Instance {
async writeCredGraph(credGraph: CredGraph): Promise<void> {
const cgJson = stringify(credGraph.toJSON());
const outputPath = pathJoin(...CREDGRAPH_PATH);
return this._zipStorage.set(outputPath, encode(cgJson));
return this._writableZipStorage.set(outputPath, encode(cgJson));
}

async writeCredGrainView(credGrainView: CredGrainView): Promise<void> {
const json = stringify(credGrainView.toJSON());
const outputPath = pathJoin(...CREDGRAINVIEW_PATH);
return this._zipStorage.set(outputPath, encode(json));
return this._writableZipStorage.set(outputPath, encode(json));
}

async writePluginGraph(
Expand All @@ -375,14 +178,14 @@ export class LocalInstance implements Instance {
);
const outputDir = this.createPluginGraphDirectory(pluginId);
const outputPath = pathJoin(outputDir, ...GRAPHS_PATH);
return this._zipStorage.set(outputPath, serializedGraph);
return this._writableZipStorage.set(outputPath, serializedGraph);
}

async writeDependenciesConfig(
dependenciesConfig: DependenciesConfig
): Promise<void> {
const dependenciesPath = pathJoin(...DEPENDENCIES_PATH);
return this._storage.set(
return this._writableStorage.set(
dependenciesPath,
stringify(dependenciesConfig, {space: 4})
);
Expand All @@ -392,11 +195,14 @@ export class LocalInstance implements Instance {
config: PersonalAttributionsConfig
): Promise<void> {
const path = pathJoin(...PERSONAL_ATTRIBUTIONS_PATH);
return this._storage.set(path, stringify(config, {space: 4}));
return this._writableStorage.set(path, stringify(config, {space: 4}));
}

async writeCredAccounts(credAccounts: CredAccountData): Promise<void> {
const accountsPath = pathJoin(...ACCOUNTS_PATH);
return this._storage.set(accountsPath, encode(stringify(credAccounts)));
return this._writableStorage.set(
accountsPath,
encode(stringify(credAccounts))
);
}
}

0 comments on commit f44ae63

Please sign in to comment.