Browse Source

Fix end date of calendar year date ranges

pull/8095/head
Thomas Kaul 1 week ago
parent
commit
dc0109dd06
  1. 9
      apps/api/src/app/portfolio/calculator/portfolio-calculator.ts
  2. 44
      apps/api/src/app/portfolio/calculator/roai/portfolio-calculator.spec.ts

9
apps/api/src/app/portfolio/calculator/portfolio-calculator.ts

@ -1407,7 +1407,7 @@ export abstract class PortfolioCalculator {
return this.snapshot.totalLiabilitiesWithCurrencyEffect; return this.snapshot.totalLiabilitiesWithCurrencyEffect;
} }
public async getPerformance({ end, start }) { public async getPerformance({ end, start }: { end: Date; start: Date }) {
await this.snapshotPromise; await this.snapshotPromise;
const { historicalData } = this.snapshot; const { historicalData } = this.snapshot;
@ -1418,6 +1418,11 @@ export abstract class PortfolioCalculator {
let netPerformanceAtStartDate: number; let netPerformanceAtStartDate: number;
let netPerformanceWithCurrencyEffectAtStartDate: number; let netPerformanceWithCurrencyEffectAtStartDate: number;
// The historical data items are dated at midnight in UTC, hence a start
// or an end at the end of a day in the time zone of the instance is
// changed to this date
const endDate = isEqual(end, endOfDay(end)) ? resetHours(end) : end;
const startDate = isEqual(start, endOfDay(start)) const startDate = isEqual(start, endOfDay(start))
? resetHours(start) ? resetHours(start)
: start; : start;
@ -1425,7 +1430,7 @@ export abstract class PortfolioCalculator {
for (const historicalDataItem of historicalData) { for (const historicalDataItem of historicalData) {
const date = resetHours(parseDate(historicalDataItem.date)); const date = resetHours(parseDate(historicalDataItem.date));
if (!isBefore(date, startDate) && !isAfter(date, end)) { if (!isBefore(date, startDate) && !isAfter(date, endDate)) {
// Take the values at the start date from the first day of the date // Take the values at the start date from the first day of the date
// range // range
if (historicalDataItemsOfDateRange.length === 0) { if (historicalDataItemsOfDateRange.length === 0) {

44
apps/api/src/app/portfolio/calculator/roai/portfolio-calculator.spec.ts

@ -9,6 +9,7 @@ import { ConfigurationService } from '@ghostfolio/api/services/configuration/con
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service';
import { PortfolioSnapshotService } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service'; import { PortfolioSnapshotService } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service';
import { PortfolioSnapshotServiceMock } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service.mock'; import { PortfolioSnapshotServiceMock } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service.mock';
import { getIntervalFromDateRange } from '@ghostfolio/common/calculation-helper';
import { HistoricalDataItem } from '@ghostfolio/common/interfaces'; import { HistoricalDataItem } from '@ghostfolio/common/interfaces';
import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type'; import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type';
@ -183,4 +184,47 @@ describe('PortfolioCalculator', () => {
).toBeCloseTo(0.23, 10); ).toBeCloseTo(0.23, 10);
}); });
}); });
describe('get performance', () => {
// The chart items are dated at midnight in UTC, but a calendar year date
// range ends at the end of 31 December in the time zone of the instance.
// Hence this test must give the same result with the instance in any time
// zone. Set TEST_TZ to run it with the instance in another time zone.
it('with a calendar year date range', async () => {
const snapshot = await portfolioCalculator.getSnapshot();
snapshot.historicalData = [
getHistoricalDataItem({
date: '2016-12-31',
investment: 100,
netPerformance: 0
}),
getHistoricalDataItem({
date: '2017-12-31',
investment: 100,
netPerformance: 10
}),
getHistoricalDataItem({
date: '2018-01-01',
investment: 100,
netPerformance: 20
})
];
const { endDate, startDate } = getIntervalFromDateRange({
dateRange: '2017'
});
const { chart } = await portfolioCalculator.getPerformance({
end: endDate,
start: startDate
});
expect(
chart.map(({ date }) => {
return date;
})
).toEqual(['2016-12-31', '2017-12-31']);
});
});
}); });

Loading…
Cancel
Save