bulltrackers-module 1.0.590 → 1.0.591

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.
@@ -3,6 +3,7 @@
3
3
  * UPDATED: Fixed bug where Alert Computations failed to trigger Pub/Sub on empty FINAL flush.
4
4
  * UPDATED: Added support for 'isPage' mode to store per-user data in subcollections.
5
5
  * UPDATED: Implemented TTL retention policy. Defaults to 90 days from the computation date.
6
+ * UPDATED: Fixed issue where switching to 'isPage' mode didn't clean up old sharded/raw data.
6
7
  */
7
8
  const { commitBatchInChunks, generateDataHash } = require('../utils/utils');
8
9
  const { updateComputationStatus } = require('./StatusRepository');
@@ -26,7 +27,9 @@ async function commitResults(stateObj, dStr, passName, config, deps, skipStatusW
26
27
  const schemas = [];
27
28
  const cleanupTasks = [];
28
29
  const alertTriggers = [];
29
- const { logger, db } = deps;
30
+ const { logger, db, calculationUtils } = deps; // Extract calculationUtils if available
31
+ const withRetry = calculationUtils?.withRetry || (fn => fn());
32
+
30
33
  const pid = generateProcessId(PROCESS_TYPES.STORAGE, passName, dStr);
31
34
 
32
35
  const flushMode = options.flushMode || 'STANDARD';
@@ -141,6 +144,30 @@ async function commitResults(stateObj, dStr, passName, config, deps, skipStatusW
141
144
  .collection(config.resultsSubcollection).doc(calc.manifest.category)
142
145
  .collection(config.computationsSubcollection).doc(name);
143
146
 
147
+ // --- CLEANUP START: Remove old storage formats (Sharded/Compressed) ---
148
+ // If the computation previously ran in Standard mode, we must remove shards
149
+ // and clear the main document data to avoid conflicting flags.
150
+ try {
151
+ const docSnap = await mainDocRef.get();
152
+ if (docSnap.exists) {
153
+ const dData = docSnap.data();
154
+ if (dData._sharded) {
155
+ const shardCol = mainDocRef.collection('_shards');
156
+ const shardDocs = await withRetry(() => shardCol.listDocuments());
157
+
158
+ if (shardDocs.length > 0) {
159
+ const cleanupOps = shardDocs.map(d => ({ type: 'DELETE', ref: d }));
160
+ await commitBatchInChunks(config, deps, cleanupOps, `${name}::PageModeCleanup`);
161
+ runMetrics.io.deletes += cleanupOps.length;
162
+ logger.log('INFO', `[PageMode] ${name}: Cleaned up ${cleanupOps.length} old shard documents.`);
163
+ }
164
+ }
165
+ }
166
+ } catch (cleanupErr) {
167
+ logger.log('WARN', `[PageMode] ${name}: Cleanup warning: ${cleanupErr.message}`);
168
+ }
169
+ // --- CLEANUP END ---
170
+
144
171
  // Calculate expiration based on computation date
145
172
  const expireAt = calculateExpirationDate(dStr, ttlDays);
146
173
 
@@ -181,7 +208,9 @@ async function commitResults(stateObj, dStr, passName, config, deps, skipStatusW
181
208
  _expireAt: expireAt // Ensure the header also gets deleted
182
209
  };
183
210
 
184
- await mainDocRef.set(headerData, { merge: true });
211
+ // CHANGED: Use merge: false to FORCE overwrite.
212
+ // This clears old "payload" (compressed), raw data keys, and old flags like "_sharded".
213
+ await mainDocRef.set(headerData, { merge: false });
185
214
  runMetrics.io.writes += 1;
186
215
 
187
216
  if (calc.manifest.hash) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bulltrackers-module",
3
- "version": "1.0.590",
3
+ "version": "1.0.591",
4
4
  "description": "Helper Functions for Bulltrackers.",
5
5
  "main": "index.js",
6
6
  "files": [