forked from JuanIrache/gopro-telemetry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoCsv.js
93 lines (89 loc) · 3.31 KB
/
toCsv.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const deduceHeaders = require('../utils/deduceHeaders');
const breathe = require('../utils/breathe');
//Returns the data as a string
async function createCSV(data) {
let files = {};
for (const key in data) {
let device = key;
if (data[key]['device name'] != null) device = data[key]['device name'];
if (data[key].streams) {
for (const stream in data[key].streams) {
await breathe();
//We try to save all valid streams
if (
data[key].streams[stream].samples &&
data[key].streams[stream].samples.length
) {
//Prepare string
let rows = [];
//Get name and units to prepare headers
let name = stream;
if (data[key].streams[stream].name != null)
name = data[key].streams[stream].name;
let units;
if (data[key].streams[stream].units != null)
units = data[key].streams[stream].units;
const headers = deduceHeaders({ name, units });
let sticky = {};
//Loop all the samples
for (let i = 0; i < data[key].streams[stream].samples.length; i++) {
const s = data[key].streams[stream].samples[i];
//Check that at least we have the valid values
if (s.value != null) {
//Force convert to array
if (!Array.isArray(s.value)) s.value = [s.value];
//Update and remember sticky data
if (s.sticky) sticky = { ...sticky, ...s.sticky };
//If first row
if (!rows.length) {
let firstRow = [];
//Add time
if (s.cts != null) firstRow.push('cts');
if (s.date != null) firstRow.push('date');
//Fill missing headers
for (let ii = 0; ii < s.value.length; ii++) {
firstRow.push(headers[ii] || ii);
}
//Add stickies headers
firstRow.push(...Object.keys(sticky));
//Escape commas and add first row
rows.push(
firstRow.map(e => e.toString().replace(/,/g, '|')).join(',')
);
}
let row = [];
//Add time
if (s.cts != null) row.push(s.cts);
if (s.date != null) {
let processedDate = s.date;
if (typeof s.date != 'object') processedDate = new Date(s.date);
try {
row.push(processedDate.toISOString());
} catch (e) {
row.push(processedDate);
}
s.date = processedDate;
}
//Add all values
s.value.forEach(v => {
if (typeof v === 'number' || typeof v === 'string') row.push(v);
else row.push(JSON.stringify(v));
});
//Add stickies values
for (const key in sticky) row.push(sticky[key]);
//Add line to rows
rows.push(
row.map(e => e.toString().replace(/,/g, '|')).join(',')
);
}
}
//Join all lines
files[`${device}-${stream}`] = rows.join('\n');
}
}
}
}
return files;
}
//Converts the processed data to csv
module.exports = createCSV;