From 55cf88b4946e4d672c234e3cce0f20c6a7c90966 Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sat, 31 Oct 2026 18:08:17 +0100 Subject: [PATCH] Fix floating point noise in merged sector weights --- .../trackinsight/trackinsight.service.spec.ts | 18 ++++++++++++++++++ .../trackinsight/trackinsight.service.ts | 6 ++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.spec.ts b/apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.spec.ts index 461cdf405d..978f41f669 100644 --- a/apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.spec.ts +++ b/apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.spec.ts @@ -380,6 +380,24 @@ describe('TrackinsightDataEnhancerService', () => { ]); }); + it('should merge the sectors without floating point noise', async () => { + mockResponses({ + holdings: Response.json({ + ...HOLDINGS, + sectors: { + 'Consumer Cyclicals': { weight: 0.1 }, + 'Consumer Services': { weight: 0.2 } + } + }) + }); + + const response = await enhance(); + + expect(response.sectors).toEqual([ + { name: 'Consumer Cyclical', weight: 0.3 } + ]); + }); + it('should merge the sectors without a mapping', async () => { const warn = jest.spyOn(Logger.prototype, 'warn').mockImplementation(); diff --git a/apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.ts b/apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.ts index 5b4c290211..5815131000 100644 --- a/apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.ts +++ b/apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.ts @@ -10,6 +10,7 @@ import { SectorName } from '@ghostfolio/common/types'; import { Injectable, Logger } from '@nestjs/common'; import { SymbolProfile } from '@prisma/client'; +import { Big } from 'big.js'; import { isPlainObject } from 'lodash-es'; @Injectable() @@ -179,8 +180,9 @@ export class TrackinsightDataEnhancerService implements DataEnhancerInterface { aliases: TrackinsightDataEnhancerService.sectorsMapping }); - weightsBySector[sectorName] = - (weightsBySector[sectorName] ?? 0) + value.weight; + weightsBySector[sectorName] = new Big(weightsBySector[sectorName] ?? 0) + .plus(value.weight) + .toNumber(); } response.sectors = Object.entries(weightsBySector).map(