Browse Source

Support date range in benchmark endpoint

pull/3190/head
Thomas Kaul 1 year ago
parent
commit
c1fbd66448
  1. 14
      apps/api/src/app/benchmark/benchmark.controller.ts
  2. 2
      apps/api/src/app/benchmark/benchmark.module.ts
  3. 1
      apps/client/src/app/pages/portfolio/analysis/analysis-page.component.ts
  4. 8
      apps/client/src/app/services/data.service.ts

14
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 { HasPermission } from '@ghostfolio/api/decorators/has-permission.decorator';
import { HasPermissionGuard } from '@ghostfolio/api/guards/has-permission.guard'; import { HasPermissionGuard } from '@ghostfolio/api/guards/has-permission.guard';
import { TransformDataSourceInRequestInterceptor } from '@ghostfolio/api/interceptors/transform-data-source-in-request.interceptor'; import { TransformDataSourceInRequestInterceptor } from '@ghostfolio/api/interceptors/transform-data-source-in-request.interceptor';
@ -8,7 +9,7 @@ import type {
UniqueAsset UniqueAsset
} from '@ghostfolio/common/interfaces'; } from '@ghostfolio/common/interfaces';
import { permissions } from '@ghostfolio/common/permissions'; import { permissions } from '@ghostfolio/common/permissions';
import type { RequestWithUser } from '@ghostfolio/common/types'; import type { DateRange, RequestWithUser } from '@ghostfolio/common/types';
import { import {
Body, Body,
@ -19,6 +20,7 @@ import {
Inject, Inject,
Param, Param,
Post, Post,
Query,
UseGuards, UseGuards,
UseInterceptors UseInterceptors
} from '@nestjs/common'; } from '@nestjs/common';
@ -33,6 +35,7 @@ import { BenchmarkService } from './benchmark.service';
export class BenchmarkController { export class BenchmarkController {
public constructor( public constructor(
private readonly benchmarkService: BenchmarkService, private readonly benchmarkService: BenchmarkService,
private readonly portfolioService: PortfolioService,
@Inject(REQUEST) private readonly request: RequestWithUser @Inject(REQUEST) private readonly request: RequestWithUser
) {} ) {}
@ -106,13 +109,18 @@ export class BenchmarkController {
public async getBenchmarkMarketDataBySymbol( public async getBenchmarkMarketDataBySymbol(
@Param('dataSource') dataSource: DataSource, @Param('dataSource') dataSource: DataSource,
@Param('startDateString') startDateString: string, @Param('startDateString') startDateString: string,
@Param('symbol') symbol: string @Param('symbol') symbol: string,
@Query('range') dateRange: DateRange = 'max'
): Promise<BenchmarkMarketDataDetails> { ): Promise<BenchmarkMarketDataDetails> {
const startDate = new Date(startDateString); const { endDate, startDate } = this.portfolioService.getInterval(
dateRange,
new Date(startDateString)
);
const userCurrency = this.request.user.Settings.settings.baseCurrency; const userCurrency = this.request.user.Settings.settings.baseCurrency;
return this.benchmarkService.getMarketDataBySymbol({ return this.benchmarkService.getMarketDataBySymbol({
dataSource, dataSource,
endDate,
startDate, startDate,
symbol, symbol,
userCurrency userCurrency

2
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 { RedisCacheModule } from '@ghostfolio/api/app/redis-cache/redis-cache.module';
import { SymbolModule } from '@ghostfolio/api/app/symbol/symbol.module'; import { SymbolModule } from '@ghostfolio/api/app/symbol/symbol.module';
import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module'; import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module';
@ -21,6 +22,7 @@ import { BenchmarkService } from './benchmark.service';
DataProviderModule, DataProviderModule,
ExchangeRateDataModule, ExchangeRateDataModule,
MarketDataModule, MarketDataModule,
PortfolioModule,
PrismaModule, PrismaModule,
PropertyModule, PropertyModule,
RedisCacheModule, RedisCacheModule,

1
apps/client/src/app/pages/portfolio/analysis/analysis-page.component.ts

@ -352,6 +352,7 @@ export class AnalysisPageComponent implements OnDestroy, OnInit {
.fetchBenchmarkBySymbol({ .fetchBenchmarkBySymbol({
dataSource, dataSource,
symbol, symbol,
range: this.user?.settings?.dateRange,
startDate: this.firstOrderDate startDate: this.firstOrderDate
}) })
.pipe(takeUntil(this.unsubscribeSubject)) .pipe(takeUntil(this.unsubscribeSubject))

8
apps/client/src/app/services/data.service.ts

@ -269,16 +269,22 @@ export class DataService {
public fetchBenchmarkBySymbol({ public fetchBenchmarkBySymbol({
dataSource, dataSource,
range,
startDate, startDate,
symbol symbol
}: { }: {
range: DateRange;
startDate: Date; startDate: Date;
} & UniqueAsset): Observable<BenchmarkMarketDataDetails> { } & UniqueAsset): Observable<BenchmarkMarketDataDetails> {
let params = new HttpParams();
params = params.append('range', range);
return this.http.get<BenchmarkMarketDataDetails>( return this.http.get<BenchmarkMarketDataDetails>(
`/api/v1/benchmark/${dataSource}/${symbol}/${format( `/api/v1/benchmark/${dataSource}/${symbol}/${format(
startDate, startDate,
DATE_FORMAT DATE_FORMAT
)}` )}`,
{ params }
); );
} }

Loading…
Cancel
Save