Refactored when statistics cache from database is created.

refs #315
This commit is contained in:
softsimon 2021-01-30 21:40:04 +07:00
parent 9b9c605cbe
commit 35521f4871
No known key found for this signature in database
GPG Key ID: 488D7DCFB5A430D7
3 changed files with 27 additions and 27 deletions

View File

@ -8,6 +8,9 @@ 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': [],
};
public setNewStatisticsEntryCallback(fn: (stats: OptimizedStatistic) => void) { public setNewStatisticsEntryCallback(fn: (stats: OptimizedStatistic) => void) {
this.newStatisticsEntryCallback = fn; this.newStatisticsEntryCallback = fn;
@ -29,6 +32,23 @@ 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();
logger.debug('Statistics cache created');
} }
private async runStatistics(): Promise<void> { private async runStatistics(): Promise<void> {

View File

@ -11,7 +11,6 @@ export class DB {
password: config.DATABASE.PASSWORD, password: config.DATABASE.PASSWORD,
connectionLimit: 10, connectionLimit: 10,
supportBigNumbers: true, supportBigNumbers: true,
acquireTimeout: 60000,
}); });
} }

View File

@ -20,26 +20,7 @@ import loadingIndicators from './api/loading-indicators';
import { Common } from './api/common'; import { Common } from './api/common';
class Routes { class Routes {
private cache: { [date: string]: OptimizedStatistic[] } = { constructor() {}
'24h': [], '1w': [], '1m': [], '3m': [], '6m': [], '1y': [],
};
constructor() {
if (config.DATABASE.ENABLED && config.STATISTICS.ENABLED) {
this.createCache();
setInterval(this.createCache.bind(this), 600000);
}
}
private async createCache() {
this.cache['24h'] = await statistics.$list24H();
this.cache['1w'] = await statistics.$list1W();
this.cache['1m'] = await statistics.$list1M();
this.cache['3m'] = await statistics.$list3M();
this.cache['6m'] = await statistics.$list6M();
this.cache['1y'] = await statistics.$list1Y();
logger.debug('Statistics cache created');
}
public async get2HStatistics(req: Request, res: Response) { public async get2HStatistics(req: Request, res: Response) {
const result = await statistics.$list2H(); const result = await statistics.$list2H();
@ -47,27 +28,27 @@ class Routes {
} }
public get24HStatistics(req: Request, res: Response) { public get24HStatistics(req: Request, res: Response) {
res.json(this.cache['24h']); res.json(statistics.getCache()['24h']);
} }
public get1WHStatistics(req: Request, res: Response) { public get1WHStatistics(req: Request, res: Response) {
res.json(this.cache['1w']); res.json(statistics.getCache()['1w']);
} }
public get1MStatistics(req: Request, res: Response) { public get1MStatistics(req: Request, res: Response) {
res.json(this.cache['1m']); res.json(statistics.getCache()['1m']);
} }
public get3MStatistics(req: Request, res: Response) { public get3MStatistics(req: Request, res: Response) {
res.json(this.cache['3m']); res.json(statistics.getCache()['3m']);
} }
public get6MStatistics(req: Request, res: Response) { public get6MStatistics(req: Request, res: Response) {
res.json(this.cache['6m']); res.json(statistics.getCache()['6m']);
} }
public get1YStatistics(req: Request, res: Response) { public get1YStatistics(req: Request, res: Response) {
res.json(this.cache['1y']); res.json(statistics.getCache()['1y']);
} }
public getInitData(req: Request, res: Response) { public getInitData(req: Request, res: Response) {