aiden-shared-calculations-unified 1.0.22 → 1.0.23

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.
@@ -0,0 +1,144 @@
1
+ /**
2
+ * @fileoverview Tracks user activity by comparing portfolio snapshots.
3
+ * This is a historical calculation that defines an "active user" as someone
4
+ * who has opened, closed, or reallocated a position within the last 24 hours.
5
+ *
6
+ * This provides the "Daily Active Users" count for the monitored cohort.
7
+ * This depreciates the user activity sampler cloud function which was inefficient for the api usage and now provides this data for free
8
+ */
9
+
10
+ class DailyUserActivityTracker {
11
+ constructor() {
12
+ this.activeUserIds = new Set();
13
+ this.activityEvents = {
14
+ new_position: 0,
15
+ closed_position: 0,
16
+ reallocation: 0
17
+ };
18
+ }
19
+
20
+ /**
21
+ * Helper to get a simplified map of positions for comparison.
22
+ * @param {object} portfolio - A user's full portfolio object.
23
+ * @returns {object} { posMap: Map<InstrumentID, {invested: number}>, hasAggregated: boolean }
24
+ */
25
+ _getPortfolioMaps(portfolio) {
26
+ // Prioritize AggregatedPositions, but fall back to PublicPositions
27
+ const positions = portfolio?.AggregatedPositions || portfolio?.PublicPositions;
28
+ if (!positions || !Array.isArray(positions)) {
29
+ return { posMap: new Map(), hasAggregated: false };
30
+ }
31
+
32
+ const posMap = new Map();
33
+ for (const pos of positions) {
34
+ const key = pos.InstrumentID;
35
+ if (key) {
36
+ posMap.set(key, {
37
+ // 'InvestedAmount' or 'Invested' is the portfolio percentage
38
+ // We use this for reallocation logic.
39
+ invested: pos.InvestedAmount || pos.Invested || pos.Amount || 0
40
+ });
41
+ }
42
+ }
43
+ // Return the map and a flag indicating if we can trust the 'invested' field
44
+ return { posMap, hasAggregated: !!portfolio.AggregatedPositions };
45
+ }
46
+
47
+ /**
48
+ * Processes a single user's daily data.
49
+ */
50
+ process(todayPortfolio, yesterdayPortfolio, userId) {
51
+ // This calculation requires both days to find changes.
52
+ if (!todayPortfolio || !yesterdayPortfolio) {
53
+ return;
54
+ }
55
+
56
+ const { posMap: yPosMap, hasAggregated: yHasAgg } = this._getPortfolioMaps(yesterdayPortfolio);
57
+ const { posMap: tPosMap, hasAggregated: tHasAgg } = this._getPortfolioMaps(todayPortfolio);
58
+
59
+ // Skip if user has no positions on either day
60
+ if (tPosMap.size === 0 && yPosMap.size === 0) {
61
+ return;
62
+ }
63
+
64
+ const yIds = new Set(yPosMap.keys());
65
+ const tIds = new Set(tPosMap.keys());
66
+ let isActive = false;
67
+
68
+ // 1. Check for new positions (high-confidence activity)
69
+ for (const tId of tIds) {
70
+ if (!yIds.has(tId)) {
71
+ isActive = true;
72
+ this.activityEvents.new_position++;
73
+ break; // Found activity, no need to check more
74
+ }
75
+ }
76
+
77
+ if (isActive) {
78
+ this.activeUserIds.add(userId);
79
+ return;
80
+ }
81
+
82
+ // 2. Check for closed positions (high-confidence activity)
83
+ for (const yId of yIds) {
84
+ if (!tIds.has(yId)) {
85
+ isActive = true;
86
+ this.activityEvents.closed_position++;
87
+ break; // Found activity
88
+ }
89
+ }
90
+
91
+ if (isActive) {
92
+ this.activeUserIds.add(userId);
93
+ return;
94
+ }
95
+
96
+ // 3. Check for reallocation (only possible if we have AggregatedPositions for both days)
97
+ // This checks for changes in the 'Invested' percentage
98
+ if (yHasAgg && tHasAgg) {
99
+ for (const tId of tIds) {
100
+ // We know tId is also in yIds from the checks above
101
+ const tInvested = tPosMap.get(tId).invested;
102
+ const yInvested = yPosMap.get(yId).invested;
103
+
104
+ // Check for a meaningful change (e.g., > 0.01% to avoid float noise)
105
+ if (Math.abs(tInvested - yInvested) > 0.0001) {
106
+ isActive = true;
107
+ this.activityEvents.reallocation++;
108
+ break; // Found activity
109
+ }
110
+ }
111
+ }
112
+
113
+ if (isActive) {
114
+ this.activeUserIds.add(userId);
115
+ }
116
+ }
117
+
118
+ /**
119
+ * Returns the final aggregated counts for the day.
120
+ */
121
+ getResult() {
122
+ return {
123
+ // This is the main metric for your graph
124
+ rawActiveUserCount: this.activeUserIds.size,
125
+
126
+ // This is a bonus metric to see *what* users are doing
127
+ activityBreakdown: this.activityEvents
128
+ };
129
+ }
130
+
131
+ /**
132
+ * Resets the counters for the next run.
133
+ */
134
+ reset() {
135
+ this.activeUserIds.clear();
136
+ this.activityEvents = {
137
+ new_position: 0,
138
+ closed_position: 0,
139
+ reallocation: 0
140
+ };
141
+ }
142
+ }
143
+
144
+ module.exports = DailyUserActivityTracker;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aiden-shared-calculations-unified",
3
- "version": "1.0.22",
3
+ "version": "1.0.23",
4
4
  "description": "Shared calculation modules for the BullTrackers Computation System.",
5
5
  "main": "index.js",
6
6
  "files": [