Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Port CSV Generation into master #105

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"mongodb-memory-server": "^8.4.1",
"mongoose": "^6.2.7",
"node-cron": "^3.0.1",
"objects-to-csv": "^1.3.6",
"picomatch": "^2.3.1",
"toml": "^3.0.0",
"winston": "^3.6.0",
Expand Down
45 changes: 45 additions & 0 deletions src/replication/DealReplicationService.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint @typescript-eslint/no-var-requires: "off" */
import bodyParser from 'body-parser';
import config from 'config';
import express, { Express, Request, Response } from 'express';
Expand All @@ -10,6 +11,9 @@ import GetReplicationDetailsResponse from './GetReplicationDetailsResponse';
import { GetReplicationsResponse } from './GetReplicationsResponse';
import UpdateReplicationRequest from './UpdateReplicationRequest';
import { ObjectId } from 'mongodb';
import fs from 'fs-extra';
import path from 'path';
const ObjectsToCsv: any = require('objects-to-csv');// no TS support
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add @types/objects-to-csv as dev dependency so we get TS support


export default class DealReplicationService extends BaseService {

Expand All @@ -21,6 +25,7 @@ export default class DealReplicationService extends BaseService {
this.handleUpdateReplicationRequest = this.handleUpdateReplicationRequest.bind(this);
this.handleListReplicationRequests = this.handleListReplicationRequests.bind(this);
this.handleGetReplicationRequest = this.handleGetReplicationRequest.bind(this);
this.handlePrintCSVReplicationRequest = this.handlePrintCSVReplicationRequest.bind(this);
this.startCleanupHealthCheck = this.startCleanupHealthCheck.bind(this);
if (!this.enabled) {
this.logger.warn('Deal Replication Service is not enabled. Exit now...');
Expand All @@ -37,6 +42,7 @@ export default class DealReplicationService extends BaseService {
this.app.post('/replication/:id', this.handleUpdateReplicationRequest);
this.app.get('/replications', this.handleListReplicationRequests);
this.app.get('/replication/:id', this.handleGetReplicationRequest);
this.app.get('/replication/:id/csv', this.handlePrintCSVReplicationRequest);
}

public start (): void {
Expand Down Expand Up @@ -247,6 +253,45 @@ export default class DealReplicationService extends BaseService {
response.end(JSON.stringify({ id: replicationRequest.id }));
}

private async handlePrintCSVReplicationRequest (request: Request, response: Response) {
const id = request.params['id'];
const replicationRequest = await Datastore.ReplicationRequestModel.findById(id);
if (replicationRequest) {
const deals = await Datastore.DealStateModel.find({
replicationRequestId: id
});
this.logger.info(`Found ${deals.length} deals from replication request ${id}`);
if (deals.length > 0) {
const csvRow = [];
for (let i = 0; i < deals.length; i++) {
const deal = deals[i];
csvRow.push({
miner_id: deal.provider,
deal_cid: deal.dealCid,
filename: `${deal.pieceCid}.car`,
piece_cid: deal.pieceCid,
start_epoch: deal.startEpoch,
full_url: `${replicationRequest.urlPrefix}/${deal.pieceCid}.car`
});
}
const csv = new ObjectsToCsv(csvRow);
const configDir = config.util.getEnv('NODE_CONFIG_DIR');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

output dir should not be hardcoded here. Either, the request support an output directory/filename, or the response should contain the content of CSV so that the caller can decide which file to save the csv to.

await fs.mkdirp(path.join(configDir, 'csv'));
if (await fs.pathExists(path.join(configDir, 'csv'))) {
const filename = `${configDir}/csv/${deals[0].provider}_${id}.csv`;
await csv.toDisk(filename);
response.end(`CSV saved to ${filename}`);
} else {
this.logger.error('CSV dir could not be created');
response.end(`CSV dir could not be created`);
}
}
} else {
console.error(`Could not find replication request ${id}`);
}
response.end();
}

private async cleanupHealthCheck (): Promise<void> {
this.logger.debug(`Cleaning up health check table`);
// Find all active workerId
Expand Down
14 changes: 14 additions & 0 deletions src/singularity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,20 @@ replication.command('resume').description('Resume a paused deal replication requ
}
CliUtil.renderResponse(response.data, options.json);
});

replication.command('csv').description('Write a deal replication result as csv.')
.argument('<id>', 'Existing ID of deal replication request.')
.action(async (id) => {
let response!: AxiosResponse;
try {
const url: string = config.get('connection.deal_replication_service');
response = await axios.get(`${url}/replication/${id}/csv`);
} catch (error) {
CliUtil.renderErrorAndExit(error);
}
CliUtil.renderResponse(response.data, false);
});

program.showSuggestionAfterError();
program.showHelpAfterError('(add --help for additional information)');
program.parse();