From 5b6bf4de1b8e6dc9c1a24e9231aa8b3ff97bdc5f Mon Sep 17 00:00:00 2001 From: saubyk <39208279+saubyk@users.noreply.github.com> Date: Wed, 1 Jan 2020 16:23:31 -0500 Subject: [PATCH] transaction count added on fee report --- controllers/lnd/fees.js | 62 ++++++++++++++++++++++++++++++++++++++- controllers/lnd/switch.js | 2 +- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/controllers/lnd/fees.js b/controllers/lnd/fees.js index 058fac0a..e15e01e2 100644 --- a/controllers/lnd/fees.js +++ b/controllers/lnd/fees.js @@ -32,7 +32,28 @@ exports.getFees = (req, res, next) => { } else { body.btc_month_fee_sum = common.convertToBTC(body.month_fee_sum); } - res.status(200).json(body); + + let startDate = new Date(); + let day_start_time = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate() - 1); + day_start_time = Math.round(day_start_time.getTime() / 1000).toString(); + let week_start_time = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate() - 7); + week_start_time = Math.round(week_start_time.getTime() / 1000).toString(); + let month_start_time = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate() - 30); + return getFwdTransactions(Math.round(month_start_time.getTime() / 1000).toString()) + .then((history) => { + let daily_tx_count = history.filter(event => event.timestamp >= day_start_time); + body.daily_tx_count = daily_tx_count && daily_tx_count.length ? daily_tx_count.length : 0; + let weekly_tx_count = history.filter(event => event.timestamp >= week_start_time); + body.weekly_tx_count = weekly_tx_count && weekly_tx_count.length ? weekly_tx_count.length : 0; + body.monthly_tx_count = history && history.length ? history.length : 0; + return res.status(200).json(body); + }) + .catch(err => { + return res.status(500).json({ + message: "Fetching fee failed!", + error: err.error + }); + }); } }) .catch(function (err) { @@ -42,3 +63,42 @@ exports.getFees = (req, res, next) => { }); }); }; + +getFwdTransactions = (start_time) => { + return new Promise(function(resolve, reject) { + options.url = common.getSelLNServerUrl() + '/switch'; + options.form = {}; + let endDate = new Date(); + + if (undefined !== start_time) { + options.form.start_time = start_time; + } + end_time = new Date(endDate.getFullYear(), endDate.getMonth(), endDate.getDate()); + options.form.end_time = Math.round(this.end_time.getTime() / 1000).toString(); + options.form = JSON.stringify(options.form); + logger.info({fileName: 'Fees', msg: 'Switch Post Options: ' + JSON.stringify(options)}); + request.post(options).then((body) => { + try { + logger.info({fileName: 'Fees', msg: 'Switch Post Response: ' + JSON.stringify(body)}); + if(undefined === body || body.error) { + logger.error({fileName: 'Fees', lineNum: 78, msg: 'Switch Post Error: ' + JSON.stringify((undefined === body) ? 'Error From Server!' : body.error)}); + res.status(500).json({ + message: "Switch post failed!", + error: (undefined === body) ? 'Error From Server!' : body.error + }); + } else { + if (undefined !== body.forwarding_events && body.forwarding_events.length > 0) { + body.forwarding_events.forEach(event => { + event.timestamp_str = (undefined === event.timestamp) ? '' : common.convertTimestampToDate(event.timestamp); + }); + body.forwarding_events = common.sortDescByKey(body.forwarding_events, 'timestamp'); + logger.info({fileName: 'Fees', msg: 'Forwarding History Received: ' + JSON.stringify(body)}); + resolve(body.forwarding_events); + } + } + } catch (err) { + resolve(err); + } + }) + }); +} \ No newline at end of file diff --git a/controllers/lnd/switch.js b/controllers/lnd/switch.js index da5e40ab..4a48155e 100644 --- a/controllers/lnd/switch.js +++ b/controllers/lnd/switch.js @@ -24,7 +24,7 @@ exports.forwardingHistory = (req, res, next) => { request.post(options).then((body) => { logger.info({fileName: 'Switch', msg: 'Switch Post Response: ' + JSON.stringify(body)}); if(undefined === body || body.error) { - logger.error({fileName: 'Switch', lineNum: 27, msg: 'Switch Post Erroe: ' + JSON.stringify((undefined === body) ? 'Error From Server!' : body.error)}); + logger.error({fileName: 'Switch', lineNum: 27, msg: 'Switch Post Error: ' + JSON.stringify((undefined === body) ? 'Error From Server!' : body.error)}); res.status(500).json({ message: "Switch post failed!", error: (undefined === body) ? 'Error From Server!' : body.error