From 29581f325f2b00af7c5b00014f7fe4669ada848c Mon Sep 17 00:00:00 2001 From: softsimon Date: Wed, 12 Jan 2022 20:57:25 +0400 Subject: [PATCH] Removing statistics cache and setting headers --- backend/src/api/statistics.ts | 24 +---------- backend/src/index.ts | 18 ++++---- backend/src/routes.ts | 79 +++++++++++++++++++---------------- 3 files changed, 54 insertions(+), 67 deletions(-) diff --git a/backend/src/api/statistics.ts b/backend/src/api/statistics.ts index 5c66153c8..4fc8cc5af 100644 --- a/backend/src/api/statistics.ts +++ b/backend/src/api/statistics.ts @@ -10,9 +10,6 @@ class Statistics { protected intervalTimer: NodeJS.Timer | undefined; protected newStatisticsEntryCallback: ((stats: OptimizedStatistic) => void) | undefined; protected queryTimeout = 120000; - protected cache: { [date: string]: OptimizedStatistic[] } = { - '24h': [], '1w': [], '1m': [], '3m': [], '6m': [], '1y': [], '2y': [], '3y': [] - }; public setNewStatisticsEntryCallback(fn: (stats: OptimizedStatistic) => void) { this.newStatisticsEntryCallback = fn; @@ -34,25 +31,6 @@ class Statistics { this.runStatistics(); }, 1 * 60 * 1000); }, 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 { @@ -365,7 +343,7 @@ class Statistics { ORDER BY added DESC;`; } - public async $get(id: number): Promise { + private async $get(id: number): Promise { try { const connection = await DB.pool.getConnection(); const query = `SELECT *, UNIX_TIMESTAMP(added) as added FROM statistics WHERE id = ?`; diff --git a/backend/src/index.ts b/backend/src/index.ts index f6fe237a2..c1b6427c7 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -225,15 +225,15 @@ class Server { if (config.STATISTICS.ENABLED && config.DATABASE.ENABLED) { this.app - .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/2h', routes.get2HStatistics) - .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/24h', routes.get24HStatistics.bind(routes)) - .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/1w', routes.get1WHStatistics.bind(routes)) - .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/1m', routes.get1MStatistics.bind(routes)) - .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/3m', routes.get3MStatistics.bind(routes)) - .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/6m', routes.get6MStatistics.bind(routes)) - .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/1y', routes.get1YStatistics.bind(routes)) - .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/2y', routes.get2YStatistics.bind(routes)) - .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/3y', routes.get3YStatistics.bind(routes)) + .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/2h', routes.$getStatisticsByTime.bind(routes, '2h')) + .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/24h', routes.$getStatisticsByTime.bind(routes, '24h')) + .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/1w', routes.$getStatisticsByTime.bind(routes, '1w')) + .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/1m', routes.$getStatisticsByTime.bind(routes, '1m')) + .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/3m', routes.$getStatisticsByTime.bind(routes, '3m')) + .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/6m', routes.$getStatisticsByTime.bind(routes, '6m')) + .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/1y', routes.$getStatisticsByTime.bind(routes, '1y')) + .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/2y', routes.$getStatisticsByTime.bind(routes, '2y')) + .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/3y', routes.$getStatisticsByTime.bind(routes, '3y')) ; } diff --git a/backend/src/routes.ts b/backend/src/routes.ts index 53387511f..8484ec2ab 100644 --- a/backend/src/routes.ts +++ b/backend/src/routes.ts @@ -24,41 +24,50 @@ import icons from './api/liquid/icons'; class Routes { constructor() {} - public async get2HStatistics(req: Request, res: Response) { - const result = await statistics.$list2H(); - res.json(result); - } + public async $getStatisticsByTime(time: '2h' | '24h' | '1w' | '1m' | '3m' | '6m' | '1y' | '2y' | '3y', req: Request, res: Response) { + res.header('Pragma', 'public'); + res.header('Cache-control', 'public'); + res.setHeader('Expires', new Date(Date.now() + 1000 * 300).toUTCString()); - public get24HStatistics(req: Request, res: Response) { - res.json(statistics.getCache()['24h']); - } - - public get1WHStatistics(req: Request, res: Response) { - res.json(statistics.getCache()['1w']); - } - - public get1MStatistics(req: Request, res: Response) { - res.json(statistics.getCache()['1m']); - } - - public get3MStatistics(req: Request, res: Response) { - res.json(statistics.getCache()['3m']); - } - - public get6MStatistics(req: Request, res: Response) { - res.json(statistics.getCache()['6m']); - } - - public get1YStatistics(req: Request, res: Response) { - res.json(statistics.getCache()['1y']); - } - - public get2YStatistics(req: Request, res: Response) { - res.json(statistics.getCache()['2y']); - } - - public get3YStatistics(req: Request, res: Response) { - res.json(statistics.getCache()['3y']); + try { + let result; + switch (time as string) { + case '2h': + result = await statistics.$list2H(); + res.setHeader('Expires', new Date(Date.now() + 1000 * 30).toUTCString()); + break; + case '24h': + result = await statistics.$list24H(); + res.setHeader('Expires', new Date(Date.now() + 1000 * 60).toUTCString()); + break; + case '1w': + result = await statistics.$list1W(); + break; + case '1m': + result = await statistics.$list1M(); + break; + case '3m': + result = await statistics.$list3M(); + break; + case '6m': + result = await statistics.$list6M(); + break; + case '1y': + result = await statistics.$list1Y(); + break; + case '2y': + result = await statistics.$list2Y(); + break; + 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) { @@ -70,7 +79,7 @@ class Routes { } } - public async getRecommendedFees(req: Request, res: Response) { + public getRecommendedFees(req: Request, res: Response) { if (!mempool.isInSync()) { res.statusCode = 503; res.send('Service Unavailable');