Skip to content

Commit 06df423

Browse files
committed
feat: refactor logs loading to enable extra datasource
1 parent d5e7a15 commit 06df423

7 files changed

Lines changed: 648 additions & 651 deletions

File tree

src/App.js

Lines changed: 78 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import LogTable from "./LogTable";
88
import ToggleBar from "./ToggleBar";
99
import TripLogs from "./TripLogs";
1010
import TaskLogs from "./TaskLogs";
11-
import CloudLogging from "./CloudLogging";
11+
import DatasetLoading from "./DatasetLoading";
1212
import {
1313
uploadFile,
1414
getUploadedData,
@@ -337,35 +337,13 @@ class App extends React.Component {
337337
);
338338
};
339339

340-
handleLongPress = async (index) => {
341-
log(`Long press detected for button ${index}`);
342-
try {
343-
log(`Attempting to delete data for button ${index}`);
344-
await deleteUploadedData(index);
345-
log(`Data deleted successfully for button ${index}`);
346-
this.setState((prevState) => {
347-
const newUploadedDatasets = [...prevState.uploadedDatasets];
348-
newUploadedDatasets[index] = null;
349-
log(`Updated state after deletion for button ${index}:`, newUploadedDatasets);
350-
return {
351-
uploadedDatasets: newUploadedDatasets,
352-
activeDatasetIndex: prevState.activeDatasetIndex === index ? null : prevState.activeDatasetIndex,
353-
};
354-
});
355-
} catch (error) {
356-
console.error("Error deleting uploaded data:", error);
357-
alert("Error deleting uploaded data. Please try again.");
358-
}
359-
};
360-
361340
renderUploadButton = (index) => {
362341
const isUploaded = this.state.uploadedDatasets[index] === "Uploaded";
363342
const isActive = this.state.activeDatasetIndex === index;
364343
const isMenuOpen = this.state.activeMenuIndex === index;
365344

366345
const toggleMenu = (e) => {
367346
e.stopPropagation();
368-
log(`Toggle menu for dataset ${index}`);
369347
this.setState((prevState) => ({
370348
activeMenuIndex: prevState.activeMenuIndex === index ? null : index,
371349
}));
@@ -390,34 +368,27 @@ class App extends React.Component {
390368
};
391369
});
392370
} catch (error) {
393-
console.error("Error deleting local storage data:", error);
394-
alert("Error deleting local storage data. Please try again.");
371+
log("Error deleting local storage data. Please try again.", error);
395372
}
396373
};
397374

398375
const handleSaveClick = async (e) => {
399376
e.stopPropagation();
400377
log(`Export initiated for dataset ${index}`);
401-
402-
// Close menu
403-
this.setState({ activeMenuIndex: null });
378+
this.setState({ activeMenuIndex: null }); // Close menu
404379

405380
try {
406381
await saveDatasetAsJson(index);
407-
toast.success(`Dataset ${index + 1} exported successfully`);
408-
log(`Dataset ${index} exported successfully`);
382+
log(`Dataset ${index + 1} exported successfully`, "success");
409383
} catch (error) {
410-
console.error("Error exporting dataset:", error);
411-
toast.error(`Error exporting dataset: ${error.message}`);
384+
log(`Error exporting dataset: ${error.message}`, error);
412385
}
413386
};
414387

415388
const handlePruneClick = async (e) => {
416389
e.stopPropagation();
417390
log(`Prune initiated for dataset ${index}`);
418-
419-
// Close menu
420-
this.setState({ activeMenuIndex: null });
391+
this.setState({ activeMenuIndex: null }); // Close menu
421392

422393
try {
423394
const { minTime, maxTime } = this.state.timeRange;
@@ -451,8 +422,7 @@ class App extends React.Component {
451422

452423
data.rawLogs = remainingLogs;
453424

454-
// Save the pruned dataset back to storage
455-
await saveToIndexedDB(data, index);
425+
await saveToIndexedDB(data, index); // Save the pruned dataset back to storage
456426

457427
// Update the current dataset if this is the active one
458428
if (this.state.activeDatasetIndex === index) {
@@ -471,44 +441,59 @@ class App extends React.Component {
471441
}),
472442
() => {
473443
log("handlePruneClick: setState callback executed, selecting first row.");
474-
// Select first row after pruning
475-
this.selectFirstRow();
444+
this.selectFirstRow(); // Select first row after pruning
476445
}
477446
);
478447
}
479448

480-
toast.success(`Dataset pruned: removed ${removeCount} logs outside the selected time range.`);
449+
log(`Dataset pruned: removed ${removeCount} logs outside the selected time range.`, "info");
481450
} catch (error) {
482-
console.error("Error pruning dataset:", error);
483-
toast.error(`Error pruning dataset: ${error.message}`);
451+
log(`Error pruning dataset: ${error.message}`, error);
484452
}
485453
};
486454

