Browse Source

Feature/refactor data gathering items (#4032)

* Refactoring
pull/4033/head
Thomas Kaul 3 months ago
committed by GitHub
parent
commit
190bb4b6fb
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 21
      apps/api/src/app/portfolio/current-rate.service.ts
  2. 16
      apps/api/src/app/portfolio/portfolio.service.ts
  3. 2
      apps/api/src/app/symbol/symbol.service.ts
  4. 47
      apps/api/src/services/data-provider/data-provider.service.ts
  5. 2
      apps/api/src/services/queues/data-gathering/data-gathering.processor.ts
  6. 2
      apps/api/src/services/queues/data-gathering/data-gathering.service.ts

21
apps/api/src/app/portfolio/current-rate.service.ts

@ -52,27 +52,24 @@ export class CurrentRateService {
.then((dataResultProvider) => { .then((dataResultProvider) => {
const result: GetValueObject[] = []; const result: GetValueObject[] = [];
for (const dataGatheringItem of dataGatheringItems) { for (const { dataSource, symbol } of dataGatheringItems) {
if ( if (dataResultProvider?.[symbol]?.dataProviderInfo) {
dataResultProvider?.[dataGatheringItem.symbol]?.dataProviderInfo
) {
dataProviderInfos.push( dataProviderInfos.push(
dataResultProvider[dataGatheringItem.symbol].dataProviderInfo dataResultProvider[symbol].dataProviderInfo
); );
} }
if (dataResultProvider?.[dataGatheringItem.symbol]?.marketPrice) { if (dataResultProvider?.[symbol]?.marketPrice) {
result.push({ result.push({
dataSource: dataGatheringItem.dataSource, dataSource,
symbol,
date: today, date: today,
marketPrice: marketPrice: dataResultProvider?.[symbol]?.marketPrice
dataResultProvider?.[dataGatheringItem.symbol]?.marketPrice,
symbol: dataGatheringItem.symbol
}); });
} else { } else {
quoteErrors.push({ quoteErrors.push({
dataSource: dataGatheringItem.dataSource, dataSource,
symbol: dataGatheringItem.symbol symbol
}); });
} }
} }

16
apps/api/src/app/portfolio/portfolio.service.ts

@ -413,15 +413,16 @@ export class PortfolioService {
); );
} }
const dataGatheringItems = positions.map(({ dataSource, symbol }) => { const assetProfileIdentifiers = positions.map(({ dataSource, symbol }) => {
return { return {
dataSource, dataSource,
symbol symbol
}; };
}); });
const symbolProfiles = const symbolProfiles = await this.symbolProfileService.getSymbolProfiles(
await this.symbolProfileService.getSymbolProfiles(dataGatheringItems); assetProfileIdentifiers
);
const symbolProfileMap: { [symbol: string]: EnhancedSymbolProfile } = {}; const symbolProfileMap: { [symbol: string]: EnhancedSymbolProfile } = {};
for (const symbolProfile of symbolProfiles) { for (const symbolProfile of symbolProfiles) {
@ -848,7 +849,7 @@ export class PortfolioService {
if (isEmpty(historicalData)) { if (isEmpty(historicalData)) {
try { try {
historicalData = await this.dataProviderService.getHistoricalRaw({ historicalData = await this.dataProviderService.getHistoricalRaw({
dataGatheringItems: [ assetProfileIdentifiers: [
{ dataSource: DataSource.YAHOO, symbol: aSymbol } { dataSource: DataSource.YAHOO, symbol: aSymbol }
], ],
from: portfolioStart, from: portfolioStart,
@ -953,7 +954,7 @@ export class PortfolioService {
return !quantity.eq(0); return !quantity.eq(0);
}); });
const dataGatheringItems = positions.map(({ dataSource, symbol }) => { const assetProfileIdentifiers = positions.map(({ dataSource, symbol }) => {
return { return {
dataSource, dataSource,
symbol symbol
@ -961,7 +962,10 @@ export class PortfolioService {
}); });
const [dataProviderResponses, symbolProfiles] = await Promise.all([ const [dataProviderResponses, symbolProfiles] = await Promise.all([
this.dataProviderService.getQuotes({ user, items: dataGatheringItems }), this.dataProviderService.getQuotes({
user,
items: assetProfileIdentifiers
}),
this.symbolProfileService.getSymbolProfiles( this.symbolProfileService.getSymbolProfiles(
positions.map(({ dataSource, symbol }) => { positions.map(({ dataSource, symbol }) => {
return { dataSource, symbol }; return { dataSource, symbol };

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

@ -86,7 +86,7 @@ export class SymbolService {
try { try {
historicalData = await this.dataProviderService.getHistoricalRaw({ historicalData = await this.dataProviderService.getHistoricalRaw({
dataGatheringItems: [{ dataSource, symbol }], assetProfileIdentifiers: [{ dataSource, symbol }],
from: date, from: date,
to: date to: date
}); });

47
apps/api/src/services/data-provider/data-provider.service.ts

@ -91,11 +91,11 @@ export class DataProviderService {
const promises = []; const promises = [];
for (const [dataSource, dataGatheringItems] of Object.entries( for (const [dataSource, assetProfileIdentifiers] of Object.entries(
itemsGroupedByDataSource itemsGroupedByDataSource
)) { )) {
const symbols = dataGatheringItems.map((dataGatheringItem) => { const symbols = assetProfileIdentifiers.map(({ symbol }) => {
return dataGatheringItem.symbol; return symbol;
}); });
for (const symbol of symbols) { for (const symbol of symbols) {
@ -242,11 +242,11 @@ export class DataProviderService {
} }
public async getHistoricalRaw({ public async getHistoricalRaw({
dataGatheringItems, assetProfileIdentifiers,
from, from,
to to
}: { }: {
dataGatheringItems: AssetProfileIdentifier[]; assetProfileIdentifiers: AssetProfileIdentifier[];
from: Date; from: Date;
to: Date; to: Date;
}): Promise<{ }): Promise<{
@ -255,25 +255,32 @@ export class DataProviderService {
for (const { currency, rootCurrency } of DERIVED_CURRENCIES) { for (const { currency, rootCurrency } of DERIVED_CURRENCIES) {
if ( if (
this.hasCurrency({ this.hasCurrency({
dataGatheringItems, assetProfileIdentifiers,
currency: `${DEFAULT_CURRENCY}${currency}` currency: `${DEFAULT_CURRENCY}${currency}`
}) })
) { ) {
// Skip derived currency // Skip derived currency
dataGatheringItems = dataGatheringItems.filter(({ symbol }) => { assetProfileIdentifiers = assetProfileIdentifiers.filter(
return symbol !== `${DEFAULT_CURRENCY}${currency}`; ({ symbol }) => {
}); return symbol !== `${DEFAULT_CURRENCY}${currency}`;
}
);
// Add root currency // Add root currency
dataGatheringItems.push({ assetProfileIdentifiers.push({
dataSource: this.getDataSourceForExchangeRates(), dataSource: this.getDataSourceForExchangeRates(),
symbol: `${DEFAULT_CURRENCY}${rootCurrency}` symbol: `${DEFAULT_CURRENCY}${rootCurrency}`
}); });
} }
} }
dataGatheringItems = uniqWith(dataGatheringItems, (obj1, obj2) => { assetProfileIdentifiers = uniqWith(
return obj1.dataSource === obj2.dataSource && obj1.symbol === obj2.symbol; assetProfileIdentifiers,
}); (obj1, obj2) => {
return (
obj1.dataSource === obj2.dataSource && obj1.symbol === obj2.symbol
);
}
);
const result: { const result: {
[symbol: string]: { [date: string]: IDataProviderHistoricalResponse }; [symbol: string]: { [date: string]: IDataProviderHistoricalResponse };
@ -283,7 +290,7 @@ export class DataProviderService {
data: { [date: string]: IDataProviderHistoricalResponse }; data: { [date: string]: IDataProviderHistoricalResponse };
symbol: string; symbol: string;
}>[] = []; }>[] = [];
for (const { dataSource, symbol } of dataGatheringItems) { for (const { dataSource, symbol } of assetProfileIdentifiers) {
const dataProvider = this.getDataProvider(dataSource); const dataProvider = this.getDataProvider(dataSource);
if (dataProvider.canHandle(symbol)) { if (dataProvider.canHandle(symbol)) {
if (symbol === `${DEFAULT_CURRENCY}USX`) { if (symbol === `${DEFAULT_CURRENCY}USX`) {
@ -418,7 +425,7 @@ export class DataProviderService {
const promises: Promise<any>[] = []; const promises: Promise<any>[] = [];
for (const [dataSource, dataGatheringItems] of Object.entries( for (const [dataSource, assetProfileIdentifiers] of Object.entries(
itemsGroupedByDataSource itemsGroupedByDataSource
)) { )) {
const dataProvider = this.getDataProvider(DataSource[dataSource]); const dataProvider = this.getDataProvider(DataSource[dataSource]);
@ -431,7 +438,7 @@ export class DataProviderService {
continue; continue;
} }
const symbols = dataGatheringItems const symbols = assetProfileIdentifiers
.filter(({ symbol }) => { .filter(({ symbol }) => {
return !isDerivedCurrency(getCurrencyFromSymbol(symbol)); return !isDerivedCurrency(getCurrencyFromSymbol(symbol));
}) })
@ -634,13 +641,13 @@ export class DataProviderService {
} }
private hasCurrency({ private hasCurrency({
currency, assetProfileIdentifiers,
dataGatheringItems currency
}: { }: {
assetProfileIdentifiers: AssetProfileIdentifier[];
currency: string; currency: string;
dataGatheringItems: AssetProfileIdentifier[];
}) { }) {
return dataGatheringItems.some(({ dataSource, symbol }) => { return assetProfileIdentifiers.some(({ dataSource, symbol }) => {
return ( return (
dataSource === this.getDataSourceForExchangeRates() && dataSource === this.getDataSourceForExchangeRates() &&
symbol === currency symbol === currency

2
apps/api/src/services/queues/data-gathering/data-gathering.processor.ts

@ -89,7 +89,7 @@ export class DataGatheringProcessor {
); );
const historicalData = await this.dataProviderService.getHistoricalRaw({ const historicalData = await this.dataProviderService.getHistoricalRaw({
dataGatheringItems: [{ dataSource, symbol }], assetProfileIdentifiers: [{ dataSource, symbol }],
from: currentDate, from: currentDate,
to: new Date() to: new Date()
}); });

2
apps/api/src/services/queues/data-gathering/data-gathering.service.ts

@ -122,7 +122,7 @@ export class DataGatheringService {
}) { }) {
try { try {
const historicalData = await this.dataProviderService.getHistoricalRaw({ const historicalData = await this.dataProviderService.getHistoricalRaw({
dataGatheringItems: [{ dataSource, symbol }], assetProfileIdentifiers: [{ dataSource, symbol }],
from: date, from: date,
to: date to: date
}); });

Loading…
Cancel
Save