From cbe079ae66f482f39a394b2737a8d46823c4c516 Mon Sep 17 00:00:00 2001 From: Valentin Zickner Date: Tue, 13 Jul 2021 22:51:32 +0200 Subject: [PATCH] ignore missing values --- apps/api/src/app/core/portfolio-calculator.ts | 27 ++++++++++++++----- .../src/app/portfolio/portfolio.service.ts | 14 +++++----- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/apps/api/src/app/core/portfolio-calculator.ts b/apps/api/src/app/core/portfolio-calculator.ts index 6ad5ea838..11cd13ecb 100644 --- a/apps/api/src/app/core/portfolio-calculator.ts +++ b/apps/api/src/app/core/portfolio-calculator.ts @@ -169,7 +169,10 @@ export class PortfolioCalculator { ) { j++; } - timelinePeriodPromises.push(this.getTimePeriodForDate(j, currentDate)); + const timePeriodForDate = this.getTimePeriodForDate(j, currentDate); + if (timePeriodForDate != null) { + timelinePeriodPromises.push(timePeriodForDate); + } } console.timeEnd('calculate-timeline-calculations'); @@ -185,7 +188,10 @@ export class PortfolioCalculator { return timelinePeriods; } - private async getTimePeriodForDate(j: number, currentDate: Date) { + private async getTimePeriodForDate( + j: number, + currentDate: Date + ): Promise { let investment: Big = new Big(0); const promises = []; if (j >= 0) { @@ -204,10 +210,19 @@ export class PortfolioCalculator { } } - const value = (await Promise.all(promises)).reduce( - (a, b) => a.add(b), - new Big(0) - ); + const result = await Promise.all(promises).catch((e) => { + console.error( + `failed to fetch info for date ${currentDate} with exception`, + e + ); + return null; + }); + + if (result == null) { + return null; + } + + const value = result.reduce((a, b) => a.add(b), new Big(0)); return { date: format(currentDate, DATE_FORMAT), grossPerformance: value.minus(investment), diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index 82a6208df..2b829722c 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -26,7 +26,6 @@ import Big from 'big.js'; import { add, addMonths, - endOfToday, format, getDate, getMonth, @@ -43,7 +42,6 @@ import { subDays, subYears } from 'date-fns'; -import { port } from 'envalid'; import { isEmpty } from 'lodash'; import * as roundTo from 'round-to'; @@ -204,11 +202,13 @@ export class PortfolioService { format(new Date(), dateFormat) ); - return timeline.map((timelineItem) => ({ - date: timelineItem.date, - value: timelineItem.grossPerformance, - marketPrice: timelineItem.value - })); + return timeline + .filter((timelineItem) => timelineItem !== null) + .map((timelineItem) => ({ + date: timelineItem.date, + value: timelineItem.grossPerformance, + marketPrice: timelineItem.value + })); } private getStartDate(aDateRange: DateRange, portfolioStart: Date) {