Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty CSV generation. #24

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cats-configurator",
"version": "0.3.3",
"version": "0.3.4",
"private": true,
"scripts": {
"start": "vue-cli-service electron:serve",
Expand Down
24 changes: 15 additions & 9 deletions src/modules/flightlog.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ export function exportFlightLogToCSVs(flightLog) {
const flightLogDir = "flight-log-export"
let flightLogSections = ["imu", "baro", "flightInfo", "orientationInfo", "filteredDataInfo", "gnssInfo", "flightStates", "eventInfo", "voltageInfo"];

if (!fs.existsSync(flightLogDir)){
if (!fs.existsSync(flightLogDir)) {
fs.mkdirSync(flightLogDir);
}

for (let flightLogSection of flightLogSections) {
fs.writeFile(`${flightLogDir}/${flightLogSection}.csv`, objectArrayToCSV(flightLog[flightLogSection]), "utf8", function (err) {
fs.writeFile(`${flightLogDir}/${flightLogSection}.csv`, objectArrayToCSV(flightLogSection, flightLog[flightLogSection]), "utf8", function (err) {
if (err) {
console.log("An error occurred while writing CSV object to file.");
return console.log(err);
Expand All @@ -29,20 +29,26 @@ export function exportJSON(flightLog) {
});
}

function objectArrayToCSV(arr, separator = ",") {
function objectArrayToCSV(section, arr, separator = ",") {
if (!Array.isArray(arr)) {
console.log("objectArrayToCSV first argument must be an array.");
return;
}

let CSVColumnNames = getCSVColumnNames(arr[0]);
if (arr.length > 0) {
console.log("Section " + section + " :" + arr[0])
let CSVColumnNames = getCSVColumnNames(arr[0]);

let CSVHeader = CSVColumnNames.join(separator);
let CSVBody = arr.map(obj =>
CSVColumnNames.map(header => getObjectValue(obj, header)).join(separator)
).join("\n");
let CSVHeader = CSVColumnNames.join(separator);
let CSVBody = arr.map(obj =>
CSVColumnNames.map(header => getObjectValue(obj, header)).join(separator)
).join("\n");

return CSVHeader + "\n" + CSVBody;
return CSVHeader + "\n" + CSVBody;
} else {
console.log("Array length of " + section + " is 0!");
return "No data recorded"
}
}

function getCSVColumnNames(obj) {
Expand Down
Loading