Hi,
i wanted to manipulate data of a large csv file upto a certain date.The csv file contains;epoch timestamp,transaction_type,Token,amount as such:
timestamp,transaction_type,token,amount 1571967208,DEPOSIT,BTC,0.298660 1571967200,DEPOSIT,ETH,0.683640 1571967189,WITHDRAWAL,ETH,0.493839 1571967150,DEPOSIT,XRP,0.693272
i tried using if statement to stop line reading of data from csv ,once desired date was reached in the csv and then show the total value until that date.but it turns out it will just not add a particular line where condition is false:
the main code is
const csv= require(‘csv-parser’)const fs=require(‘fs’);const request = require(‘request’);const readline = require(‘readline’).createInterface({input:process.stdin,output:process.stdout})let BTCsum=0;let Esum=0;let Xsum=0;let parseDate=0;//this is to parse input datereadline.question(`Enter Date? \n date must be of valid type in GMT,i.e:01 Jan 1970 00:00:00 GMT\n`,( date => {parseDate=Date.parse(date);//to remove additional 000 in end of parsed date.console.log(`Date in Epoch ${parseDate/1000}`)fs.createReadStream(‘transactions.csv’).pipe(csv()).on(‘data’,(data)=>{if(!(data.timestamp==parseDate/1000)){if(data.token===’BTC’&&data.transaction_type===’DEPOSIT’){BTCsum+=+data.amount;}elseif(data.token===’BTC’&&data.transaction_type===’WITHDRAWAL’){BTCsum-=+data.amount;}elseif(data.token===’XRP’&&data.transaction_type===’DEPOSIT’){Xsum+=+data.amount;}elseif(data.token===’XRP’&&data.transaction_type===’WITHDRAWAL’){Xsum-=+data.amount;}elseif(data.token===’ETH’&&data.transaction_type===’DEPOSIT’){Esum+=+data.amount;}elseif(data.token===’ETH’&&data.transaction_type===’WITHDRAWAL’){Esum-=+data.amount;}} else if((data.timestamp==parseDate/1000)){return;}}).on(‘end’,()=>{console.log({BTCsum,Esum,Xsum})request({url:url},(error,res,body)=>{if(!error&&res.statusCode==200){constparseData=JSON.parse(body);constBTCvalue=parseData[‘BTC’][‘USD’]*BTCsum;constETHvalue=parseData[‘ETH’][‘USD’]*Esum;constXRPvalue=parseData[‘XRP’][‘USD’]*Xsum;console.log({BTCvalue,ETHvalue,XRPvalue});constTotalvalue=BTCvalue+ETHvalue+XRPvalue;console.log(‘TotalValue:USD ‘+Totalvalue.toLocaleString());}})});readline.close();}));
Help
From the structure of your code, the return statement is inside an anonymous arrow function that runs on ‘data’ event. So, it runs for every line of CSV and returning from the function will simply return from the function called on that particular line. What you need to do is pause and end the stream when the date is reached. Here’s how you should do it