1. /**
  2. * Batches operations on datasets for diagnostics with dry-run mode.
  3. *
  4. * @param {Array<object>} datasets An array of datasets to process.
  5. * @param {Array<Function>} operations An array of functions to apply to each dataset.
  6. * @param {boolean} dryRun Whether to perform a dry run (log changes without applying). Defaults to false.
  7. * @returns {Promise<Array<object>>} A promise that resolves to an array of processed datasets.
  8. */
  9. async function batchDatasetOperations(datasets, operations, dryRun = false) {
  10. if (!datasets || datasets.length === 0) {
  11. console.warn("No datasets provided.");
  12. return [];
  13. }
  14. if (!operations || operations.length === 0) {
  15. console.warn("No operations provided.");
  16. return datasets;
  17. }
  18. const processedDatasets = [];
  19. for (const dataset of datasets) {
  20. let currentDataset = dataset; //make a copy to avoid mutating original
  21. for (const operation of operations) {
  22. try {
  23. if (dryRun) {
  24. console.log("Dry Run: Applying operation to dataset:", currentDataset);
  25. operation(currentDataset); // Execute operation, but don't modify the dataset
  26. } else {
  27. console.log("Applying operation to dataset:", currentDataset);
  28. currentDataset = operation(currentDataset); // Apply the operation and update the dataset
  29. }
  30. } catch (error) {
  31. console.error("Error applying operation to dataset:", currentDataset, error);
  32. // Optionally handle the error (e.g., skip the dataset, log to a file)
  33. }
  34. }
  35. processedDatasets.push(currentDataset);
  36. }
  37. return processedDatasets;
  38. }

Add your comment