From 190bb4b6fbaa468ee4d9d1455225a9175bea68ca Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Thu, 7 Nov 2024 19:57:37 +0100 Subject: [PATCH] Feature/refactor data gathering items (#4032) * Refactoring --- .../src/app/portfolio/current-rate.service.ts | 21 ++++----- .../src/app/portfolio/portfolio.service.ts | 16 ++++--- apps/api/src/app/symbol/symbol.service.ts | 2 +- .../data-provider/data-provider.service.ts | 47 +++++++++++-------- .../data-gathering.processor.ts | 2 +- .../data-gathering/data-gathering.service.ts | 2 +- 6 files changed, 49 insertions(+), 41 deletions(-) diff --git a/apps/api/src/app/portfolio/current-rate.service.ts b/apps/api/src/app/portfolio/current-rate.service.ts index cd1994826..ab7bf2ebf 100644 --- a/apps/api/src/app/portfolio/current-rate.service.ts +++ b/apps/api/src/app/portfolio/current-rate.service.ts @@ -52,27 +52,24 @@ export class CurrentRateService { .then((dataResultProvider) => { const result: GetValueObject[] = []; - for (const dataGatheringItem of dataGatheringItems) { - if ( - dataResultProvider?.[dataGatheringItem.symbol]?.dataProviderInfo - ) { + for (const { dataSource, symbol } of dataGatheringItems) { + if (dataResultProvider?.[symbol]?.dataProviderInfo) { dataProviderInfos.push( - dataResultProvider[dataGatheringItem.symbol].dataProviderInfo + dataResultProvider[symbol].dataProviderInfo ); } - if (dataResultProvider?.[dataGatheringItem.symbol]?.marketPrice) { + if (dataResultProvider?.[symbol]?.marketPrice) { result.push({ - dataSource: dataGatheringItem.dataSource, + dataSource, + symbol, date: today, - marketPrice: - dataResultProvider?.[dataGatheringItem.symbol]?.marketPrice, - symbol: dataGatheringItem.symbol + marketPrice: dataResultProvider?.[symbol]?.marketPrice }); } else { quoteErrors.push({ - dataSource: dataGatheringItem.dataSource, - symbol: dataGatheringItem.symbol + dataSource, + symbol }); } } diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index d4018686c..15ca227e9 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/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 { dataSource, symbol }; }); - const symbolProfiles = - await this.symbolProfileService.getSymbolProfiles(dataGatheringItems); + const symbolProfiles = await this.symbolProfileService.getSymbolProfiles( + assetProfileIdentifiers + ); const symbolProfileMap: { [symbol: string]: EnhancedSymbolProfile } = {}; for (const symbolProfile of symbolProfiles) { @@ -848,7 +849,7 @@ export class PortfolioService { if (isEmpty(historicalData)) { try { historicalData = await this.dataProviderService.getHistoricalRaw({ - dataGatheringItems: [ + assetProfileIdentifiers: [ { dataSource: DataSource.YAHOO, symbol: aSymbol } ], from: portfolioStart, @@ -953,7 +954,7 @@ export class PortfolioService { return !quantity.eq(0); }); - const dataGatheringItems = positions.map(({ dataSource, symbol }) => { + const assetProfileIdentifiers = positions.map(({ dataSource, symbol }) => { return { dataSource, symbol @@ -961,7 +962,10 @@ export class PortfolioService { }); const [dataProviderResponses, symbolProfiles] = await Promise.all([ - this.dataProviderService.getQuotes({ user, items: dataGatheringItems }), + this.dataProviderService.getQuotes({ + user, + items: assetProfileIdentifiers + }), this.symbolProfileService.getSymbolProfiles( positions.map(({ dataSource, symbol }) => { return { dataSource, symbol }; diff --git a/apps/api/src/app/symbol/symbol.service.ts b/apps/api/src/app/symbol/symbol.service.ts index ae864e2fc..56befb9b6 100644 --- a/apps/api/src/app/symbol/symbol.service.ts +++ b/apps/api/src/app/symbol/symbol.service.ts @@ -86,7 +86,7 @@ export class SymbolService { try { historicalData = await this.dataProviderService.getHistoricalRaw({ - dataGatheringItems: [{ dataSource, symbol }], + assetProfileIdentifiers: [{ dataSource, symbol }], from: date, to: date }); diff --git a/apps/api/src/services/data-provider/data-provider.service.ts b/apps/api/src/services/data-provider/data-provider.service.ts index bf23f96c1..c8a7422d0 100644 --- a/apps/api/src/services/data-provider/data-provider.service.ts +++ b/apps/api/src/services/data-provider/data-provider.service.ts @@ -91,11 +91,11 @@ export class DataProviderService { const promises = []; - for (const [dataSource, dataGatheringItems] of Object.entries( + for (const [dataSource, assetProfileIdentifiers] of Object.entries( itemsGroupedByDataSource )) { - const symbols = dataGatheringItems.map((dataGatheringItem) => { - return dataGatheringItem.symbol; + const symbols = assetProfileIdentifiers.map(({ symbol }) => { + return symbol; }); for (const symbol of symbols) { @@ -242,11 +242,11 @@ export class DataProviderService { } public async getHistoricalRaw({ - dataGatheringItems, + assetProfileIdentifiers, from, to }: { - dataGatheringItems: AssetProfileIdentifier[]; + assetProfileIdentifiers: AssetProfileIdentifier[]; from: Date; to: Date; }): Promise<{ @@ -255,25 +255,32 @@ export class DataProviderService { for (const { currency, rootCurrency } of DERIVED_CURRENCIES) { if ( this.hasCurrency({ - dataGatheringItems, + assetProfileIdentifiers, currency: `${DEFAULT_CURRENCY}${currency}` }) ) { // Skip derived currency - dataGatheringItems = dataGatheringItems.filter(({ symbol }) => { - return symbol !== `${DEFAULT_CURRENCY}${currency}`; - }); + assetProfileIdentifiers = assetProfileIdentifiers.filter( + ({ symbol }) => { + return symbol !== `${DEFAULT_CURRENCY}${currency}`; + } + ); // Add root currency - dataGatheringItems.push({ + assetProfileIdentifiers.push({ dataSource: this.getDataSourceForExchangeRates(), symbol: `${DEFAULT_CURRENCY}${rootCurrency}` }); } } - dataGatheringItems = uniqWith(dataGatheringItems, (obj1, obj2) => { - return obj1.dataSource === obj2.dataSource && obj1.symbol === obj2.symbol; - }); + assetProfileIdentifiers = uniqWith( + assetProfileIdentifiers, + (obj1, obj2) => { + return ( + obj1.dataSource === obj2.dataSource && obj1.symbol === obj2.symbol + ); + } + ); const result: { [symbol: string]: { [date: string]: IDataProviderHistoricalResponse }; @@ -283,7 +290,7 @@ export class DataProviderService { data: { [date: string]: IDataProviderHistoricalResponse }; symbol: string; }>[] = []; - for (const { dataSource, symbol } of dataGatheringItems) { + for (const { dataSource, symbol } of assetProfileIdentifiers) { const dataProvider = this.getDataProvider(dataSource); if (dataProvider.canHandle(symbol)) { if (symbol === `${DEFAULT_CURRENCY}USX`) { @@ -418,7 +425,7 @@ export class DataProviderService { const promises: Promise[] = []; - for (const [dataSource, dataGatheringItems] of Object.entries( + for (const [dataSource, assetProfileIdentifiers] of Object.entries( itemsGroupedByDataSource )) { const dataProvider = this.getDataProvider(DataSource[dataSource]); @@ -431,7 +438,7 @@ export class DataProviderService { continue; } - const symbols = dataGatheringItems + const symbols = assetProfileIdentifiers .filter(({ symbol }) => { return !isDerivedCurrency(getCurrencyFromSymbol(symbol)); }) @@ -634,13 +641,13 @@ export class DataProviderService { } private hasCurrency({ - currency, - dataGatheringItems + assetProfileIdentifiers, + currency }: { + assetProfileIdentifiers: AssetProfileIdentifier[]; currency: string; - dataGatheringItems: AssetProfileIdentifier[]; }) { - return dataGatheringItems.some(({ dataSource, symbol }) => { + return assetProfileIdentifiers.some(({ dataSource, symbol }) => { return ( dataSource === this.getDataSourceForExchangeRates() && symbol === currency diff --git a/apps/api/src/services/queues/data-gathering/data-gathering.processor.ts b/apps/api/src/services/queues/data-gathering/data-gathering.processor.ts index dc8cc5996..eedad7475 100644 --- a/apps/api/src/services/queues/data-gathering/data-gathering.processor.ts +++ b/apps/api/src/services/queues/data-gathering/data-gathering.processor.ts @@ -89,7 +89,7 @@ export class DataGatheringProcessor { ); const historicalData = await this.dataProviderService.getHistoricalRaw({ - dataGatheringItems: [{ dataSource, symbol }], + assetProfileIdentifiers: [{ dataSource, symbol }], from: currentDate, to: new Date() }); diff --git a/apps/api/src/services/queues/data-gathering/data-gathering.service.ts b/apps/api/src/services/queues/data-gathering/data-gathering.service.ts index 72b8ac716..b79b2a098 100644 --- a/apps/api/src/services/queues/data-gathering/data-gathering.service.ts +++ b/apps/api/src/services/queues/data-gathering/data-gathering.service.ts @@ -122,7 +122,7 @@ export class DataGatheringService { }) { try { const historicalData = await this.dataProviderService.getHistoricalRaw({ - dataGatheringItems: [{ dataSource, symbol }], + assetProfileIdentifiers: [{ dataSource, symbol }], from: date, to: date });