aiden-shared-calculations-unified 1.0.89 → 1.0.91

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,13 @@
1
1
  /**
2
2
  * @fileoverview Core Metric (Pass 2)
3
- * REFACTORED: Buckets users by P&L status (Win/Loss).
3
+ * REFACTORED: Buckets P&L status by User Profile.
4
+ * - Pivots data to be User-Centric (User -> Winners/Losers).
5
+ * - Removes sharding complexity (fits within 1MB via structure optimization).
4
6
  */
5
7
  class AssetPnlStatus {
6
8
  constructor() {
7
- this.tickerBuckets = new Map();
8
- this.sectorBuckets = new Map();
9
+ // Map<UserId, { winners: Set<String>, losers: Set<String> }>
10
+ this.userStats = new Map();
9
11
  }
10
12
 
11
13
  static getMetadata() {
@@ -21,80 +23,92 @@ class AssetPnlStatus {
21
23
  static getDependencies() { return []; }
22
24
 
23
25
  static getSchema() {
24
- const cohortSchema = {
25
- "type": "object",
26
- "properties": {
27
- "winners": { "type": "array", "items": { "type": "string" } },
28
- "losers": { "type": "array", "items": { "type": "string" } }
29
- },
30
- "required": ["winners", "losers"]
31
- };
32
26
  return {
33
27
  "type": "object",
34
28
  "properties": {
35
- "by_ticker": { "type": "object", "patternProperties": { "^.*$": cohortSchema } },
36
- "by_sector": { "type": "object", "patternProperties": { "^.*$": cohortSchema } }
29
+ "by_user": {
30
+ "type": "object",
31
+ "description": "Map of User IDs to their winning/losing assets and sectors.",
32
+ "patternProperties": {
33
+ "^[0-9]+$": { // Matches User IDs (CIDs)
34
+ "type": "object",
35
+ "properties": {
36
+ "winners": { "type": "array", "items": { "type": "string" } },
37
+ "losers": { "type": "array", "items": { "type": "string" } }
38
+ },
39
+ "required": ["winners", "losers"]
40
+ }
41
+ }
42
+ }
37
43
  }
38
44
  };
39
45
  }
40
46
 
41
- _init(map, key) {
42
- if (!map.has(key)) map.set(key, { winners: new Set(), losers: new Set() });
47
+ _initUser(userId) {
48
+ if (!this.userStats.has(userId)) {
49
+ this.userStats.set(userId, { winners: new Set(), losers: new Set() });
50
+ }
51
+ return this.userStats.get(userId);
43
52
  }
44
53
 
45
54
  process(context) {
46
55
  const { extract } = context.math;
47
56
  const { mappings, user } = context;
57
+ const userId = user.id;
48
58
 
59
+ // 1. Get Positions using the standard extractor
49
60
  const positions = extract.getPositions(user.portfolio.today, user.type);
61
+ if (!positions || positions.length === 0) return;
62
+
63
+ // 2. Lazy init stats only if needed
64
+ let stats = null;
50
65
 
51
66
  for (const pos of positions) {
52
67
  const instId = extract.getInstrumentId(pos);
53
68
  if (!instId) continue;
54
69
 
55
70
  const pnl = extract.getNetProfit(pos);
56
- if (pnl === 0) continue;
71
+ if (pnl === 0) continue; // Ignore flat positions
57
72
 
58
73
  const ticker = mappings.instrumentToTicker[instId];
59
74
  const sector = mappings.instrumentToSector[instId];
60
75
  const isWinner = pnl > 0;
61
76
 
62
- if (ticker) {
63
- this._init(this.tickerBuckets, ticker);
64
- const asset = this.tickerBuckets.get(ticker);
65
- if (isWinner) asset.winners.add(user.id);
66
- else asset.losers.add(user.id);
67
- }
68
-
69
- if (sector) {
70
- this._init(this.sectorBuckets, sector);
71
- const sec = this.sectorBuckets.get(sector);
72
- if (isWinner) sec.winners.add(user.id);
73
- else sec.losers.add(user.id);
77
+ if (ticker || sector) {
78
+ if (!stats) stats = this._initUser(userId);
79
+
80
+ if (ticker) {
81
+ if (isWinner) stats.winners.add(ticker);
82
+ else stats.losers.add(ticker);
83
+ }
84
+
85
+ if (sector) {
86
+ if (isWinner) stats.winners.add(sector);
87
+ else stats.losers.add(sector);
88
+ }
74
89
  }
75
90
  }
76
91
  }
77
92
 
78
93
  async getResult() {
79
- const result = { by_ticker: {}, by_sector: {} };
80
-
81
- const buildResult = (map, out) => {
82
- for (const [key, data] of map.entries()) {
83
- out[key] = {
84
- winners: Array.from(data.winners),
85
- losers: Array.from(data.losers)
94
+ const byUser = {};
95
+
96
+ for (const [userId, stats] of this.userStats) {
97
+ // Only include users who actually have relevant P&L data
98
+ if (stats.winners.size > 0 || stats.losers.size > 0) {
99
+ byUser[userId] = {
100
+ winners: Array.from(stats.winners),
101
+ losers: Array.from(stats.losers)
86
102
  };
87
103
  }
88
- };
104
+ }
89
105
 
90
- buildResult(this.tickerBuckets, result.by_ticker);
91
- buildResult(this.sectorBuckets, result.by_sector);
92
- return result;
106
+ return { by_user: byUser };
93
107
  }
94
108
 
95
109
  reset() {
96
- this.tickerBuckets.clear();
97
- this.sectorBuckets.clear();
110
+ this.userStats.clear();
98
111
  }
99
112
  }
113
+
100
114
  module.exports = AssetPnlStatus;
@@ -87,9 +87,9 @@ class CorePriceMetrics {
87
87
 
88
88
  process(context) {
89
89
  const { mappings, prices, date } = context;
90
- const { instrumentToTicker, sectorMapping } = mappings;
90
+ // FIX 1: Use the correct property name 'instrumentToSector'
91
+ const { instrumentToTicker, instrumentToSector } = mappings;
91
92
 
92
- // Uses history from context
93
93
  const priceData = prices?.history;
94
94
  const todayDateStr = date.today;
95
95
 
@@ -101,7 +101,8 @@ class CorePriceMetrics {
101
101
  const by_instrument = {};
102
102
  const tickerToInstrument = {};
103
103
 
104
- for (const p of priceData) {
104
+ // FIX 2: Iterate over Object.values() since priceData is a map, not an array
105
+ for (const p of Object.values(priceData)) {
105
106
  const ticker = instrumentToTicker[p.instrumentId];
106
107
  if (!ticker) continue;
107
108
  tickerToInstrument[ticker] = p.instrumentId;
@@ -125,7 +126,9 @@ class CorePriceMetrics {
125
126
  const sectorAggs = {};
126
127
  for (const ticker in by_instrument) {
127
128
  const instId = tickerToInstrument[ticker];
128
- const sector = sectorMapping[instId] || "Unknown";
129
+ // FIX 1 (Usage): Use the corrected variable name
130
+ const sector = instrumentToSector[instId] || "Unknown";
131
+
129
132
  if (!sectorAggs[sector]) sectorAggs[sector] = { metrics: {}, counts: {} };
130
133
 
131
134
  const data = by_instrument[ticker];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aiden-shared-calculations-unified",
3
- "version": "1.0.89",
3
+ "version": "1.0.91",
4
4
  "description": "Shared calculation modules for the BullTrackers Computation System.",
5
5
  "main": "index.js",
6
6
  "files": [