UNPKG

bitcore-wallet-service

Version:
197 lines 6.64 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Stats = void 0; const async = __importStar(require("async")); const _ = __importStar(require("lodash")); const moment_1 = __importDefault(require("moment")); const mongodb = __importStar(require("mongodb")); const config_1 = __importDefault(require("../config")); const logger_1 = __importDefault(require("./logger")); const INITIAL_DATE = '2015-01-01'; class Stats { constructor(opts) { opts = opts || {}; this.network = opts.network || 'livenet'; this.coin = opts.coin || 'btc'; this.from = (0, moment_1.default)(opts.from || INITIAL_DATE).format('YYYY-MM-DD'); this.to = (0, moment_1.default)(opts.to).format('YYYY-MM-DD'); } run(cb) { let dbConfig = config_1.default.storageOpts.mongoDb; let uri = dbConfig.uri; uri = uri + 'readPreference=secondaryPreferred'; console.log('Connected to ', uri); if (!dbConfig.dbname) { return cb(new Error('No dbname at config.')); } mongodb.MongoClient.connect(dbConfig.uri, { useUnifiedTopology: true }, (err, client) => { if (err) { return cb(err); } this.db = client.db(dbConfig.dbname); this.client = client; this._getStats((err, stats) => { if (err) return cb(err); this.client.close(err => { if (err) logger_1.default.error('%o', err); return cb(null, stats); }); }); }); } _getStats(cb) { let result = {}; async.series([ next => { this._getNewWallets(next); }, next => { this._getTxProposals(next); }, next => { this._getFiatRates(next); } ], (err, results) => { if (err) return cb(err); result = { newWallets: results[0], txProposals: results[1], fiatRates: results[2] }; return cb(null, result); }); } _getNewWallets(cb) { this.db .collection('stats_wallets') .find({ '_id.network': this.network, '_id.coin': this.coin, '_id.day': { $gte: this.from, $lte: this.to } }) .sort({ '_id.day': 1 }) .toArray((err, results) => { if (err) return cb(err); const stats = { byDay: _.map(results, record => { const day = (0, moment_1.default)(record._id.day).format('YYYYMMDD'); return { day, coin: record._id.coin, value: record._id.value, count: record.count ? record.count : record.value.count }; }) }; return cb(null, stats); }); } _getFiatRates(cb) { this.db .collection('stats_fiat_rates') .find({ '_id.coin': this.coin, '_id.day': { $gte: this.from, $lte: this.to } }) .sort({ '_id.day': 1 }) .toArray((err, results) => { if (err) return cb(err); const stats = { byDay: _.map(results, record => { const day = (0, moment_1.default)(record._id.day).format('YYYYMMDD'); return { day, coin: record._id.coin, value: record.value }; }) }; return cb(null, stats); }); } _getTxProposals(cb) { this.db .collection('stats_txps') .find({ '_id.network': this.network, '_id.coin': this.coin, '_id.day': { $gte: this.from, $lte: this.to } }) .sort({ '_id.day': 1 }) .toArray((err, results) => { if (err) return cb(err); const stats = { nbByDay: [], amountByDay: [] }; _.each(results, record => { const day = (0, moment_1.default)(record._id.day).format('YYYYMMDD'); stats.nbByDay.push({ day, coin: record._id.coin, count: record.count ? record.count : record.value.count }); stats.amountByDay.push({ day, amount: record.amount ? record.amount : record.value.amount }); }); return cb(null, stats); }); } } exports.Stats = Stats; //# sourceMappingURL=stats.js.map