From a269897610f6ac6d413f182380672fe67e13f3a6 Mon Sep 17 00:00:00 2001 From: pbca26 Date: Sun, 19 Feb 2017 18:41:34 +0300 Subject: [PATCH] switched foreach with async, parse sessionkey param from url --- routes/shepherd.js | 241 +++++++++++++++++++++++---------------------- 1 file changed, 122 insertions(+), 119 deletions(-) diff --git a/routes/shepherd.js b/routes/shepherd.js index 91d5ded..8a89794 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -12,7 +12,8 @@ const electron = require('electron'), md5 = require('md5'), pm2 = require('pm2'), readLastLines = require('read-last-lines'), - request = require('request'); + request = require('request'), + async = require('async'); Promise = require('bluebird'); @@ -86,135 +87,137 @@ shepherd.get('/appconf', function(req, res, next) { res.send(obj); }); -shepherd.post('/allcoins', function(req, res, next) { - var sessionKey = req.body.sessionKey, - _obj = { - 'msg': 'error', - 'result': 'error' - }, - outObj = { - basilisk: {} - }, - pubkey, - writeCache = function() { - fs.writeFile(iguanaDir + '/cache-' + pubkey + '.json', JSON.stringify(outObj), function(err) { - if (err) { - return console.log(err); - } +var allcoinsInProgress = false; + +shepherd.get('/allcoins', function(req, res, next) { + if (!allcoinsInProgress) { + allcoinsInProgress = true; + + var sessionKey = req.query.userpass, + _obj = { + 'msg': 'error', + 'result': 'error' + }, + outObj = { + basilisk: {} + }, + pubkey, + writeCache = function() { + fs.writeFile(iguanaDir + '/cache-' + pubkey + '.json', JSON.stringify(outObj), function(err) { + if (err) { + return console.log(err); + } - console.log('file ' + iguanaDir + '/cache-' + pubkey + '.json is updated'); - }); - }; + console.log('file ' + iguanaDir + '/cache-' + pubkey + '.json is updated'); + }); + }; - res.end(JSON.stringify({ - 'msg': 'success', - 'result': 'call is initiated' - })); - - console.time('allcoins'); - request({ - url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/SuperNET/activehandle?userpass=' + sessionKey, - method: 'GET' - }, function (error, response, body) { - if (response.statusCode && response.statusCode === 200) { - pubkey = JSON.parse(body).pubkey; - - request({ - url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/InstantDEX/allcoins?userpass=' + sessionKey, - method: 'GET' - }, function (error, response, body) { - if (response.statusCode && response.statusCode === 200) { - body = JSON.parse(body); - // basilisk coins - if (body.basilisk.length) { - // get coin addresses - body.basilisk.forEach(function(coin) { - outObj.basilisk[coin] = {}; - writeCache(); - - request({ - url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*', - method: 'GET' - }, function (error, response, body) { - if (response.statusCode && response.statusCode === 200) { - outObj.basilisk[coin].addresses = JSON.parse(body).result; - writeCache(); - - outObj.basilisk[coin].addresses.forEach(function(address) { - console.log('KMD address ' + address); - outObj.basilisk[coin][address] = {}; + res.end(JSON.stringify({ + 'msg': 'success', + 'result': 'call is initiated' + })); + + /*console.time('allcoins'); + request({ + url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/SuperNET/activehandle?userpass=' + sessionKey, + method: 'GET' + }, function (error, response, body) { + if (response.statusCode && response.statusCode === 200) { + pubkey = JSON.parse(body).pubkey; + + request({ + url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/InstantDEX/allcoins?userpass=' + sessionKey, + method: 'GET' + }, function (error, response, body) { + if (response.statusCode && response.statusCode === 200) { + var callStack = []; + body = JSON.parse(body); + // basilisk coins + if (body.basilisk.length) { + // get coin addresses + async.each(body.basilisk, function(coin) { + outObj.basilisk[coin] = {}; + writeCache(); + + request({ + url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*', + method: 'GET' + }, function (error, response, body) { + if (response.statusCode && response.statusCode === 200) { + outObj.basilisk[coin].addresses = JSON.parse(body).result; writeCache(); - // get listunspent - request({ - url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, - method: 'GET' - }, function (error, response, body) { - if (response.statusCode && response.statusCode === 200) { - outObj.basilisk[coin][address].listunspent = JSON.parse(body); - console.log(body); - - writeCache(); - } - }); - - // get listtransactions - request({ - url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address, - method: 'GET' - }, function (error, response, body) { - if (response.statusCode && response.statusCode === 200) { - outObj.basilisk[coin][address].listtransactions = JSON.parse(body); - console.log(body); - - writeCache(); - } - }); - - // get listtransactions2 - request({ - url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions2?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address, - method: 'GET' - }, function (error, response, body) { - if (response.statusCode && response.statusCode === 200) { - outObj.basilisk[coin][address].listtransactions2 = JSON.parse(body); - console.log(body); - - writeCache(); + async.each(outObj.basilisk[coin].addresses, function(address) { + console.log('KMD address ' + address); + outObj.basilisk[coin][address] = {}; + writeCache(); + + // get listunspent + console.log('http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent' + (coin !== 'BTC' ? '2' : '') + '?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address); + request({ + url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent' + (coin !== 'BTC' ? '2' : '') + '?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, + method: 'GET' + }, function (error, response, body) { + if (response.statusCode && response.statusCode === 200) { + outObj.basilisk[coin][address].listunspent = JSON.parse(body); + console.log(body); + + writeCache(); + } + }); + + // get listtransactions + request({ + url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions' + (coin !== 'BTC' ? '2' : '') + '?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address, + method: 'GET' + }, function (error, response, body) { + if (response.statusCode && response.statusCode === 200) { + outObj.basilisk[coin][address].listtransactions = JSON.parse(body); + console.log(body); + + writeCache(); + } + }); + + if (coin !== 'BTC') { + // get refresh + request({ + url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address, + method: 'GET' + }, function (error, response, body) { + if (response.statusCode && response.statusCode === 200) { + outObj.basilisk[coin][address].refresh = JSON.parse(body); + console.log(body); + + writeCache(); + } + }); } }); - // get refresh - request({ - url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address, - method: 'GET' - }, function (error, response, body) { - if (response.statusCode && response.statusCode === 200) { - outObj.basilisk[coin][address].refresh = JSON.parse(body); - console.log(body); - - writeCache(); - } - }); - }); - - console.timeEnd('allcoins'); // !not an actual time taken by the call! - } else { - // TODO: error - } + console.timeEnd('allcoins'); // !not an actual time taken by the call! + } else { + // TODO: error + } + }); }); - }); + } else { + // TODO: error + } } else { // TODO: error } - } else { - // TODO: error - } - }); - } else { - // TODO: error - } - }); + }); + } else { + // TODO: error + } + });*/ + } else { + res.end(JSON.stringify({ + 'msg': 'error', + 'result': 'another call is in progress already' + })); + } }); shepherd.post('/debuglog', function(req, res) {