From b593f64326d289cb58505c69a6088cfb4e91eb56 Mon Sep 17 00:00:00 2001 From: kubedav Date: Thu, 27 Oct 2016 15:53:51 +0200 Subject: [PATCH] Not deleting irrelevant orders. --- MySQLdb/database_setup.sql | 40 +++++++++++++++++++++++++++++++++++ dataAccess.js | 4 ++-- index.js | 34 ++++++++++++++--------------- sqlBase.js | 18 +++++++++------- test_events/event_unique.json | 2 +- 5 files changed, 70 insertions(+), 28 deletions(-) create mode 100644 MySQLdb/database_setup.sql diff --git a/MySQLdb/database_setup.sql b/MySQLdb/database_setup.sql new file mode 100644 index 0000000..ebbab4b --- /dev/null +++ b/MySQLdb/database_setup.sql @@ -0,0 +1,40 @@ +CREATE SCHEMA `microexchange` ; + +CREATE TABLE `history` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `full` varchar(45) NOT NULL, + `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`id`,`full`) +) ENGINE=InnoDB AUTO_INCREMENT=561 DEFAULT CHARSET=latin1; + +CREATE TABLE `orderbook` ( + `order_id` int(11) NOT NULL AUTO_INCREMENT, + `order_type` varchar(4) NOT NULL, + `product_name` varchar(10) NOT NULL, + `price` int(11) NOT NULL, + `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`order_id`,`order_type`,`price`,`product_name`) +) ENGINE=InnoDB AUTO_INCREMENT=233 DEFAULT CHARSET=latin1; + +CREATE TABLE `order_types` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `type` varchar(4) NOT NULL, + PRIMARY KEY (`id`,`type`) +) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1; + +CREATE TABLE `products` ( + `product_id` int(11) NOT NULL AUTO_INCREMENT, + `product_name` varchar(45) NOT NULL, + PRIMARY KEY (`product_id`,`product_name`) +) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1; + +INSERT INTO `microexchange`.`order_types` +(`type`) +VALUES +('buy'), ('sell'); + +INSERT INTO `microexchange`.`products` +( +`product_name`) +VALUES +('wood'), ('stone'), ('gold'), ('oil'); diff --git a/dataAccess.js b/dataAccess.js index 9456cea..0b9bd1c 100644 --- a/dataAccess.js +++ b/dataAccess.js @@ -76,13 +76,13 @@ exports.deleteLowestBid = function(data, callback) exports.deleteHighestAsk = function(data, callback) { var query = "DELETE FROM orderbook WHERE product_name='" + data.product + "' AND order_type='" + data.command + "' ORDER BY price DESC LIMIT 1"; - + console.log(query); sqlBase.getSingleRecord(query, callback); } // This is actually match making. If this succeeds, it will inform user about successful trade! exports.deleteMatchedOrders = function(data, callback) { - var query = "DELETE t1,t2 FROM orderbook t1, orderbook t2 WHERE t1.product_name = '" + data.product +"' AND t1.product_name = t2.product_name AND t1.price = " + data.price + " AND t1.price = t2.price AND t1.order_type <> t2.order_type"; + var query = "delete from microexchange.orderbook where order_id in ( select order_id from ( (select order_id from microexchange.orderbook where price = " + data.price + " and product_name = '" + data.product +"' and order_type='SELL' limit 1) union (select order_id from microexchange.orderbook where price = " + data.price + " and product_name = '" + data.product +"' and order_type='BUY' limit 1) ) as t1 )" sqlBase.getStaticData(query, callback); } \ No newline at end of file diff --git a/index.js b/index.js index a421bb0..8e3e510 100644 --- a/index.js +++ b/index.js @@ -93,29 +93,29 @@ exports.handler = function(event, context) { var totalOrders = ''; _.forEach(countRows, function(value) { totalOrders = value; - if (totalOrders => market_depth) { - - + if (totalOrders > market_depth) { // SWITCH pro rozeznani, zda-li potrebujeme smazat na buy nebo sell side. switch (data.command) { case "BUY": - da.deleteLowestBid(data, function(err, delRows) { - if (err !== null) { - context.fail(err); - } else { - console.log('Irrelevant BUY orders found!'); - } - }); + da.deleteLowestBid(data, function(err, delRows) { + if (err !== null) { + context.fail(err); + } else { + console.log('Irrelevant BUY orders found!'); + } + }); break; case "SELL": + console.log('Amount of orders in the book: ' + totalOrders); da.deleteHighestAsk(data, function(err, delRows) { - if (err !== null) { - context.fail(err); - } else { - console.log('Irrelevant SELL orders found!'); - } - }); - break; + if (err !== null) { + console.log(err + 'here'); + context.fail(err); + } else { + console.log('Irrelevant SELL orders found!' + delRows); + } + }); + break; } // Konec Switche. diff --git a/sqlBase.js b/sqlBase.js index cf0b3e6..6c69d4c 100644 --- a/sqlBase.js +++ b/sqlBase.js @@ -3,10 +3,10 @@ var mysql = require('mysql'); function connectionStart() { var connection = mysql.createConnection({ - host : 'futuredb.cbhsjvpjrptr.us-west-2.rds.amazonaws.com', + host : 'microexchange.cbhsjvpjrptr.us-west-2.rds.amazonaws.com', database : 'microexchange', - user : 'marty', - password : 'martymarty', + user : 'microadmin', + password : 'micropassword', }); return connection; } @@ -34,8 +34,8 @@ exports.getStaticData = function(sqlQuery, callback) exports.getSingleRecord = function(sqlQuery, callback) { var connection = connectionStart(); - //console.log(callback); - //console.log(sqlQuery); + console.log(callback); + console.log(sqlQuery); connection.connect(); connection.query(sqlQuery, function(err, rows, fields){ if (err) @@ -43,9 +43,11 @@ exports.getSingleRecord = function(sqlQuery, callback) callback(err,null); } else - // console.log('ROWS ' + rows[0]); - // console.log('FIELDS ' + fields[0]); - callback(null, rows[0]); + { + console.log('ROWS ' + rows); + console.log('FIELDS ' + fields[0]); + callback(null, rows[0]); + } }); connection.end(); } \ No newline at end of file diff --git a/test_events/event_unique.json b/test_events/event_unique.json index 9c25cd8..b1ebf5a 100644 --- a/test_events/event_unique.json +++ b/test_events/event_unique.json @@ -1,3 +1,3 @@ { - "text" : "sell iron 99" + "text" : "buy wood 21" } \ No newline at end of file