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 cachedPortfolioSnapshot: PortfolioSnapshot;
let isCachedPortfolioSnapshotExpired = false; let isCachedPortfolioSnapshotExpired = false;
const jobId = this.userId; const portfolioSnapshotKey = this.redisCacheService.getPortfolioSnapshotKey(
{
filters: this.filters,
userId: this.userId
}
);
const jobId = portfolioSnapshotKey;
try { try {
const cachedPortfolioSnapshotValue = await this.redisCacheService.get( const cachedPortfolioSnapshotValue =
this.redisCacheService.getPortfolioSnapshotKey({ await this.redisCacheService.get(portfolioSnapshotKey);
filters: this.filters,
userId: this.userId
})
);
const { expiration, portfolioSnapshot }: PortfolioSnapshotValue = const { expiration, portfolioSnapshot }: PortfolioSnapshotValue =
JSON.parse(cachedPortfolioSnapshotValue); 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 user = await this.userService.user({ id: userId });
const userSettings = user.settings.settings; const userSettings = user.settings.settings;
const filters = this.apiService.buildFiltersFromUserSettings({
userSettings
});
// Recompute in the background to avoid a cold start on the next request // Recompute in the background to avoid a cold start on the next request
await this.portfolioSnapshotService.addJobToQueue({ await this.portfolioSnapshotService.addJobToQueue({
data: { data: {
filters,
userId, userId,
calculationType: userSettings.performanceCalculationType, calculationType: userSettings.performanceCalculationType,
filters: this.apiService.buildFiltersFromUserSettings({
userSettings
}),
userCurrency: userSettings.baseCurrency userCurrency: userSettings.baseCurrency
}, },
name: PORTFOLIO_SNAPSHOT_PROCESS_JOB_NAME, name: PORTFOLIO_SNAPSHOT_PROCESS_JOB_NAME,
opts: { opts: {
...PORTFOLIO_SNAPSHOT_PROCESS_JOB_OPTIONS, ...PORTFOLIO_SNAPSHOT_PROCESS_JOB_OPTIONS,
jobId: userId, jobId: this.redisCacheService.getPortfolioSnapshotKey({
filters,
userId
}),
priority: PORTFOLIO_SNAPSHOT_COMPUTATION_QUEUE_PRIORITY_LOW priority: PORTFOLIO_SNAPSHOT_COMPUTATION_QUEUE_PRIORITY_LOW
} }
}); });
} catch (error) { } catch (error) {
this.logger.error(error); this.logger.error(
`Portfolio snapshot of user '${userId}' could not be recomputed`,
error
);
} }
} }
} }

Loading…
Cancel
Save