fix: offline fallback for analysis dashboard scan result#107
fix: offline fallback for analysis dashboard scan result#107vaishnavidesai09 wants to merge 1 commit into
Conversation
|
@vaishnavidesai09 is attempting to deploy a commit to the karan3431's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
Warning
|
| Layer / File(s) | Summary |
|---|---|
ONNX result serialized to sessionStorage in ScannerPage src/pages/ScannerPage.tsx |
After ONNX fusion inference, constructs a complete offlineScanResult object (grade derivation, confidence parsing, classification mapping, placeholder species/biomarkers/recommendations) and writes it to sessionStorage under "offlineScanResult". On-screen rendering continues to use fusion outputs directly. |
AnalysisDashboard offline fallback on fetch failure src/pages/AnalysisDashboard.tsx |
load() resets error state and uses double-quoted keys to read id and lastScanId. On API failure, reads and JSON-parses offlineScanResult from sessionStorage; if freshness_index is present, populates scan state silently. Otherwise sets error from the thrown value. loading is always cleared in finally. |
Sequence Diagram(s)
sequenceDiagram
actor User
participant ScannerPage
participant ONNX as ONNX Runtime
participant sessionStorage
participant AnalysisDashboard
participant api as API Server
User->>ScannerPage: initiate scan (offline)
ScannerPage->>ONNX: run edge inference
ONNX-->>ScannerPage: fusion outputs (label, freshness, confidence)
ScannerPage->>ScannerPage: build offlineScanResult object
ScannerPage->>sessionStorage: setItem("offlineScanResult", JSON)
ScannerPage->>AnalysisDashboard: navigate to /analysis
AnalysisDashboard->>api: getScan(scanId)
api-->>AnalysisDashboard: network error (offline)
AnalysisDashboard->>sessionStorage: getItem("offlineScanResult")
sessionStorage-->>AnalysisDashboard: parsed offline result
AnalysisDashboard->>AnalysisDashboard: setScan(offlineResult), setLoading(false)
AnalysisDashboard-->>User: render analysis from offline data
Estimated code review effort
🎯 2 (Simple) | ⏱️ ~10 minutes
Possibly related PRs
- jpdevhub/FreshScanAi#97: Both PRs modify the same offline/ONNX inference path inside
ScannerPage'srunScanfunction; that PR updated the backgroundapi.submitScancall in the same code block this PR now extends.
Suggested labels
SSoC26, Hard, size/level: hard
Poem
🐇 Hoppity-hop through the offline fog,
No server? No worry — check the sessionStorage log!
The scanner writes freshness, the dashboard reads back,
A fallback so nimble, nothing goes black.
Even without Wi-Fi, the scan still shows,
🥦 Fresh data delivered wherever the bunny goes!
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. | Write docstrings for the functions missing them to satisfy the coverage threshold. |
✅ Passed checks (4 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check | ✅ Passed | The title accurately describes the main change: implementing an offline fallback mechanism for the analysis dashboard when scan results cannot be fetched from the API. |
| Linked Issues check | ✅ Passed | All coding requirements from issue #100 are implemented: ScannerPage saves edge inference results to sessionStorage under offlineScanResult, and AnalysisDashboard attempts API fetch first then falls back to sessionStorage on failure. |
| Out of Scope Changes check | ✅ Passed | All changes are directly related to implementing the offline fallback mechanism specified in issue #100; no unrelated modifications are present in the changeset. |
✏️ Tip: You can configure your own custom pre-merge checks in the settings.
✨ Finishing Touches
🧪 Generate unit tests (beta)
- Create PR with unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/pages/AnalysisDashboard.tsx`:
- Around line 35-37: The fallback data validation needs to ensure that cached
offline scan results are only used when they match the requested ID. In the
AnalysisDashboard component where targetId is determined from idParam or lastId,
add validation logic to check that any offlineScanResult being used has a
scan_id that matches the targetId. Specifically, when an explicit id parameter
is provided (idParam is not null), only use the cached offlineScanResult if its
scan_id matches idParam, otherwise discard it. This validation should be applied
at the point where offlineScanResult is accessed and used (lines 45-55) to
prevent displaying unrelated scan data from the cache when a specific scan ID is
explicitly requested.
In `@src/pages/ScannerPage.tsx`:
- Around line 302-305: The sessionStorage.setItem call for persisting
offlineScanResult is not handling potential failures, which causes the entire
offline inference operation to abort if the write fails. Wrap the
sessionStorage.setItem call in a try-catch block so that if the persistence
fails (due to quota exceeded or other reasons), the error is caught and logged
without interrupting the successful offline inference flow.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: a6f7747c-96d9-495f-9876-96bd441b7ac3
📒 Files selected for processing (2)
src/pages/AnalysisDashboard.tsxsrc/pages/ScannerPage.tsx
| const idParam = params.get("id"); | ||
| const lastId = sessionStorage.getItem("lastScanId"); | ||
| const targetId = idParam || lastId; |
There was a problem hiding this comment.
Guard fallback data against request-ID mismatch.
When id is explicitly requested, catch-path fallback currently accepts any cached offlineScanResult, which can show unrelated scan data.
Use fallback only when no explicit id exists, or when cached scan_id matches the requested ID.
Suggested patch
- try {
- const idParam = params.get("id");
- const lastId = sessionStorage.getItem("lastScanId");
- const targetId = idParam || lastId;
+ const idParam = params.get("id");
+ const lastId = sessionStorage.getItem("lastScanId");
+ const targetId = idParam || lastId;
+ try {
@@
- if (offlineData) {
+ if (offlineData) {
try {
const parsed = JSON.parse(offlineData);
-
- if (parsed?.freshness_index != null) {
+ const matchesRequestedId =
+ !idParam || parsed?.scan_id === idParam;
+ if (parsed?.freshness_index != null && matchesRequestedId) {
setScan(parsed);
setLoading(false);
return;
}
} catch (e) {Also applies to: 45-55
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/pages/AnalysisDashboard.tsx` around lines 35 - 37, The fallback data
validation needs to ensure that cached offline scan results are only used when
they match the requested ID. In the AnalysisDashboard component where targetId
is determined from idParam or lastId, add validation logic to check that any
offlineScanResult being used has a scan_id that matches the targetId.
Specifically, when an explicit id parameter is provided (idParam is not null),
only use the cached offlineScanResult if its scan_id matches idParam, otherwise
discard it. This validation should be applied at the point where
offlineScanResult is accessed and used (lines 45-55) to prevent displaying
unrelated scan data from the cache when a specific scan ID is explicitly
requested.
| sessionStorage.setItem( | ||
| "offlineScanResult", | ||
| JSON.stringify(offlineScanResult), | ||
| ); |
There was a problem hiding this comment.
Make offlineScanResult persistence non-fatal.
A sessionStorage.setItem(...) failure here aborts the successful offline inference path and sends users to error state. The cache write should be best-effort only.
Suggested patch
- sessionStorage.setItem(
- "offlineScanResult",
- JSON.stringify(offlineScanResult),
- );
+ try {
+ sessionStorage.setItem(
+ "offlineScanResult",
+ JSON.stringify(offlineScanResult),
+ );
+ } catch (storageErr) {
+ console.warn("Failed to persist offline scan result", storageErr);
+ }📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| sessionStorage.setItem( | |
| "offlineScanResult", | |
| JSON.stringify(offlineScanResult), | |
| ); | |
| try { | |
| sessionStorage.setItem( | |
| "offlineScanResult", | |
| JSON.stringify(offlineScanResult), | |
| ); | |
| } catch (storageErr) { | |
| console.warn("Failed to persist offline scan result", storageErr); | |
| } |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/pages/ScannerPage.tsx` around lines 302 - 305, The sessionStorage.setItem
call for persisting offlineScanResult is not handling potential failures, which
causes the entire offline inference operation to abort if the write fails. Wrap
the sessionStorage.setItem call in a try-catch block so that if the persistence
fails (due to quota exceeded or other reasons), the error is caught and logged
without interrupting the successful offline inference flow.
|
Thanks @jpdevhub for the opportunity to work on this issue! This PR fixes the offline analysis dashboard issue where scan results were not displayed when the backend is unavailable. What changed
Why this approachsessionStorage was used to persist edge inference results across route navigation ( Safety
Tested
Let me know if any changes or improvements are needed. Happy to iterate 👍 |
Description
This PR fixes an issue where the Analysis Dashboard would show an empty or error state when a scan was performed in offline mode.
Previously, the dashboard depended entirely on the backend API response. When the backend was unavailable (offline mode), the scan data could not be fetched, resulting in a blank or broken UI.
This update introduces a fallback mechanism using
sessionStorageto ensure offline scan results are still rendered correctly.Closes #100
Fix
Added offline fallback for analysis dashboard when backend is unavailable.
Changes
sessionStorage(offlineScanResult) before navigating to analysis pagesessionStoragedataTested
sessionStoragecontains validofflineScanResultNotes
Checklist
npm run lintpasses with no errorsnpm run buildcompiles without TypeScript errorspython -m pytestpasses (not applicable / frontend-only change).envfiles, API keys, secrets, model weights, or__pycache__in this diffmain, not mergedSummary by CodeRabbit