bulltrackers-module 1.0.104 → 1.0.106
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.
- package/README.MD +222 -222
- package/functions/appscript-api/helpers/errors.js +19 -19
- package/functions/appscript-api/index.js +58 -58
- package/functions/computation-system/helpers/orchestration_helpers.js +647 -113
- package/functions/computation-system/utils/data_loader.js +191 -191
- package/functions/computation-system/utils/utils.js +149 -254
- package/functions/core/utils/firestore_utils.js +433 -433
- package/functions/core/utils/pubsub_utils.js +53 -53
- package/functions/dispatcher/helpers/dispatch_helpers.js +47 -47
- package/functions/dispatcher/index.js +52 -52
- package/functions/etoro-price-fetcher/helpers/handler_helpers.js +124 -124
- package/functions/fetch-insights/helpers/handler_helpers.js +91 -91
- package/functions/generic-api/helpers/api_helpers.js +379 -379
- package/functions/generic-api/index.js +150 -150
- package/functions/invalid-speculator-handler/helpers/handler_helpers.js +75 -75
- package/functions/orchestrator/helpers/discovery_helpers.js +226 -226
- package/functions/orchestrator/helpers/update_helpers.js +92 -92
- package/functions/orchestrator/index.js +147 -147
- package/functions/price-backfill/helpers/handler_helpers.js +116 -123
- package/functions/social-orchestrator/helpers/orchestrator_helpers.js +61 -61
- package/functions/social-task-handler/helpers/handler_helpers.js +288 -288
- package/functions/task-engine/handler_creator.js +78 -78
- package/functions/task-engine/helpers/discover_helpers.js +125 -125
- package/functions/task-engine/helpers/update_helpers.js +118 -118
- package/functions/task-engine/helpers/verify_helpers.js +162 -162
- package/functions/task-engine/utils/firestore_batch_manager.js +258 -258
- package/index.js +105 -113
- package/package.json +45 -45
- package/functions/computation-system/computation_dependencies.json +0 -120
- package/functions/computation-system/helpers/worker_helpers.js +0 -340
- package/functions/computation-system/utils/computation_state_manager.js +0 -178
- package/functions/computation-system/utils/dependency_graph.js +0 -191
- package/functions/speculator-cleanup-orchestrator/helpers/cleanup_helpers.js +0 -160
|
@@ -1,163 +1,163 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Sub-pipe: pipe.taskEngine.handleVerify
|
|
3
|
-
* REFACTORED: Now stateless and receives dependencies.
|
|
4
|
-
* OPTIMIZED: Fetches all user portfolios in parallel to reduce function runtime.
|
|
5
|
-
*/
|
|
6
|
-
const { FieldValue } = require('@google-cloud/firestore');
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Internal helper to fetch and process a single user's portfolio.
|
|
10
|
-
* This allows the main function to run these in parallel.
|
|
11
|
-
* @param {object} user - The user object { cid, isBronze }
|
|
12
|
-
* @param {object} dependencies - Contains proxyManager, headerManager
|
|
13
|
-
* @param {object} config - The configuration object
|
|
14
|
-
* @param {Set<number>} speculatorInstrumentSet - Pre-built Set of instrument IDs
|
|
15
|
-
* @returns {Promise<object|null>} A result object for batching, or null on failure.
|
|
16
|
-
*/
|
|
17
|
-
async function fetchAndVerifyUser(user, dependencies, config, speculatorInstrumentSet) {
|
|
18
|
-
const { logger, headerManager, proxyManager } = dependencies;
|
|
19
|
-
const userId = user.cid;
|
|
20
|
-
|
|
21
|
-
const portfolioUrl = `${config.ETORO_API_PORTFOLIO_URL}?cid=${userId}`;
|
|
22
|
-
const selectedHeader = await headerManager.selectHeader();
|
|
23
|
-
if (!selectedHeader) return null; // Cannot fetch
|
|
24
|
-
|
|
25
|
-
let wasSuccess = false;
|
|
26
|
-
try {
|
|
27
|
-
const response = await proxyManager.fetch(portfolioUrl, { headers: selectedHeader.header });
|
|
28
|
-
if (!response.ok) {
|
|
29
|
-
wasSuccess = false;
|
|
30
|
-
return null; // API error or private user
|
|
31
|
-
}
|
|
32
|
-
wasSuccess = true;
|
|
33
|
-
|
|
34
|
-
const portfolioData = await response.json();
|
|
35
|
-
|
|
36
|
-
// Process verification logic *within* the parallel task
|
|
37
|
-
if (config.userType === 'speculator') {
|
|
38
|
-
const matchingInstruments = portfolioData.AggregatedPositions
|
|
39
|
-
.map(p => p.InstrumentID)
|
|
40
|
-
.filter(id => speculatorInstrumentSet.has(id));
|
|
41
|
-
|
|
42
|
-
if (matchingInstruments.length > 0) {
|
|
43
|
-
logger.log('INFO', `[VERIFY] Speculator user ${userId} holds assets: ${matchingInstruments.join(', ')}`);
|
|
44
|
-
// Return data needed for the speculator batch update
|
|
45
|
-
return {
|
|
46
|
-
type: 'speculator',
|
|
47
|
-
userId: userId,
|
|
48
|
-
isBronze: user.isBronze,
|
|
49
|
-
updateData: {
|
|
50
|
-
instruments: matchingInstruments,
|
|
51
|
-
lastVerified: new Date(),
|
|
52
|
-
lastHeldSpeculatorAsset: new Date()
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
} else {
|
|
56
|
-
logger.log('INFO', `[VERIFY] Speculator user ${userId} does not hold any speculator assets.`);
|
|
57
|
-
return null; // Valid fetch, but failed verification
|
|
58
|
-
}
|
|
59
|
-
} else { // Normal user verification
|
|
60
|
-
// Return data needed for the normal user batch update
|
|
61
|
-
return {
|
|
62
|
-
type: 'normal',
|
|
63
|
-
userId: userId,
|
|
64
|
-
isBronze: user.isBronze,
|
|
65
|
-
updateData: {
|
|
66
|
-
lastVerified: new Date()
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
} catch (error) {
|
|
71
|
-
logger.log('WARN', `[VERIFY] Error processing user ${userId}`, { errorMessage: error.message });
|
|
72
|
-
return null;
|
|
73
|
-
} finally {
|
|
74
|
-
if (selectedHeader) headerManager.updatePerformance(selectedHeader.id, wasSuccess);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Sub-pipe: pipe.taskEngine.handleVerify
|
|
81
|
-
* @param {object} task The Pub/Sub task payload.
|
|
82
|
-
* @param {string} taskId A unique ID for logging.
|
|
83
|
-
* @param {object} dependencies - Contains db, pubsub, logger, headerManager, proxyManager, batchManager.
|
|
84
|
-
* @param {object} config The configuration object.
|
|
85
|
-
*/
|
|
86
|
-
async function handleVerify(task, taskId, dependencies, config) {
|
|
87
|
-
const { logger, db } = dependencies;
|
|
88
|
-
const { users, blockId, instrument, userType } = task;
|
|
89
|
-
|
|
90
|
-
// Use db from dependencies
|
|
91
|
-
const batch = db.batch();
|
|
92
|
-
let validUserCount = 0;
|
|
93
|
-
const speculatorUpdates = {};
|
|
94
|
-
const normalUserUpdates = {};
|
|
95
|
-
const bronzeStateUpdates = {}; // This can be one object
|
|
96
|
-
|
|
97
|
-
const speculatorInstrumentSet = new Set(config.SPECULATOR_INSTRUMENTS_ARRAY);
|
|
98
|
-
|
|
99
|
-
// --- OPTIMIZATION: Run all user fetches in parallel ---
|
|
100
|
-
const verificationPromises = users.map(user =>
|
|
101
|
-
fetchAndVerifyUser(
|
|
102
|
-
user,
|
|
103
|
-
dependencies,
|
|
104
|
-
{ ...config, userType: userType }, // Pass userType into the helper config
|
|
105
|
-
speculatorInstrumentSet
|
|
106
|
-
)
|
|
107
|
-
);
|
|
108
|
-
|
|
109
|
-
const results = await Promise.allSettled(verificationPromises);
|
|
110
|
-
// --- END OPTIMIZATION ---
|
|
111
|
-
|
|
112
|
-
// Process results (this is fast, no I/O)
|
|
113
|
-
results.forEach(result => {
|
|
114
|
-
if (result.status === 'rejected' || !result.value) {
|
|
115
|
-
// Log rejection reason if needed: result.reason
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
const data = result.value;
|
|
120
|
-
|
|
121
|
-
if (data.type === 'speculator') {
|
|
122
|
-
speculatorUpdates[`users.${data.userId}`] = data.updateData;
|
|
123
|
-
bronzeStateUpdates[data.userId] = data.isBronze;
|
|
124
|
-
validUserCount++;
|
|
125
|
-
} else if (data.type === 'normal') {
|
|
126
|
-
normalUserUpdates[`users.${data.userId}`] = data.updateData;
|
|
127
|
-
bronzeStateUpdates[data.userId] = data.isBronze;
|
|
128
|
-
validUserCount++;
|
|
129
|
-
}
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
if (Object.keys(speculatorUpdates).length > 0 || Object.keys(normalUserUpdates).length > 0) {
|
|
134
|
-
if (userType === 'speculator') {
|
|
135
|
-
const speculatorBlockRef = db.collection(config.FIRESTORE_COLLECTION_SPECULATOR_BLOCKS).doc(String(blockId));
|
|
136
|
-
batch.set(speculatorBlockRef, speculatorUpdates, { merge: true });
|
|
137
|
-
const bronzeStateRef = db.collection(config.FIRESTORE_COLLECTION_BRONZE_SPECULATORS).doc(String(blockId));
|
|
138
|
-
batch.set(bronzeStateRef, bronzeStateUpdates, {merge: true});
|
|
139
|
-
|
|
140
|
-
} else {
|
|
141
|
-
const normalBlockRef = db.collection(config.FIRESTORE_COLLECTION_NORMAL_PORTFOLIOS).doc(String(blockId));
|
|
142
|
-
batch.set(normalBlockRef, normalUserUpdates, { merge: true });
|
|
143
|
-
const bronzeStateRef = db.collection(config.FIRESTORE_COLLECTION_BRONZE_NORMAL).doc(String(blockId));
|
|
144
|
-
batch.set(bronzeStateRef, bronzeStateUpdates, {merge: true});
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
if (validUserCount > 0) {
|
|
148
|
-
const blockCountsRef = db.doc(userType === 'speculator'
|
|
149
|
-
? config.FIRESTORE_DOC_SPECULATOR_BLOCK_COUNTS
|
|
150
|
-
: config.FIRESTORE_DOC_BLOCK_COUNTS);
|
|
151
|
-
|
|
152
|
-
const incrementField = userType === 'speculator' ? `counts.${instrument}_${blockId}` : `counts.${blockId}`;
|
|
153
|
-
batch.set(blockCountsRef, { [incrementField]: FieldValue.increment(validUserCount) }, { merge: true });
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
await batch.commit();
|
|
158
|
-
if(validUserCount > 0) {
|
|
159
|
-
logger.log('INFO', `[VERIFY] Verified and stored ${validUserCount} new ${userType} users.`);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Sub-pipe: pipe.taskEngine.handleVerify
|
|
3
|
+
* REFACTORED: Now stateless and receives dependencies.
|
|
4
|
+
* OPTIMIZED: Fetches all user portfolios in parallel to reduce function runtime.
|
|
5
|
+
*/
|
|
6
|
+
const { FieldValue } = require('@google-cloud/firestore');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Internal helper to fetch and process a single user's portfolio.
|
|
10
|
+
* This allows the main function to run these in parallel.
|
|
11
|
+
* @param {object} user - The user object { cid, isBronze }
|
|
12
|
+
* @param {object} dependencies - Contains proxyManager, headerManager
|
|
13
|
+
* @param {object} config - The configuration object
|
|
14
|
+
* @param {Set<number>} speculatorInstrumentSet - Pre-built Set of instrument IDs
|
|
15
|
+
* @returns {Promise<object|null>} A result object for batching, or null on failure.
|
|
16
|
+
*/
|
|
17
|
+
async function fetchAndVerifyUser(user, dependencies, config, speculatorInstrumentSet) {
|
|
18
|
+
const { logger, headerManager, proxyManager } = dependencies;
|
|
19
|
+
const userId = user.cid;
|
|
20
|
+
|
|
21
|
+
const portfolioUrl = `${config.ETORO_API_PORTFOLIO_URL}?cid=${userId}`;
|
|
22
|
+
const selectedHeader = await headerManager.selectHeader();
|
|
23
|
+
if (!selectedHeader) return null; // Cannot fetch
|
|
24
|
+
|
|
25
|
+
let wasSuccess = false;
|
|
26
|
+
try {
|
|
27
|
+
const response = await proxyManager.fetch(portfolioUrl, { headers: selectedHeader.header });
|
|
28
|
+
if (!response.ok) {
|
|
29
|
+
wasSuccess = false;
|
|
30
|
+
return null; // API error or private user
|
|
31
|
+
}
|
|
32
|
+
wasSuccess = true;
|
|
33
|
+
|
|
34
|
+
const portfolioData = await response.json();
|
|
35
|
+
|
|
36
|
+
// Process verification logic *within* the parallel task
|
|
37
|
+
if (config.userType === 'speculator') {
|
|
38
|
+
const matchingInstruments = portfolioData.AggregatedPositions
|
|
39
|
+
.map(p => p.InstrumentID)
|
|
40
|
+
.filter(id => speculatorInstrumentSet.has(id));
|
|
41
|
+
|
|
42
|
+
if (matchingInstruments.length > 0) {
|
|
43
|
+
logger.log('INFO', `[VERIFY] Speculator user ${userId} holds assets: ${matchingInstruments.join(', ')}`);
|
|
44
|
+
// Return data needed for the speculator batch update
|
|
45
|
+
return {
|
|
46
|
+
type: 'speculator',
|
|
47
|
+
userId: userId,
|
|
48
|
+
isBronze: user.isBronze,
|
|
49
|
+
updateData: {
|
|
50
|
+
instruments: matchingInstruments,
|
|
51
|
+
lastVerified: new Date(),
|
|
52
|
+
lastHeldSpeculatorAsset: new Date()
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
} else {
|
|
56
|
+
logger.log('INFO', `[VERIFY] Speculator user ${userId} does not hold any speculator assets.`);
|
|
57
|
+
return null; // Valid fetch, but failed verification
|
|
58
|
+
}
|
|
59
|
+
} else { // Normal user verification
|
|
60
|
+
// Return data needed for the normal user batch update
|
|
61
|
+
return {
|
|
62
|
+
type: 'normal',
|
|
63
|
+
userId: userId,
|
|
64
|
+
isBronze: user.isBronze,
|
|
65
|
+
updateData: {
|
|
66
|
+
lastVerified: new Date()
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
} catch (error) {
|
|
71
|
+
logger.log('WARN', `[VERIFY] Error processing user ${userId}`, { errorMessage: error.message });
|
|
72
|
+
return null;
|
|
73
|
+
} finally {
|
|
74
|
+
if (selectedHeader) headerManager.updatePerformance(selectedHeader.id, wasSuccess);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Sub-pipe: pipe.taskEngine.handleVerify
|
|
81
|
+
* @param {object} task The Pub/Sub task payload.
|
|
82
|
+
* @param {string} taskId A unique ID for logging.
|
|
83
|
+
* @param {object} dependencies - Contains db, pubsub, logger, headerManager, proxyManager, batchManager.
|
|
84
|
+
* @param {object} config The configuration object.
|
|
85
|
+
*/
|
|
86
|
+
async function handleVerify(task, taskId, dependencies, config) {
|
|
87
|
+
const { logger, db } = dependencies;
|
|
88
|
+
const { users, blockId, instrument, userType } = task;
|
|
89
|
+
|
|
90
|
+
// Use db from dependencies
|
|
91
|
+
const batch = db.batch();
|
|
92
|
+
let validUserCount = 0;
|
|
93
|
+
const speculatorUpdates = {};
|
|
94
|
+
const normalUserUpdates = {};
|
|
95
|
+
const bronzeStateUpdates = {}; // This can be one object
|
|
96
|
+
|
|
97
|
+
const speculatorInstrumentSet = new Set(config.SPECULATOR_INSTRUMENTS_ARRAY);
|
|
98
|
+
|
|
99
|
+
// --- OPTIMIZATION: Run all user fetches in parallel ---
|
|
100
|
+
const verificationPromises = users.map(user =>
|
|
101
|
+
fetchAndVerifyUser(
|
|
102
|
+
user,
|
|
103
|
+
dependencies,
|
|
104
|
+
{ ...config, userType: userType }, // Pass userType into the helper config
|
|
105
|
+
speculatorInstrumentSet
|
|
106
|
+
)
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
const results = await Promise.allSettled(verificationPromises);
|
|
110
|
+
// --- END OPTIMIZATION ---
|
|
111
|
+
|
|
112
|
+
// Process results (this is fast, no I/O)
|
|
113
|
+
results.forEach(result => {
|
|
114
|
+
if (result.status === 'rejected' || !result.value) {
|
|
115
|
+
// Log rejection reason if needed: result.reason
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const data = result.value;
|
|
120
|
+
|
|
121
|
+
if (data.type === 'speculator') {
|
|
122
|
+
speculatorUpdates[`users.${data.userId}`] = data.updateData;
|
|
123
|
+
bronzeStateUpdates[data.userId] = data.isBronze;
|
|
124
|
+
validUserCount++;
|
|
125
|
+
} else if (data.type === 'normal') {
|
|
126
|
+
normalUserUpdates[`users.${data.userId}`] = data.updateData;
|
|
127
|
+
bronzeStateUpdates[data.userId] = data.isBronze;
|
|
128
|
+
validUserCount++;
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
if (Object.keys(speculatorUpdates).length > 0 || Object.keys(normalUserUpdates).length > 0) {
|
|
134
|
+
if (userType === 'speculator') {
|
|
135
|
+
const speculatorBlockRef = db.collection(config.FIRESTORE_COLLECTION_SPECULATOR_BLOCKS).doc(String(blockId));
|
|
136
|
+
batch.set(speculatorBlockRef, speculatorUpdates, { merge: true });
|
|
137
|
+
const bronzeStateRef = db.collection(config.FIRESTORE_COLLECTION_BRONZE_SPECULATORS).doc(String(blockId));
|
|
138
|
+
batch.set(bronzeStateRef, bronzeStateUpdates, {merge: true});
|
|
139
|
+
|
|
140
|
+
} else {
|
|
141
|
+
const normalBlockRef = db.collection(config.FIRESTORE_COLLECTION_NORMAL_PORTFOLIOS).doc(String(blockId));
|
|
142
|
+
batch.set(normalBlockRef, normalUserUpdates, { merge: true });
|
|
143
|
+
const bronzeStateRef = db.collection(config.FIRESTORE_COLLECTION_BRONZE_NORMAL).doc(String(blockId));
|
|
144
|
+
batch.set(bronzeStateRef, bronzeStateUpdates, {merge: true});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (validUserCount > 0) {
|
|
148
|
+
const blockCountsRef = db.doc(userType === 'speculator'
|
|
149
|
+
? config.FIRESTORE_DOC_SPECULATOR_BLOCK_COUNTS
|
|
150
|
+
: config.FIRESTORE_DOC_BLOCK_COUNTS);
|
|
151
|
+
|
|
152
|
+
const incrementField = userType === 'speculator' ? `counts.${instrument}_${blockId}` : `counts.${blockId}`;
|
|
153
|
+
batch.set(blockCountsRef, { [incrementField]: FieldValue.increment(validUserCount) }, { merge: true });
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
await batch.commit();
|
|
158
|
+
if(validUserCount > 0) {
|
|
159
|
+
logger.log('INFO', `[VERIFY] Verified and stored ${validUserCount} new ${userType} users.`);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
163
|
module.exports = { handleVerify };
|