Skip to content

Dd #3

@atul-mane01

Description

@atul-mane01

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions