Removing statistics cache and setting headers

This commit is contained in:
softsimon 2022-01-12 20:57:25 +04:00
parent a9c1dc3726
commit 29581f325f
No known key found for this signature in database
GPG Key ID: 488D7DCFB5A430D7
3 changed files with 54 additions and 67 deletions

View File

@ -10,9 +10,6 @@ class Statistics {
protected intervalTimer: NodeJS.Timer | undefined; protected intervalTimer: NodeJS.Timer | undefined;
protected newStatisticsEntryCallback: ((stats: OptimizedStatistic) => void) | undefined; protected newStatisticsEntryCallback: ((stats: OptimizedStatistic) => void) | undefined;
protected queryTimeout = 120000; protected queryTimeout = 120000;
protected cache: { [date: string]: OptimizedStatistic[] } = {
'24h': [], '1w': [], '1m': [], '3m': [], '6m': [], '1y': [], '2y': [], '3y': []
};
public setNewStatisticsEntryCallback(fn: (stats: OptimizedStatistic) => void) { public setNewStatisticsEntryCallback(fn: (stats: OptimizedStatistic) => void) {
this.newStatisticsEntryCallback = fn; this.newStatisticsEntryCallback = fn;
@ -34,25 +31,6 @@ class Statistics {
this.runStatistics(); this.runStatistics();
}, 1 * 60 * 1000); }, 1 * 60 * 1000);
}, difference); }, difference);
this.createCache();
setInterval(this.createCache.bind(this), 600000);
}
public getCache() {
return this.cache;
}
private async createCache() {
this.cache['24h'] = await this.$list24H();
this.cache['1w'] = await this.$list1W();
this.cache['1m'] = await this.$list1M();
this.cache['3m'] = await this.$list3M();
this.cache['6m'] = await this.$list6M();
this.cache['1y'] = await this.$list1Y();
this.cache['2y'] = await this.$list2Y();
this.cache['3y'] = await this.$list3Y();
logger.debug('Statistics cache created');
} }
private async runStatistics(): Promise<void> { private async runStatistics(): Promise<void> {
@ -365,7 +343,7 @@ class Statistics {
ORDER BY added DESC;`; ORDER BY added DESC;`;
} }
public async $get(id: number): Promise<OptimizedStatistic | undefined> { private async $get(id: number): Promise<OptimizedStatistic | undefined> {
try { try {
const connection = await DB.pool.getConnection(); const connection = await DB.pool.getConnection();
const query = `SELECT *, UNIX_TIMESTAMP(added) as added FROM statistics WHERE id = ?`; const query = `SELECT *, UNIX_TIMESTAMP(added) as added FROM statistics WHERE id = ?`;

View File

@ -225,15 +225,15 @@ class Server {
if (config.STATISTICS.ENABLED && config.DATABASE.ENABLED) { if (config.STATISTICS.ENABLED && config.DATABASE.ENABLED) {
this.app this.app
.get(config.MEMPOOL.API_URL_PREFIX + 'statistics/2h', routes.get2HStatistics) .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/2h', routes.$getStatisticsByTime.bind(routes, '2h'))
.get(config.MEMPOOL.API_URL_PREFIX + 'statistics/24h', routes.get24HStatistics.bind(routes)) .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/24h', routes.$getStatisticsByTime.bind(routes, '24h'))
.get(config.MEMPOOL.API_URL_PREFIX + 'statistics/1w', routes.get1WHStatistics.bind(routes)) .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/1w', routes.$getStatisticsByTime.bind(routes, '1w'))
.get(config.MEMPOOL.API_URL_PREFIX + 'statistics/1m', routes.get1MStatistics.bind(routes)) .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/1m', routes.$getStatisticsByTime.bind(routes, '1m'))
.get(config.MEMPOOL.API_URL_PREFIX + 'statistics/3m', routes.get3MStatistics.bind(routes)) .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/3m', routes.$getStatisticsByTime.bind(routes, '3m'))
.get(config.MEMPOOL.API_URL_PREFIX + 'statistics/6m', routes.get6MStatistics.bind(routes)) .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/6m', routes.$getStatisticsByTime.bind(routes, '6m'))
.get(config.MEMPOOL.API_URL_PREFIX + 'statistics/1y', routes.get1YStatistics.bind(routes)) .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/1y', routes.$getStatisticsByTime.bind(routes, '1y'))
.get(config.MEMPOOL.API_URL_PREFIX + 'statistics/2y', routes.get2YStatistics.bind(routes)) .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/2y', routes.$getStatisticsByTime.bind(routes, '2y'))
.get(config.MEMPOOL.API_URL_PREFIX + 'statistics/3y', routes.get3YStatistics.bind(routes)) .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/3y', routes.$getStatisticsByTime.bind(routes, '3y'))
; ;
} }

View File

@ -24,41 +24,50 @@ import icons from './api/liquid/icons';
class Routes { class Routes {
constructor() {} constructor() {}
public async get2HStatistics(req: Request, res: Response) { public async $getStatisticsByTime(time: '2h' | '24h' | '1w' | '1m' | '3m' | '6m' | '1y' | '2y' | '3y', req: Request, res: Response) {
const result = await statistics.$list2H(); res.header('Pragma', 'public');
res.json(result); res.header('Cache-control', 'public');
} res.setHeader('Expires', new Date(Date.now() + 1000 * 300).toUTCString());
public get24HStatistics(req: Request, res: Response) { try {
res.json(statistics.getCache()['24h']); let result;
} switch (time as string) {
case '2h':
public get1WHStatistics(req: Request, res: Response) { result = await statistics.$list2H();
res.json(statistics.getCache()['1w']); res.setHeader('Expires', new Date(Date.now() + 1000 * 30).toUTCString());
} break;
case '24h':
public get1MStatistics(req: Request, res: Response) { result = await statistics.$list24H();
res.json(statistics.getCache()['1m']); res.setHeader('Expires', new Date(Date.now() + 1000 * 60).toUTCString());
} break;
case '1w':
public get3MStatistics(req: Request, res: Response) { result = await statistics.$list1W();
res.json(statistics.getCache()['3m']); break;
} case '1m':
result = await statistics.$list1M();
public get6MStatistics(req: Request, res: Response) { break;
res.json(statistics.getCache()['6m']); case '3m':
} result = await statistics.$list3M();
break;
public get1YStatistics(req: Request, res: Response) { case '6m':
res.json(statistics.getCache()['1y']); result = await statistics.$list6M();
} break;
case '1y':
public get2YStatistics(req: Request, res: Response) { result = await statistics.$list1Y();
res.json(statistics.getCache()['2y']); break;
} case '2y':
result = await statistics.$list2Y();
public get3YStatistics(req: Request, res: Response) { break;
res.json(statistics.getCache()['3y']); case '3y':
result = await statistics.$list3Y();
break;
default:
result = await statistics.$list2H();
}
res.json(result);
} catch (e) {
res.status(500).send(e instanceof Error ? e.message : e);
}
} }
public getInitData(req: Request, res: Response) { public getInitData(req: Request, res: Response) {
@ -70,7 +79,7 @@ class Routes {
} }
} }
public async getRecommendedFees(req: Request, res: Response) { public getRecommendedFees(req: Request, res: Response) {
if (!mempool.isInSync()) { if (!mempool.isInSync()) {
res.statusCode = 503; res.statusCode = 503;
res.send('Service Unavailable'); res.send('Service Unavailable');