487455
const handleClick = async () => {
488456
if (isUploaded) {
489457
this.switchDataset(index);
490458
} else {
491-
log("Opening Cloud Logging dialog directly for index " + index);
492-
const result = await this.showCloudLoggingDialog();
493-
if (result) {
494-
try {
495-
if (result.file) {
496-
log("Processing file upload from dialog");
497-
const uploadEvent = { target: { files: [result.file] } };
498-
await this.handleFileUpload(uploadEvent, index);
499-
} else if (result.logs) {
500-
log("Processing cloud logs");
501-
await uploadCloudLogs(result.logs, index);
502-
this.setState((prevState) => ({
503-
uploadedDatasets: prevState.uploadedDatasets.map((dataset, i) => (i === index ? "Uploaded" : dataset)),
504-
activeDatasetIndex: index,
505-
}));
506-
this.switchDataset(index);
507-
}
508-
} catch (error) {
509-
console.error("Failed to process data:", error);
510-
alert("Failed to process data: " + error.message);
459+
const result = await this.showDatasetLoadingDialog();
460+
if (!result) {
461+
log("Dataset loading dialog was cancelled or returned no data.");
462+
return; // User cancelled the dialog
463+
}
464+
465+
try {
466+
if (result.file) {
467+
const uploadEvent = { target: { files: [result.file] } };
468+
await this.handleFileUpload(uploadEvent, index);
469+
return;
470+
}
471+
472+
let logsToProcess;
473+
if (result.logs) {
474+
logsToProcess = result.logs;
475+
} else if (result.extraLogs) {
476+
// The 'extraLogs' are raw payloads, so we wrap them in the structure
477+
// that our uploader and processor expects ({ jsonPayload: ... }).
478+
logsToProcess = result.extraLogs.map((logEntry) => ({
479+
jsonPayload: logEntry,
480+
timestamp: logEntry.timestamp,
481+
}));
511482
}
483+
484+
if (logsToProcess) {
485+
await uploadCloudLogs(logsToProcess, index);
486+
this.setState(
487+
(prevState) => {
488+
const newUploadedDatasets = [...prevState.uploadedDatasets];
489+
newUploadedDatasets[index] = "Uploaded";
490+
return { uploadedDatasets: newUploadedDatasets };
491+
},
492+
() => this.switchDataset(index)
493+
);
494+
}
495+
} catch (error) {
496+
log(`Failed to process data from dialog: ${error.message}`, error);
512497
}
513498
}
514499
};
@@ -599,7 +584,8 @@ class App extends React.Component {
599584
};
600585
});
601586
}
602-
async showCloudLoggingDialog() {
587+
async showDatasetLoadingDialog() {
588+
log("Executing showDatasetLoadingDialog");
603589
const dialog = document.createElement("dialog");
604590
dialog.className = "cloud-logging-dialog";
605591

@@ -608,41 +594,52 @@ class App extends React.Component {
608594
dialog.appendChild(dialogRootElement);
609595
const dialogRoot = createRoot(dialogRootElement);
610596
dialog.showModal();
597+
611598
return new Promise((resolve) => {
612599
const cleanupAndResolve = (result) => {
600+
log("Closing and cleaning up DatasetLoading dialog");
613601
dialogRoot.unmount();
614602
dialog.remove();
615603
resolve(result);
616604
};
617-
const handleError = (error) => {
618-
console.error("Cloud Logging Error:", error);
619-
toast.error(error.message || "Failed to fetch logs. Please try again.");
605+
dialog.addEventListener("close", () => {
620606
cleanupAndResolve(null);
621-
};
607+
});
622608

623609
const handleFileUpload = (event) => {
624-
console.log("File upload selected from Cloud Logging dialog");
610+
log("File upload selected from DatasetLoading dialog");
625611
const file = event?.target?.files?.[0];
626612
if (file) {
627613
cleanupAndResolve({ file });
628614
}
629615
};
630616

631-
const cloudLoggingComponent = React.createElement(CloudLogging, {
632-
onLogsReceived: (logs) => {
633-
log(`Received ${logs.length} logs from Cloud Logging`);
634-
if (logs.length === 0) {
635-
toast.warning("No logs found matching your criteria.");
636-
cleanupAndResolve(null);
637-
} else {
638-
resolve({ logs });
639-
cleanupAndResolve({ logs });
640-
}
641-
},
617+
const handleCloudLogsReceived = (logs) => {
618+
log(`Received ${logs.length} logs from Cloud Logging`);
619+
if (logs.length > 0) {
620+
cleanupAndResolve({ logs });
621+
} else {
622+
// If no logs, we don't close the dialog, just show a toast.
623+
// The user might want to adjust params.
624+
toast.warning("No logs found matching your criteria.");
625+
}
626+
};
627+
628+
const handleExtraLogsReceived = (logs) => {
629+
log(`Received logs from extra data source`);
630+
if (logs) {
631+
cleanupAndResolve({ extraLogs: logs });
632+
} else {
633+
toast.warning("No logs found from the extra data source.");
634+
}
635+
};
636+
637+
const datasetLoadingComponent = React.createElement(DatasetLoading, {
638+
onLogsReceived: handleCloudLogsReceived,
639+
onExtraLogsReceived: handleExtraLogsReceived,
642640
onFileUpload: handleFileUpload,
643-
setError: handleError,
644641
});
645-
dialogRoot.render(cloudLoggingComponent);
642+
dialogRoot.render(datasetLoadingComponent);
646643
});
647644
}
648645

0 commit comments

Comments
 (0)