Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/tools/logger.js b/tools/logger.js
- index 356a91a..c788760 100644
- --- a/tools/logger.js
- +++ b/tools/logger.js
- @@ -1,21 +1,50 @@
- -var bittrex = require('node.bittrex.api');
- +var bittrex = require('node-bittrex-api');
- var argv = require('minimist')(process.argv.slice(2));
- var request = require('request');
- var mysql = require('mysql');
- +var fs = require("fs");
- var connection;
- +var config = loadConfigFile("./loggerConfig.json");
- +if(typeof config == "undefined")
- +{
- + return;
- +}
- +
- var enableBackup = false;
- var iAmASlave = false;
- var isMasterUp = true;
- var listeningPort = 8800;
- +var timeOfCandle = 3000;
- +
- +var updateDb = 0;
- +var updateSocket = 0;
- +
- +var updateDbOrderBook = 0;
- +var updateDbOrderBookOk = 0;
- +var updateSocketOrderBook = 0;
- +
- +var availablePairs = [];
- +var websocketsclient;
- +var websocketsclientSubscribe;
- +
- +var minimumVolumeToSubscribe = 150;
- +var maximumAllowedDrift = 4;
- +var timeBetweenOrderBookChecking = 20; //In seconds
- +
- +var orderBooks = {};
- +var nounce = {};
- +
- +var rescueDbs = [
- + {
- + host: "85.69.196.124",
- + port : 3306,
- + user : "root",
- + password : "",
- + database : 'trading'
- + }
- +];
- if(argv.database)
- {
- @@ -142,24 +171,6 @@ function handleDisconnect() {
- handleDisconnect();
- -var timeOfCandle = 3000;
- -
- -var updateDb = 0;
- -var updateSocket = 0;
- -
- -var updateDbOrderBook = 0;
- -var updateDbOrderBookOk = 0;
- -var updateSocketOrderBook = 0;
- -
- -var availablePairs = [];
- -var websocketsclient;
- -var websocketsclientSubscribe;
- -
- -var minimumVolumeToSubscribe = 150;
- -var maximumAllowedDrift = 4;
- -var timeBetweenOrderBookChecking = 20; //In seconds
- -
- -var orderBooks = {};
- function connectWebSocket(){
- console.log("Connection to websockets");
- @@ -329,11 +340,27 @@ function addToOrderBook(index, data)
- }
- if(typeof(orderBooks[index].pushedOnce) == "undefined")
- {
- - console.log("The orderBooks has not already been pused for " + index);
- + console.log("The orderBooks has not already been pushed for " + index);
- return;
- }
- updateSocketOrderBook += data.Buys.length + data.Sells.length;//+ data.Fills.length;
- + //Control the nouce
- + if(typeof nounce[data.MarketName] == "undefined")
- + {
- + console.log("Creating nouce for "+data.MarketName+
- + ", beginning at "+data.Nounce);
- + nounce[data.MarketName] = data.Nounce - 1;
- + }
- + if((data.Nounce - 1 ) != nounce[data.MarketName])
- + {
- + console.log("There is missing data for " +data.Nounce + " : " +
- + data.Nounce + "-"+ nouce[data.MarketName]);
- + fs.appendFile( "missedNounce.log",data.Nounce + ":"+data.MarketName);
- + }
- + //Update the nouce
- + nounce[data.MarketName] = Number(data.Nounce);
- +
- for(var i in data.Buys)
- {
- let tmpObject = {};
- @@ -342,6 +369,7 @@ function addToOrderBook(index, data)
- tmpObject.type = data.Buys[i].Type;
- tmpObject.rate = data.Buys[i].Rate;
- tmpObject.quantity = data.Buys[i].Quantity;
- + tmpObject.nounce = Number(data.Nounce);
- orderBooks[index].buy.push(tmpObject);
- }
- @@ -353,6 +381,7 @@ function addToOrderBook(index, data)
- tmpObject.type = data.Sells[i].Type;
- tmpObject.rate = data.Sells[i].Rate;
- tmpObject.quantity = data.Sells[i].Quantity;
- + tmpObject.nounce = Number(data.Nounce);
- orderBooks[index].sell.push(tmpObject);
- }
- @@ -503,6 +532,54 @@ function createOrderBookSnapshot(index)
- });
- }
- +function getMissingDataFromNounce(nounce, index, connectionsToTry)
- +{
- + console.log("Trying to get missing data for " + pair + " with nounce " + nounce);
- + var connectionToTry = connectionsToTry.shift();
- + if(typeof connectionToTry == "undefined")
- + {
- + console.log("We havent been able to retrieve data, sorry dude");
- + return;
- + }
- + var connectionHandler = mysql.createConnection(connectionsToTry);
- + connectionHandler.connect(function(err)
- + {
- + if(err)
- + {
- + console.log("Cannot connect to rescue db, trying with another one");
- + getMissingDataFromNonce(nounce, index, connectionToTry);
- + return;
- + }
- + console.log("Connected to rescue db, getting data");
- + connectionHandler.query("SELECT * FROM orderBooks WHERE nounce = ? AND name = ?", [nounce, index], function(error, results, fields)
- + {
- + console.log("Got rescue data for " + index + " !");
- +
- + results.forEach((row)=>{
- + connection.query("INSERT INTO orderBooks (name, buyOrSell, type, rate, quantity, date, nounce) VALUES (?,?,?,?,?,?,?)", [row.name, row.buyOrSell, row.type, row.rate, row.quantity, row.date, row.nounce], function(error, results, field)
- + {
- + if(err)
- + {
- + throw err;
- + }
- + });
- + });
- + });
- + });
- +}
- +
- +function loadConfigFile(pathToConfig)
- +{
- + console.log("Loading configuration file");
- + var tempConfig = JSON.parse(fs.readFileSync(pathToConfig, 'utf-8'));
- + if(typeof tempConfig == "undefined")
- + {
- + console.log("Cannot load configuration file");
- + return undefined;
- + }
- + return tempConfig;
- +}
- +
- websocketsclient.serviceHandlers.connected = function(connection)
- {
- console.log("Websocket connected");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement