Skip to content

Commit

Permalink
Merge pull request bitpay#2394 from micahriggan/fix/daily-tx-count
Browse files Browse the repository at this point in the history
Fixing daily tx count to work with all chains
  • Loading branch information
matiu authored Sep 17, 2019
2 parents b396886 + a72b549 commit 2a18ba9
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions packages/bitcore-node/src/routes/api/stats.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
import { Request, Response } from 'express';
import { ChainStateProvider } from '../../providers/chain-state';
import { SetCache, CacheTimes } from '../middleware';
const router = require('express').Router({ mergeParams: true });

router.get('/', async function(_: Request, res: Response) {
return res.send(404);
});

let cacheThroughTruncatedDate;
let cachedDailyTransactions;
router.get('/daily-transactions', async function(req: Request, res: Response) {
const { chain, network } = req.params;
const truncatedUTC = new Date().toISOString().split('T')[0];
if (truncatedUTC === cacheThroughTruncatedDate) {
return res.json(cachedDailyTransactions);
}
try {
cachedDailyTransactions = await ChainStateProvider.getDailyTransactions({ chain, network });
if (!cachedDailyTransactions) {
let dailyTxs = await ChainStateProvider.getDailyTransactions({ chain, network });
if (!dailyTxs) {
return res.send(500);
}
cacheThroughTruncatedDate = truncatedUTC;
return res.json(cachedDailyTransactions);
SetCache(res, CacheTimes.Day);
return res.json(dailyTxs);
} catch (err) {
return res.status(500).send(err);
}
Expand Down

0 comments on commit 2a18ba9

Please sign in to comment.