Browse Source

Fix getExchangeRates()

pull/2834/head
Thomas Kaul 2 years ago
parent
commit
2ea4564b03
  1. 6
      apps/api/src/app/benchmark/benchmark.service.ts
  2. 28
      apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts

6
apps/api/src/app/benchmark/benchmark.service.ts

@ -236,11 +236,9 @@ export class BenchmarkService {
]); ]);
const exchangeRates = await this.exchangeRateDataService.getExchangeRates({ const exchangeRates = await this.exchangeRateDataService.getExchangeRates({
startDate,
currencyFrom: currentSymbolItem.currency, currencyFrom: currentSymbolItem.currency,
currencyTo: userCurrency, currencyTo: userCurrency
dates: marketDataItems.map(({ date }) => {
return date;
})
}); });
const exchangeRateAtStartDate = const exchangeRateAtStartDate =

28
apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts

@ -13,7 +13,7 @@ import {
resetHours resetHours
} from '@ghostfolio/common/helper'; } from '@ghostfolio/common/helper';
import { Injectable, Logger } from '@nestjs/common'; import { Injectable, Logger } from '@nestjs/common';
import { addDays, format, isAfter, isToday } from 'date-fns'; import { addDays, eachDayOfInterval, format, isAfter, isToday } from 'date-fns';
import { isNumber, uniq } from 'lodash'; import { isNumber, uniq } from 'lodash';
import ms from 'ms'; import ms from 'ms';
@ -53,21 +53,13 @@ export class ExchangeRateDataService {
return {}; return {};
} }
let currentDate = resetHours(startDate);
let dates: Date[] = [];
let exchangeRatesByCurrency: { let exchangeRatesByCurrency: {
[currency: string]: { [dateString: string]: number }; [currency: string]: { [dateString: string]: number };
} = {}; } = {};
while (isAfter(endDate, currentDate)) { for (let currency of uniq(currencies)) {
dates.push(currentDate);
currentDate = addDays(currentDate, 1);
}
for (let currency of uniq(Object.values(currencies))) {
exchangeRatesByCurrency[currency] = await this.getExchangeRates({ exchangeRatesByCurrency[currency] = await this.getExchangeRates({
dates, startDate,
currencyFrom: currency, currencyFrom: currency,
currencyTo: targetCurrency currencyTo: targetCurrency
}); });
@ -94,12 +86,15 @@ export class ExchangeRateDataService {
public async getExchangeRates({ public async getExchangeRates({
currencyFrom, currencyFrom,
currencyTo, currencyTo,
dates endDate = new Date(),
startDate
}: { }: {
currencyFrom: string; currencyFrom: string;
currencyTo: string; currencyTo: string;
dates: Date[]; endDate?: Date;
startDate: Date;
}) { }) {
const dates = eachDayOfInterval({ end: endDate, start: startDate });
let factors: { [dateString: string]: number } = {}; let factors: { [dateString: string]: number } = {};
if (currencyFrom === currencyTo) { if (currencyFrom === currencyTo) {
@ -112,7 +107,7 @@ export class ExchangeRateDataService {
const symbol = `${currencyFrom}${currencyTo}`; const symbol = `${currencyFrom}${currencyTo}`;
const marketData = await this.marketDataService.getRange({ const marketData = await this.marketDataService.getRange({
dateQuery: { in: dates }, dateQuery: { gte: startDate, lt: endDate },
uniqueAssets: [ uniqueAssets: [
{ {
dataSource, dataSource,
@ -143,7 +138,7 @@ export class ExchangeRateDataService {
} }
} else { } else {
const marketData = await this.marketDataService.getRange({ const marketData = await this.marketDataService.getRange({
dateQuery: { in: dates }, dateQuery: { gte: startDate, lt: endDate },
uniqueAssets: [ uniqueAssets: [
{ {
dataSource, dataSource,
@ -167,7 +162,8 @@ export class ExchangeRateDataService {
} else { } else {
const marketData = await this.marketDataService.getRange({ const marketData = await this.marketDataService.getRange({
dateQuery: { dateQuery: {
in: dates gte: startDate,
lt: endDate
}, },
uniqueAssets: [ uniqueAssets: [
{ {

Loading…
Cancel
Save