From 2cd3b907a1d6e90e19b4d3ffdf8d79bd6ecedb5c Mon Sep 17 00:00:00 2001 From: Mario Colque Date: Wed, 15 Jan 2014 12:39:45 -0300 Subject: [PATCH 1/8] added new route for transactions --- config/routes.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/config/routes.js b/config/routes.js index 51a3413c..07086bc9 100644 --- a/config/routes.js +++ b/config/routes.js @@ -18,8 +18,7 @@ module.exports = function(app) { var transactions = require('../app/controllers/transactions'); app.get('/api/tx/:txid', transactions.show); app.param('txid', transactions.transaction); - - app.get('/api/txs', transactions.transactions); + app.get('/api/txs', transactions.list); // Address routes var addresses = require('../app/controllers/addresses'); From 0d09aaec51e07a59f716ef6abeecb687a7503c3e Mon Sep 17 00:00:00 2001 From: Mario Colque Date: Wed, 15 Jan 2014 12:40:24 -0300 Subject: [PATCH 2/8] added init for controller --- public/views/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/views/index.html b/public/views/index.html index e9192bbd..c2c4495c 100644 --- a/public/views/index.html +++ b/public/views/index.html @@ -1,4 +1,4 @@ -
+
From 950ffe01e6a98d984b6b13418d2904e1c717c0df Mon Sep 17 00:00:00 2001 From: Mario Colque Date: Wed, 15 Jan 2014 12:41:59 -0300 Subject: [PATCH 3/8] added limit to find --- app/controllers/blocks.js | 12 +++++----- app/controllers/transactions.js | 39 ++++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/app/controllers/blocks.js b/app/controllers/blocks.js index 21f82d00..fd355c80 100644 --- a/app/controllers/blocks.js +++ b/app/controllers/blocks.js @@ -3,10 +3,8 @@ /** * Module dependencies. */ - var mongoose = require('mongoose'), Block = mongoose.model('Block'); -//, _ = require('lodash'); /** @@ -35,10 +33,13 @@ exports.show = function(req, res) { } }; + /** * List of blocks by date */ exports.list = function(req, res) { + var limit = req.query.limit || 0; + //helper to convert timestamps to yyyy-mm-dd format var formatTimestamp = function (date) { var yyyy = date.getUTCFullYear().toString(); @@ -69,11 +70,10 @@ exports.list = function(req, res) { '$lte': lte } }) + .limit(limit) .exec(function(err, blocks) { if (err) { - res.render('error', { - status: 500 - }); + res.status(500).send(err); } else { res.jsonp({ blocks: blocks, @@ -86,5 +86,3 @@ exports.list = function(req, res) { } }); }; - - diff --git a/app/controllers/transactions.js b/app/controllers/transactions.js index 8146d046..a36c6034 100644 --- a/app/controllers/transactions.js +++ b/app/controllers/transactions.js @@ -1,17 +1,12 @@ 'use strict'; - -var Transaction = require('../models/Transaction'); -var Block = require('../models/Block'); -var Address = require('../models/Address'); -var async = require('async'); -//, _ = require('lodash'); - - - /** * Module dependencies. */ +var Transaction = require('../models/Transaction'); +var Block = require('../models/Block'); +var Address = require('../models/Address'); +var async = require('async'); /** @@ -33,6 +28,7 @@ exports.transaction = function(req, res, next, txid) { /** + * Show transaction */ exports.show = function(req, res) { @@ -41,6 +37,7 @@ exports.show = function(req, res) { } }; + var getTransaction = function(txid, cb) { Transaction.fromIdWithInfo(txid, function(err, tx) { if (err) { @@ -51,9 +48,14 @@ var getTransaction = function(txid, cb) { }); }; -exports.transactions = function(req, res, next) { + +/** + * List of transaction + */ +exports.list = function(req, res, next) { var bId = req.query.block; var aId = req.query.address; + var limit = req.query.limit || 1000; if (bId) { Block.fromHashWithInfo(bId, function(err, block) { @@ -69,7 +71,7 @@ exports.transactions = function(req, res, next) { }); }); } - else { + else if (aId) { var a = Address.new(aId); a.update(function(err) { @@ -84,7 +86,18 @@ exports.transactions = function(req, res, next) { res.jsonp(results); }); }); - + } + else { + Transaction + .find() + .limit(limit) + .sort('-_id') + .exec(function(err, txs) { + if (err) { + res.status(500).send(err); + } else { + res.jsonp(txs); + } + }); } }; - From 908e8410ddb089d8860234479d3e151980da8580 Mon Sep 17 00:00:00 2001 From: Mario Colque Date: Wed, 15 Jan 2014 12:44:17 -0300 Subject: [PATCH 4/8] added limit to index --- public/js/controllers/index.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/public/js/controllers/index.js b/public/js/controllers/index.js index 3af4836b..ef75fc11 100755 --- a/public/js/controllers/index.js +++ b/public/js/controllers/index.js @@ -2,10 +2,10 @@ var TRANSACTION_DISPLAYED = 5; var BLOCKS_DISPLAYED = 5; -angular.module('mystery.system').controller('IndexController', ['$scope', 'Global', 'socket', function($scope, Global, socket) { +angular.module('mystery.system').controller('IndexController', ['$scope', '$routeParams', 'Global', 'socket', 'Blocks', 'Transactions', function($scope, $routeParams, Global, socket, Blocks, Transactions) { $scope.global = Global; - socket.on('tx', function(data) { - var tx = data; + + socket.on('tx', function(tx) { console.log('Transaction received! ' + JSON.stringify(tx)); if ($scope.txs.length === TRANSACTION_DISPLAYED) { $scope.txs.pop(); @@ -13,8 +13,7 @@ angular.module('mystery.system').controller('IndexController', ['$scope', 'Globa $scope.txs.unshift(tx); }); - socket.on('block', function(data) { - var block = data; + socket.on('block', function(block) { console.log('Block received! ' + JSON.stringify(block)); if ($scope.blocks.length === BLOCKS_DISPLAYED) { $scope.blocks.pop(); @@ -22,8 +21,21 @@ angular.module('mystery.system').controller('IndexController', ['$scope', 'Globa $scope.blocks.unshift(block); }); + $scope.index = function() { + Blocks.get({ + limit: BLOCKS_DISPLAYED + }, function(res) { + $scope.blocks = res.blocks; + }); + + Transactions.query({ + limit: TRANSACTION_DISPLAYED + }, function(txs) { + $scope.txs = txs; + }); + }; + $scope.txs = []; $scope.blocks = []; - }]); From 26b0cdc7e4d13e5eb715ec8716290bff72566252 Mon Sep 17 00:00:00 2001 From: Mario Colque Date: Wed, 15 Jan 2014 12:50:55 -0300 Subject: [PATCH 5/8] added sort --- app/controllers/blocks.js | 1 + app/controllers/transactions.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/blocks.js b/app/controllers/blocks.js index fd355c80..a21d52e9 100644 --- a/app/controllers/blocks.js +++ b/app/controllers/blocks.js @@ -71,6 +71,7 @@ exports.list = function(req, res) { } }) .limit(limit) + .sort('-time') .exec(function(err, blocks) { if (err) { res.status(500).send(err); diff --git a/app/controllers/transactions.js b/app/controllers/transactions.js index a36c6034..833fc662 100644 --- a/app/controllers/transactions.js +++ b/app/controllers/transactions.js @@ -91,7 +91,7 @@ exports.list = function(req, res, next) { Transaction .find() .limit(limit) - .sort('-_id') + .sort('-time') .exec(function(err, txs) { if (err) { res.status(500).send(err); From bce481324800a5bd2f2d62fbd84bb6dc349303e6 Mon Sep 17 00:00:00 2001 From: Mario Colque Date: Wed, 15 Jan 2014 13:00:02 -0300 Subject: [PATCH 6/8] added new service for transactions --- public/js/services/transactions.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/public/js/services/transactions.js b/public/js/services/transactions.js index d245dbf0..33c3795c 100644 --- a/public/js/services/transactions.js +++ b/public/js/services/transactions.js @@ -32,3 +32,6 @@ angular.module('mystery.transactions').factory('TransactionsByAddress', ['$resou }); }]); +angular.module('mystery.transactions').factory('Transactions', ['$resource', function($resource) { + return $resource('/api/txs'); +}]); From 3d22ae49be3b4d7fbe6b13eabf0d29558ed0f17e Mon Sep 17 00:00:00 2001 From: Mario Colque Date: Wed, 15 Jan 2014 13:07:07 -0300 Subject: [PATCH 7/8] removed unnecessary service --- public/js/controllers/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/js/controllers/index.js b/public/js/controllers/index.js index ef75fc11..30de7da0 100755 --- a/public/js/controllers/index.js +++ b/public/js/controllers/index.js @@ -2,7 +2,7 @@ var TRANSACTION_DISPLAYED = 5; var BLOCKS_DISPLAYED = 5; -angular.module('mystery.system').controller('IndexController', ['$scope', '$routeParams', 'Global', 'socket', 'Blocks', 'Transactions', function($scope, $routeParams, Global, socket, Blocks, Transactions) { +angular.module('mystery.system').controller('IndexController', ['$scope', 'Global', 'socket', 'Blocks', 'Transactions', function($scope, Global, socket, Blocks, Transactions) { $scope.global = Global; socket.on('tx', function(tx) { From 54b38f45c8d2b88409dead1cfd62b8cdc58782d7 Mon Sep 17 00:00:00 2001 From: Mario Colque Date: Wed, 15 Jan 2014 13:09:57 -0300 Subject: [PATCH 8/8] removed unnecessary var --- public/js/controllers/search.js | 1 - 1 file changed, 1 deletion(-) diff --git a/public/js/controllers/search.js b/public/js/controllers/search.js index 878e713a..43a31992 100644 --- a/public/js/controllers/search.js +++ b/public/js/controllers/search.js @@ -5,7 +5,6 @@ angular.module('mystery.search').controller('SearchController', ['$scope', '$rou $scope.search = function() { var q = $scope.q; - var path; $scope.badQuery = false; $scope.q = '';