Skip to content

fix: Make log exporting feature asynchronous, preventing UI freeze in the front-end (fixes #92). #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 24, 2025

Conversation

Henry8192
Copy link
Collaborator

@Henry8192 Henry8192 commented Mar 6, 2025

Description

Fixes #92.

Checklist

  • The PR satisfies the contribution guidelines.
  • This is a breaking change and that has been indicated in the PR title, OR this isn't a
    breaking change.
  • Necessary docs have been updated, OR no docs need to be updated.

Validation performed

  1. Start the debug server and load example file.
  2. Click on "Export Logs" button and observe the logs being exported.
  3. Before the log export finishes, click the page navigation buttons to switch pages and observe that the pages are switching.

Summary by CodeRabbit

  • New Features
    • Log export functionality now offers more consistent progress feedback for improved user experience.
  • Bug Fixes
    • Removed handling for slow loading state, simplifying UI interactions during log exports.
  • Refactor
    • Streamlined log data retrieval and export processes to enhance overall efficiency and reliability.
    • Updated request structure for worker communications related to log exports, including renaming of constants for clarity.
    • Introduced a new helper function to manage log chunk exports more effectively.
    • Enhanced parameter handling in the log file manager for improved readability and maintainability.

@Henry8192 Henry8192 requested a review from a team as a code owner March 6, 2025 21:14
Copy link

coderabbitai bot commented Mar 6, 2025

Walkthrough

This pull request refactors the log export functionality by renaming constants and updating related code references. The constants tracking log export progress have been renamed from EXPORT_LOG_PROGRESS_VALUE_MAX/MIN to EXPORT_LOGS_PROGRESS_VALUE_MAX/MIN across UI components, context providers, and service managers. The worker request handling has been updated to use the new export code, with corresponding changes in type mappings. Additionally, the loadChunk method in the Log File Manager has been modified to invoke a callback instead of returning an object, and the SLOW_LOADING UI state has been removed. Overall, the modifications standardize naming and streamline the control flow.

Changes

File(s) Change Summary
src/components/…/ExportLogsButton.tsx, src/contexts/…/StateContextProvider.tsx, src/services/…/LogExportManager.ts Renamed constants from EXPORT_LOG_PROGRESS_VALUE_MAX/MIN to EXPORT_LOGS_PROGRESS_VALUE_MAX/MIN and updated their usage in UI logic and service methods.
src/services/MainWorker.ts, src/typings/worker.ts Replaced WORKER_REQ_CODE.EXPORT_LOG with EXPORT_LOGS and updated type mappings and response structure to simplify log export control flow.
src/services/LogFileManager/index.ts Updated the loadChunk method to exportChunkAndScheduleNext, which now invokes a callback with log data instead of returning an object.
src/typings/states.ts Removed the SLOW_LOADING state from the UI_STATE enum and its associated configuration.

Sequence Diagram(s)

sequenceDiagram
    participant UI as ExportLogsButton
    participant SC as StateContextProvider
    participant MW as MainWorker
    participant LF as LogFileManager

    UI->>SC: User initiates log export
    SC->>MW: Send EXPORT_LOGS request
    MW->>LF: Invoke exportChunkAndScheduleNext()
    LF-->>MW: Return log data (string)
    MW->>SC: Respond with log data
    SC->>UI: Update export progress display
Loading

Assessment against linked issues

Objective Addressed Explanation
Address service worker responsiveness during log export (#92)
Ensure consistent naming conventions for log export constants

Possibly related PRs

Suggested reviewers

  • davemarco
  • junhaoliao

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 88f7851 and e175fe1.

📒 Files selected for processing (1)
  • src/services/LogFileManager/index.ts (7 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.{cpp,hpp,java,js,jsx,tpp,ts,tsx}`: - Prefer `false == ` rather than `!`.

**/*.{cpp,hpp,java,js,jsx,tpp,ts,tsx}: - Prefer false == <expression> rather than !<expression>.

  • src/services/LogFileManager/index.ts
🧬 Code Definitions (1)
src/services/LogFileManager/index.ts (3)
src/typings/decoders.ts (3)
  • Decoder (105-105)
  • DecoderOptions (107-107)
  • DecodeResult (106-106)
src/typings/query.ts (1)
  • QueryResults (23-23)
src/typings/worker.ts (1)
  • FileSrcType (194-194)
🔇 Additional comments (6)
src/services/LogFileManager/index.ts (6)

56-57: Good addition of the callback for log export chunks.

The addition of the #onExportChunk callback member variable follows the established pattern in this class and maintains consistency with how other callbacks are managed.


73-96: Constructor parameters properly updated.

The constructor has been updated to accept and store the new onExportChunk callback parameter. The JSDoc is properly updated to document this new parameter.


123-156: Well-structured parameter refactoring in static create method.

The refactoring of the create method to use an object parameter instead of individual parameters improves maintainability as the number of parameters grows. The JSDoc is properly updated to document all parameters.


212-214: Updated docstring addresses previous review comment.

The docstring has been updated as suggested in the previous review, clearly explaining that the method now sends results to the renderer and schedules the next chunk if necessary.


218-240: Method properly renamed and implementation updated.

The rename from loadChunk to exportChunkAndScheduleNext is more descriptive of what the method actually does. The implementation now correctly invokes the callback with the concatenated messages instead of returning an object, which addresses the service worker clogging issue mentioned in the PR objectives.


408-445: Processable code organization.

The repositioning of the #processQueryDecodedEvents method improves code organization by keeping related methods together.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
src/services/MainWorker.ts (1)

61-61: Removed redundant ESLint rule.

Keeping only max-lines-per-function is sufficient as it effectively covers the same concerns as the removed max-statements rule.

🧰 Tools
🪛 Biome (1.9.4)

[error] 61-62: A global variable should not be reassigned.

Assigning to a global variable can override essential functionality.

(lint/suspicious/noGlobalAssign)

src/contexts/StateContextProvider.tsx (1)

287-294: Key fix: Added continuous log chunk processing.

This change is the core improvement that addresses the service worker clogging issue. Now the worker will continue to request more log chunks until the process is complete, rather than potentially stalling.

However, consider adding an additional null check instead of relying on the non-null assertion:

} else {
-    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-    workerPostReq(mainWorkerRef.current!, WORKER_REQ_CODE.EXPORT_LOGS, {
+    if (mainWorkerRef.current) {
+        workerPostReq(mainWorkerRef.current, WORKER_REQ_CODE.EXPORT_LOGS, {
             decodedEventIdx: args.nextDecodedEventIdx,
-        });
+        });
+    }
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0418be7 and 84428b2.

📒 Files selected for processing (7)
  • src/components/MenuBar/ExportLogsButton.tsx (2 hunks)
  • src/contexts/StateContextProvider.tsx (3 hunks)
  • src/services/LogExportManager.ts (3 hunks)
  • src/services/LogFileManager/index.ts (2 hunks)
  • src/services/MainWorker.ts (1 hunks)
  • src/typings/states.ts (1 hunks)
  • src/typings/worker.ts (3 hunks)
✅ Files skipped from review due to trivial changes (2)
  • src/services/LogExportManager.ts
  • src/components/MenuBar/ExportLogsButton.tsx
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.{cpp,hpp,java,js,jsx,tpp,ts,tsx}`: - Prefer `false == ...

**/*.{cpp,hpp,java,js,jsx,tpp,ts,tsx}: - Prefer false == <expression> rather than !<expression>.

  • src/typings/states.ts
  • src/services/MainWorker.ts
  • src/services/LogFileManager/index.ts
  • src/typings/worker.ts
  • src/contexts/StateContextProvider.tsx
🪛 Biome (1.9.4)
src/services/MainWorker.ts

[error] 61-62: A global variable should not be reassigned.

Assigning to a global variable can override essential functionality.

(lint/suspicious/noGlobalAssign)

🔇 Additional comments (11)
src/typings/states.ts (1)

97-97: Enabling navigation during slow loading is a good improvement.

This change allows users to access the navigation bar during slow loading operations, which is particularly important during log exports. Users can now navigate away from the page while an export is in progress, preventing the UI from becoming unresponsive.

src/services/LogFileManager/index.ts (2)

198-198: Simplified method signature improves clarity.

The return type was changed from an object to a string, which better represents the actual value being returned and simplifies the API.


214-214: Direct string return matches the updated method signature.

This change improves code clarity by directly returning the concatenated string instead of wrapping it in an object.

src/typings/worker.ts (3)

82-82: Consistent naming with 'EXPORT_LOGS' instead of 'EXPORT_LOG'.

The plural form is more accurate since the function exports multiple logs, and this matches the naming pattern used elsewhere in the code.


99-101: Added necessary parameter for export logs request.

The request now properly includes the decodedEventIdx parameter needed to track export progress.


125-125: Added next event index to improve export flow control.

Including nextDecodedEventIdx in the response allows for proper continuation of the export process without having to recalculate this value on the client side.

src/services/MainWorker.ts (2)

68-68: Updated case statement to match renamed request code.

This change ensures consistency with the updated enum in worker.ts.


73-80: Simplified log export response handling.

The implementation has been streamlined by:

  1. Directly accessing the decodedEventIdx from args
  2. Using a single postResp call instead of a loop
  3. Including the next index position for proper progress tracking

This refactoring likely addresses the service worker clogging issue by making the export process more efficient and responsive.

src/contexts/StateContextProvider.tsx (3)

14-15: LGTM: Consistent renaming of constants to plural form.

The constants have been renamed from EXPORT_LOG_PROGRESS_VALUE_MAX/MIN to EXPORT_LOGS_PROGRESS_VALUE_MAX/MIN which brings better consistency with the rest of the codebase, such as the existing EXPORT_LOGS_CHUNK_SIZE constant.


393-393: LGTM: Updated to use the renamed constant.

The export progress is now correctly set using the renamed constant EXPORT_LOGS_PROGRESS_VALUE_MIN.


400-402: LGTM: Worker request updated to new export logs API.

The export request has been properly updated to use the new EXPORT_LOGS worker request code (replacing EXPORT_LOG) and includes the necessary starting decodedEventIdx parameter.

@davemarco
Copy link
Contributor

Took a look at a high level, had a couple questions. I think it overall idea looks good

  • Did you consider using defer like the search query implementation? Maybe that could work as well instead having the client sending new request?
  • Should we go into FAST_LOADING state, or stay in READY_STATE when starting the export?
    - If we change to READY_STATE, we will probably need to consider case where user opens a new file during export

@Henry8192
Copy link
Collaborator Author

  • Did you consider using defer like the search query implementation? Maybe that could work as well instead having the client sending new request?

Good point. Let me try that.

  • Should we go into FAST_LOADING state, or stay in READY_STATE when starting the export?
    • If we change to READY_STATE, we will probably need to consider case where user opens a new file during export

AFAIK this is not a problem. The major time cost is internal exporting process, and if a user decides to open a new file, that worker will be dropped. You can verify that.
If the user decides to open a new file after export is finished and download is started, well, I guess the dropping the download is expected.

@davemarco
Copy link
Contributor

davemarco commented Mar 13, 2025

AFAIK this is not a problem.

I see

Anyways still consider moving to ready state, since in fast loading we can't switch pages

@Henry8192
Copy link
Collaborator Author

Anyways still consider moving to ready state, since in fast loading we can't switch pages

It can. It just freezes the query input box. But anyway we should use the ready state.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c8e0b25 and 830c13a.

📒 Files selected for processing (3)
  • src/contexts/StateContextProvider.tsx (3 hunks)
  • src/services/MainWorker.ts (2 hunks)
  • src/typings/worker.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/typings/worker.ts
  • src/contexts/StateContextProvider.tsx
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.{cpp,hpp,java,js,jsx,tpp,ts,tsx}`: - Prefer `false == ...

**/*.{cpp,hpp,java,js,jsx,tpp,ts,tsx}: - Prefer false == <expression> rather than !<expression>.

  • src/services/MainWorker.ts
🪛 Biome (1.9.4)
src/services/MainWorker.ts

[error] 85-86: A global variable should not be reassigned.

Assigning to a global variable can override essential functionality.

(lint/suspicious/noGlobalAssign)

🪛 GitHub Check: lint-check
src/services/MainWorker.ts

[failure] 60-60:
Missing JSDoc @throws declaration

🪛 GitHub Actions: lint
src/services/MainWorker.ts

[warning] 60-60: ESLint found too many warnings (maximum: 0). Missing JSDoc @throws declaration (jsdoc/require-throws)

🔇 Additional comments (5)
src/services/MainWorker.ts (5)

15-15: Good addition of the defer utility.

Adding the defer utility from the time utils is a solid choice here. This will help prevent the service worker from blocking during log exports, which directly addresses the issue described in the PR.


66-68: Correct null check pattern.

Good job using null === LOG_FILE_MANAGER instead of !LOG_FILE_MANAGER. This follows the coding guidelines that prefer false == <expression> rather than !<expression>.


78-80: Smart use of defer for asynchronous progression.

Using defer to schedule the next export chunk is excellent. This non-blocking approach ensures the service worker remains responsive during large exports, preventing UI freeze and allowing users to navigate between pages as mentioned in the PR testing steps.


85-85: ESLint directive improvement.

Good change to only disable the max-lines-per-function rule while enabling other rules. This helps maintain better code quality while acknowledging that this function needs to be longer than usual.

🧰 Tools
🪛 Biome (1.9.4)

[error] 85-86: A global variable should not be reassigned.

Assigning to a global variable can override essential functionality.

(lint/suspicious/noGlobalAssign)


92-94: Successfully updated export logs implementation.

The case statement has been properly renamed from EXPORT_LOG to EXPORT_LOGS, matching updates in the worker typings. The implementation has been simplified by delegating to the new helper function, which should resolve the service worker clogging issue.

Copy link
Contributor

@davemarco davemarco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good.I added minor comments

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/services/LogFileManager/index.ts (1)

216-216: JSDoc update needed for exportChunkAndScheduleNext method

The JSDoc comment still references "loads log events" rather than accurately describing that it exports logs.

-     * Loads log events in the range and sends them to the renderer. The range is defined as
+     * Exports log events in the range and sends them to the renderer. The range is defined as
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d4bd78a and 8ccc3eb.

📒 Files selected for processing (2)
  • src/services/LogFileManager/index.ts (8 hunks)
  • src/services/MainWorker.ts (2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.{cpp,hpp,java,js,jsx,tpp,ts,tsx}`: - Prefer `false == ` rather than `!`.

**/*.{cpp,hpp,java,js,jsx,tpp,ts,tsx}: - Prefer false == <expression> rather than !<expression>.

  • src/services/LogFileManager/index.ts
  • src/services/MainWorker.ts
🧬 Code Definitions (1)
src/services/MainWorker.ts (1)
src/typings/worker.ts (3) (3)
  • WORKER_RESP_CODE (188:188)
  • MainWorkerReqMessage (195:195)
  • WORKER_REQ_CODE (187:187)
🪛 Biome (1.9.4)
src/services/MainWorker.ts

[error] 69-70: A global variable should not be reassigned.

Assigning to a global variable can override essential functionality.

(lint/suspicious/noGlobalAssign)

🔇 Additional comments (16)
src/services/MainWorker.ts (5)

50-57: Well-structured callback function for exporting logs

This new callback function is well designed for handling log exports. It encapsulates the responsibility of posting responses with exported logs, following the single responsibility principle.


69-69: ESLint directive update improves code quality control

The ESLint directive has been updated to only disable max-lines-per-function while removing the max-statements rule. This is a good practice as it maintains better control over the function complexity.

🧰 Tools
🪛 Biome (1.9.4)

[error] 69-70: A global variable should not be reassigned.

Assigning to a global variable can override essential functionality.

(lint/suspicious/noGlobalAssign)


76-76: Request code properly renamed to match type definition

The request code has been updated from EXPORT_LOG to EXPORT_LOGS, which aligns with the changes in the worker type definitions shown in the relevant code snippets.


80-80: Improved log export implementation

The code now delegates the log export functionality to the LogFileManager, which enhances modularity and separation of concerns. This should resolve the service worker clogging issue mentioned in the PR objectives.


84-90: Constructor parameters updated with onExportChunk callback

The LogFileManager creation now includes the onExportChunk callback, which completes the implementation of the new export functionality. The object parameter style makes the code more maintainable for future parameter additions.

src/services/LogFileManager/index.ts (11)

40-47: Well-structured interface for constructor parameters

This new interface clearly defines the parameters needed for the LogFileManager constructor, making the code more maintainable and self-documenting. The inclusion of the onExportChunk callback parameter supports the new export functionality.


49-55: Clean interface for create method parameters

This interface nicely separates the parameters for the static create method from the constructor parameters, following good object-oriented design principles. The parameters are well organized and documented.


73-73: Private member for export chunk callback

The addition of this private member variable properly encapsulates the callback function for exporting log chunks, maintaining good encapsulation and information hiding.


93-100: Updated constructor with proper parameter destructuring

The constructor now uses object destructuring for its parameters, which makes the code more readable and maintainable. The parameter list matches the defined interface.


105-105: Initialization of onExportChunk callback

This line properly initializes the private onExportChunk member with the callback provided in the constructor parameters.


142-148: Static create method with updated parameters

The create method uses object destructuring for its parameters, consistent with the constructor. The addition of onExportChunk parameter allows for proper callback functionality.


158-158: Proper callback passing to constructor

The onExportChunk callback is correctly passed to the constructor, ensuring the export functionality works correctly throughout the component lifecycle.


223-223: Method renamed to better reflect its functionality

The method name has been updated from loadChunk to exportChunkAndScheduleNext, which better describes its purpose and behavior. This improved naming makes the code more self-documenting.


238-238: Improved handling of exported logs

Instead of returning an object with logs, the method now directly calls the onExportChunk callback with the joined log messages. This is more efficient and aligns with the callback pattern used throughout the codebase.


240-244: Non-blocking chunk export with defer

This implementation uses defer to schedule the next chunk export without blocking the main thread. This is a key improvement that should resolve the service worker clogging issue mentioned in the PR objectives.


413-450: Well-structured helper method for query processing

The #processQueryDecodedEvents method has been extracted to improve code organization. This method encapsulates the logic for processing decoded events and populating query results, following the single responsibility principle.

Copy link
Contributor

@davemarco davemarco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my last comment, then I will approve. Everything else looks good

@Henry8192 Henry8192 requested a review from davemarco March 20, 2025 21:29
davemarco
davemarco previously approved these changes Mar 23, 2025
Copy link
Contributor

@davemarco davemarco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit. Approved

Copy link
Contributor

@davemarco davemarco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix: Make log exporting feature asynchronous preventing UI freeze in the front-end (fixes #92).

@Henry8192 Henry8192 changed the title fix: Service worker getting clogged up during log export (fixes 92). fix: Make log exporting feature asynchronous, preventing UI freeze in the front-end (fixes #92). Mar 24, 2025
@Henry8192 Henry8192 merged commit 27020fd into y-scope:main Mar 24, 2025
4 checks passed
@Henry8192 Henry8192 deleted the log-export-worker-unblock branch March 24, 2025 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

new-log-viewer: Service worker getting clogged up during log export.
3 participants