Browse Source

Recompute portfolio snapshot on portfolio change

pull/7403/head
Thomas Kaul 1 month ago
parent
commit
b5777d3e70
  1. 17
      apps/api/src/app/portfolio/calculator/portfolio-calculator.ts
  2. 18
      apps/api/src/events/portfolio-changed.listener.ts

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

@ -1097,15 +1097,18 @@ export abstract class PortfolioCalculator {
let cachedPortfolioSnapshot: PortfolioSnapshot;
let isCachedPortfolioSnapshotExpired = false;
const jobId = this.userId;
const portfolioSnapshotKey = this.redisCacheService.getPortfolioSnapshotKey(
{
filters: this.filters,
userId: this.userId
}
);
const jobId = portfolioSnapshotKey;
try {
const cachedPortfolioSnapshotValue = await this.redisCacheService.get(
this.redisCacheService.getPortfolioSnapshotKey({
filters: this.filters,
userId: this.userId
})
);
const cachedPortfolioSnapshotValue =
await this.redisCacheService.get(portfolioSnapshotKey);
const { expiration, portfolioSnapshot }: PortfolioSnapshotValue =
JSON.parse(cachedPortfolioSnapshotValue);

18
apps/api/src/events/portfolio-changed.listener.ts

@ -58,25 +58,33 @@ export class PortfolioChangedListener {
const user = await this.userService.user({ id: userId });
const userSettings = user.settings.settings;
const filters = this.apiService.buildFiltersFromUserSettings({
userSettings
});
// Recompute in the background to avoid a cold start on the next request
await this.portfolioSnapshotService.addJobToQueue({
data: {
filters,
userId,
calculationType: userSettings.performanceCalculationType,
filters: this.apiService.buildFiltersFromUserSettings({
userSettings
}),
userCurrency: userSettings.baseCurrency
},
name: PORTFOLIO_SNAPSHOT_PROCESS_JOB_NAME,
opts: {
...PORTFOLIO_SNAPSHOT_PROCESS_JOB_OPTIONS,
jobId: userId,
jobId: this.redisCacheService.getPortfolioSnapshotKey({
filters,
userId
}),
priority: PORTFOLIO_SNAPSHOT_COMPUTATION_QUEUE_PRIORITY_LOW
}
});
} catch (error) {
this.logger.error(error);
this.logger.error(
`Portfolio snapshot of user '${userId}' could not be recomputed`,
error
);
}
}
}

Loading…
Cancel
Save