bulltrackers-module 1.0.0 → 1.0.2

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.
@@ -47,8 +47,6 @@ async function getLatestNormalUserPortfolios(normalUserCollectionName, snapshots
47
47
 
48
48
  /**
49
49
  * Resets the isLocked status for all proxies.
50
- * Note: This implementation differs from the original orchestrator's FIRESTORE_DOC_PROXY_PERFORMANCE logic.
51
- * This logic matches the original file provided for the module.
52
50
  * @async
53
51
  * @param {string} proxiesCollectionName - e.g., 'Proxies'.
54
52
  * @returns {Promise<void>}
@@ -117,8 +115,8 @@ async function getBlockCapacities(userType, speculatorBlockCountsDocPath, normal
117
115
  * @param {object} config - Configuration object.
118
116
  * @param {string} config.specBlocksCollection - e.g., 'SpeculatorBlocks'.
119
117
  * @param {string} config.pendingSpecCollection - e.g., 'PendingSpeculators'.
120
- * @param {string} config.normalUserCollection - e.g., 'NormalUserPortfolios'.
121
118
  * @param {string} config.invalidSpecCollection - e.g., 'InvalidSpeculators'.
119
+ * @param {Set<string>} config.existingNormalUserIds - PRE-FETCHED normal user IDs.
122
120
  * @returns {Promise<Set<string>>} A Set containing unique user IDs.
123
121
  */
124
122
  async function getExclusionIds(userType, config) {
@@ -126,11 +124,14 @@ async function getExclusionIds(userType, config) {
126
124
  const {
127
125
  specBlocksCollection,
128
126
  pendingSpecCollection,
129
- normalUserCollection,
130
- invalidSpecCollection
127
+ invalidSpecCollection,
128
+ existingNormalUserIds // Get the pre-fetched IDs
131
129
  } = config;
132
130
 
133
- const exclusionIds = new Set();
131
+ // Initialize with the pre-fetched normal users
132
+ const exclusionIds = new Set(existingNormalUserIds);
133
+ logger.log('TRACE', `[Core Utils] Loaded ${exclusionIds.size} existing normal user IDs for exclusion.`);
134
+
134
135
  const promises = [];
135
136
 
136
137
  try {
@@ -155,31 +156,18 @@ async function getExclusionIds(userType, config) {
155
156
  }));
156
157
  }
157
158
 
158
- // 3. Existing Normal Users
159
- const normalPortfoliosRef = db.collection(normalUserCollection);
160
- promises.push(normalPortfoliosRef.listDocuments().then(async blockRefs => {
161
- for(const blockRef of blockRefs) {
162
- // Assuming 'snapshots' and 'parts' subcollections as per original logic
163
- const snapshotRef = blockRef.collection('snapshots');
164
- const latestSnapshotQuery = snapshotRef.orderBy(FieldPath.documentId(), 'desc').limit(1);
165
- const latestSnapshot = await latestSnapshotQuery.get();
166
- if (!latestSnapshot.empty) {
167
- const partsRef = latestSnapshot.docs[0].ref.collection('parts');
168
- const partsSnapshot = await partsRef.get();
169
- partsSnapshot.forEach(partDoc => {
170
- Object.keys(partDoc.data()).forEach(uid => exclusionIds.add(uid));
171
- });
172
- }
173
- }
174
- logger.log('TRACE','[Core Utils] Fetched existing normal user IDs for exclusion.');
175
- }));
176
-
159
+ // 3. Existing Normal Users (REMOVED - Now passed in)
160
+ logger.log('TRACE','[Core Utils] Skipping normal user fetch, already provided.');
177
161
 
178
162
  // 4. Invalid Speculators
179
163
  const invalidRef = db.collection(invalidSpecCollection);
180
164
  promises.push(invalidRef.get().then(snapshot => {
181
165
  snapshot.forEach(doc => {
182
- Object.keys(doc.data().users || {}).forEach(cid => exclusionIds.add(cid));
166
+ const data = doc.data();
167
+ if (data) {
168
+ // Use .users map as per original orchestrator
169
+ Object.keys(data.users || {}).forEach(cid => exclusionIds.add(cid));
170
+ }
183
171
  });
184
172
  logger.log('TRACE','[Core Utils] Fetched invalid speculator IDs for exclusion.');
185
173
  }));
@@ -200,21 +188,16 @@ async function getExclusionIds(userType, config) {
200
188
  * @async
201
189
  * @param {Set<string>} exclusionIds - IDs to exclude.
202
190
  * @param {Set<number>} speculatorInstrumentSet - Set of instrument IDs.
203
- * @param {string} normalUserCollectionName - e.g., 'NormalUserPortfolios'.
204
- * @param {string} snapshotsSubCollectionName - e.g., 'snapshots'.
205
- * @param {string} partsSubCollectionName - e.g., 'parts'.
191
+ * @param {object} latestNormalPortfolios - PRE-FETCHED portfolio object.
206
192
  * @returns {Promise<string[]>} Array of prioritized speculator CIDs.
207
193
  */
208
- async function getPrioritizedSpeculators(exclusionIds, speculatorInstrumentSet, normalUserCollectionName, snapshotsSubCollectionName, partsSubCollectionName) {
194
+ async function getPrioritizedSpeculators(exclusionIds, speculatorInstrumentSet, latestNormalPortfolios) {
209
195
  logger.log('INFO','[Core Utils] Scanning normal users for prioritized speculators...');
210
196
  const candidates = new Set();
211
197
 
212
198
  try {
213
- const latestNormalPortfolios = await getLatestNormalUserPortfolios(
214
- normalUserCollectionName,
215
- snapshotsSubCollectionName,
216
- partsSubCollectionName
217
- );
199
+ // No longer need to fetch, just use the provided object
200
+ // const latestNormalPortfolios = await getLatestNormalUserPortfolios(...);
218
201
 
219
202
  for (const userId in latestNormalPortfolios) {
220
203
  if (exclusionIds.has(userId)) continue;
@@ -96,21 +96,31 @@ async function getDiscoveryCandidates(userType, blocksToFill, config) {
96
96
  const isSpeculator = userType === 'speculator';
97
97
  const dispatchedCids = new Set();
98
98
 
99
+ // --- OPTIMIZATION: Fetch normal user data ONCE ---
100
+ const latestNormalPortfolios = await firestoreUtils.getLatestNormalUserPortfolios(
101
+ normalUserCollection,
102
+ snapshotsSubCollectionName,
103
+ partsSubCollectionName
104
+ );
105
+ const existingNormalUserIds = new Set(Object.keys(latestNormalPortfolios));
106
+ // --- End Optimization ---
107
+
108
+ // Pass the fetched normal user IDs directly into getExclusionIds
99
109
  const exclusionIds = await firestoreUtils.getExclusionIds(userType, {
100
110
  specBlocksCollection,
101
111
  pendingSpecCollection,
102
- normalUserCollection,
103
- invalidSpecCollection
112
+ normalUserCollection, // Pass name for consistency, though fetch is skipped
113
+ invalidSpecCollection,
114
+ existingNormalUserIds: existingNormalUserIds // <-- Pass pre-fetched IDs
104
115
  });
105
116
 
106
117
  // --- Prioritization for Speculators ---
107
118
  if (isSpeculator) {
119
+ // Pass the fetched portfolio data directly to avoid a second fetch
108
120
  const prioritizedCandidates = await firestoreUtils.getPrioritizedSpeculators(
109
121
  exclusionIds,
110
122
  speculatorInstrumentSet,
111
- normalUserCollection,
112
- snapshotsSubCollectionName,
113
- partsSubCollectionName
123
+ latestNormalPortfolios // <-- Pass pre-fetched data
114
124
  );
115
125
  logger.log('INFO', `Found ${prioritizedCandidates.length} potential new speculators from existing user pool.`);
116
126
 
@@ -178,7 +188,7 @@ async function dispatchDiscovery(userType, candidates, config) {
178
188
  topicName,
179
189
  dispatchBatchSize,
180
190
  pubsubBatchSize,
181
- pendingSpeculatorsCollection,
191
+ pendingSpecCollection, // <-- Change to singular
182
192
  pendingMaxFieldsPerDoc,
183
193
  pendingMaxWritesPerBatch
184
194
  } = config;
@@ -193,9 +203,9 @@ async function dispatchDiscovery(userType, candidates, config) {
193
203
  const cidsArray = Array.from(candidates);
194
204
 
195
205
  if (isSpeculator) {
196
- await firestoreUtils.clearCollection(pendingSpeculatorsCollection);
206
+ await firestoreUtils.clearCollection(pendingSpecCollection); // <-- Change to singular
197
207
  await firestoreUtils.batchWriteShardedIds(
198
- pendingSpeculatorsCollection,
208
+ pendingSpecCollection, // <-- And this line
199
209
  cidsArray,
200
210
  new Date(),
201
211
  pendingMaxFieldsPerDoc,
@@ -218,7 +228,7 @@ async function dispatchDiscovery(userType, candidates, config) {
218
228
  }
219
229
  }
220
230
 
221
- await pubsubUtils.batchPublishTasks(
231
+ await pubsubUtils.batchWriteShardedTasks(
222
232
  topicName,
223
233
  tasks,
224
234
  `${userType} discovery`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bulltrackers-module",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Helper Functions for Bulltrackers.",
5
5
  "main": "index.js",
6
6
  "files": [