diff --git a/public/js/controllers/AccountsController.js b/public/js/controllers/AccountsController.js index 38363b0e1..f19a1f45b 100755 --- a/public/js/controllers/AccountsController.js +++ b/public/js/controllers/AccountsController.js @@ -58,9 +58,6 @@ angular.module('BlocksApp').controller('AccountsController', function($statePara { render: function(data, type, row) { - if (data & 0x1) { - return "Contract"; - } if (data & 0x4) { // user defined account type var accountType = data >> 3; accountType = accountType.toString(); @@ -69,6 +66,9 @@ angular.module('BlocksApp').controller('AccountsController', function($statePara } return "Genesis Alloc"; } + if (data & 0x1) { + return "Contract"; + } return "Account"; }, targets: [2] diff --git a/routes/richlist.js b/routes/richlist.js index 8516645c2..e833465d7 100644 --- a/routes/richlist.js +++ b/routes/richlist.js @@ -3,41 +3,105 @@ * Endpoint for richlist */ +var _ = require('lodash'); var async = require('async'); var mongoose = require('mongoose'); require( '../db.js' ); var Account = mongoose.model('Account'); +// load config.json +var config = { nodeAddr: 'localhost', gethPort: 8545 }; +try { + var local = require('../config.json'); + _.extend(config, local); + console.log('config.json found.'); +} catch (error) { + if (error.code === 'MODULE_NOT_FOUND') { + var local = require('../config.example.json'); + _.extend(config, local); + console.log('No config file found. Using default configuration... (config.example.json)'); + } else { + throw error; + process.exit(1); + } +} + var getAccounts = function(req, res) { var self = getAccounts; if (!self.totalSupply) { - self.totalSupply = -1; - self.timestamp = 0; + self.totalSupply = {}; + self.timestamp = {}; + self.count = {}; + self.cacheTime = config.settings.cacheTime || 30*60; + } + + // search word + var search = req.body.search && req.body.search.value; + var searchArg = {}; + var searchType = -1; + if (search) { + // check user defined accountTypes + if (config.settings.accountTypes) { + try { + var searchre = new RegExp(search, 'i'); + _.forEach(config.settings.accountTypes, function(v, k) { + console.log(v, k, search); + if (v.match(searchre)) { + searchType = (k << 3) | 0x4; // user defined type + return false; + } + }); + } catch (e) { + // ignore + } + } + + // check default account types + if (searchType < 0) { + try { + var searchre = new RegExp(search, 'i'); + if ('account'.match(searchre)) { + searchType = 0; // normal account + } else if ('contract'.match(searchre)) { + searchType = 1; // contract + } + } catch (e) { + // ignore + } + } + if (searchType >= 0) { + searchArg = { type: searchType }; + } } - // check cached totalSupply - if (new Date() - self.timestamp > 30*60*1000) { - self.totalSupply = -1; - self.timestamp = 0; + // check cached totalSupply and count + if (!self.timestamp[searchType] || new Date() - self.timestamp[searchType] > self.cacheTime*1000) { + self.totalSupply[searchType] = -1; + self.timestamp[searchType] = 0; + self.count[searchType] = 0; } // count accounts only once - var count = req.body.recordsTotal || 0; + var count = self.count[searchType] || 0; count = parseInt(count); if (count < 0) { count = 0; } // get totalSupply only once - var queryTotalSupply = self.totalSupply || req.body.totalSupply || null; + var queryTotalSupply = self.totalSupply[searchType] || -1; + + var aggregateArg = []; + if (searchType >= 0) { + aggregateArg.push({ $match: searchArg }); + } + aggregateArg.push({ $group: { _id: null, totalSupply: { $sum: '$balance' } } }); async.waterfall([ function(callback) { if (queryTotalSupply < 0) { - Account.aggregate([ - { $group: { _id: null, totalSupply: { $sum: '$balance' } } } - ]).exec(function(err, docs) { + Account.aggregate(aggregateArg).exec(function(err, docs) { if (err) { callbck(err); return; @@ -45,8 +109,8 @@ var getAccounts = function(req, res) { var totalSupply = docs[0].totalSupply; // update cache - self.timestamp = new Date(); - self.totalSupply = totalSupply; + self.timestamp[searchType] = new Date(); + self.totalSupply[searchType] = totalSupply; callback(null, totalSupply); }); } else { @@ -56,13 +120,14 @@ var getAccounts = function(req, res) { function(totalSupply, callback) { if (!count) { // get the number of all accounts - Account.count({}, function(err, count) { + Account.count(searchArg, function(err, count) { if (err) { callbck(err); return; } count = parseInt(count); + self.count[searchType] = count; callback(null, totalSupply, count); }); } else { @@ -102,7 +167,7 @@ var getAccounts = function(req, res) { data.totalSupply = totalSupply; } - Account.find({}).lean(true).sort(sortOrder).skip(start).limit(limit) + Account.find(searchArg).lean(true).sort(sortOrder).skip(start).limit(limit) .exec(function (err, accounts) { if (err) { res.write(JSON.stringify({"error": true}));