11import fs from "fs" ;
22
3- export function exportCSV ( flightLog ) {
4- let csvObject = {
5- imu : flightLog . imu ,
6- baro : flightLog . baro ,
7- flightInfo : flightLog . flightInfo ,
8- orientationInf : flightLog . orientationInf ,
3+ export function exportFlightLogToCSVs ( flightLog ) {
4+ const flightLogDir = "flight-log-export"
5+ let flightLogSections = [ " imu" , "baro" , "flightInfo" , "orientationInfo" , "filteredDataInfo" , "flightStates" , "eventInfo" , "voltageInfo" ] ;
6+
7+ if ( ! fs . existsSync ( flightLogDir ) ) {
8+ fs . mkdirSync ( flightLogDir ) ;
99 }
1010
11- fs . writeFile ( "flightlog.csv" , objectToCsv ( csvObject ) , 'utf8' , function ( err ) {
12- if ( err ) {
13- console . log ( "An error occurred while writing CSV Object to File." ) ;
14- return console . log ( err ) ;
15- }
16- console . log ( "CSV file has been saved." ) ;
17- } ) ;
11+ for ( let flightLogSection of flightLogSections ) {
12+ fs . writeFile ( `${ flightLogDir } /${ flightLogSection } .csv` , objectArrayToCSV ( flightLog [ flightLogSection ] ) , "utf8" , function ( err ) {
13+ if ( err ) {
14+ console . log ( "An error occurred while writing CSV object to file." ) ;
15+ return console . log ( err ) ;
16+ }
17+ console . log ( "CSV file has been saved." ) ;
18+ } ) ;
19+ }
1820}
1921
2022export function exportJSON ( flightLog ) {
21- fs . writeFile ( "flightlog.json" , JSON . stringify ( flightLog ) , ' utf8' , function ( err ) {
23+ fs . writeFile ( "flightlog.json" , JSON . stringify ( flightLog ) , " utf8" , function ( err ) {
2224 if ( err ) {
2325 console . log ( "An error occurred while writing JSON Object to File." ) ;
2426 return console . log ( err ) ;
@@ -27,53 +29,42 @@ export function exportJSON(flightLog) {
2729 } ) ;
2830}
2931
30- function objectToCsv ( obj , separator = "," ) {
31-
32- let flattened = { }
33- function headerMaker ( item ) {
34- for ( const [ key , value ] of Object . entries ( item ) ) {
35- if ( Array . isArray ( value ) ) {
36- flattened [ key ] = value ;
37- } else if ( typeof value === 'object' ) {
38- headerMaker ( value ) ;
39- }
40- }
32+ function objectArrayToCSV ( arr , separator = "," ) {
33+ if ( ! Array . isArray ( arr ) ) {
34+ console . log ( "objectArrayToCSV first argument must be an array." ) ;
35+ return ;
4136 }
42- headerMaker ( obj ) ;
4337
44- function transposeArray ( array ) {
45- let arrayLength = array [ 0 ] . length ;
46- for ( var i = 0 ; i < array . length ; i ++ ) {
47- if ( array [ i ] . length < arrayLength )
48- arrayLength = array [ i ] . length ;
49- } ;
38+ let CSVColumnNames = getCSVColumnNames ( arr [ 0 ] ) ;
5039
51- let newArray = [ ] ;
52- for ( var i = 0 ; i < arrayLength ; i ++ ) {
53- let sub = [ ]
54- for ( var j = 0 ; j < array . length ; j ++ ) {
55- sub . push ( ...Object . values ( array [ j ] [ i ] ) ) ;
56- } ;
57- newArray . push ( sub )
58- } ;
59- return newArray ;
60- }
40+ let CSVHeader = CSVColumnNames . join ( separator ) ;
41+ let CSVBody = arr . map ( obj =>
42+ CSVColumnNames . map ( header => getObjectValue ( obj , header ) ) . join ( separator )
43+ ) . join ( "\n" ) ;
6144
62- let header = [ ]
63- for ( const [ key , value ] of Object . entries ( flattened ) ) {
64- for ( const sub of Object . keys ( value [ 0 ] ) ) {
65- header . push ( key + "." + sub )
66- }
67- }
68- let rows = transposeArray ( Object . values ( flattened ) )
45+ return CSVHeader + "\n" + CSVBody ;
46+ }
47+
48+ function getCSVColumnNames ( obj ) {
49+ let headerSet = new Set ( ) ;
6950
70- let headerLength = header . length
51+ for ( let key of Object . keys ( obj ) ) {
52+ if ( typeof obj [ key ] == "object" ) {
53+ headerSet = new Set ( ...headerSet , extractCSVHeaders ( obj [ key ] ) . map ( v => `${ key } .${ v } ` ) ) ;
54+ }
55+ else headerSet . add ( key ) ;
56+ } ;
57+
58+ return [ ...headerSet ] ;
59+ } ;
60+
61+ function getObjectValue ( obj , path ) {
62+ let value = obj ;
7163
72- let text = header . join ( separator ) + "\n" ;
73- for ( const row of rows ) {
74- if ( row . length != headerLength )
75- console . log ( "length mismatch" )
76- text += row . join ( separator ) + "\n" ;
64+ for ( let key of path . split ( "." ) ) {
65+ value = value ?. [ key ] ;
66+ if ( value === undefined ) return value ;
7767 }
78- return text
79- }
68+
69+ return value ;
70+ } ;
0 commit comments