Browse Source

Add option to group investments by year

pull/1568/head
yksolanki9 3 years ago
parent
commit
666d967888
  1. 33
      apps/api/src/app/portfolio/portfolio-calculator.ts
  2. 23
      apps/api/src/app/portfolio/portfolio.service.ts
  3. 3
      apps/client/src/app/pages/portfolio/analysis/analysis-page.component.ts
  4. 2
      libs/common/src/lib/types/group-by.type.ts

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

@ -2,6 +2,7 @@ import { TimelineInfoInterface } from '@ghostfolio/api/app/portfolio/interfaces/
import { IDataGatheringItem } from '@ghostfolio/api/services/interfaces/interfaces'; import { IDataGatheringItem } from '@ghostfolio/api/services/interfaces/interfaces';
import { DATE_FORMAT, parseDate, resetHours } from '@ghostfolio/common/helper'; import { DATE_FORMAT, parseDate, resetHours } from '@ghostfolio/common/helper';
import { ResponseError, TimelinePosition } from '@ghostfolio/common/interfaces'; import { ResponseError, TimelinePosition } from '@ghostfolio/common/interfaces';
import { GroupBy } from '@ghostfolio/common/types';
import { Logger } from '@nestjs/common'; import { Logger } from '@nestjs/common';
import { Type as TypeOfOrder } from '@prisma/client'; import { Type as TypeOfOrder } from '@prisma/client';
import Big from 'big.js'; import Big from 'big.js';
@ -478,46 +479,54 @@ export class PortfolioCalculator {
}); });
} }
public getInvestmentsByMonth(): { date: string; investment: Big }[] { public getInvestmentsByGroup(
groupBy: GroupBy
): { date: string; investment: Big }[] {
if (this.orders.length === 0) { if (this.orders.length === 0) {
return []; return [];
} }
const investments = []; const investments = [];
let currentDate: Date; let currentDate: Date;
let investmentByMonth = new Big(0); let investmentByGroup = new Big(0);
for (const [index, order] of this.orders.entries()) { for (const [index, order] of this.orders.entries()) {
if ( if (
isSameMonth(parseDate(order.date), currentDate) && isSameYear(parseDate(order.date), currentDate) &&
isSameYear(parseDate(order.date), currentDate) (groupBy === 'year' || isSameMonth(parseDate(order.date), currentDate))
) { ) {
// Same month: Add up investments // Same group: Add up investments
investmentByMonth = investmentByMonth.plus( investmentByGroup = investmentByGroup.plus(
order.quantity.mul(order.unitPrice).mul(this.getFactor(order.type)) order.quantity.mul(order.unitPrice).mul(this.getFactor(order.type))
); );
} else { } else {
// New month: Store previous month and reset // New group: Store previous group and reset
if (currentDate) { if (currentDate) {
investments.push({ investments.push({
date: format(set(currentDate, { date: 1 }), DATE_FORMAT), date: format(set(currentDate, { date: 1 }), DATE_FORMAT),
investment: investmentByMonth investment: investmentByGroup
}); });
} }
currentDate = parseDate(order.date); currentDate = parseDate(order.date);
investmentByMonth = order.quantity investmentByGroup = order.quantity
.mul(order.unitPrice) .mul(order.unitPrice)
.mul(this.getFactor(order.type)); .mul(this.getFactor(order.type));
} }
if (index === this.orders.length - 1) { if (index === this.orders.length - 1) {
// Store current month (latest order) // Store current group (latest order)
investments.push({ investments.push({
date: format(set(currentDate, { date: 1 }), DATE_FORMAT), date: format(
investment: investmentByMonth set(
currentDate,
groupBy === 'month' ? { date: 1 } : { date: 1, month: 1 }
),
DATE_FORMAT
),
investment: investmentByGroup
}); });
} }
} }

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

@ -282,26 +282,31 @@ export class PortfolioService {
let investments: InvestmentItem[]; let investments: InvestmentItem[];
if (groupBy === 'month') { if (groupBy) {
investments = portfolioCalculator.getInvestmentsByMonth().map((item) => { investments = portfolioCalculator
.getInvestmentsByGroup(groupBy)
.map((item) => {
return { return {
date: item.date, date: item.date,
investment: item.investment.toNumber() investment: item.investment.toNumber()
}; };
}); });
// Add investment of current month // Add investment of current group
const dateOfCurrentMonth = format( const dateOfCurrentGroup = format(
set(new Date(), { date: 1 }), set(
new Date(),
groupBy === 'month' ? { date: 1 } : { date: 1, month: 1 }
),
DATE_FORMAT DATE_FORMAT
); );
const investmentOfCurrentMonth = investments.filter(({ date }) => { const investmentOfCurrentGroup = investments.filter(({ date }) => {
return date === dateOfCurrentMonth; return date === dateOfCurrentGroup;
}); });
if (investmentOfCurrentMonth.length <= 0) { if (investmentOfCurrentGroup.length <= 0) {
investments.push({ investments.push({
date: dateOfCurrentMonth, date: dateOfCurrentGroup,
investment: 0 investment: 0
}); });
} }

3
apps/client/src/app/pages/portfolio/analysis/analysis-page.component.ts

@ -202,6 +202,7 @@ export class AnalysisPageComponent implements OnDestroy, OnInit {
public onChangeGroupBy(aMode: GroupBy) { public onChangeGroupBy(aMode: GroupBy) {
this.mode = aMode; this.mode = aMode;
this.update();
} }
public ngOnDestroy() { public ngOnDestroy() {
@ -308,7 +309,7 @@ export class AnalysisPageComponent implements OnDestroy, OnInit {
this.dataService this.dataService
.fetchInvestments({ .fetchInvestments({
filters: this.activeFilters, filters: this.activeFilters,
groupBy: 'month', groupBy: this.mode,
range: this.user?.settings?.dateRange range: this.user?.settings?.dateRange
}) })
.pipe(takeUntil(this.unsubscribeSubject)) .pipe(takeUntil(this.unsubscribeSubject))

2
libs/common/src/lib/types/group-by.type.ts

@ -1 +1 @@
export type GroupBy = 'month'; export type GroupBy = 'month' | 'year';

Loading…
Cancel
Save