optimize order loading

pull/239/head
Valentin Zickner 3 years ago committed by Thomas
parent f1b3c61675
commit 8e4ee7feea

@ -90,6 +90,7 @@ export class PortfolioController {
@Query('range') range, @Query('range') range,
@Res() res: Response @Res() res: Response
): Promise<HistoricalDataItem[]> { ): Promise<HistoricalDataItem[]> {
console.log(`------- ${range} ------`);
console.time('get-chart-request'); console.time('get-chart-request');
console.time('portfolio-service-get-chart'); console.time('portfolio-service-get-chart');
let chartData = await this.portfolioService.getChart( let chartData = await this.portfolioService.getChart(

@ -51,6 +51,7 @@ import {
HistoricalDataItem, HistoricalDataItem,
PortfolioPositionDetail PortfolioPositionDetail
} from './interfaces/portfolio-position-detail.interface'; } from './interfaces/portfolio-position-detail.interface';
import { OrderType } from '@ghostfolio/api/models/order-type';
@Injectable() @Injectable()
export class PortfolioService { export class PortfolioService {
@ -91,14 +92,7 @@ export class PortfolioService {
).createFromData({ orders, portfolioItems, user }); ).createFromData({ orders, portfolioItems, user });
} else { } else {
// Get portfolio from database // Get portfolio from database
const orders = await this.orderService.orders({ const orders = await this.getOrders(aUserId);
include: {
Account: true,
SymbolProfile: true
},
orderBy: { date: 'asc' },
where: { userId: aUserId }
});
portfolio = new Portfolio( portfolio = new Portfolio(
this.accountService, this.accountService,
@ -160,49 +154,26 @@ export class PortfolioService {
console.timeEnd('impersonation-service'); console.timeEnd('impersonation-service');
console.time('create-portfolio'); console.time('create-portfolio');
const portfolio = await this.createPortfolio( const userId = impersonationUserId || this.request.user.id;
impersonationUserId || this.request.user.id const orders = await this.getOrders(userId);
);
console.timeEnd('create-portfolio'); console.timeEnd('create-portfolio');
const orders = portfolio.getOrders();
if (orders.length <= 0) { if (orders.length <= 0) {
return []; return [];
} }
const dateRangeDate = this.convertDateRangeToDate(
aDateRange,
portfolio.getMinDate()
);
const portfolioCalculator = new PortfolioCalculator( const portfolioCalculator = new PortfolioCalculator(
this.currentRateService, this.currentRateService,
this.request.user.Settings.currency this.request.user.Settings.currency
); );
const portfolioOrders: PortfolioOrder[] = orders const portfolioOrders: PortfolioOrder[] = orders.map((order) => ({
.filter((portfolioItem) => { date: format(order.date, 'yyyy-MM-dd'),
if (isAfter(parseISO(portfolioItem.getDate()), endOfToday())) { quantity: new Big(order.quantity),
// Filter out future dates symbol: order.symbol,
return false; type: <OrderType>order.type,
} unitPrice: new Big(order.unitPrice),
currency: order.currency
if (dateRangeDate === undefined) {
return true;
}
return (
isSameDay(parseISO(portfolioItem.getDate()), dateRangeDate) ||
isAfter(parseISO(portfolioItem.getDate()), dateRangeDate)
);
})
.map((order) => ({
date: order.getDate().substr(0, 10),
quantity: new Big(order.getQuantity()),
symbol: order.getSymbol(),
type: order.getType(),
unitPrice: new Big(order.getUnitPrice()),
currency: order.getCurrency()
})); }));
portfolioCalculator.computeTransactionPoints(portfolioOrders); portfolioCalculator.computeTransactionPoints(portfolioOrders);
const transactionPoints = portfolioCalculator.getTransactionPoints(); const transactionPoints = portfolioCalculator.getTransactionPoints();
@ -505,4 +476,17 @@ export class PortfolioService {
return undefined; return undefined;
} }
} }
private getOrders(aUserId: string) {
return this.orderService.orders({
include: {
// eslint-disable-next-line @typescript-eslint/naming-convention
Account: true,
// eslint-disable-next-line @typescript-eslint/naming-convention
SymbolProfile: true
},
orderBy: { date: 'asc' },
where: { userId: aUserId }
});
}
} }

Loading…
Cancel
Save