-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
function saveToCSV(csvData) {
return new Promise((resolve, reject) => {
const timestamp = new Date().toISOString().replace(/:/g, '-'); // Timestamp for filename
const filename = alerts_data_${timestamp}.csv;
const directory = 'files';
// Ensure 'files' directory exists, if not, create it
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory);
}
// Construct full file path
const filePath = path.join(directory, filename);
// Convert csvData to string if it's not already
if (typeof csvData !== 'string') {
csvData = JSON.stringify(csvData);
}
// Write data to CSV file
fs.writeFile(filePath, csvData, 'utf8', (err) => {
if (err) {
console.error('Error saving to CSV file:', err);
reject(err); // Reject the promise with the error
} else {
console.log(`Data saved to ${filePath}`);
resolve(filePath); // Resolve the promise with the file path
}
});
});
}
Metadata
Metadata
Assignees
Labels
No labels