diff --git a/src/ORM/client-statistics/client-statistics.service.ts b/src/ORM/client-statistics/client-statistics.service.ts index de288d7..9db40dc 100644 --- a/src/ORM/client-statistics/client-statistics.service.ts +++ b/src/ORM/client-statistics/client-statistics.service.ts @@ -1,20 +1,27 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, OnModuleDestroy } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; -import { Repository } from 'typeorm'; +import { DataSource, QueryRunner, Repository } from 'typeorm'; import { ClientStatisticsEntity } from './client-statistics.entity'; @Injectable() -export class ClientStatisticsService { +export class ClientStatisticsService implements OnModuleDestroy { + + private readUncommittedQueryRunner: QueryRunner; constructor( - + private readonly dataSource: DataSource, @InjectRepository(ClientStatisticsEntity) private clientStatisticsRepository: Repository, ) { + this.readUncommittedQueryRunner = this.dataSource.createQueryRunner(); + this.readUncommittedQueryRunner.query('SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;') + } + onModuleDestroy() { + this.readUncommittedQueryRunner.release(); } public async save(clientStatistic: Partial) { @@ -47,9 +54,9 @@ export class ClientStatisticsService { var yesterday = new Date(new Date().getTime() - (24 * 60 * 60 * 1000)); - const query = ` - SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + var result = await this.readUncommittedQueryRunner.query( + ` SELECT time AS label, ROUND(((SUM(shares) * 4294967296) / 600)) AS data @@ -61,11 +68,9 @@ export class ClientStatisticsService { time ORDER BY time - LIMIT 144; - - `; - - const result: any[] = await this.clientStatisticsRepository.query(query); + LIMIT 144; + ` + ); return result.map(res => { diff --git a/src/app.controller.ts b/src/app.controller.ts index 8c6e592..6af196b 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -24,14 +24,29 @@ export class AppController { @Get('info') public async info() { + + const CACHE_KEY = 'SITE_INFO'; + const cachedResult = await this.cacheManager.get(CACHE_KEY); + + if (cachedResult != null) { + return cachedResult; + } + + const blockData = await this.blocksService.getFoundBlocks(); const userAgents = await this.clientService.getUserAgents(); - return { + const data = { blockData, userAgents, uptime: this.uptime }; + + //1 min + await this.cacheManager.set(CACHE_KEY, data, 1 * 60 * 1000); + + return data; + } @Get('network')