prepare for multi-day database fetch
This commit is contained in:
parent
b4dc21dd61
commit
66e7ad3fd2
@ -15,11 +15,15 @@ import {
|
|||||||
addDays,
|
addDays,
|
||||||
addMonths,
|
addMonths,
|
||||||
addYears,
|
addYears,
|
||||||
|
endOfDay,
|
||||||
format,
|
format,
|
||||||
isAfter,
|
isAfter,
|
||||||
isBefore,
|
isBefore,
|
||||||
|
max,
|
||||||
|
min,
|
||||||
parse
|
parse
|
||||||
} from 'date-fns';
|
} from 'date-fns';
|
||||||
|
import { flatten } from 'lodash';
|
||||||
|
|
||||||
const DATE_FORMAT = 'yyyy-MM-dd';
|
const DATE_FORMAT = 'yyyy-MM-dd';
|
||||||
|
|
||||||
@ -168,7 +172,7 @@ export class PortfolioCalculator {
|
|||||||
const start = dparse(startDate);
|
const start = dparse(startDate);
|
||||||
const end = dparse(endDate);
|
const end = dparse(endDate);
|
||||||
|
|
||||||
const timelinePeriodPromises: Promise<TimelinePeriod>[] = [];
|
const timelinePeriodPromises: Promise<TimelinePeriod[]>[] = [];
|
||||||
let i = 0;
|
let i = 0;
|
||||||
let j = -1;
|
let j = -1;
|
||||||
for (
|
for (
|
||||||
@ -188,33 +192,50 @@ export class PortfolioCalculator {
|
|||||||
) {
|
) {
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
const timePeriodForDate = this.getTimePeriodForDate(j, currentDate);
|
|
||||||
if (timePeriodForDate != null) {
|
let endDate = endOfDay(currentDate);
|
||||||
timelinePeriodPromises.push(timePeriodForDate);
|
if (timelineSpecification[i].accuracy === 'day') {
|
||||||
|
let nextEndDate: Date = end;
|
||||||
|
if (j + 1 < this.transactionPoints.length) {
|
||||||
|
nextEndDate = dparse(this.transactionPoints[j + 1].date);
|
||||||
|
}
|
||||||
|
endDate = min([
|
||||||
|
addMonths(currentDate, 1),
|
||||||
|
max([currentDate, nextEndDate])
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
const timePeriodForDates = this.getTimePeriodForDate(
|
||||||
|
j,
|
||||||
|
currentDate,
|
||||||
|
endDate
|
||||||
|
);
|
||||||
|
if (timePeriodForDates != null) {
|
||||||
|
timelinePeriodPromises.push(timePeriodForDates);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.timeEnd('calculate-timeline-calculations');
|
console.timeEnd('calculate-timeline-calculations');
|
||||||
|
|
||||||
console.time('calculate-timeline-periods');
|
console.time('calculate-timeline-periods');
|
||||||
|
|
||||||
const timelinePeriods: TimelinePeriod[] = await Promise.all(
|
const timelinePeriods: TimelinePeriod[][] = await Promise.all(
|
||||||
timelinePeriodPromises
|
timelinePeriodPromises
|
||||||
);
|
);
|
||||||
|
|
||||||
console.timeEnd('calculate-timeline-periods');
|
console.timeEnd('calculate-timeline-periods');
|
||||||
console.timeEnd('calculate-timeline-total');
|
console.timeEnd('calculate-timeline-total');
|
||||||
|
|
||||||
return timelinePeriods;
|
return flatten(timelinePeriods);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getTimePeriodForDate(
|
private async getTimePeriodForDate(
|
||||||
j: number,
|
j: number,
|
||||||
currentDate: Date
|
startDate: Date,
|
||||||
): Promise<TimelinePeriod> {
|
endDate: Date
|
||||||
|
): Promise<TimelinePeriod[]> {
|
||||||
let investment: Big = new Big(0);
|
let investment: Big = new Big(0);
|
||||||
|
|
||||||
let value = new Big(0);
|
let value = new Big(0);
|
||||||
const currentDateAsString = format(currentDate, DATE_FORMAT);
|
const currentDateAsString = format(startDate, DATE_FORMAT);
|
||||||
if (j >= 0) {
|
if (j >= 0) {
|
||||||
const currencies: { [name: string]: Currency } = {};
|
const currencies: { [name: string]: Currency } = {};
|
||||||
const symbols: string[] = [];
|
const symbols: string[] = [];
|
||||||
@ -229,15 +250,15 @@ export class PortfolioCalculator {
|
|||||||
if (symbols.length > 0) {
|
if (symbols.length > 0) {
|
||||||
try {
|
try {
|
||||||
marketSymbols = await this.currentRateService.getValues({
|
marketSymbols = await this.currentRateService.getValues({
|
||||||
dateRangeStart: resetHours(currentDate),
|
dateRangeStart: resetHours(startDate),
|
||||||
dateRangeEnd: resetHours(currentDate),
|
dateRangeEnd: resetHours(startDate),
|
||||||
symbols,
|
symbols,
|
||||||
currencies,
|
currencies,
|
||||||
userCurrency: this.currency
|
userCurrency: this.currency
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(
|
console.error(
|
||||||
`failed to fetch info for date ${currentDate} with exception`,
|
`failed to fetch info for date ${startDate} with exception`,
|
||||||
e
|
e
|
||||||
);
|
);
|
||||||
return null;
|
return null;
|
||||||
@ -269,12 +290,14 @@ export class PortfolioCalculator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return [
|
||||||
date: currentDateAsString,
|
{
|
||||||
grossPerformance: value.minus(investment),
|
date: currentDateAsString,
|
||||||
investment,
|
grossPerformance: value.minus(investment),
|
||||||
value
|
investment,
|
||||||
};
|
value
|
||||||
|
}
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
private getFactor(type: OrderType) {
|
private getFactor(type: OrderType) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user