From be5a1a6518b602743a6c4754eaf1902257036cd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20G=C3=B3mez?= Date: Fri, 6 Dec 2024 00:58:12 +0100 Subject: [PATCH] Add logs to scheduled tasks (#667) --- src/companies/domain/companies.service.ts | 6 +++++- src/portfolios/domain/portfolios.service.ts | 9 ++++++--- src/portfolios/domain/positions.service.ts | 18 ++++++++++++++---- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/companies/domain/companies.service.ts b/src/companies/domain/companies.service.ts index f9d25f17..b7a7c927 100644 --- a/src/companies/domain/companies.service.ts +++ b/src/companies/domain/companies.service.ts @@ -130,12 +130,16 @@ export class CompaniesService implements OnApplicationBootstrap { const companies = await this.repository.findAll(); await Promise.all( companies.map(async (company) => { - await this.companyStatesService.createCompanyState(company); + const companyState = + await this.companyStatesService.createCompanyState(company); const metrics = await this.companyStatesService.getMetricsByCompanyUuid( company.uuid, ); await this.repository.updateMetricsByUuid(company.uuid, metrics); + this.logger.log( + `${company.symbol} refreshed: price ${companyState.price} [PEG: ${companyState.peg} (avg: ${metrics.avgPeg}), EV/Rev: ${companyState.enterpriseToRevenue} (avg: ${metrics.avgEnterpriseToRevenue}), EV/Ebitda: ${companyState.enterpriseToEbitda} (avg: ${metrics.avgEnterpriseToEbitda}), Short %: ${companyState.shortPercentOfFloat}]`, + ); }), ); } catch (err) { diff --git a/src/portfolios/domain/portfolios.service.ts b/src/portfolios/domain/portfolios.service.ts index f4e2d278..63c0fbe0 100644 --- a/src/portfolios/domain/portfolios.service.ts +++ b/src/portfolios/domain/portfolios.service.ts @@ -405,9 +405,12 @@ export class PortfoliosService implements OnApplicationBootstrap { try { const portfolios = await this.repository.findAll(); await Promise.all( - portfolios.map((portfolio) => - this.positionService.updatePortfolioState(portfolio), - ), + portfolios.map((portfolio) => { + this.logger.log( + `Refreshing portfolio ${portfolio.name} (uuid: ${portfolio.uuid})`, + ); + return this.positionService.updatePortfolioState(portfolio); + }), ); } catch (err) { this.logger.error( diff --git a/src/portfolios/domain/positions.service.ts b/src/portfolios/domain/positions.service.ts index 2d774c7d..0efb5bc0 100644 --- a/src/portfolios/domain/positions.service.ts +++ b/src/portfolios/domain/positions.service.ts @@ -1,6 +1,7 @@ import { ConflictException, Injectable, + Logger, NotFoundException, UnauthorizedException, } from '@nestjs/common'; @@ -20,6 +21,8 @@ import { PortfolioStatesService } from './portfolio-states.service'; @Injectable() export class PositionsService { + private readonly logger = new Logger(PositionsService.name); + constructor( private readonly repository: PositionsRepository, private readonly portfoliosRepository: PortfoliosRepository, @@ -208,11 +211,18 @@ export class PositionsService { } async updatePortfolioState(portfolio: Portfolio) { - const positions = await this.getPositionDetailsByPortfolioUuid(portfolio); - await this.portfolioStatesService.createPortfolioState( - portfolio, - this.mapToPositions(positions, portfolio.uuid), // TODO: refactor when implementing PositionState + const positionDetailDTOs = + await this.getPositionDetailsByPortfolioUuid(portfolio); + const positions = this.mapToPositions(positionDetailDTOs, portfolio.uuid); // TODO: refactor when implementing PositionState + const portfolioState = + await this.portfolioStatesService.createPortfolioState( + portfolio, + positions, + ); + this.logger.log( + `Updated portfolio ${portfolio.name} state. Total value: ${portfolioState.totalValueEUR}€, ROIC: ${portfolioState.roicEUR}€`, ); + return portfolioState; } private checkOwner(user: User, portfolio: Portfolio): void {