From c1fbd66448ebaf810a902a64d78a222cbc62192c Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sat, 30 Mar 2024 12:22:25 +0100 Subject: [PATCH] Support date range in benchmark endpoint --- apps/api/src/app/benchmark/benchmark.controller.ts | 14 +++++++++++--- apps/api/src/app/benchmark/benchmark.module.ts | 2 ++ .../portfolio/analysis/analysis-page.component.ts | 1 + apps/client/src/app/services/data.service.ts | 8 +++++++- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/apps/api/src/app/benchmark/benchmark.controller.ts b/apps/api/src/app/benchmark/benchmark.controller.ts index d3b91c6df..833e2b9e8 100644 --- a/apps/api/src/app/benchmark/benchmark.controller.ts +++ b/apps/api/src/app/benchmark/benchmark.controller.ts @@ -1,3 +1,4 @@ +import { PortfolioService } from '@ghostfolio/api/app/portfolio/portfolio.service'; import { HasPermission } from '@ghostfolio/api/decorators/has-permission.decorator'; import { HasPermissionGuard } from '@ghostfolio/api/guards/has-permission.guard'; import { TransformDataSourceInRequestInterceptor } from '@ghostfolio/api/interceptors/transform-data-source-in-request.interceptor'; @@ -8,7 +9,7 @@ import type { UniqueAsset } from '@ghostfolio/common/interfaces'; import { permissions } from '@ghostfolio/common/permissions'; -import type { RequestWithUser } from '@ghostfolio/common/types'; +import type { DateRange, RequestWithUser } from '@ghostfolio/common/types'; import { Body, @@ -19,6 +20,7 @@ import { Inject, Param, Post, + Query, UseGuards, UseInterceptors } from '@nestjs/common'; @@ -33,6 +35,7 @@ import { BenchmarkService } from './benchmark.service'; export class BenchmarkController { public constructor( private readonly benchmarkService: BenchmarkService, + private readonly portfolioService: PortfolioService, @Inject(REQUEST) private readonly request: RequestWithUser ) {} @@ -106,13 +109,18 @@ export class BenchmarkController { public async getBenchmarkMarketDataBySymbol( @Param('dataSource') dataSource: DataSource, @Param('startDateString') startDateString: string, - @Param('symbol') symbol: string + @Param('symbol') symbol: string, + @Query('range') dateRange: DateRange = 'max' ): Promise { - const startDate = new Date(startDateString); + const { endDate, startDate } = this.portfolioService.getInterval( + dateRange, + new Date(startDateString) + ); const userCurrency = this.request.user.Settings.settings.baseCurrency; return this.benchmarkService.getMarketDataBySymbol({ dataSource, + endDate, startDate, symbol, userCurrency diff --git a/apps/api/src/app/benchmark/benchmark.module.ts b/apps/api/src/app/benchmark/benchmark.module.ts index 7371588d1..9e7b8fe75 100644 --- a/apps/api/src/app/benchmark/benchmark.module.ts +++ b/apps/api/src/app/benchmark/benchmark.module.ts @@ -1,3 +1,4 @@ +import { PortfolioModule } from '@ghostfolio/api/app/portfolio/portfolio.module'; import { RedisCacheModule } from '@ghostfolio/api/app/redis-cache/redis-cache.module'; import { SymbolModule } from '@ghostfolio/api/app/symbol/symbol.module'; import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module'; @@ -21,6 +22,7 @@ import { BenchmarkService } from './benchmark.service'; DataProviderModule, ExchangeRateDataModule, MarketDataModule, + PortfolioModule, PrismaModule, PropertyModule, RedisCacheModule, diff --git a/apps/client/src/app/pages/portfolio/analysis/analysis-page.component.ts b/apps/client/src/app/pages/portfolio/analysis/analysis-page.component.ts index c0569b9f4..184297b26 100644 --- a/apps/client/src/app/pages/portfolio/analysis/analysis-page.component.ts +++ b/apps/client/src/app/pages/portfolio/analysis/analysis-page.component.ts @@ -352,6 +352,7 @@ export class AnalysisPageComponent implements OnDestroy, OnInit { .fetchBenchmarkBySymbol({ dataSource, symbol, + range: this.user?.settings?.dateRange, startDate: this.firstOrderDate }) .pipe(takeUntil(this.unsubscribeSubject)) diff --git a/apps/client/src/app/services/data.service.ts b/apps/client/src/app/services/data.service.ts index c263b0cb0..ae1d04004 100644 --- a/apps/client/src/app/services/data.service.ts +++ b/apps/client/src/app/services/data.service.ts @@ -269,16 +269,22 @@ export class DataService { public fetchBenchmarkBySymbol({ dataSource, + range, startDate, symbol }: { + range: DateRange; startDate: Date; } & UniqueAsset): Observable { + let params = new HttpParams(); + params = params.append('range', range); + return this.http.get( `/api/v1/benchmark/${dataSource}/${symbol}/${format( startDate, DATE_FORMAT - )}` + )}`, + { params } ); }