Skip to content

Commit

Permalink
Add logs to scheduled tasks (#667)
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorgomezv authored Dec 5, 2024
1 parent 77ff85e commit be5a1a6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/companies/domain/companies.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 6 additions & 3 deletions src/portfolios/domain/portfolios.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
18 changes: 14 additions & 4 deletions src/portfolios/domain/positions.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ConflictException,
Injectable,
Logger,
NotFoundException,
UnauthorizedException,
} from '@nestjs/common';
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit be5a1a6

Please sign in to comment.