This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Port CSV Generation into master #105
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
|
@@ -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 | ||
|
||
export default class DealReplicationService extends BaseService { | ||
|
||
|
@@ -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...'); | ||
|
@@ -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 { | ||
|
@@ -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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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