Browse Source

Refactoring

pull/2790/head
Thomas Kaul 2 years ago
parent
commit
ee38cdb91d
  1. 2
      apps/api/src/app/benchmark/benchmark.service.ts
  2. 45
      apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts

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

@ -236,7 +236,7 @@ export class BenchmarkService {
currencyFrom: currentSymbolItem.currency, currencyFrom: currentSymbolItem.currency,
currencyTo: baseCurrency, currencyTo: baseCurrency,
dates: marketDataItems.map(({ date }) => { dates: marketDataItems.map(({ date }) => {
return format(date, DATE_FORMAT); return date;
}) })
}); });

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

@ -7,11 +7,7 @@ import {
DEFAULT_CURRENCY, DEFAULT_CURRENCY,
PROPERTY_CURRENCIES PROPERTY_CURRENCIES
} from '@ghostfolio/common/config'; } from '@ghostfolio/common/config';
import { import { DATE_FORMAT, getYesterday } from '@ghostfolio/common/helper';
DATE_FORMAT,
getYesterday,
parseDate
} from '@ghostfolio/common/helper';
import { Injectable, Logger } from '@nestjs/common'; import { Injectable, Logger } from '@nestjs/common';
import { format, isToday } from 'date-fns'; import { format, isToday } from 'date-fns';
import { isNumber, uniq } from 'lodash'; import { isNumber, uniq } from 'lodash';
@ -44,14 +40,13 @@ export class ExchangeRateDataService {
}: { }: {
currencyFrom: string; currencyFrom: string;
currencyTo: string; currencyTo: string;
dates: string[]; dates: Date[];
}) { }) {
let factors: { [dateString: string]: number } = {}; let factors: { [dateString: string]: number } = {};
const startDate = parseDate(dates[0]);
if (currencyFrom === currencyTo) { if (currencyFrom === currencyTo) {
for (const date of dates) { for (const date of dates) {
factors[date] = 1; factors[format(date, DATE_FORMAT)] = 1;
} }
} else { } else {
const dataSource = const dataSource =
@ -59,7 +54,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: { gte: startDate }, dateQuery: { in: dates },
uniqueAssets: [ uniqueAssets: [
{ {
dataSource, dataSource,
@ -85,11 +80,12 @@ export class ExchangeRateDataService {
try { try {
if (currencyFrom === DEFAULT_CURRENCY) { if (currencyFrom === DEFAULT_CURRENCY) {
for (const date of dates) { for (const date of dates) {
marketPriceBaseCurrencyFromCurrency[date] = 1; marketPriceBaseCurrencyFromCurrency[format(date, DATE_FORMAT)] =
1;
} }
} else { } else {
const marketData = await this.marketDataService.getRange({ const marketData = await this.marketDataService.getRange({
dateQuery: { gte: startDate }, dateQuery: { in: dates },
uniqueAssets: [ uniqueAssets: [
{ {
dataSource, dataSource,
@ -108,11 +104,13 @@ export class ExchangeRateDataService {
try { try {
if (currencyTo === DEFAULT_CURRENCY) { if (currencyTo === DEFAULT_CURRENCY) {
for (const date of dates) { for (const date of dates) {
marketPriceBaseCurrencyToCurrency[date] = 1; marketPriceBaseCurrencyToCurrency[format(date, DATE_FORMAT)] = 1;
} }
} else { } else {
const marketData = await this.marketDataService.getRange({ const marketData = await this.marketDataService.getRange({
dateQuery: { gte: startDate }, dateQuery: {
in: dates
},
uniqueAssets: [ uniqueAssets: [
{ {
dataSource, dataSource,
@ -129,9 +127,24 @@ export class ExchangeRateDataService {
} catch {} } catch {}
for (const date of dates) { for (const date of dates) {
factors[date] = try {
(1 / marketPriceBaseCurrencyFromCurrency[date]) * const factor =
marketPriceBaseCurrencyToCurrency[date]; (1 /
marketPriceBaseCurrencyFromCurrency[
format(date, DATE_FORMAT)
]) *
marketPriceBaseCurrencyToCurrency[format(date, DATE_FORMAT)];
factors[format(date, DATE_FORMAT)] = factor;
} catch {
Logger.error(
`No exchange rate has been found for ${currencyFrom}${currencyTo} at ${format(
date,
DATE_FORMAT
)}`,
'ExchangeRateDataService'
);
}
} }
} }
} }

Loading…
Cancel
Save