Feature finished
This commit is contained in:
parent
50a77fdeb0
commit
6d3d537d0d
|
@ -40,13 +40,11 @@ const requestIteration = async(set, divisa, flag, timestamp, amount, resolution)
|
||||||
if(flag) {
|
if(flag) {
|
||||||
opt.uri = `https://apiv2.bitcoinaverage.com/indices/global/history/${divisa}${set[element]}?at=${timestamp}&resolution=${resolution}`;
|
opt.uri = `https://apiv2.bitcoinaverage.com/indices/global/history/${divisa}${set[element]}?at=${timestamp}&resolution=${resolution}`;
|
||||||
let { average, time } = await request(opt);
|
let { average, time } = await request(opt);
|
||||||
if(res.time === undefined) res.time = moment.utc(time, 'YYYY-MM-DD HH:mm:ss').format('DD-MM-YYYY HH:mm Z');
|
|
||||||
res[set[element]] = average * amount;
|
res[set[element]] = average * amount;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
opt.uri = `https://apiv2.bitcoinaverage.com/indices/global/history/${set[element]}${divisa}?at=${timestamp}&resolution=${resolution}`;
|
opt.uri = `https://apiv2.bitcoinaverage.com/indices/global/history/${set[element]}${divisa}?at=${timestamp}&resolution=${resolution}`;
|
||||||
let { average, time } = await request(opt);
|
let { average, time } = await request(opt);
|
||||||
if(res.time === undefined) res.time = moment.utc(time, 'YYYY-MM-DD HH:mm:ss').format('DD-MM-YYYY HH:mm Z');
|
|
||||||
res[set[element]] = amount / average;
|
res[set[element]] = amount / average;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
86
interfaz.js
86
interfaz.js
|
@ -5,6 +5,7 @@ let inquirer = require('inquirer');
|
||||||
let moment = require('moment');
|
let moment = require('moment');
|
||||||
let hasher = require('node-object-hash')
|
let hasher = require('node-object-hash')
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
const addressDB = 'decaCCDB';
|
const addressDB = 'decaCCDB';
|
||||||
|
|
||||||
|
@ -17,6 +18,9 @@ const hashSortCoerce = hasher({ sort: true, coerce: true });
|
||||||
|
|
||||||
//Function to start application
|
//Function to start application
|
||||||
const main = async () => {
|
const main = async () => {
|
||||||
|
if (!fs.existsSync('./receipts')){
|
||||||
|
fs.mkdirSync('./receipts');
|
||||||
|
}
|
||||||
const orbitdb = await OrbitDB.createInstance(ipfs)
|
const orbitdb = await OrbitDB.createInstance(ipfs)
|
||||||
const db = await orbitdb.docs(addressDB, { indexBy: 'CCID' });
|
const db = await orbitdb.docs(addressDB, { indexBy: 'CCID' });
|
||||||
await db.load();
|
await db.load();
|
||||||
|
@ -36,8 +40,10 @@ const main = async () => {
|
||||||
}]);
|
}]);
|
||||||
if (mainMenu == 'Show carbon credit records') {
|
if (mainMenu == 'Show carbon credit records') {
|
||||||
let data = db.get('');
|
let data = db.get('');
|
||||||
|
console.log(` ${data.length} records found`);
|
||||||
while (true) {
|
while (true) {
|
||||||
let { showMenu } = await inquirer.prompt([{
|
const fileName = moment.utc().format();
|
||||||
|
let { showMenu } = await inquirer.prompt({
|
||||||
type: 'list',
|
type: 'list',
|
||||||
name: 'showMenu',
|
name: 'showMenu',
|
||||||
message: 'Select an option',
|
message: 'Select an option',
|
||||||
|
@ -45,24 +51,33 @@ const main = async () => {
|
||||||
'Show in terminal',
|
'Show in terminal',
|
||||||
'Save as JSON',
|
'Save as JSON',
|
||||||
'Save as CSV',
|
'Save as CSV',
|
||||||
|
'Save as TXT',
|
||||||
'Back to main menu'
|
'Back to main menu'
|
||||||
]
|
]
|
||||||
}]);
|
});
|
||||||
if (showMenu == 'Show in terminal') {
|
if (showMenu == 'Show in terminal') {
|
||||||
for (element in data) {
|
for (element in data) {
|
||||||
showData(data[element], 0);
|
showData(data[element], 0);
|
||||||
console.log('-------------------------------------\n');
|
console.log('-------------------------------------\n');
|
||||||
}
|
}
|
||||||
} else if (showMenu == 'Save as JSON') {
|
} else if (showMenu == 'Save as JSON') {
|
||||||
fs.writeFileSync(`/tmp/${moment().format()}.json`, JSON.stringify(data));
|
fs.writeFileSync(path.resolve('receipts', `${fileName}.json`), JSON.stringify(data));
|
||||||
console.log(`File saved in /tmp/${moment().format()}.json`);
|
console.log(`File saved in ${path.resolve('receipts', fileName + '.json')}`);
|
||||||
} else if (showMenu == 'Save as CSV') {
|
} else if (showMenu == 'Save as CSV') {
|
||||||
let csv = '';
|
let csv = await JSONToCSV(data);
|
||||||
for (element in data) csv += await JSONToCSV(data[element]);
|
fs.writeFileSync(path.resolve('receipts', `${fileName}.csv`), csv);
|
||||||
fs.writeFileSync(`/tmp/${moment().format()}.csv`, csv);
|
console.log(`File saved in ${path.resolve('receipts', fileName + '.csv')}`);
|
||||||
console.log(`File saved in /tmp/${moment().format()}.csv`);
|
} else if (showMenu == 'Save as TXT') {
|
||||||
|
let stream = fs.createWriteStream(path.resolve('receipts', `${fileName}.txt`));
|
||||||
|
let result = await elemInsert(data);
|
||||||
|
stream.once('open', function (fd) {
|
||||||
|
for (let i = 0; i < result.length; i++) {
|
||||||
|
stream.write(result[i] + '\n');
|
||||||
}
|
}
|
||||||
else if (showMenu == 'Back to main menu') {
|
stream.end();
|
||||||
|
});
|
||||||
|
console.log(`File saved in ${path.resolve('receipts', fileName + '.txt')}`);
|
||||||
|
} else if (showMenu == 'Back to main menu') {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -91,7 +106,7 @@ const main = async () => {
|
||||||
}
|
}
|
||||||
while (true) {
|
while (true) {
|
||||||
await init();
|
await init();
|
||||||
console.clear();
|
//console.clear();
|
||||||
console.log('Select one to modify the field.\n');
|
console.log('Select one to modify the field.\n');
|
||||||
let carbonCreditMenu = await generateData(carbonCredit, 0);
|
let carbonCreditMenu = await generateData(carbonCredit, 0);
|
||||||
let { insertMenu } = await inquirer.prompt([{
|
let { insertMenu } = await inquirer.prompt([{
|
||||||
|
@ -122,7 +137,7 @@ const main = async () => {
|
||||||
serialNoPart=carbonCredit.SerialNo.split('-');
|
serialNoPart=carbonCredit.SerialNo.split('-');
|
||||||
serialFloor=serialNoPart[7];
|
serialFloor=serialNoPart[7];
|
||||||
serialTop=serialNoPart[8];
|
serialTop=serialNoPart[8];
|
||||||
var totalCC = 1+parseInt(serialTop)-parseInt(serialFloor);
|
let totalCC = 1+parseInt(serialTop)-parseInt(serialFloor);
|
||||||
let { input } = await inquirer.prompt([{
|
let { input } = await inquirer.prompt([{
|
||||||
type: 'input',
|
type: 'input',
|
||||||
name: 'input',
|
name: 'input',
|
||||||
|
@ -133,17 +148,24 @@ const main = async () => {
|
||||||
carbonCredit.cancelDate = moment(carbonCredit.cancelDate + ' +0000', 'DD-MM-YYYY HH:mm Z').unix();
|
carbonCredit.cancelDate = moment(carbonCredit.cancelDate + ' +0000', 'DD-MM-YYYY HH:mm Z').unix();
|
||||||
carbonCredit.ccVintageStart = moment(carbonCredit.ccVintageStart + ' +0000', 'DD-MM-YYYY HH:mm Z').unix();
|
carbonCredit.ccVintageStart = moment(carbonCredit.ccVintageStart + ' +0000', 'DD-MM-YYYY HH:mm Z').unix();
|
||||||
carbonCredit.ccVintageEnd = moment(carbonCredit.ccVintageEnd + ' +0000', 'DD-MM-YYYY HH:mm Z').unix();
|
carbonCredit.ccVintageEnd = moment(carbonCredit.ccVintageEnd + ' +0000', 'DD-MM-YYYY HH:mm Z').unix();
|
||||||
|
const fileName = moment().format();
|
||||||
|
let stream = fs.createWriteStream(path.resolve('receipts', fileName + '.txt'));
|
||||||
|
stream.once('open', async function (fd) {
|
||||||
carbonCredit.CCID = await hashSortCoerce.hash(carbonCredit);
|
carbonCredit.CCID = await hashSortCoerce.hash(carbonCredit);
|
||||||
const hash = await db.put(carbonCredit);
|
const hash = await db.put(carbonCredit);
|
||||||
console.log(`Successful insertion, CCID: ${carbonCredit.CCID}, hash: ${hash}.`);
|
stream.write('CCID: ' + carbonCredit.CCID + ' SerialNo: ' + carbonCredit.SerialNo + '\n');
|
||||||
for(var i=parseInt(serialFloor)+1; i<=serialTop; i++){
|
for (let i = parseInt(serialFloor) + 1; i <= serialTop; i++) {
|
||||||
var tempCarbonCredit = {...carbonCredit};
|
let tempCarbonCredit = {
|
||||||
|
...carbonCredit
|
||||||
|
};
|
||||||
tempCarbonCredit.SerialNo = await asyncCCSerialNo(serialNoPart, i)
|
tempCarbonCredit.SerialNo = await asyncCCSerialNo(serialNoPart, i)
|
||||||
tempCarbonCredit.CCID = await hashSortCoerce.hash(tempCarbonCredit);
|
tempCarbonCredit.CCID = await hashSortCoerce.hash(tempCarbonCredit);
|
||||||
const hash = await db.put(tempCarbonCredit);
|
const hash = await db.put(tempCarbonCredit);
|
||||||
console.log(`Successful insertion, CCID: ${tempCarbonCredit.CCID}, hash: ${hash}.`);
|
|
||||||
}
|
}
|
||||||
|
stream.end();
|
||||||
|
});
|
||||||
console.log(`Inserted ${totalCC} carbon credits.`);
|
console.log(`Inserted ${totalCC} carbon credits.`);
|
||||||
|
console.log(`Receipts saved in ${path.resolve('receipts', fileName + '.txt')}`);
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
console.clear();
|
console.clear();
|
||||||
|
@ -522,6 +544,14 @@ const elemSearch = async (objectSearch) => {
|
||||||
return elems;
|
return elems;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const elemInsert = async (objectShow) => {
|
||||||
|
let elems = [];
|
||||||
|
for (let key in objectShow) {
|
||||||
|
elems.push('CCID: ' + objectShow[key]['CCID'] + ' SerialNumber: ' + objectShow[key]['SerialNo']);
|
||||||
|
}
|
||||||
|
return elems;
|
||||||
|
}
|
||||||
|
|
||||||
const generateData = async (data, n) => {
|
const generateData = async (data, n) => {
|
||||||
let menu = [];
|
let menu = [];
|
||||||
let tab = ' '.repeat(n);
|
let tab = ' '.repeat(n);
|
||||||
|
@ -583,14 +613,28 @@ const showErrorData = async (data) => {
|
||||||
|
|
||||||
//Parse json to csv
|
//Parse json to csv
|
||||||
const JSONToCSV = async (data, n) => {
|
const JSONToCSV = async (data, n) => {
|
||||||
let csv = '';
|
let csv;
|
||||||
for (element in data) {
|
for (element in data[0]) {
|
||||||
if (typeof (data[element]) == 'object') {
|
console.log(typeof(data[0][element]));
|
||||||
validateInsertion(data[element]);
|
console.log(data[0][element]);
|
||||||
|
if (typeof data[0][element] == 'object') {
|
||||||
|
console.log('Entro aqui')
|
||||||
|
for(subelement in data[0][element]) csv += `${element} - ${subelement}, `;
|
||||||
}
|
}
|
||||||
else csv += `${data[element]}, `
|
else csv += `${element}, `;
|
||||||
|
}
|
||||||
|
csv = csv.slice(0, -2);
|
||||||
|
csv += '\n';
|
||||||
|
for(record in data) {
|
||||||
|
for (element in data[record]) {
|
||||||
|
if (typeof data[record][element] == 'object') {
|
||||||
|
for(subelement in data[0][element]) csv += `${data[record][element][subelement]}, `;
|
||||||
|
}
|
||||||
|
else csv += `${data[record][element]}, `;
|
||||||
|
}
|
||||||
|
csv = csv.slice(0, -2);
|
||||||
|
csv += '\n';
|
||||||
}
|
}
|
||||||
if (n == 0) csv += '\n';
|
|
||||||
return csv;
|
return csv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue