@@ -3,8 +3,9 @@ import wasmInit, { readParquet, wasmMemory } from "parquet-wasm/esm";
33// Enables zero-copy transer of Arrow data from wasm memory to JS
44import * as arrowJSFFI from "arrow-js-ffi" ;
55
6- import { DataRow , schema } from "./datatypes" ;
7-
6+ // Tries to call one of the HTTP endpoints of the server, either local or remote
7+ // depending on isProduction. Returns the Response, with some error handling.
8+ // Internal; should be used by the public-facing functions below
89async function tryFetch ( path : string , isProduction : boolean ) {
910 const url = isProduction ? "https://telemetry.formulaslug.com" : "http://localhost:9000" ;
1011 try {
@@ -19,6 +20,8 @@ async function tryFetch(path: string, isProduction: boolean) {
1920 }
2021}
2122
23+ // Fetch the DBC data associated with a given Recording (filepath). Contains a
24+ // map of ColumnName to information like units, range, etc
2225export async function getDBCForRecording (
2326 filepath : string ,
2427 isProduction : boolean ,
@@ -28,16 +31,19 @@ export async function getDBCForRecording(
2831 ) ;
2932}
3033
31- // ???
32- export async function getRecording ( file : string | File ) {
34+ // Fetch the recording data of a filepath (by querying the server) or from a
35+ // directly uploaded file (drag-n-drop). Returned data is in Arrow Table format.
36+ export async function getRecording ( file : string | File , isProduction : boolean ) {
3337 // Initializes WebAssembly memory for parquet-wasm, and gets a reference to it
3438 await wasmInit ( ) ;
3539 const WASM_MEMORY = wasmMemory ( ) ;
3640
3741 let arrayBuffer : ArrayBuffer | undefined ;
3842
43+ // If `file` is a string, fetch it from the server. If it's a File, extract
44+ // the arraybuffer data from that directly
3945 if ( typeof file == "string" ) { // string (pathname)
40- const resp = await tryFetch ( `/api/get-recording/${ file } ` , false )
46+ const resp = await tryFetch ( `/api/get-recording/${ file } ` , isProduction )
4147 arrayBuffer = await resp ?. arrayBuffer ( ) ;
4248 } else { // File object
4349 arrayBuffer = await file . arrayBuffer ( ) ;
@@ -49,7 +55,7 @@ export async function getRecording(file: string | File) {
4955 }
5056
5157 const arrowTableWasm = readParquet ( new Uint8Array ( arrayBuffer ) ) . intoFFI ( ) ;
52- const arrowTable = arrowJSFFI . parseTable < DataRow > (
58+ const arrowTable = arrowJSFFI . parseTable (
5359 WASM_MEMORY . buffer ,
5460 arrowTableWasm . arrayAddrs ( ) ,
5561 arrowTableWasm . schemaAddr ( ) ,
@@ -72,53 +78,3 @@ export async function availableRecordings(isProduction: boolean) {
7278 const resp = await tryFetch ( `/api/available-recordings` , isProduction ) ;
7379 return resp ?. json ( ) ;
7480}
75-
76- // export async function initRecordingSource(
77- // filepath: string,
78- // data: RefObject<DataArrays>,
79- // dataTrimmed: RefObject<DataArrays>,
80- // setNumRows: Dispatch<SetStateAction<number>>,
81- // viewLength: number,
82- // ) {
83- // const arrowTable = (await getRecording(filepath))!;
84- //
85- // // Completely reset data, so keys unused by the recording are left as null
86- // data.current = nullDataArrays();
87- // dataTrimmed.current = nullDataArrays();
88- //
89- // for (const [i, key] of arrowTable.schema.names.entries()) {
90- // // Get Arrow vector from table
91- // const vec = arrowTable.getChildAt(i);
92- // // Extract JS array from Arrow vector
93- // let dataArr = vec?.toArray();
94- // let dataArrTrimmed = vec
95- // ?.slice(Math.max(0, arrowTable.numRows - viewLength - 1))
96- // .toArray();
97- //
98- // // Chartjs doesn't support bigints ootb. The easiest solution for now is
99- // // just to replace them with supported TypedArrays
100- // if (dataArr instanceof BigInt64Array || dataArr instanceof BigUint64Array) {
101- // dataArr = convertToInt32Array(dataArr);
102- // dataArrTrimmed = convertToInt32Array(dataArrTrimmed);
103- // }
104- //
105- // // Set the data arrays directly
106- // data.current[key] = dataArr;
107- // dataTrimmed.current[key] = dataArrTrimmed;
108- // }
109- //
110- // setNumRows(arrowTable.numRows);
111- // }
112-
113- // Thanks claude
114- function convertToInt32Array ( bigIntArray : BigInt64Array | BigUint64Array ) {
115- const int32Array = new Int32Array ( bigIntArray . length ) ;
116- const INT32_MIN = - 2147483648 ;
117- const INT32_MAX = 2147483647 ;
118-
119- for ( let i = 0 ; i < bigIntArray . length ; i ++ ) {
120- const num = Number ( bigIntArray [ i ] ) ;
121- int32Array [ i ] = Math . max ( INT32_MIN , Math . min ( INT32_MAX , num ) ) ;
122- }
123- return int32Array ;
124- }
0 commit comments