Browse Source

cache.js code refactoring

all-modes
pbca26 8 years ago
parent
commit
3dc599721b
  1. 10
      compile-dev.sh
  2. 2
      package.json
  3. 313
      routes/cache.js

10
compile-dev.sh

@ -1,10 +0,0 @@
#!/usr/bin/env bash
cd "tmp/Iguana-application"
gulp prod
cd ../../
rm "gui/Iguana-GUI" -rf
cp -rf "tmp/Iguana-application/compiled/prod" "gui/Iguana-GUI"

2
package.json

@ -1,6 +1,6 @@
{ {
"name": "iguana_app", "name": "iguana_app",
"productName": "Iguana", "productName": "Agama",
"version": "0.1.0", "version": "0.1.0",
"description": "iguana Desktop App", "description": "iguana Desktop App",
"main": "main.js", "main": "main.js",

313
routes/cache.js

@ -1,7 +1,6 @@
const fs = require('fs-extra'), const fs = require('fs-extra'),
request = require('request'), request = require('request'),
async = require('async'), async = require('async');
rimraf = require('rimraf');
var cache = {}; var cache = {};
@ -182,7 +181,7 @@ var cacheCallInProgress = false,
cacheGlobLifetime = 300; // sec cacheGlobLifetime = 300; // sec
// TODO: reset calls' states on new /cache call start // TODO: reset calls' states on new /cache call start
cache.all = function(req, res, next) { /*cache.all = function(req, res, next) {
if (req.query.pubkey && !fs.existsSync(cache.iguanaDir + '/shepherd/cache-' + req.query.pubkey + '.json')) { if (req.query.pubkey && !fs.existsSync(cache.iguanaDir + '/shepherd/cache-' + req.query.pubkey + '.json')) {
cacheCallInProgress = false; cacheCallInProgress = false;
} }
@ -491,7 +490,7 @@ cache.all = function(req, res, next) {
'result': 'another call is in progress already' 'result': 'another call is in progress already'
})); }));
} }
} }*/
/* /*
* type: GET * type: GET
@ -552,8 +551,6 @@ cache.one = function(req, res, next) {
} }
} }
}); });
// add timestamp to cache file
// writeCache(Date.now());
} }
}, },
checkTimestamp = function(dateToCheck) { checkTimestamp = function(dateToCheck) {
@ -637,151 +634,207 @@ cache.one = function(req, res, next) {
} }
} }
}); });
request({ function getAddresses(coin) {
url: 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*', request({
method: 'GET' url: 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*',
}, function (error, response, body) { method: 'GET'
if (response && response.statusCode && response.statusCode === 200) { }, function (error, response, body) {
cache.io.emit('messages', { if (response && response.statusCode && response.statusCode === 200) {
'message': { cache.io.emit('messages', {
'shepherd': { 'message': {
'method': 'cache-one', 'shepherd': {
'status': 'in progress', 'method': 'cache-one',
'iguanaAPI': { 'status': 'in progress',
'method': 'getaddressesbyaccount', 'iguanaAPI': {
'coin': coin, 'method': 'getaddressesbyaccount',
'status': 'done', 'coin': coin,
'resp': body 'status': 'done',
'resp': body
}
} }
} }
}
});
outObj.basilisk[coin].addresses = JSON.parse(body).result;
console.log(JSON.parse(body).result);
writeCache();
var addrCount = outObj.basilisk[coin].addresses ? outObj.basilisk[coin].addresses.length : 0;
callStack[coin] = callStack[coin] + addrCount * (coin === 'BTC' || coin === 'SYS' ? callsArray.length - 2 : callsArray.length);
console.log(coin + ' stack len ' + callStack[coin]);
async.each(outObj.basilisk[coin].addresses, function(address) {
var dexUrls = {
'listunspent': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/dex/listunspent?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address,
'listtransactions': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/dex/listtransactions?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address,
'getbalance': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address,
'refresh': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address
},
_dexUrls = {};
for (var a = 0; a < callsArray.length; a++) {
_dexUrls[callsArray[a]] = dexUrls[callsArray[a]];
}
if (coin === 'BTC' || coin === 'SYS') {
delete _dexUrls.refresh;
delete _dexUrls.getbalance;
}
//console.log(JSON.stringify(dexUrls));
console.log(coin + ' address ' + address);
if (!outObj.basilisk[coin][address]) {
outObj.basilisk[coin][address] = {};
writeCache();
}
// set current call status
async.forEachOf(_dexUrls, function(dexUrl, key) {
if (!outObj.basilisk[coin][address][key]) {
outObj.basilisk[coin][address][key] = {};
outObj.basilisk[coin][address][key].status = 'waiting';
} else {
outObj.basilisk[coin][address][key].status = 'waiting';
}
}); });
outObj.basilisk[coin].addresses = JSON.parse(body).result;
console.log(JSON.parse(body).result);
writeCache(); writeCache();
var addrCount = outObj.basilisk[coin].addresses ? outObj.basilisk[coin].addresses.length : 0;
callStack[coin] = callStack[coin] + addrCount * (coin === 'BTC' || coin === 'SYS' ? callsArray.length - 2 : callsArray.length);
console.log(coin + ' stack len ' + callStack[coin]);
async.each(outObj.basilisk[coin].addresses, function(address) {
var dexUrls = {
'listunspent': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/dex/listunspent?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address,
'listtransactions': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/dex/listtransactions?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address,
'getbalance': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address,
'refresh': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address
},
_dexUrls = {};
for (var a = 0; a < callsArray.length; a++) {
_dexUrls[callsArray[a]] = dexUrls[callsArray[a]];
}
if (coin === 'BTC' || coin === 'SYS') {
delete _dexUrls.refresh;
delete _dexUrls.getbalance;
}
//console.log(JSON.stringify(dexUrls));
console.log(coin + ' address ' + address);
if (!outObj.basilisk[coin][address]) {
outObj.basilisk[coin][address] = {};
writeCache();
}
async.forEachOf(_dexUrls, function(dexUrl, key) { // set current call status
var tooEarly = false; async.forEachOf(_dexUrls, function(dexUrl, key) {
if (outObj.basilisk[coin][address][key] && if (!outObj.basilisk[coin][address][key]) {
outObj.basilisk[coin][address][key].timestamp && outObj.basilisk[coin][address][key] = {};
checkTimestamp(outObj.basilisk[coin][address][key].timestamp) < cacheGlobLifetime) { outObj.basilisk[coin][address][key].status = 'waiting';
tooEarly = true; } else {
outObj.basilisk[coin][address][key].status = 'done'; outObj.basilisk[coin][address][key].status = 'waiting';
cache.io.emit('messages', { }
'message': { });
'shepherd': { writeCache();
'method': 'cache-one',
'status': 'in progress', async.forEachOf(_dexUrls, function(dexUrl, key) {
'iguanaAPI': { var tooEarly = false;
'method': key, if (outObj.basilisk[coin][address][key] &&
'coin': coin, outObj.basilisk[coin][address][key].timestamp &&
'address': address, checkTimestamp(outObj.basilisk[coin][address][key].timestamp) < cacheGlobLifetime) {
'status': 'done', tooEarly = true;
'resp': 'too early' outObj.basilisk[coin][address][key].status = 'done';
cache.io.emit('messages', {
'message': {
'shepherd': {
'method': 'cache-one',
'status': 'in progress',
'iguanaAPI': {
'method': key,
'coin': coin,
'address': address,
'status': 'done',
'resp': 'too early'
}
} }
} }
});
}
tooEarly = skipTimeout ? false : tooEarly;
if (!tooEarly) {
cache.io.emit('messages', {
'message': {
'shepherd': {
'method': 'cache-one',
'status': 'in progress',
'iguanaAPI': {
'method': key,
'coin': coin,
'address': address,
'status': 'in progress'
}
}
}
});
outObj.basilisk[coin][address][key].status = 'in progress';
request({
url: dexUrl,
method: 'GET'
}, function (error, response, body) {
if (response && response.statusCode && response.statusCode === 200) {
cache.io.emit('messages', {
'message': {
'shepherd': {
'method': 'cache-one',
'status': 'in progress',
'iguanaAPI': {
'method': key,
'coin': coin,
'address': address,
'status': 'done',
'resp': body
}
}
}
});
outObj.basilisk[coin][address][key] = {};
outObj.basilisk[coin][address][key].data = JSON.parse(body);
outObj.basilisk[coin][address][key].timestamp = Date.now(); // add timestamp
outObj.basilisk[coin][address][key].status = 'done';
console.log(dexUrl);
console.log(body);
callStack[coin]--;
console.log(coin + ' _stack len ' + callStack[coin]);
checkCallStack();
writeCache();
}
});
} else {
console.log(key + ' is fresh, check back in ' + (cacheGlobLifetime - checkTimestamp(outObj.basilisk[coin][address][key].timestamp)) + 's');
callStack[coin]--;
console.log(coin + ' _stack len ' + callStack[coin]);
checkCallStack();
}
});
});
} else {
// TODO: error
}
});
}
if (coin === 'all') {
request({
url: 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/InstantDEX/allcoins?userpass=' + sessionKey,
method: 'GET'
}, function (error, response, body) {
if (response && response.statusCode && response.statusCode === 200) {
cache.io.emit('messages', {
'message': {
'shepherd': {
'method': 'cache-one',
'status': 'in progress',
'iguanaAPI': {
'method': 'allcoins',
'status': 'done',
'resp': body
} }
}); }
} }
tooEarly = skipTimeout ? false : tooEarly; });
if (!tooEarly) { body = JSON.parse(body);
// basilisk coins
if (body.basilisk && body.basilisk.length) {
// get coin addresses
async.each(body.basilisk, function(coin) {
callStack[coin] = 1;
});
async.each(body.basilisk, function(coin) {
outObj.basilisk[coin] = {};
writeCache();
cache.io.emit('messages', { cache.io.emit('messages', {
'message': { 'message': {
'shepherd': { 'shepherd': {
'method': 'cache-one', 'method': 'cache-one',
'status': 'in progress', 'status': 'in progress',
'iguanaAPI': { 'iguanaAPI': {
'method': key, 'method': 'getaddressesbyaccount',
'coin': coin, 'coin': coin,
'address': address,
'status': 'in progress' 'status': 'in progress'
} }
} }
} }
}); });
outObj.basilisk[coin][address][key].status = 'in progress';
request({ getAddresses(coin);
url: dexUrl, });
method: 'GET' }
}, function (error, response, body) { }
if (response && response.statusCode && response.statusCode === 200) { });
cache.io.emit('messages', { } else {
'message': { getAddresses(coin);
'shepherd': { }
'method': 'cache-one',
'status': 'in progress',
'iguanaAPI': {
'method': key,
'coin': coin,
'address': address,
'status': 'done',
'resp': body
}
}
}
});
outObj.basilisk[coin][address][key] = {};
outObj.basilisk[coin][address][key].data = JSON.parse(body);
outObj.basilisk[coin][address][key].timestamp = Date.now(); // add timestamp
outObj.basilisk[coin][address][key].status = 'done';
console.log(dexUrl);
console.log(body);
callStack[coin]--;
console.log(coin + ' _stack len ' + callStack[coin]);
checkCallStack();
writeCache();
}
});
} else {
console.log(key + ' is fresh, check back in ' + (cacheGlobLifetime - checkTimestamp(outObj.basilisk[coin][address][key].timestamp)) + 's');
callStack[coin]--;
console.log(coin + ' _stack len ' + callStack[coin]);
checkCallStack();
}
});
});
} else {
// TODO: error
}
});
} else { } else {
callStack[coin] = callStack[coin] + (coin === 'BTC' ? callsArray.length : callsArray.length - 2); callStack[coin] = callStack[coin] + (coin === 'BTC' ? callsArray.length : callsArray.length - 2);
console.log(coin + ' stack len ' + callStack[coin]); console.log(coin + ' stack len ' + callStack[coin]);

Loading…
Cancel
Save