/**
* Batches operations on datasets for diagnostics with dry-run mode.
*
* @param {Array<object>} datasets An array of datasets to process.
* @param {Array<Function>} operations An array of functions to apply to each dataset.
* @param {boolean} dryRun Whether to perform a dry run (log changes without applying). Defaults to false.
* @returns {Promise<Array<object>>} A promise that resolves to an array of processed datasets.
*/
async function batchDatasetOperations(datasets, operations, dryRun = false) {
if (!datasets || datasets.length === 0) {
console.warn("No datasets provided.");
return [];
}
if (!operations || operations.length === 0) {
console.warn("No operations provided.");
return datasets;
}
const processedDatasets = [];
for (const dataset of datasets) {
let currentDataset = dataset; //make a copy to avoid mutating original
for (const operation of operations) {
try {
if (dryRun) {
console.log("Dry Run: Applying operation to dataset:", currentDataset);
operation(currentDataset); // Execute operation, but don't modify the dataset
} else {
console.log("Applying operation to dataset:", currentDataset);
currentDataset = operation(currentDataset); // Apply the operation and update the dataset
}
} catch (error) {
console.error("Error applying operation to dataset:", currentDataset, error);
// Optionally handle the error (e.g., skip the dataset, log to a file)
}
}
processedDatasets.push(currentDataset);
}
return processedDatasets;
}
Add your comment