Feature/skip caching in portfolio calculator if active filters (#3348)
* Skip caching if active filters * Update changelog
This commit is contained in:
parent
261f5844dd
commit
a5833566a8
@ -14,6 +14,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
- Added a form validation against the DTO in the platform management of the admin control panel
|
||||
- Added a form validation against the DTO in the tag management of the admin control panel
|
||||
|
||||
### Changed
|
||||
|
||||
- Skipped the caching in the portfolio calculator if there are active filters (experimental)
|
||||
|
||||
### Fixed
|
||||
|
||||
- Fixed an issue in the calculation of the portfolio summary caused by future liabilities
|
||||
|
@ -32,6 +32,7 @@ export class PortfolioCalculatorFactory {
|
||||
calculationType,
|
||||
currency,
|
||||
dateRange = 'max',
|
||||
hasFilters,
|
||||
isExperimentalFeatures = false,
|
||||
userId
|
||||
}: {
|
||||
@ -40,9 +41,12 @@ export class PortfolioCalculatorFactory {
|
||||
calculationType: PerformanceCalculationType;
|
||||
currency: string;
|
||||
dateRange?: DateRange;
|
||||
hasFilters: boolean;
|
||||
isExperimentalFeatures?: boolean;
|
||||
userId: string;
|
||||
}): PortfolioCalculator {
|
||||
const useCache = !hasFilters && isExperimentalFeatures;
|
||||
|
||||
switch (calculationType) {
|
||||
case PerformanceCalculationType.MWR:
|
||||
return new MWRPortfolioCalculator({
|
||||
@ -50,7 +54,7 @@ export class PortfolioCalculatorFactory {
|
||||
activities,
|
||||
currency,
|
||||
dateRange,
|
||||
isExperimentalFeatures,
|
||||
useCache,
|
||||
userId,
|
||||
configurationService: this.configurationService,
|
||||
currentRateService: this.currentRateService,
|
||||
@ -64,7 +68,7 @@ export class PortfolioCalculatorFactory {
|
||||
currency,
|
||||
currentRateService: this.currentRateService,
|
||||
dateRange,
|
||||
isExperimentalFeatures,
|
||||
useCache,
|
||||
userId,
|
||||
configurationService: this.configurationService,
|
||||
exchangeRateDataService: this.exchangeRateDataService,
|
||||
|
@ -56,14 +56,15 @@ export abstract class PortfolioCalculator {
|
||||
private currency: string;
|
||||
private currentRateService: CurrentRateService;
|
||||
private dataProviderInfos: DataProviderInfo[];
|
||||
private dateRange: DateRange;
|
||||
private endDate: Date;
|
||||
private exchangeRateDataService: ExchangeRateDataService;
|
||||
private isExperimentalFeatures: boolean;
|
||||
private redisCacheService: RedisCacheService;
|
||||
private snapshot: PortfolioSnapshot;
|
||||
private snapshotPromise: Promise<void>;
|
||||
private startDate: Date;
|
||||
private transactionPoints: TransactionPoint[];
|
||||
private useCache: boolean;
|
||||
private userId: string;
|
||||
|
||||
public constructor({
|
||||
@ -74,8 +75,8 @@ export abstract class PortfolioCalculator {
|
||||
currentRateService,
|
||||
dateRange,
|
||||
exchangeRateDataService,
|
||||
isExperimentalFeatures,
|
||||
redisCacheService,
|
||||
useCache,
|
||||
userId
|
||||
}: {
|
||||
accountBalanceItems: HistoricalDataItem[];
|
||||
@ -85,16 +86,16 @@ export abstract class PortfolioCalculator {
|
||||
currentRateService: CurrentRateService;
|
||||
dateRange: DateRange;
|
||||
exchangeRateDataService: ExchangeRateDataService;
|
||||
isExperimentalFeatures: boolean;
|
||||
redisCacheService: RedisCacheService;
|
||||
useCache: boolean;
|
||||
userId: string;
|
||||
}) {
|
||||
this.accountBalanceItems = accountBalanceItems;
|
||||
this.configurationService = configurationService;
|
||||
this.currency = currency;
|
||||
this.currentRateService = currentRateService;
|
||||
this.dateRange = dateRange;
|
||||
this.exchangeRateDataService = exchangeRateDataService;
|
||||
this.isExperimentalFeatures = isExperimentalFeatures;
|
||||
|
||||
this.activities = activities
|
||||
.map(
|
||||
@ -129,6 +130,7 @@ export abstract class PortfolioCalculator {
|
||||
});
|
||||
|
||||
this.redisCacheService = redisCacheService;
|
||||
this.useCache = useCache;
|
||||
this.userId = userId;
|
||||
|
||||
const { endDate, startDate } = getInterval(dateRange);
|
||||
@ -1047,11 +1049,13 @@ export abstract class PortfolioCalculator {
|
||||
}
|
||||
|
||||
private async initialize() {
|
||||
if (this.isExperimentalFeatures) {
|
||||
if (this.useCache) {
|
||||
const startTimeTotal = performance.now();
|
||||
|
||||
const cachedSnapshot = await this.redisCacheService.get(
|
||||
this.redisCacheService.getPortfolioSnapshotKey(this.userId)
|
||||
this.redisCacheService.getPortfolioSnapshotKey({
|
||||
userId: this.userId
|
||||
})
|
||||
);
|
||||
|
||||
if (cachedSnapshot) {
|
||||
@ -1074,7 +1078,9 @@ export abstract class PortfolioCalculator {
|
||||
);
|
||||
|
||||
this.redisCacheService.set(
|
||||
this.redisCacheService.getPortfolioSnapshotKey(this.userId),
|
||||
this.redisCacheService.getPortfolioSnapshotKey({
|
||||
userId: this.userId
|
||||
}),
|
||||
JSON.stringify(this.snapshot),
|
||||
this.configurationService.get('CACHE_QUOTES_TTL')
|
||||
);
|
||||
|
@ -123,6 +123,7 @@ describe('PortfolioCalculator', () => {
|
||||
activities,
|
||||
calculationType: PerformanceCalculationType.TWR,
|
||||
currency: 'CHF',
|
||||
hasFilters: false,
|
||||
userId: userDummyData.id
|
||||
});
|
||||
|
||||
|
@ -108,6 +108,7 @@ describe('PortfolioCalculator', () => {
|
||||
activities,
|
||||
calculationType: PerformanceCalculationType.TWR,
|
||||
currency: 'CHF',
|
||||
hasFilters: false,
|
||||
userId: userDummyData.id
|
||||
});
|
||||
|
||||
|
@ -93,6 +93,7 @@ describe('PortfolioCalculator', () => {
|
||||
activities,
|
||||
calculationType: PerformanceCalculationType.TWR,
|
||||
currency: 'CHF',
|
||||
hasFilters: false,
|
||||
userId: userDummyData.id
|
||||
});
|
||||
|
||||
|
@ -121,6 +121,7 @@ describe('PortfolioCalculator', () => {
|
||||
activities,
|
||||
calculationType: PerformanceCalculationType.TWR,
|
||||
currency: 'CHF',
|
||||
hasFilters: false,
|
||||
userId: userDummyData.id
|
||||
});
|
||||
|
||||
|
@ -93,6 +93,7 @@ describe('PortfolioCalculator', () => {
|
||||
activities,
|
||||
calculationType: PerformanceCalculationType.TWR,
|
||||
currency: 'USD',
|
||||
hasFilters: false,
|
||||
userId: userDummyData.id
|
||||
});
|
||||
|
||||
|
@ -106,6 +106,7 @@ describe('PortfolioCalculator', () => {
|
||||
activities,
|
||||
calculationType: PerformanceCalculationType.TWR,
|
||||
currency: 'CHF',
|
||||
hasFilters: false,
|
||||
userId: userDummyData.id
|
||||
});
|
||||
|
||||
|
@ -93,6 +93,7 @@ describe('PortfolioCalculator', () => {
|
||||
activities,
|
||||
calculationType: PerformanceCalculationType.TWR,
|
||||
currency: 'USD',
|
||||
hasFilters: false,
|
||||
userId: userDummyData.id
|
||||
});
|
||||
|
||||
|
@ -93,6 +93,7 @@ describe('PortfolioCalculator', () => {
|
||||
activities,
|
||||
calculationType: PerformanceCalculationType.TWR,
|
||||
currency: 'USD',
|
||||
hasFilters: false,
|
||||
userId: userDummyData.id
|
||||
});
|
||||
|
||||
|
@ -121,6 +121,7 @@ describe('PortfolioCalculator', () => {
|
||||
activities,
|
||||
calculationType: PerformanceCalculationType.TWR,
|
||||
currency: 'USD',
|
||||
hasFilters: false,
|
||||
userId: userDummyData.id
|
||||
});
|
||||
|
||||
|
@ -71,6 +71,7 @@ describe('PortfolioCalculator', () => {
|
||||
activities: [],
|
||||
calculationType: PerformanceCalculationType.TWR,
|
||||
currency: 'CHF',
|
||||
hasFilters: false,
|
||||
userId: userDummyData.id
|
||||
});
|
||||
|
||||
|
@ -108,6 +108,7 @@ describe('PortfolioCalculator', () => {
|
||||
activities,
|
||||
calculationType: PerformanceCalculationType.TWR,
|
||||
currency: 'CHF',
|
||||
hasFilters: false,
|
||||
userId: userDummyData.id
|
||||
});
|
||||
|
||||
|
@ -108,6 +108,7 @@ describe('PortfolioCalculator', () => {
|
||||
activities,
|
||||
calculationType: PerformanceCalculationType.TWR,
|
||||
currency: 'CHF',
|
||||
hasFilters: false,
|
||||
userId: userDummyData.id
|
||||
});
|
||||
|
||||
|
@ -277,9 +277,11 @@ export class PortfolioService {
|
||||
|
||||
const portfolioCalculator = this.calculatorFactory.createCalculator({
|
||||
activities,
|
||||
dateRange,
|
||||
userId,
|
||||
calculationType: PerformanceCalculationType.TWR,
|
||||
currency: this.request.user.Settings.settings.baseCurrency,
|
||||
hasFilters: filters?.length > 0,
|
||||
isExperimentalFeatures:
|
||||
this.request.user.Settings.settings.isExperimentalFeatures
|
||||
});
|
||||
@ -358,6 +360,7 @@ export class PortfolioService {
|
||||
userId,
|
||||
calculationType: PerformanceCalculationType.TWR,
|
||||
currency: userCurrency,
|
||||
hasFilters: filters?.length > 0,
|
||||
isExperimentalFeatures:
|
||||
this.request.user?.Settings.settings.isExperimentalFeatures
|
||||
});
|
||||
@ -660,6 +663,7 @@ export class PortfolioService {
|
||||
}),
|
||||
calculationType: PerformanceCalculationType.TWR,
|
||||
currency: userCurrency,
|
||||
hasFilters: true,
|
||||
isExperimentalFeatures:
|
||||
this.request.user.Settings.settings.isExperimentalFeatures
|
||||
});
|
||||
@ -931,6 +935,7 @@ export class PortfolioService {
|
||||
userId,
|
||||
calculationType: PerformanceCalculationType.TWR,
|
||||
currency: this.request.user.Settings.settings.baseCurrency,
|
||||
hasFilters: filters?.length > 0,
|
||||
isExperimentalFeatures:
|
||||
this.request.user.Settings.settings.isExperimentalFeatures
|
||||
});
|
||||
@ -1085,7 +1090,7 @@ export class PortfolioService {
|
||||
)
|
||||
);
|
||||
|
||||
const { endDate, startDate } = getInterval(dateRange);
|
||||
const { endDate } = getInterval(dateRange);
|
||||
|
||||
const { activities } = await this.orderService.getOrders({
|
||||
endDate,
|
||||
@ -1123,6 +1128,7 @@ export class PortfolioService {
|
||||
userId,
|
||||
calculationType: PerformanceCalculationType.TWR,
|
||||
currency: userCurrency,
|
||||
hasFilters: filters?.length > 0,
|
||||
isExperimentalFeatures:
|
||||
this.request.user.Settings.settings.isExperimentalFeatures
|
||||
});
|
||||
@ -1220,6 +1226,7 @@ export class PortfolioService {
|
||||
userId,
|
||||
calculationType: PerformanceCalculationType.TWR,
|
||||
currency: this.request.user.Settings.settings.baseCurrency,
|
||||
hasFilters: false,
|
||||
isExperimentalFeatures:
|
||||
this.request.user.Settings.settings.isExperimentalFeatures
|
||||
});
|
||||
|
@ -24,7 +24,7 @@ export class RedisCacheService {
|
||||
return this.cache.get(key);
|
||||
}
|
||||
|
||||
public getPortfolioSnapshotKey(userId: string) {
|
||||
public getPortfolioSnapshotKey({ userId }: { userId: string }) {
|
||||
return `portfolio-snapshot-${userId}`;
|
||||
}
|
||||
|
||||
|
@ -17,7 +17,9 @@ export class PortfolioChangedListener {
|
||||
);
|
||||
|
||||
this.redisCacheService.remove(
|
||||
this.redisCacheService.getPortfolioSnapshotKey(event.getUserId())
|
||||
this.redisCacheService.getPortfolioSnapshotKey({
|
||||
userId: event.getUserId()
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user