bulltrackers-module 1.0.92 → 1.0.94

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.
@@ -17,22 +17,35 @@ const {
17
17
 
18
18
  // --- MODIFIED: Import new meta calculation objects ---
19
19
  const {
20
- historicalCalculations, dailyCalculations, metaCalculations,
21
- HISTORICAL_CALC_NAMES, META_CALC_NAMES,
22
- withRetry,
20
+ categorizeCalculations, // <-- IMPORT THE NEW FUNCTION
21
+ // REMOVED: withRetry
23
22
  getExpectedDateStrings, processJobsInParallel, getFirstDateFromSourceData,
24
- commitBatchInChunks, unifiedUtils
23
+ commitBatchInChunks
24
+ // REMOVED: unifiedUtils
25
25
  } = require('../utils/utils.js');
26
26
  // --- END MODIFIED ---
27
27
 
28
28
  /**
29
29
  * Main pipe: pipe.computationSystem.runOrchestration
30
30
  * @param {object} config - The computation system configuration object.
31
- * @param {object} dependencies - Contains db, logger.
31
+ * @param {object} dependencies - Contains db, logger, calculationUtils.
32
+ * @param {object} calculations - The injected calculations object from 'aiden-shared-calculations-unified'.
32
33
  * @returns {Promise<Object>} Summary of all passes.
33
34
  */
34
- async function runComputationOrchestrator(config, dependencies) {
35
- const { logger, db } = dependencies; // Added db here
35
+ async function runComputationOrchestrator(config, dependencies, calculations) { // <-- ADDED 'calculations'
36
+ const { logger, db, calculationUtils } = dependencies; // Added db and calculationUtils
37
+ const { withRetry } = calculationUtils; // <-- Get withRetry from injected utils
38
+
39
+ // --- NEW: Categorize the injected calculations ---
40
+ const {
41
+ historicalCalculations,
42
+ dailyCalculations,
43
+ metaCalculations,
44
+ HISTORICAL_CALC_NAMES,
45
+ META_CALC_NAMES
46
+ } = categorizeCalculations(calculations);
47
+ // --- END NEW ---
48
+
36
49
  // --- MODIFIED: Add pass3_results to summary ---
37
50
  const summary = { pass1_results: [], pass2_results: [], pass3_results: [] };
38
51
  // --- END MODIFIED ---
@@ -46,9 +59,12 @@ async function runComputationOrchestrator(config, dependencies) {
46
59
  const masterDailyList = Object.entries(dailyCalculations).flatMap(([cat, calcs]) =>
47
60
  Object.keys(calcs).map(name => ({ category: cat, calcName: name }))
48
61
  );
49
- const insightsCalculations = require('aiden-shared-calculations-unified').calculations.insights || {};
62
+
63
+ // --- MODIFIED: Use the injected 'calculations' object ---
64
+ const insightsCalculations = calculations.insights || {};
50
65
  const masterInsightsList = Object.keys(insightsCalculations).map(name => ({ category: 'insights', calcName: name }));
51
- const socialPostCalculations = require('aiden-shared-calculations-unified').calculations.socialPosts || {};
66
+ const socialPostCalculations = calculations.socialPosts || {};
67
+ // --- END MODIFIED ---
52
68
  const masterSocialPostList = Object.keys(socialPostCalculations).map(name => ({ category: 'socialPosts', calcName: name }));
53
69
 
54
70
  // --- NEW: Create master list for meta calcs ---
@@ -227,7 +243,7 @@ async function streamAndProcess(
227
243
  todaySocialPostInsights = null,
228
244
  yesterdaySocialPostInsights = null
229
245
  ) {
230
- const { db, logger } = dependencies;
246
+ const { db, logger, calculationUtils } = dependencies; // <-- Get calculationUtils
231
247
  logger.log('INFO', `[${passName}] Streaming ${todayRefs.length} 'today' part docs for ${dateStr}...`);
232
248
 
233
249
  // Calculate yesterday's date string
@@ -235,7 +251,10 @@ async function streamAndProcess(
235
251
  yesterdayDate.setUTCDate(yesterdayDate.getUTCDate() - 1);
236
252
  const yesterdayStr = yesterdayDate.toISOString().slice(0, 10);
237
253
 
238
- const { instrumentToTicker, instrumentToSector } = await unifiedUtils.loadInstrumentMappings();
254
+ // --- MODIFIED: Get mapping functions from injected utils ---
255
+ const { instrumentToTicker, instrumentToSector } = await calculationUtils.loadInstrumentMappings();
256
+ // --- END MODIFIED ---
257
+
239
258
  // Add date strings to the context
240
259
  const context = {
241
260
  instrumentMappings: instrumentToTicker,
@@ -2,18 +2,22 @@
2
2
  * @fileoverview Data loader sub-pipes for the Computation System.
3
3
  * REFACTORED: Now stateless and receive dependencies.
4
4
  */
5
- const { withRetry } = require('aiden-shared-calculations-unified').utils;
6
- const { FieldPath } = require('@google-cloud/firestore'); // <<< --- ADD FieldPath
5
+
6
+ // <<< FIX: REMOVED all top-level 'require' and 'dependencies' lines >>>
7
7
 
8
8
  /**
9
9
  * Sub-pipe: pipe.computationSystem.dataLoader.getPortfolioPartRefs
10
10
  * @param {object} config - The computation system configuration object.
11
- * @param {object} dependencies - Contains db, logger.
11
+ * @param {object} dependencies - Contains db, logger, calculationUtils.
12
12
  * @param {string} dateString - The date in YYYY-MM-DD format.
13
13
  * @returns {Promise<Firestore.DocumentReference[]>} An array of DocumentReferences.
14
14
  */
15
15
  async function getPortfolioPartRefs(config, dependencies, dateString) {
16
- const { db, logger } = dependencies;
16
+ // <<< FIX: Destructure all dependencies here, inside the function >>>
17
+ const { db, logger, calculationUtils } = dependencies;
18
+ const { withRetry } = calculationUtils;
19
+ // <<< END FIX >>>
20
+
17
21
  logger.log('INFO', `Getting portfolio part references for date: ${dateString}`);
18
22
  const allPartRefs = [];
19
23
  const collectionsToQuery = [config.normalUserPortfolioCollection, config.speculatorPortfolioCollection];
@@ -47,12 +51,16 @@ async function getPortfolioPartRefs(config, dependencies, dateString) {
47
51
  /**
48
52
  * Sub-pipe: pipe.computationSystem.dataLoader.loadDataByRefs
49
53
  * @param {object} config - The computation system configuration object.
50
- * @param {object} dependencies - Contains db, logger.
54
+ * @param {object} dependencies - Contains db, logger, calculationUtils.
51
55
  * @param {Firestore.DocumentReference[]} refs - An array of DocumentReferences to load.
52
56
  * @returns {Promise<object>} A single map of { [userId]: portfolioData }.
53
57
  */
54
58
  async function loadDataByRefs(config, dependencies, refs) {
55
- const { db, logger } = dependencies;
59
+ // <<< FIX: Destructure all dependencies here, inside the function >>>
60
+ const { db, logger, calculationUtils } = dependencies;
61
+ const { withRetry } = calculationUtils;
62
+ // <<< END FIX >>>
63
+
56
64
  if (!refs || refs.length === 0) { return {}; }
57
65
  const mergedPortfolios = {};
58
66
  const batchSize = config.partRefBatchSize || 50;
@@ -86,7 +94,10 @@ async function loadDataByRefs(config, dependencies, refs) {
86
94
  * @returns {Promise<object>} A single map of { [userId]: portfolioData }.
87
95
  */
88
96
  async function loadFullDayMap(config, dependencies, partRefs) {
97
+ // <<< FIX: Destructure only what's needed for this specific function >>>
89
98
  const { logger } = dependencies;
99
+ // <<< END FIX >>>
100
+
90
101
  if (partRefs.length === 0) return {};
91
102
  logger.log('TRACE', `Loading full day map from ${partRefs.length} references...`);
92
103
 
@@ -100,12 +111,16 @@ async function loadFullDayMap(config, dependencies, partRefs) {
100
111
  * Sub-pipe: pipe.computationSystem.dataLoader.loadDailyInsights
101
112
  * Fetches the daily instrument insights document for a specific date.
102
113
  * @param {object} config - The computation system configuration object.
103
- * @param {object} dependencies - Contains db, logger.
114
+ * @param {object} dependencies - Contains db, logger, calculationUtils.
104
115
  * @param {string} dateString - The date in YYYY-MM-DD format.
105
116
  * @returns {Promise<object|null>} The insights data object or null if not found/error.
106
117
  */
107
118
  async function loadDailyInsights(config, dependencies, dateString) {
108
- const { db, logger } = dependencies;
119
+ // <<< FIX: Destructure all dependencies here, inside the function >>>
120
+ const { db, logger, calculationUtils } = dependencies;
121
+ const { withRetry } = calculationUtils;
122
+ // <<< END FIX >>>
123
+
109
124
  const insightsCollectionName = config.insightsCollectionName || 'daily_instrument_insights'; // Use config or default
110
125
  logger.log('INFO', `Loading daily insights for date: ${dateString} from ${insightsCollectionName}`);
111
126
  try {
@@ -129,12 +144,16 @@ async function loadDailyInsights(config, dependencies, dateString) {
129
144
  * Sub-pipe: pipe.computationSystem.dataLoader.loadDailySocialPostInsights
130
145
  * Fetches all analyzed social post documents for a specific date.
131
146
  * @param {object} config - The computation system configuration object.
132
- * @param {object} dependencies - Contains db, logger.
147
+ * @param {object} dependencies - Contains db, logger, calculationUtils.
133
148
  * @param {string} dateString - The date in YYYY-MM-DD format.
134
149
  * @returns {Promise<object|null>} An object map of { [postId]: postData } or null.
135
150
  */
136
151
  async function loadDailySocialPostInsights(config, dependencies, dateString) {
137
- const { db, logger } = dependencies;
152
+ // <<< FIX: Destructure all dependencies here, inside the function >>>
153
+ const { db, logger, calculationUtils } = dependencies;
154
+ const { withRetry } = calculationUtils;
155
+ // <<< END FIX >>>
156
+
138
157
  // Use the new config property, or fall back to a default
139
158
  const socialInsightsCollectionName = config.socialInsightsCollectionName || 'daily_social_insights';
140
159
  logger.log('INFO', `Loading social post insights for date: ${dateString} from ${socialInsightsCollectionName}`);
@@ -170,4 +189,4 @@ module.exports = {
170
189
  loadFullDayMap,
171
190
  loadDailyInsights,
172
191
  loadDailySocialPostInsights,
173
- };
192
+ };
@@ -1,62 +1,79 @@
1
1
  /**
2
2
  * @fileoverview Computation system sub-pipes and utils.
3
3
  * REFACTORED: Now stateless and receive dependencies where needed.
4
- * DYNAMIC: Automatically categorizes meta and historical calcs based on file path.
4
+ * DYNAMIC: Categorization logic is now an exported function.
5
5
  */
6
6
 
7
7
  const { FieldValue, FieldPath } = require('@google-cloud/firestore');
8
- const { calculations, utils } = require('aiden-shared-calculations-unified');
9
- const { withRetry } = utils;
10
-
11
- // --- Dynamic Calculation Categorization ---
12
-
13
- // These sets will be populated dynamically by inspecting the file structure
14
- const HISTORICAL_CALC_NAMES = new Set();
15
- const META_CALC_NAMES = new Set();
16
-
17
- const historicalCalculations = {};
18
- const dailyCalculations = {};
19
- const metaCalculations = {};
20
-
21
- for (const category in calculations) {
22
- // 1. Check for 'meta' category first (by top-level directory name)
23
- if (category === 'meta') {
24
- if (!metaCalculations[category]) metaCalculations[category] = {};
25
- for (const calcName in calculations[category]) {
26
- const CalculationClass = calculations[category][calcName];
27
- metaCalculations[category][calcName] = CalculationClass;
28
- META_CALC_NAMES.add(calcName);
29
- }
30
- continue; // Done with this category
31
- }
32
8
 
33
- // 2. Process other categories (e.g., 'pnl', 'capital_flow')
34
- for (const subKey in calculations[category]) {
35
- const item = calculations[category][subKey];
36
-
37
- // Check if the key is 'historical' and it contains an object of calculations
38
- if (subKey === 'historical' && typeof item === 'object' && item !== null && !Array.isArray(item)) {
39
- // This is the historical subdirectory, e.g., calculations.capital_flow.historical
40
-
41
- if (!historicalCalculations[category]) historicalCalculations[category] = {};
42
-
43
- for (const calcName in item) {
44
- const CalculationClass = item[calcName];
45
- // Add it to the historical list, using the parent 'category'
46
- historicalCalculations[category][calcName] = CalculationClass;
47
- HISTORICAL_CALC_NAMES.add(calcName);
9
+ // --- REMOVED ---
10
+ // const { utils } = require('aiden-shared-calculations-unified');
11
+ // const { withRetry } = utils;
12
+ // --- END REMOVED ---
13
+
14
+
15
+ /**
16
+ * NEW FUNCTION: This logic is now wrapped and exported.
17
+ * It receives the 'calculations' object from the entry point.
18
+ * @param {object} calculations - The full calculations object from the 'aiden-shared-calculations-unified' package.
19
+ * @returns {object} An object containing categorized calculations and name sets.
20
+ */
21
+ function categorizeCalculations(calculations) {
22
+ const HISTORICAL_CALC_NAMES = new Set();
23
+ const META_CALC_NAMES = new Set();
24
+
25
+ const historicalCalculations = {};
26
+ const dailyCalculations = {};
27
+ const metaCalculations = {};
28
+
29
+ for (const category in calculations) {
30
+ // 1. Check for 'meta' category first (by top-level directory name)
31
+ if (category === 'meta') {
32
+ if (!metaCalculations[category]) metaCalculations[category] = {};
33
+ for (const calcName in calculations[category]) {
34
+ const CalculationClass = calculations[category][calcName];
35
+ metaCalculations[category][calcName] = CalculationClass;
36
+ META_CALC_NAMES.add(calcName);
48
37
  }
38
+ continue; // Done with this category
49
39
  }
50
- // Check if the item is a function (a standard daily calc at the root of the category)
51
- else if (typeof item === 'function') {
52
- // This is a standard daily calc, e.g., calculations.pnl.average-daily-pnl
53
- const calcName = subKey;
54
- const CalculationClass = item;
55
-
56
- if (!dailyCalculations[category]) dailyCalculations[category] = {};
57
- dailyCalculations[category][calcName] = CalculationClass;
40
+
41
+ // 2. Process other categories (e.g., 'pnl', 'capital_flow')
42
+ for (const subKey in calculations[category]) {
43
+ const item = calculations[category][subKey];
44
+
45
+ // Check if the key is 'historical' and it contains an object of calculations
46
+ if (subKey === 'historical' && typeof item === 'object' && item !== null && !Array.isArray(item)) {
47
+ // This is the historical subdirectory, e.g., calculations.capital_flow.historical
48
+
49
+ if (!historicalCalculations[category]) historicalCalculations[category] = {};
50
+
51
+ for (const calcName in item) {
52
+ const CalculationClass = item[calcName];
53
+ // Add it to the historical list, using the parent 'category'
54
+ historicalCalculations[category][calcName] = CalculationClass;
55
+ HISTORICAL_CALC_NAMES.add(calcName);
56
+ }
57
+ }
58
+ // Check if the item is a function (a standard daily calc at the root of the category)
59
+ else if (typeof item === 'function') {
60
+ // This is a standard daily calc, e.g., calculations.pnl.average-daily-pnl
61
+ const calcName = subKey;
62
+ const CalculationClass = item;
63
+
64
+ if (!dailyCalculations[category]) dailyCalculations[category] = {};
65
+ dailyCalculations[category][calcName] = CalculationClass;
66
+ }
58
67
  }
59
68
  }
69
+
70
+ return {
71
+ historicalCalculations,
72
+ dailyCalculations,
73
+ metaCalculations,
74
+ HISTORICAL_CALC_NAMES,
75
+ META_CALC_NAMES
76
+ };
60
77
  }
61
78
  // --- End Dynamic Categorization ---
62
79
 
@@ -64,12 +81,16 @@ for (const category in calculations) {
64
81
  /**
65
82
  * Sub-pipe: pipe.computationSystem.computationUtils.commitBatchInChunks
66
83
  * @param {object} config - The computation system configuration object.
67
- * @param {object} dependencies - Contains db, logger.
84
+ * @param {object} dependencies - Contains db, logger, calculationUtils.
68
85
  * @param {Array<object>} writes - Array of { ref: DocumentReference, data: object }.
69
86
  * @param {string} operationName - Name for logging.
70
87
  */
71
88
  async function commitBatchInChunks(config, dependencies, writes, operationName) {
72
- const { db, logger } = dependencies;
89
+ // --- MODIFIED: Get withRetry from dependencies ---
90
+ const { db, logger, calculationUtils } = dependencies;
91
+ const { withRetry } = calculationUtils;
92
+ // --- END MODIFIED ---
93
+
73
94
  const batchSizeLimit = config.batchSizeLimit || 450;
74
95
 
75
96
  if (writes.length === 0) {
@@ -136,7 +157,10 @@ async function processJobsInParallel(jobs, taskFunction, passName, config) {
136
157
  * Internal helper: Finds the earliest date document in a collection.
137
158
  */
138
159
  async function getFirstDateFromCollection(config, dependencies, collectionName) {
139
- const { db, logger } = dependencies;
160
+ // --- MODIFIED: Get withRetry from dependencies ---
161
+ const { db, logger, calculationUtils } = dependencies;
162
+ const { withRetry } = calculationUtils;
163
+ // --- END MODIFIED ---
140
164
  let earliestDate = null;
141
165
  try {
142
166
  const blockDocRefs = await withRetry(
@@ -177,7 +201,7 @@ async function getFirstDateFromCollection(config, dependencies, collectionName)
177
201
  /**
178
202
  * Sub-pipe: pipe.computationSystem.computationUtils.getFirstDateFromSourceData
179
203
  * @param {object} config - The computation system configuration object.
180
- * @param {object} dependencies - Contains db, logger.
204
+ * @param {object} dependencies - Contains db, logger, calculationUtils.
181
205
  * @returns {Promise<Date>} The earliest date found or a default fallback date.
182
206
  */
183
207
  async function getFirstDateFromSourceData(config, dependencies) {
@@ -207,9 +231,9 @@ async function getFirstDateFromSourceData(config, dependencies) {
207
231
 
208
232
  module.exports = {
209
233
  FieldValue, FieldPath,
210
- historicalCalculations, dailyCalculations, metaCalculations,
211
- unifiedUtils: utils,
212
- HISTORICAL_CALC_NAMES, META_CALC_NAMES,
213
- withRetry, commitBatchInChunks,
234
+ // unifiedUtils: utils, // This is no longer defined here
235
+ categorizeCalculations, // EXPORT THE NEW FUNCTION
236
+ // withRetry, // This is no longer defined here
237
+ commitBatchInChunks,
214
238
  getExpectedDateStrings, processJobsInParallel, getFirstDateFromSourceData,
215
239
  };
@@ -5,8 +5,9 @@
5
5
  */
6
6
 
7
7
  const { FieldValue } = require('@google-cloud/firestore');
8
- // Import the mapping loader from your shared calculations package
9
- const { loadInstrumentMappings } = require('aiden-shared-calculations-unified').utils;
8
+ // --- REMOVED ---
9
+ // const { loadInstrumentMappings } = require('aiden-shared-calculations-unified').utils;
10
+ // --- END REMOVED ---
10
11
  const pLimit = require('p-limit');
11
12
 
12
13
  // How many tickers to fetch in parallel
@@ -19,16 +20,21 @@ const SHARD_SIZE = 40;
19
20
  /**
20
21
  * Main pipe: pipe.maintenance.runBackfillAssetPrices
21
22
  * @param {object} config - Configuration object.
22
- * @param {object} dependencies - Contains db, logger, headerManager, proxyManager.
23
+ * @param {object} dependencies - Contains db, logger, headerManager, proxyManager, calculationUtils.
23
24
  */
25
+ // --- MODIFIED: Removed calculationUtils, as it's in dependencies ---
24
26
  exports.runBackfillAssetPrices = async (config, dependencies) => {
25
- const { db, logger, headerManager, proxyManager } = dependencies;
27
+ const { db, logger, headerManager, proxyManager, calculationUtils } = dependencies;
28
+ const { loadInstrumentMappings } = calculationUtils; // <-- Get function from dependencies
29
+ // --- END MODIFIED ---
26
30
 
27
31
  logger.log('INFO', '[PriceBackfill] Starting historical price backfill...');
28
32
 
29
33
  let mappings;
30
34
  try {
35
+ // --- MODIFIED: Use the injected utils ---
31
36
  mappings = await loadInstrumentMappings();
37
+ // --- END MODIFIED ---
32
38
  if (!mappings || !mappings.instrumentToTicker) {
33
39
  throw new Error("Failed to load instrument mappings.");
34
40
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bulltrackers-module",
3
- "version": "1.0.92",
3
+ "version": "1.0.94",
4
4
  "description": "Helper Functions for Bulltrackers.",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -32,7 +32,6 @@
32
32
  "@google-cloud/firestore": "^7.11.3",
33
33
  "sharedsetup": "latest",
34
34
  "require-all": "^3.0.0",
35
- "aiden-shared-calculations-unified": "1.0.16",
36
35
  "@google-cloud/pubsub": "latest",
37
36
  "express": "^4.19.2",
38
37
  "cors": "^2.8.5",