Add CLI option for progress bars

Also refactors some of the other output and options.
This commit is contained in:
Râu Cao
2022-11-01 22:10:57 +01:00
parent 77c2cea4a4
commit 0ac7228bfe
4 changed files with 99 additions and 42 deletions

View File

@@ -1,32 +1,55 @@
const debug = require('debug')('ipfs-pinner');
async function promiseAllInBatches(task, items, batchSize) {
let position = 0;
let results = [];
while (position < items.length) {
const itemsForBatch = items.slice(position, position + batchSize);
results = [...results, ...await Promise.allSettled(itemsForBatch.map(item => task(item)))];
position += batchSize;
}
return results;
}
const cliProgress = require('cli-progress');
class IpfsPinner {
constructor (kredits, ipfsApi) {
constructor (kredits, options={}) {
this.kredits = kredits;
this.ipfsApi = ipfsApi || this.kredits.ipfs;
this.ipfsApi = this.kredits.ipfs;
this.progressBars = !!options.progress && !process.env.DEBUG;
if (this.progressBars) {
this.multibar = new cliProgress.MultiBar({
stopOnComplete: true,
clearOnComplete: false,
hideCursor: false,
etaBuffer: 30,
format: '{entity} [{bar}] {percentage}% | ETA: {eta_formatted} | {value}/{total}'
}, cliProgress.Presets.shades_grey);
}
}
async pinAll () {
const contributorHashes = await this._pinAllFromContract(this.kredits.Contributor);
const contributionHashes = await this._pinAllFromContract(this.kredits.Contribution);
const reimbursementHashes = await this._pinAllFromContract(this.kredits.Reimbursement);
console.log('Pinning IPFS documents for all known items...\n')
const cids = [];
const promises = [];
const contracts = [
this.kredits.Contributor,
this.kredits.Contribution,
// TODO uncomment once we have data here
// this.kredits.Reimbursement
]
return contributorHashes.concat(contributionHashes)
.concat(reimbursementHashes);
for (const contract of contracts) {
debug(`Pinning data from ${contract.constructor.name}...`);
const itemCount = await contract.count;
debug('Item count:', itemCount);
let bar;
if (this.progressBars) {
bar = this.multibar.create(itemCount, 0);
bar.update(0, {entity: `${contract.constructor.name}s`.padEnd(14)});
}
promises.push(this._pinAllFromContract(contract, itemCount, bar)
.then(res => { cids.push(...res); }));
}
await Promise.all(promises);
return cids;
}
monitor (callback) {
watch (callback) {
this.kredits.Contribution.on('ContributionAdded', (id) => {
this.kredits.Contribution.getData(id)
.then(data => { return this.ipfsApi.pin(data); })
@@ -44,23 +67,25 @@ class IpfsPinner {
});
}
async _pinAllFromContract (contract) {
debug(`Pinning data from ${contract.constructor.name}...`);
const count = await contract.count;
debug('Item count:', count);
const ids = [...Array(count).keys()].map(i => i+1);
async _pinAllFromContract (contract, itemCount, progressBar) {
const ids = [...Array(itemCount).keys()].map(i => i+1);
const cids = [];
const batchSize = 20;
let position = 0;
async function loadAndPin (id) {
debug(`Loading ${contract.constructor.name} #${id}`);
return contract.getData(id).then(data => {
debug(`Pinning ${contract.constructor.name} #${id}`);
return this.ipfsApi.pin(data).then(cid => cids.push(cid));
});
while (position < itemCount) {
const batchIds = ids.slice(position, position + batchSize);
await Promise.all(batchIds.map(async id => {
const data = await contract.getData(id);
debug(`Loaded ${contract.constructor.name} #${id}`);
const cid = await this.ipfsApi.pin(data);
debug(`Pinned ${contract.constructor.name} #${id} at ${cid}`);
cids.push(cid);
if (this.progressBars) { progressBar.increment(); }
}));
position += batchSize;
}
await promiseAllInBatches(loadAndPin.bind(this), ids, 100);
return cids;
}
}