Skip to content
Open
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
3 changes: 2 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node main.js",
"make": "npm i -g pkg && pkg . && chmod -R 777 build"
"make": "npm i -g pkg && pkg . && chmod -R 777 build",
"test:csv": "node test/session-csv.test.js"
},
"author": "",
"license": "MIT",
Expand Down
75 changes: 75 additions & 0 deletions server/test/session-csv.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const assert = require("assert");
const fs = require("fs");
const path = require("path");
const vm = require("vm");

const www = path.join(__dirname, "..", "www");

const uPlot = {
join: tables => {
const timestamps = Array.from(new Set(tables.flatMap(table => table[0]))).sort((a, b) => a - b);
return [timestamps].concat(tables.map(table => {
const values = new Map(table[0].map((timestamp, index) => [timestamp, table[1][index]]));
return timestamps.map(timestamp => values.has(timestamp) ? values.get(timestamp) : null);
}));
}
};

function exportCSV(app) {
let savedFile;
const context = {
app,
uPlot,
saveFile: (content, name) => { savedFile = { content, name }; },
buildFileName: (name, extension) => `${name}.${extension}`,
module: { exports: {} }
};

vm.runInNewContext(
`${fs.readFileSync(path.join(www, "utils", "import_export", "session.js"), "utf8")}\nmodule.exports = exportSessionCSV;`,
context
);
context.module.exports();
return savedFile;
}

const defaultExportApp = {
csvCellSeparator: ",",
csvDecimalSeparator: ".",
csvExportView: true,
telemetries: {
temperature: { type: "default", unit: "C", data: [[1000, 2000], [20.5, 21.5]] },
state: { type: "text", data: [[1500, 2500], ["warming", 'ready, "steady"']] }
}
};
const defaultExport = exportCSV(defaultExportApp);

assert.strictEqual(defaultExport.name, "session.csv");
assert.strictEqual(defaultExportApp.csvExportView, false);
assert.strictEqual(defaultExport.content, [
"timestamp(ms),temperature (C),state,",
'"1000","20.5",,',
'"1500",,"warming",',
'"2000","21.5",,',
'"2500",,"ready, ""steady""",',
""
].join("\n"));

const commaDecimalExport = exportCSV({
csvCellSeparator: ";",
csvDecimalSeparator: ",",
csvExportView: true,
telemetries: {
temperature: { type: "default", data: [[1000], [20.5]] },
display: { type: "text", data: [[1000], ["3.14"]] },
invalid: { type: "default", data: [[1000], [NaN]] }
}
});

assert.strictEqual(commaDecimalExport.content, [
"timestamp(ms);temperature;display;invalid;",
'"1000";"20,5";"3.14";;',
""
].join("\n"));

console.log("CSV export preserves text values, CSV quoting, and numeric decimal formatting");
23 changes: 17 additions & 6 deletions server/www/utils/import_export/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ function getFormatedSerieUnit(dataSerie)
return "";
}

function formatCSVValue(value) {
if (value == null) {
return "";
}

if (typeof value == "number") {
if (!Number.isFinite(value)) {
return "";
}
value = ("" + value).replace(".", app.csvDecimalSeparator);
}

return '"' + ("" + value).replace(/"/g, '""') + '"';
}

function exportSessionCSV() {

let csv = "timestamp(ms)"+app.csvCellSeparator;
Expand All @@ -39,11 +54,7 @@ function exportSessionCSV() {
for(let i=0;i<joinedData[0].length;i++) {
for(let j=0;j<joinedData.length;j++) {
let value = joinedData[j][i];
if(isFinite(value) && !isNaN(value)) {
valueStr = (""+joinedData[j][i]).replace('.',',').replace(',',app.csvDecimalSeparator)
csv += '"'+valueStr+'"';

}
csv += formatCSVValue(value);
csv += app.csvCellSeparator
}
csv += "\n";
Expand Down Expand Up @@ -98,4 +109,4 @@ function importSessionJSON(event) {
}
};
reader.readAsText(file);
}
}