Browse Source

Fix floating point noise in merged sector weights

pull/8367/head
Thomas Kaul 4 weeks ago
parent
commit
55cf88b494
  1. 18
      apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.spec.ts
  2. 6
      apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.ts

18
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();

6
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(

Loading…
Cancel
Save