bulltrackers-module 1.0.276 → 1.0.277

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,5 +1,6 @@
1
1
  /**
2
2
  * @fileoverview Execution-scoped data loader with caching.
3
+ * UPDATED: Handles Decompression of Shards.
3
4
  */
4
5
  const {
5
6
  loadDailyInsights,
@@ -7,6 +8,7 @@ const {
7
8
  getRelevantShardRefs,
8
9
  getPriceShardRefs
9
10
  } = require('../utils/data_loader');
11
+ const zlib = require('zlib'); // [NEW]
10
12
 
11
13
  class CachedDataLoader {
12
14
  constructor(config, dependencies) {
@@ -19,6 +21,19 @@ class CachedDataLoader {
19
21
  };
20
22
  }
21
23
 
24
+ // [NEW] Decompression Helper
25
+ _tryDecompress(data) {
26
+ if (data && data._compressed === true && data.payload) {
27
+ try {
28
+ return JSON.parse(zlib.gunzipSync(data.payload).toString());
29
+ } catch (e) {
30
+ console.error('[CachedDataLoader] Decompression failed', e);
31
+ return {};
32
+ }
33
+ }
34
+ return data;
35
+ }
36
+
22
37
  async loadMappings() {
23
38
  if (this.cache.mappings) return this.cache.mappings;
24
39
  const { calculationUtils } = this.deps;
@@ -52,7 +67,8 @@ class CachedDataLoader {
52
67
  try {
53
68
  const snap = await docRef.get();
54
69
  if (!snap.exists) return {};
55
- return snap.data();
70
+ // [UPDATED] Use decompression helper
71
+ return this._tryDecompress(snap.data());
56
72
  } catch (e) {
57
73
  console.error(`Error loading shard ${docRef.path}:`, e);
58
74
  return {};
@@ -1,7 +1,8 @@
1
1
  /**
2
- * @fileoverview Fetches results from previous computations, handling auto-sharding hydration.
2
+ * @fileoverview Fetches results from previous computations, handling auto-sharding and decompression.
3
3
  */
4
4
  const { normalizeName } = require('../utils/utils');
5
+ const zlib = require('zlib'); // [NEW]
5
6
 
6
7
  async function fetchExistingResults(dateStr, calcsInPass, fullManifest, config, { db }, includeSelf = false) {
7
8
  const manifestMap = new Map(fullManifest.map(c => [normalizeName(c.name), c]));
@@ -39,7 +40,20 @@ async function fetchExistingResults(dateStr, calcsInPass, fullManifest, config,
39
40
  const name = names[i];
40
41
  if (!doc.exists) return;
41
42
  const data = doc.data();
42
- if (data._sharded === true) {
43
+
44
+ // --- [NEW] DECOMPRESSION LOGIC ---
45
+ if (data._compressed === true && data.payload) {
46
+ try {
47
+ // Firestore returns Buffers automatically
48
+ const unzipped = zlib.gunzipSync(data.payload);
49
+ fetched[name] = JSON.parse(unzipped.toString());
50
+ } catch (e) {
51
+ console.error(`[Hydration] Failed to decompress ${name}:`, e);
52
+ fetched[name] = {};
53
+ }
54
+ }
55
+ // --- END NEW LOGIC ---
56
+ else if (data._sharded === true) {
43
57
  hydrationPromises.push(hydrateAutoShardedResult(doc.ref, name));
44
58
  } else if (data._completed) {
45
59
  fetched[name] = data;
@@ -1,14 +1,16 @@
1
1
  /**
2
2
  * @fileoverview Handles saving computation results with observability and Smart Cleanup.
3
+ * UPDATED: Implements GZIP Compression for efficient storage.
3
4
  * UPDATED: Implements Content-Based Hashing (ResultHash) for dependency short-circuiting.
4
5
  */
5
- const { commitBatchInChunks, generateDataHash } = require('../utils/utils'); // [UPDATED] Import generateDataHash
6
+ const { commitBatchInChunks, generateDataHash } = require('../utils/utils');
6
7
  const { updateComputationStatus } = require('./StatusRepository');
7
8
  const { batchStoreSchemas } = require('../utils/schema_capture');
8
9
  const { generateProcessId, PROCESS_TYPES } = require('../logger/logger');
9
10
  const { HeuristicValidator } = require('./ResultsValidator');
10
11
  const validationOverrides = require('../config/validation_overrides');
11
12
  const pLimit = require('p-limit');
13
+ const zlib = require('zlib'); // [NEW] Compression Lib
12
14
 
13
15
  const NON_RETRYABLE_ERRORS = [
14
16
  'PERMISSION_DENIED', 'DATA_LOSS', 'FAILED_PRECONDITION'
@@ -61,7 +63,7 @@ async function commitResults(stateObj, dStr, passName, config, deps, skipStatusW
61
63
 
62
64
  const isEmpty = !result || (typeof result === 'object' && Object.keys(result).length === 0);
63
65
 
64
- // [NEW] Calculate Result Hash (Content-Based)
66
+ // Calculate Result Hash (Content-Based)
65
67
  const resultHash = isEmpty ? 'empty' : generateDataHash(result);
66
68
 
67
69
  // Handle Empty Results
@@ -73,8 +75,8 @@ async function commitResults(stateObj, dStr, passName, config, deps, skipStatusW
73
75
  if (calc.manifest.hash) {
74
76
  successUpdates[name] = {
75
77
  hash: calc.manifest.hash,
76
- resultHash: resultHash, // [NEW] Store result hash
77
- dependencyResultHashes: calc.manifest.dependencyResultHashes || {}, // [NEW] Capture dep context
78
+ resultHash: resultHash,
79
+ dependencyResultHashes: calc.manifest.dependencyResultHashes || {},
78
80
  category: calc.manifest.category,
79
81
  composition: calc.manifest.composition,
80
82
  metrics: runMetrics
@@ -107,8 +109,8 @@ async function commitResults(stateObj, dStr, passName, config, deps, skipStatusW
107
109
  if (calc.manifest.hash) {
108
110
  successUpdates[name] = {
109
111
  hash: calc.manifest.hash,
110
- resultHash: resultHash, // [NEW]
111
- dependencyResultHashes: calc.manifest.dependencyResultHashes || {}, // [NEW]
112
+ resultHash: resultHash,
113
+ dependencyResultHashes: calc.manifest.dependencyResultHashes || {},
112
114
  category: calc.manifest.category,
113
115
  composition: calc.manifest.composition,
114
116
  metrics: runMetrics
@@ -134,8 +136,8 @@ async function commitResults(stateObj, dStr, passName, config, deps, skipStatusW
134
136
  if (calc.manifest.hash) {
135
137
  successUpdates[name] = {
136
138
  hash: calc.manifest.hash,
137
- resultHash: resultHash, // [NEW]
138
- dependencyResultHashes: calc.manifest.dependencyResultHashes || {}, // [NEW]
139
+ resultHash: resultHash,
140
+ dependencyResultHashes: calc.manifest.dependencyResultHashes || {},
139
141
  category: calc.manifest.category,
140
142
  composition: calc.manifest.composition,
141
143
  metrics: runMetrics
@@ -170,6 +172,44 @@ async function commitResults(stateObj, dStr, passName, config, deps, skipStatusW
170
172
  }
171
173
 
172
174
  async function writeSingleResult(result, docRef, name, dateContext, logger, config, deps, startShardIndex = 0, flushMode = 'STANDARD') {
175
+
176
+ // --- [NEW] COMPRESSION STRATEGY ---
177
+ // Try to compress before falling back to complex sharding
178
+ try {
179
+ const jsonString = JSON.stringify(result);
180
+ const rawBuffer = Buffer.from(jsonString);
181
+
182
+ // Only attempt if meaningful size (> 50KB)
183
+ if (rawBuffer.length > 50 * 1024) {
184
+ const compressedBuffer = zlib.gzipSync(rawBuffer);
185
+
186
+ // If compressed fits in one document (< 900KB safety limit)
187
+ if (compressedBuffer.length < 900 * 1024) {
188
+ logger.log('INFO', `[Compression] ${name}: Compressed ${(rawBuffer.length/1024).toFixed(0)}KB -> ${(compressedBuffer.length/1024).toFixed(0)}KB. Saved as Blob.`);
189
+
190
+ const compressedPayload = {
191
+ _compressed: true,
192
+ _completed: true,
193
+ _lastUpdated: new Date().toISOString(),
194
+ payload: compressedBuffer
195
+ };
196
+
197
+ // Write immediately
198
+ await docRef.set(compressedPayload, { merge: true });
199
+
200
+ return {
201
+ totalSize: compressedBuffer.length,
202
+ isSharded: false,
203
+ shardCount: 1,
204
+ nextShardIndex: startShardIndex
205
+ };
206
+ }
207
+ }
208
+ } catch (compErr) {
209
+ logger.log('WARN', `[Compression] Failed to compress ${name}. Falling back to standard sharding.`, compErr);
210
+ }
211
+ // --- END COMPRESSION STRATEGY ---
212
+
173
213
  const strategies = [
174
214
  { bytes: 900 * 1024, keys: null },
175
215
  { bytes: 450 * 1024, keys: 10000 },
@@ -4,7 +4,22 @@
4
4
  * --- NEW: Added streamPortfolioData async generator ---
5
5
  * --- FIXED: streamPortfolioData and streamHistoryData now accept optional 'providedRefs' ---
6
6
  * --- UPDATE: Added Smart Shard Indexing for specific ticker lookups ---
7
+ * --- UPDATE: Added GZIP Decompression Support for robust data loading ---
7
8
  */
9
+ const zlib = require('zlib'); // [NEW]
10
+
11
+ // [NEW] Helper for decompressing any doc if needed
12
+ function tryDecompress(data) {
13
+ if (data && data._compressed === true && data.payload) {
14
+ try {
15
+ return JSON.parse(zlib.gunzipSync(data.payload).toString());
16
+ } catch (e) {
17
+ console.error('[DataLoader] Decompression failed', e);
18
+ return {};
19
+ }
20
+ }
21
+ return data;
22
+ }
8
23
 
9
24
  /** --- Data Loader Sub-Pipes (Stateless, Dependency-Injection) --- */
10
25
 
@@ -39,7 +54,10 @@ async function loadDataByRefs(config, deps, refs) {
39
54
  const snapshots = await withRetry(() => db.getAll(...batchRefs), `getAll(batch ${Math.floor(i / batchSize)})`);
40
55
  for (const doc of snapshots) {
41
56
  if (!doc.exists) continue;
42
- const data = doc.data();
57
+ const rawData = doc.data();
58
+ // [UPDATED] Decompress if needed
59
+ const data = tryDecompress(rawData);
60
+
43
61
  if (data && typeof data === 'object') Object.assign(mergedPortfolios, data);
44
62
  else logger.log('WARN', `Doc ${doc.id} exists but data is not an object`, data);
45
63
  }
@@ -68,7 +86,8 @@ async function loadDailyInsights(config, deps, dateString) {
68
86
  const docSnap = await withRetry(() => docRef.get(), `getInsights(${dateString})`);
69
87
  if (!docSnap.exists) { logger.log('WARN', `Insights not found for ${dateString}`); return null; }
70
88
  logger.log('TRACE', `Successfully loaded insights for ${dateString}`);
71
- return docSnap.data();
89
+ // [UPDATED] Decompress
90
+ return tryDecompress(docSnap.data());
72
91
  } catch (error) {
73
92
  logger.log('ERROR', `Failed to load daily insights for ${dateString}`, { errorMessage: error.message });
74
93
  return null;
@@ -86,7 +105,10 @@ async function loadDailySocialPostInsights(config, deps, dateString) {
86
105
  const querySnapshot = await withRetry(() => postsCollectionRef.get(), `getSocialPosts(${dateString})`);
87
106
  if (querySnapshot.empty) { logger.log('WARN', `No social post insights for ${dateString}`); return null; }
88
107
  const postsMap = {};
89
- querySnapshot.forEach(doc => { postsMap[doc.id] = doc.data(); });
108
+ querySnapshot.forEach(doc => {
109
+ // [UPDATED] Decompress individual posts if needed
110
+ postsMap[doc.id] = tryDecompress(doc.data());
111
+ });
90
112
  logger.log('TRACE', `Loaded ${Object.keys(postsMap).length} social post insights`);
91
113
  return postsMap;
92
114
  } catch (error) {
@@ -168,12 +190,6 @@ async function getPriceShardRefs(config, deps) {
168
190
  * when only specific tickers are needed.
169
191
  */
170
192
 
171
- /**
172
- * Ensures the Price Shard Index exists. If not, builds it by scanning all shards.
173
- * @param {object} config
174
- * @param {object} deps
175
- * @returns {Promise<Object>} The lookup map { "instrumentId": "shardDocId" }
176
- */
177
193
  /**
178
194
  * Ensures the Price Shard Index exists. If not, builds it by scanning all shards.
179
195
  * [FIX] Added TTL check to ensure new instruments are discovered.
@@ -205,7 +221,10 @@ async function ensurePriceShardIndex(config, deps) {
205
221
 
206
222
  snapshot.forEach(doc => {
207
223
  shardCount++;
208
- const data = doc.data();
224
+ // [UPDATED] Robustly handle compressed shards during indexing
225
+ const rawData = doc.data();
226
+ const data = tryDecompress(rawData);
227
+
209
228
  if (data.history) {
210
229
  Object.keys(data.history).forEach(instId => {
211
230
  index[instId] = doc.id;
@@ -273,4 +292,4 @@ module.exports = {
273
292
  getPriceShardRefs,
274
293
  ensurePriceShardIndex,
275
294
  getRelevantShardRefs
276
- };
295
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bulltrackers-module",
3
- "version": "1.0.276",
3
+ "version": "1.0.277",
4
4
  "description": "Helper Functions for Bulltrackers.",
5
5
  "main": "index.js",
6
6
  "files": [