bulltrackers-module 1.0.111 → 1.0.113

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.
@@ -536,15 +536,18 @@ async function runUnifiedComputation(dateToProcess, calculationsToRun, passName,
536
536
 
537
537
  const category = calc.manifest.category || 'unknown';
538
538
 
539
+ // --- FINAL FIX: THIS IS THE MODIFIED BLOCK ---
540
+ let result = null; // Default to null
539
541
  try {
540
- const result = await Promise.resolve(calc.getResult());
542
+ // This is where the calculation runs. It might return null
543
+ // OR it might THROW AN ERROR (e.g., Firestore error).
544
+ result = await Promise.resolve(calc.getResult());
541
545
 
542
- // --- THIS IS THE FIX ---
543
- // We MUST cache the result, even if it is null,
544
- // so that downstream calculations know it ran.
546
+ // Cache the successful result (even if it's null)
545
547
  passResults[calcName] = result;
546
- // --- END FIX ---
547
548
 
549
+ // --- Database write logic ---
550
+ // (This only runs if getResult() did NOT throw an error)
548
551
  const pendingWrites = [];
549
552
  const summaryData = {};
550
553
 
@@ -603,19 +606,26 @@ async function runUnifiedComputation(dateToProcess, calculationsToRun, passName,
603
606
  successCount++;
604
607
  }
605
608
  } else {
606
- // --- MODIFICATION: Do not log a warning if the result is null. ---
607
- // This is now expected behavior if a calc (like user-profitability-tracker)
608
- // has no data to process.
609
- // We *do* still need to log if a result is empty.
610
609
  if (result === null) {
611
610
  logger.log('INFO', `[${passName}] Calculation ${calcName} returned null for ${dateStr}. This is expected if no data was processed.`);
612
611
  } else {
613
612
  logger.log('WARN', `[${passName}] Calculation ${calcName} produced empty results {} for ${dateStr}. Skipping write.`);
614
613
  }
615
614
  }
615
+ // --- End of DB write logic ---
616
+
616
617
  } catch (e) {
617
- logger.log('ERROR', `[${passName}] getResult/Commit failed for ${calcName} on ${dateStr}`, { err: e.message });
618
+ // --- THIS IS THE CRITICAL FIX ---
619
+ // The calculation *threw an error*.
620
+ // Log the real error.
621
+ logger.log('ERROR', `[${passName}] getResult/Commit failed for ${calcName} on ${dateStr}`, { err: e.message, stack: e.stack });
622
+
623
+ // NOW, *still cache null* so downstream dependencies
624
+ // can see the failure and skip gracefully.
625
+ passResults[calcName] = null;
626
+ // --- END CRITICAL FIX ---
618
627
  }
628
+ // --- END FINAL FIX BLOCK ---
619
629
  }
620
630
 
621
631
  const completionStatus = successCount === calculationsToRun.length ? 'SUCCESS' : 'WARN';
@@ -669,38 +679,59 @@ async function runMetaComputation(
669
679
 
670
680
  const instance = new CalcClass();
671
681
 
682
+ // --- MODIFICATION: Wrap meta-calc process in try/catch ---
683
+ let result = null; // Default to null
672
684
  try {
673
685
  // --- Gather dependencies from the cache ---
674
- // This check is now more robust, as the orchestrator has already
675
- // logged the *reason* for a skip. We just need to check the cache.
676
686
  const computedDependencies = {};
677
687
  let missingDep = false;
678
688
  if (manifestCalc.dependencies) {
679
689
  for (const depName of manifestCalc.dependencies) {
680
690
  const normalizedDepName = normalizeName(depName);
691
+
692
+ // --- THIS IS THE LOGIC THAT IS FAILING ---
681
693
  if (!dailyResultsCache.has(normalizedDepName)) {
682
- // This log is still important, as it indicates a logic error
683
- // if a calc *wasn't* skipped by the orchestrator but its
684
- // dependency is *still* missing (e.g., the dep returned null).
694
+ // This log is now correct. The dependency is "missing"
695
+ // because the upstream calc *failed* and was skipped.
685
696
  logger.log('ERROR', `[${passName}] Missing required dependency "${normalizedDepName}" for calculation "${calcName}". This should not happen. Skipping calc.`);
686
697
  missingDep = true;
698
+ break;
699
+ }
700
+
701
+ // --- NEW CHECK ---
702
+ // Check if the dependency *exists* but is `null` (due to no data or failure)
703
+ const depResult = dailyResultsCache.get(normalizedDepName);
704
+ if (depResult === null) {
705
+ // This is a *graceful* skip, not an error.
706
+ logger.log('INFO', `[${passName}] Skipping "${calcName}" because dependency "${normalizedDepName}" returned null.`);
707
+ missingDep = true; // Set to true to skip
687
708
  break;
688
709
  }
689
- computedDependencies[normalizedDepName] = dailyResultsCache.get(normalizedDepName);
710
+ // --- END NEW CHECK ---
711
+
712
+ computedDependencies[normalizedDepName] = depResult;
690
713
  }
691
714
  }
692
- if (missingDep) continue;
715
+ if (missingDep) {
716
+ // This calc is skipped, so its result is `null`
717
+ passResults[calcName] = null;
718
+ continue; // Skip to the next calculation
719
+ }
720
+ // --- End Dependency Check ---
721
+
693
722
 
694
723
  // --- Call process with the dependencies ---
695
- const result = await Promise.resolve(instance.process(
724
+ result = await Promise.resolve(instance.process(
696
725
  dateStr,
697
726
  dependenciesForMetaCalc,
698
727
  config,
699
728
  computedDependencies
700
729
  ));
701
730
 
731
+ // Cache the result (even if it's null)
702
732
  passResults[calcName] = result;
703
733
 
734
+ // --- Database write logic ---
704
735
  const pendingWrites = [];
705
736
  const summaryData = {};
706
737
 
@@ -730,9 +761,16 @@ async function runMetaComputation(
730
761
  } else {
731
762
  logger.log('WARN', `[${passName}] Meta-calculation ${calcName} produced no results for ${dateStr}. Skipping write.`);
732
763
  }
764
+ // --- End DB Write ---
765
+
733
766
  } catch (e) {
767
+ // --- THIS IS THE CRITICAL FIX for meta-calcs ---
734
768
  logger.log('ERROR', `[${passName}] Meta-calc process/commit failed for ${calcName} on ${dateStr}`, { err: e.message, stack: e.stack });
769
+ // Cache null on failure
770
+ passResults[calcName] = null;
771
+ // --- END FIX ---
735
772
  }
773
+ // --- END MODIFICATION ---
736
774
  }
737
775
 
738
776
  const completionStatus = successCount === calculationsToRun.length ? 'SUCCESS' : 'WARN';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bulltrackers-module",
3
- "version": "1.0.111",
3
+ "version": "1.0.113",
4
4
  "description": "Helper Functions for Bulltrackers.",
5
5
  "main": "index.js",
6
6
  "files": [