From e01e039a0039f991e77d4330cd07f813b5a1330a Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Mon, 8 Nov 2021 20:55:38 +0100 Subject: [PATCH] Release 1.72.0 (#459) --- CHANGELOG.md | 6 +++++ apps/api/src/app/info/info.module.ts | 2 ++ apps/api/src/app/info/info.service.ts | 27 +++++++++++++++++-- .../app/redis-cache/redis-cache.service.ts | 4 +-- package.json | 2 +- 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 47fcaf92..89ea0a3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 1.72.0 - 08.11.2021 + +### Changed + +- Cached the statistics section on the about page + ## 1.71.0 - 07.11.2021 ### Changed diff --git a/apps/api/src/app/info/info.module.ts b/apps/api/src/app/info/info.module.ts index 48c36e7a..bbb47d4d 100644 --- a/apps/api/src/app/info/info.module.ts +++ b/apps/api/src/app/info/info.module.ts @@ -1,3 +1,4 @@ +import { RedisCacheModule } from '@ghostfolio/api/app/redis-cache/redis-cache.module'; import { ConfigurationService } from '@ghostfolio/api/services/configuration.service'; import { DataGatheringModule } from '@ghostfolio/api/services/data-gathering.module'; import { DataGatheringService } from '@ghostfolio/api/services/data-gathering.service'; @@ -20,6 +21,7 @@ import { InfoService } from './info.service'; secret: process.env.JWT_SECRET_KEY, signOptions: { expiresIn: '30 days' } }), + RedisCacheModule, SymbolProfileModule ], controllers: [InfoController], diff --git a/apps/api/src/app/info/info.service.ts b/apps/api/src/app/info/info.service.ts index ccf71590..2d9c9a27 100644 --- a/apps/api/src/app/info/info.service.ts +++ b/apps/api/src/app/info/info.service.ts @@ -1,9 +1,11 @@ +import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service'; import { ConfigurationService } from '@ghostfolio/api/services/configuration.service'; import { DataGatheringService } from '@ghostfolio/api/services/data-gathering.service'; import { DataProviderService } from '@ghostfolio/api/services/data-provider/data-provider.service'; import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data.service'; import { PrismaService } from '@ghostfolio/api/services/prisma.service'; import { InfoItem } from '@ghostfolio/common/interfaces'; +import { Statistics } from '@ghostfolio/common/interfaces/statistics.interface'; import { Subscription } from '@ghostfolio/common/interfaces/subscription.interface'; import { permissions } from '@ghostfolio/common/permissions'; import { Injectable, Logger } from '@nestjs/common'; @@ -14,6 +16,7 @@ import { subDays } from 'date-fns'; @Injectable() export class InfoService { private static DEMO_USER_ID = '9b112b4d-3b7d-4bad-9bdd-3b0f7b4dac2f'; + private static CACHE_KEY_STATISTICS = 'STATISTICS'; public constructor( private readonly configurationService: ConfigurationService, @@ -21,7 +24,8 @@ export class InfoService { private readonly exchangeRateDataService: ExchangeRateDataService, private readonly dataGatheringService: DataGatheringService, private readonly jwtService: JwtService, - private readonly prismaService: PrismaService + private readonly prismaService: PrismaService, + private readonly redisCacheService: RedisCacheService ) {} public async get(): Promise { @@ -176,6 +180,18 @@ export class InfoService { return undefined; } + let statistics: Statistics; + + try { + statistics = JSON.parse( + await this.redisCacheService.get(InfoService.CACHE_KEY_STATISTICS) + ); + + if (statistics) { + return statistics; + } + } catch {} + const activeUsers1d = await this.countActiveUsers(1); const activeUsers7d = await this.countActiveUsers(7); const activeUsers30d = await this.countActiveUsers(30); @@ -183,7 +199,7 @@ export class InfoService { const gitHubContributors = await this.countGitHubContributors(); const gitHubStargazers = await this.countGitHubStargazers(); - return { + statistics = { activeUsers1d, activeUsers7d, activeUsers30d, @@ -191,6 +207,13 @@ export class InfoService { gitHubStargazers, newUsers30d }; + + await this.redisCacheService.set( + InfoService.CACHE_KEY_STATISTICS, + JSON.stringify(statistics) + ); + + return statistics; } private async getSubscriptions(): Promise { diff --git a/apps/api/src/app/redis-cache/redis-cache.service.ts b/apps/api/src/app/redis-cache/redis-cache.service.ts index 677fcb7b..f930b3a7 100644 --- a/apps/api/src/app/redis-cache/redis-cache.service.ts +++ b/apps/api/src/app/redis-cache/redis-cache.service.ts @@ -21,9 +21,9 @@ export class RedisCacheService { await this.cache.reset(); } - public async set(key: string, value: string) { + public async set(key: string, value: string, ttlInSeconds?: number) { await this.cache.set(key, value, { - ttl: this.configurationService.get('CACHE_TTL') + ttl: ttlInSeconds ?? this.configurationService.get('CACHE_TTL') }); } } diff --git a/package.json b/package.json index 4a84a55e..7beaeb51 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ghostfolio", - "version": "1.71.0", + "version": "1.72.0", "homepage": "https://ghostfol.io", "license": "AGPL-3.0", "scripts": {