Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Cure committed Nov 6, 2019
2 parents ccaf3c3 + 7572401 commit 5633585
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 34 deletions.
2 changes: 1 addition & 1 deletion db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const pool = new Pool({
host: process.env.SQLHOST || 'localhost',
port: 5432,
database: 'myfusiondatabase',
password: process.env.SQLPASS || '',
password: process.env.SQLPASS || 'postgres',
max: 5
});

Expand Down
52 changes: 45 additions & 7 deletions db_methods/populate.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ class Populate {
});
}





nodeDeleteDb(id) {
return new Promise(function(resolve, reject) {
db.getClient((err,client,done) => {
Expand Down Expand Up @@ -186,7 +182,7 @@ class Populate {
text: `INSERT INTO ${block_text}`,
values: record
}
console.log(`Inserting blocks...`);
// console.log(`Inserting blocks...`);
client.query(query)
.then(res => {
console.log(`Inserted`);
Expand Down Expand Up @@ -233,7 +229,7 @@ class Populate {
text: `INSERT INTO ${chart_text}`,
values: record
}
console.log(`Inserting charts...`);
// console.log(`Inserting charts...`);
client.query(query)
.then(res => {
console.log(`Inserted`);
Expand All @@ -253,11 +249,53 @@ class Populate {
console.error(`Query DELETE charts: FAIL ${err}`);
reject(err);
})
}
})
});
}

initDbAndTables() {
return new Promise(function(resolve, reject) {
db.getClient((err,client,done) => {
if (err) {
console.log(err.stack);
console.error('Could not connect to postgres', err);
reject(err);
}
else {

let query = `DELETE FROM charts`;

}
client.query(query)
.then(res => {
//console.log('Overwriting old block database');
let chart_text ='charts(utctime, charts) VALUES($1, $2)';

query = {
text: `INSERT INTO ${chart_text}`,
values: record
}
// console.log(`Inserting charts...`);
client.query(query)
.then(res => {
// console.log(`Inserted`);
client.release();
resolve(1);
return;
})
.catch(err => {
client.release();
console.error(`Query INSERT INTO charts: FAIL ${err}`);
reject(err);
return;
});
})
.catch(err => {
client.release();
console.error(`Query DELETE charts: FAIL ${err}`);
reject(err);
})
}
})
});
}
Expand Down
54 changes: 28 additions & 26 deletions wsclient/wsclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,62 +21,64 @@ var connect = function(){
let timestamp = 0;
let epoch0time = new Date(0);
utctime = epoch0time.toISOString();
pop.initDbAndTables();



wsclient.onopen = () => {
console.log(new Date(), ` WebSocket Client Connected`);
// console.log(new Date(), ` WebSocket Client Connected`);
};


wsclient.onerror = function() {
console.log(new Date(),` Connection Error`);
// console.log(new Date(),` Connection Error`);
};

wsclient.onmessage = async(data) => {

//console.log(data.data);
// //console.log(data.data);

now = new Date();

console.log(now, ` Received: ${data.data.length} bytes`);
// console.log(now, ` Received: ${data.data.length} bytes`);
let myData = JSON.parse(data.data);
let currentAction = myData.action;
console.log(`Current action is => ${currentAction}`);
// console.log(`Current action is => ${currentAction}`);


if (currentAction === 'init') {
console.log(`Init. => ${myData.data[0]}`);
// console.log(`Init. => ${myData.data[0]}`);
info = JSON.stringify(myData.data[0]);
};

if (myData.data.id && currentAction === 'block') {
console.log(`Block No. => ${myData.data.block.number}`);
// console.log(`Block No. => ${myData.data.block.number}`);
blockNo = myData.data.block.number;
timestamp = myData.data.block.timestamp;
utctime = new Date(timestamp*1000).toISOString(); // UTC

blockdata = JSON.stringify(myData.data.block);

//console.log(`Height => ${myData.data.height}`);
// //console.log(`Height => ${myData.data.height}`);
let recordBlock = [
utctime,
blockdata
]
/*pop.blockPostDb(recordBlock)
.then( res => {
if (res == 0) {
console.log(`Block data write finished`);
// console.log(`Block data write finished`);
}
})
.catch( err => {
console.log(err.stack);
// console.log(err.stack);
})*/

} // End of currentAction === 'blocks'


if (myData.data.id && currentAction === 'stats') {
console.log(`User ID => ${myData.data.id}`);
// console.log(`User ID => ${myData.data.id}`);
let statdata = JSON.stringify(myData.data);
let recordStats = [
myData.data.id,
Expand All @@ -85,32 +87,32 @@ var connect = function(){
statdata,
info
];
//console.log(recordStats);
// //console.log(recordStats);



//await pop.nodeDeleteDb(recordStats[0])
pop.nodeUpdateDb(recordStats)
.then((res) => {
//console.log(res);
// //console.log(res);
if (res.rowCount == 0) {
pop.nodePostDb(recordStats)
.then( res => {
if (res == 1) {
//console.log(`Posted`);
// //console.log(`Posted`);
}
else {
console.log(`Unidentified return from nodePostDb = ${res}`);
// console.log(`Unidentified return from nodePostDb = ${res}`);
}
})
.catch( err => {
//console.log(err.stack);
console.log(err.detail);
// //console.log(err.stack);
// console.log(err.detail);
return;
})
}
})
.catch( err => {
//console.log(err);
// //console.log(err);
console.error(err.stack);
})

Expand All @@ -126,11 +128,11 @@ var connect = function(){
pop.chartPostDb(recordCharts)
.then( res => {
if (res == 0) {
console.log(`Chart data write finished`);
// console.log(`Chart data write finished`);
}
})
.catch( err => {
console.log(err.stack);
// console.log(err.stack);
})
}

Expand All @@ -142,27 +144,27 @@ var connect = function(){
removeOldNodes(olderThanHours);
pop.nodeDeleteOldNodes(this.olderThanHours)
.then( res => {
console.log(`Removed old nodes ${res}`);
// console.log(`Removed old nodes ${res}`);
})
.catch( err => {
console.log(err.stack);
// console.log(err.stack);
})
}, checkTime)
}

function keepAlive() {
var timeout = 10000;
if (wsclient.readyState == wsclient.OPEN) {
//console.log('Keep alive');
// //console.log('Keep alive');
wsclient.send('');
}
timerId = setTimeout(keepAlive, timeout);
}

wsclient.onclose = function() {
console.log(new Date(), ' Client closed, attempting to restart after a delay');
// console.log(new Date(), ' Client closed, attempting to restart after a delay');
setTimeout(connect, reconnectInterval);
console.log(new Date(), ' Websocket will attempt to reopen in ',reconnectInterval/1000,' seconds');
// console.log(new Date(), ' Websocket will attempt to reopen in ',reconnectInterval/1000,' seconds');
};

function cancelKeepAlive() {
Expand Down

0 comments on commit 5633585

Please sign in to comment.