Skip to content
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

build(log-viewer-webui): Migrate server codebase to TypeScript and update dependencies. #647

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

junhaoliao
Copy link
Member

@junhaoliao junhaoliao commented Dec 31, 2024

Description

  1. Migrate log-viewer-webui/server codebase to TypeScript.
  2. Update related tasks in Taskfile.yml.
  3. In the CLP package, rename the component's path from log_viewer_webui -> log-viewer-webui.

Validation performed

  1. Built the package: https://docs.yscope.com/clp/main/dev-guide/building-package
  2. Started the package: https://docs.yscope.com/clp/main/user-guide/quick-start-cluster-setup/single-node.html
  3. Compressed sample files: clp-package/sbin/compress.sh ~/samples/hive-24hr.
  4. Loaded the webui http://localhost:4000 in a Microsoft Edge browser.
  5. Performed a search with query string "1".
  6. On the first result, clicked the "View log event in context" button which brought up the log-viewer-webui-client page in a new tab. Later, the log viewer loaded up the log viewer which displayed the log in the context. Comparing the query result from the CLP webui and the one at the log viewer cursor and confirmed they match which should suggest the logEventNum parameter is also correctly passed.

Debug mode

  1. clp-package/sbin/stop-clp.sh log_viewer_webui.
  2. cd clp/component/log-viewer-webui; npm i; npm start
  3. Repeat step 7, in the "log-viewer-webui-client page in the new tab", change the port in the address bar to 8080 and observed successful extraction job completion. The redirection to yscope-log-viewer would not work because the viewer app is not served at the same address.

Summary by CodeRabbit

  • New Features

    • Migrated log viewer web UI server from JavaScript to TypeScript.
    • Introduced new routes for handling HTTP GET and POST requests with TypeBox validation.
    • Added a new utility function for implementing delays.
  • Bug Fixes

    • Improved route handling and validation using TypeBox.
    • Updated environment variable parsing and validation.
  • Refactors

    • Restructured database management and utility functions.
    • Enhanced type safety and code structure for server-side components.
    • Updated project configuration and build scripts.
  • Chores

    • Updated dependencies and package configurations.
    • Added TypeScript compilation and linting support.

Copy link
Contributor

coderabbitai bot commented Dec 31, 2024

Walkthrough

This pull request introduces modifications to the log viewer web UI server, transitioning from JavaScript to TypeScript and enhancing the organization of the project. Key changes include renaming tasks in Taskfile.yml, updating build processes, and adding new TypeScript files for improved type safety. The updates span multiple files, including the addition of a TypeScript configuration file and the integration of TypeBox for schema validation in routes.

Changes

File/Directory Change Summary
Taskfile.yml Renamed log-viewer-webui-clients task to log-viewer-webui, updated source files and build commands, replaced checksum file.
components/clp-package-utils/clp_package_utils/scripts/start_clp.py Updated directory paths for log viewer web UI.
components/log-viewer-webui/server/package.json Migrated to TypeScript, updated scripts, dependencies, and ESLint configuration.
components/log-viewer-webui/server/src/DbManager.jsDbManager.ts Converted to TypeScript, added type interfaces, enhanced database management functionalities.
components/log-viewer-webui/server/src/main.jsmain.ts Rewrote entry point with TypeScript, improved environment variable handling.
components/log-viewer-webui/server/src/routes/example.jsexample.ts Migrated example routes to TypeScript, added TypeBox schema validation.
components/log-viewer-webui/server/src/routes/query.ts Enhanced type safety and structure of Fastify routes, added schema validation for request body.
components/log-viewer-webui/server/src/routes/static.ts Updated route function to use FastifyPluginAsync type.
components/log-viewer-webui/server/src/utils.jsutils.ts Introduced TypeScript utility functions and constants.
components/log-viewer-webui/server/tsconfig.json Added new TypeScript configuration file for the server component.
components/log-viewer-webui/server/src/typings/DbManager.ts Introduced enumerations and interfaces for job types and statuses.

Possibly Related PRs

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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. (Beta)
  • @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
Contributor

@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

🧹 Nitpick comments (7)
components/log-viewer-webui/server/src/utils.ts (1)

9-11: Consider guarding against negative wait times.
If a negative value is accidentally passed, it will still schedule a timeout with a negative delay, effectively defaulting to zero. You may want to clamp the input to non-negative values to avoid unexpected cases.

-const sleep = (seconds: number): Promise<void> => new Promise((resolve) => {
-    setTimeout(resolve, seconds * MILLIS_PER_SECOND);
-});
+const sleep = (seconds: number): Promise<void> => new Promise((resolve) => {
+    const clampedSeconds = Math.max(0, seconds);
+    setTimeout(resolve, clampedSeconds * MILLIS_PER_SECOND);
+});
components/log-viewer-webui/server/src/app.ts (1)

15-19: Consider securing credentials [en-CA]

Defining sqlDbUser and sqlDbPass on the AppProps interface is straightforward, but ensure these values are sourced and stored securely (for instance, using environment variables) to mitigate the risk of accidental disclosure.

components/log-viewer-webui/server/src/routes/query.ts (1)

22-29: Validate numeric fields more strictly
Currently, logEventIdx is only constrained to be an integer. Consider adding minimum or maximum bounds to ensure the value remains within valid ranges, preventing malformed requests.

components/log-viewer-webui/server/src/DbManager.ts (1)

55-55: Polling interval might be too frequent
A 0.5ms interval in JOB_COMPLETION_STATUS_POLL_INTERVAL_MILLIS could cause unnecessary load on both the server and database. Consider increasing this interval or implementing a more efficient notification-based approach to reduce resource usage.

-const JOB_COMPLETION_STATUS_POLL_INTERVAL_MILLIS = 0.5;
+const JOB_COMPLETION_STATUS_POLL_INTERVAL_MILLIS = 500;
components/log-viewer-webui/server/src/routes/example.ts (1)

15-23: Optional validation enhancement for GET route
You're returning a greeting for any string provided as name. For added reliability, consider adding further constraints (e.g. non-empty) to avoid unexpected edge cases.

components/log-viewer-webui/server/package.json (2)

7-8: Build scripts added.
The build and build:watch scripts will streamline TypeScript compilation. Consider verifying these scripts in CI to prevent build regressions.


38-40: Nodemon and testing tools.
Continuing to use nodemon for reloading is standard. Ensure that your TypeScript build paths are consistently referenced in watch commands.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 329edf6 and 89dfb45.

⛔ Files ignored due to path filters (1)
  • components/log-viewer-webui/server/package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (16)
  • Taskfile.yml (6 hunks)
  • components/clp-package-utils/clp_package_utils/scripts/start_clp.py (2 hunks)
  • components/log-viewer-webui/server/package.json (1 hunks)
  • components/log-viewer-webui/server/src/DbManager.js (0 hunks)
  • components/log-viewer-webui/server/src/DbManager.ts (1 hunks)
  • components/log-viewer-webui/server/src/app.test.ts (1 hunks)
  • components/log-viewer-webui/server/src/app.ts (2 hunks)
  • components/log-viewer-webui/server/src/main.js (0 hunks)
  • components/log-viewer-webui/server/src/main.ts (1 hunks)
  • components/log-viewer-webui/server/src/routes/example.js (0 hunks)
  • components/log-viewer-webui/server/src/routes/example.ts (1 hunks)
  • components/log-viewer-webui/server/src/routes/query.ts (1 hunks)
  • components/log-viewer-webui/server/src/routes/static.ts (2 hunks)
  • components/log-viewer-webui/server/src/utils.js (0 hunks)
  • components/log-viewer-webui/server/src/utils.ts (1 hunks)
  • components/log-viewer-webui/server/tsconfig.json (1 hunks)
💤 Files with no reviewable changes (4)
  • components/log-viewer-webui/server/src/utils.js
  • components/log-viewer-webui/server/src/routes/example.js
  • components/log-viewer-webui/server/src/main.js
  • components/log-viewer-webui/server/src/DbManager.js
✅ Files skipped from review due to trivial changes (1)
  • components/log-viewer-webui/server/tsconfig.json
🧰 Additional context used
📓 Path-based instructions (8)
components/log-viewer-webui/server/src/utils.ts (1)

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

components/log-viewer-webui/server/src/routes/static.ts (1)

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

components/log-viewer-webui/server/src/routes/example.ts (1)

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

components/log-viewer-webui/server/src/routes/query.ts (1)

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

components/log-viewer-webui/server/src/app.ts (1)

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

components/log-viewer-webui/server/src/main.ts (1)

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

components/log-viewer-webui/server/src/DbManager.ts (1)

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

components/log-viewer-webui/server/src/app.test.ts (1)

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

🔇 Additional comments (31)
components/log-viewer-webui/server/src/utils.ts (3)

1-1: Constant name is clear and self-explanatory.
This constant accurately conveys its purpose, aiding code readability.


3-8: Good use of JSDoc comment.
The documentation is clear and informative, enhancing code maintainability.


13-13: Export additional utilities as needed.
Currently, only sleep is exported. If other utility constants or functions are needed, consider adding them here for streamlined imports.

components/log-viewer-webui/server/src/app.ts (3)

1-5: Good use of named imports for type-safety [en-CA]

Explicitly importing fastify, FastifyInstance, and FastifyServerOptions is indeed the recommended approach for TypeScript projects, as it clearly communicates the contract used by the code and helps avoid namespace collisions in larger codebases.


24-28: Documentation is aligned with the new function signature [en-CA]

The updated docstrings properly reflect the new app function signature and parameters. This helps maintain clarity and reduces confusion.


34-34: Returning a Fastify instance is consistent [en-CA]

Returning the instantiated server matches the documented return type of Promise<FastifyInstance>, which provides clear expectations for downstream consumers.

components/log-viewer-webui/server/src/routes/query.ts (2)

31-31: Good adherence to coding guidelines
Your usage of if (false === EXTRACT_JOB_TYPES.includes(extractJobType)) is consistent with the specified preference of using false === <expression>.


9-11: 🛠️ Refactor suggestion

Use the correct extension for DbManager imports
It appears that the file you are importing is now DbManager.ts rather than DbManager.js. Please update these imports to reflect the new .ts extension to avoid confusion and potential runtime issues.

-} from "../DbManager.js";
+} from "../DbManager.js"; // Change ".js" to ".ts" if the file is indeed DbManager.ts

Likely invalid or redundant comment.

components/log-viewer-webui/server/src/DbManager.ts (1)

227-234: Ensure credentials are not logged or exposed
When registering MySQL with connectionString, watch for unintended logging. Ensure that the credentials (user/password) are protected, particularly if you log connection details.

components/log-viewer-webui/server/src/app.test.ts (1)

7-7: Integration test approach looks sound
Adopting await tap.test is a valid upgrade and ensures async behaviour is properly handled. This also aligns with typical test patterns in TypeScript-based Fastify projects.

components/log-viewer-webui/server/src/routes/example.ts (1)

25-33: Schema-based POST route is well-structured
The route properly ensures name is provided. This is a straightforward pattern for simple demonstration routes.

components/log-viewer-webui/server/src/main.ts (1)

69-74: Consistent environment validation approach
Your conditional uses the recommended false === isKnownNodeEnv(NODE_ENV). This aligns with your coding guidelines and improves readability by explicitly checking for disallowed values.

components/clp-package-utils/clp_package_utils/scripts/start_clp.py (2)

906-909: Ensure consistent usage of the renamed directory.
It appears there is still a reference to “log_viewer_webui” on line 909 instead of “log-viewer-webui”. Confirm that all references to the old directory name have been updated.


962-962: Double-check the node version alignment.
You are specifying node-22 here. Confirm that the environment and other related scripts align with Node.js 22 and that no references to an older or different Node.js version remain in the codebase.

components/log-viewer-webui/server/src/routes/static.ts (2)

1-1: Good addition of type safety.
This import of FastifyPluginAsync clarifies the contract for the routes plugin. This is a helpful enhancement for type-checking.


14-16: Well-structured route registration.
Using false === path.isAbsolute(...) is consistent with your project’s guidelines. Great job following the custom styling for boolean checks.

components/log-viewer-webui/server/package.json (8)

5-5: Entry point updated to TypeScript.
Referencing src/main.ts provides a clearer path for TypeScript compilation. Ensure all internal references are updated accordingly.


11-12: Production start changes.
Running the production script from dist/src/main.js is correct for a TypeScript build flow. Confirm the environment variables and logging level for production continue to be set as intended.


19-31: Dependency updates.
Upgrading @fastify/mongodb, @fastify/mysql, and others in conjunction with TypeScript is a good practice. Double-check compatibility with any older consumer modules before merging.


34-36: Essential devDependencies for TypeScript.
Adding the TypeScript ESLint plugins and concurrently will help with both linting and parallel build runs. Good improvement.


43-43: Root-level ESLint configuration.
Setting root: true helps unify linting across the codebase. This is helpful for multi-level TypeScript projects.


45-46: Extending React and TypeScript configs.
Combining yscope/react and yscope/typescript is an effective approach for consistent styling and best practices in a full-stack environment.


48-51: Ignoring transpiled and third-party directories.
Ignoring dist/ and node_modules/ is standard to avoid linting extraneous directories. This keeps your linting scope tight.


52-58: Fine-tuning ESLint rules.
Disabling @typescript-eslint/require-await and turning off new-cap can reduce false positives in specific coding styles. If concurrency logic is introduced, consider re-evaluating require-await.

Taskfile.yml (7)

87-87: Renamed MD5 file reference.
Switching from log-viewer-webui-clients.md5 to log-viewer-webui.md5 makes sense given the consolidated naming. Make sure all scripts that reference the old checksum file are updated.


109-109: Added log-viewer-webui as a dependency.
Ensuring that log-viewer-webui is built prior to packaging is a sensible approach. This should maintain coherence in the final package.


158-159: New rsync for log-viewer-webui build output.
Copying the compiled artifacts from log-viewer-webui into var/www/log-viewer-webui is logical. Confirm that partial or incremental updates do not break older symlinks or references.


161-161: Installing server dependencies.
Running npm clean-install ensures a fresh installation, which is good for reproducibility. Confirm no local overrides (like dev scripts) are needed for the production environment.


210-210: Renamed task: log-viewer-webui.
Renaming from log-viewer-webui-clients to log-viewer-webui clarifies its broader scope. This consolidates both client and server building under one task.


221-225: Added TypeScript sources for the server.
Including server/package.json, server/package-lock.json, server/settings.json, and server/src/**/*.ts plus the tsconfig.json ensures the server portion is fully tracked. This is a good step for maintainability.


243-250: Commands to build and copy server output.
Building TypeScript into server/dist then syncing to the output directory is well-organized. Validate that the outDir in tsconfig.json points to dist to avoid confusion.

import dotenv from "dotenv";
import process from "node:process";

import app from "./app.js";
Copy link
Contributor

@coderabbitai coderabbitai bot Dec 31, 2024

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Confirm usage of the TypeScript entry point
The file app.js may already be migrated to TypeScript (e.g. app.ts). If so, please ensure consistency in your import statement to avoid confusion.

-import app from "./app.js";
+import app from "./app.js"; // Change to ".ts" if the file is named app.ts

Committable suggestion skipped: line range outside the PR's diff.

Copy link
Member Author

Choose a reason for hiding this comment

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

We specified "moduleResolution": "node16" in tsconfig.json and therefore the filename extension of .js is expected.

Copy link
Contributor

Choose a reason for hiding this comment

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

@junhaoliao, that makes sense. Thanks for clarifying how the Node16 module resolution impacts the expected file extension for imports. Let me know if there’s anything else I can help with!


✏️ Learnings added
Learnt from: junhaoliao
PR: y-scope/clp#647
File: components/log-viewer-webui/server/src/main.ts:8-8
Timestamp: 2024-12-31T19:19:54.921Z
Learning: When "moduleResolution": "node16" is used in a TypeScript project's tsconfig.json, the import file extension is typically expected to be ".js" (or ".cjs"/".mjs"), even if the source code is originally in TypeScript (.ts). This is a part of node16’s resolution behavior.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

Comment on lines +56 to +58
"new-cap": [
"off"
]
Copy link
Member Author

Choose a reason for hiding this comment

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

To avoid raising errors on Typebox type creations (e.g., Type.Enum()).

Comment on lines +53 to +55
"@typescript-eslint/require-await": [
"off"
],
Copy link
Member Author

Choose a reason for hiding this comment

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

We currently implemented all Fastify plugin callbacks as async functions regardless whether there're async operations within. Alternatively, for the plugins that do not require async, we can remove the async specification and re-enable this ESLint rule.

"{{.G_LOG_VIEWER_WEBUI_BUILD_DIR}}/yscope-log-viewer"
"{{.OUTPUT_DIR}}/var/www/log_viewer_webui/"
"{{.G_LOG_VIEWER_WEBUI_BUILD_DIR}}/"
"{{.OUTPUT_DIR}}/var/www/log-viewer-webui"
Copy link
Member Author

Choose a reason for hiding this comment

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

Note log_viewer_webui is renamed as log-viewer-webui here.

node_path = str(container_log_viewer_webui_dir / "server" / "node_modules")
settings_json_path = (
get_clp_home() / "var" / "www" / "log_viewer_webui" / "server" / "settings.json"
get_clp_home() / "var" / "www" / "log-viewer-webui" / "server" / "dist" / "settings.json"
Copy link
Member Author

Choose a reason for hiding this comment

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

settings.json is now part of the build assets so the path is changed.


import app from "./app.js";


test("Tests the example routes", async (t) => {
const server = await app({});
await tap.test("Tests the example routes", async (t) => {
Copy link
Member Author

Choose a reason for hiding this comment

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

This change is needed because ESLint does not know test() is defined in the global scope in the context of tap runs.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added Typebox as a Fastify type provider. Also specified the schemas for the routes.

Copy link
Member Author

Choose a reason for hiding this comment

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

  • Added Typebox as a Fastify type provider.
  • Also specified the schemas for the routes.
  • Also removed POST body value checks in favour of TypeBox's schema check.

const {extractJobType, logEventIdx, streamId} = req.body;
if (false === EXTRACT_JOB_TYPES.includes(extractJobType)) {
resp.code(StatusCodes.BAD_REQUEST);
throw new Error(`Invalid extractJobType="${extractJobType}".`);
}

if ("string" !== typeof streamId || 0 === streamId.trim().length) {
Copy link
Member Author

Choose a reason for hiding this comment

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

The empty string check is now done by Type.String({minLength: 1}) though not trimmed. If needed, we can define a Regex check instead.

throw new Error("\"streamId\" must be a non-empty string.");
}

const sanitizedLogEventIdx = Number(logEventIdx);
Copy link
Member Author

Choose a reason for hiding this comment

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

This sanitization seems not needed in the first place (even before we introduce TS)

components/log-viewer-webui/server/tsconfig.json Outdated Show resolved Hide resolved
Copy link
Contributor

@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)
components/log-viewer-webui/server/tsconfig.json (2)

15-30: Consider reorganizing strict type-checking options.

The strict type-checking configuration is comprehensive, but there are some suggestions for improvement:

  1. The "strict": true setting at line 30 is redundant since all individual strict flags are already enabled.
  2. Consider grouping related options together for better readability.

Apply this diff to reorganize the strict options:

-    "alwaysStrict": true,
-    "exactOptionalPropertyTypes": true,
-    "noFallthroughCasesInSwitch": true,
-    "noImplicitAny": true,
-    "noImplicitOverride": true,
-    "noImplicitReturns": true,
-    "noImplicitThis": true,
-    "noUncheckedIndexedAccess": true,
-    "noUnusedLocals": true,
-    "noUnusedParameters": true,
-    "strictBindCallApply": true,
-    "strictFunctionTypes": true,
-    "strictNullChecks": true,
-    "strictPropertyInitialization": true,
-    "useUnknownInCatchVariables": true,
-    "strict": true
+    // Enable all strict type-checking options
+    "strict": true,
+
+    // Additional strict checks
+    "noUncheckedIndexedAccess": true,
+    "exactOptionalPropertyTypes": true,
+    "useUnknownInCatchVariables": true,
+
+    // Code quality checks
+    "noUnusedLocals": true,
+    "noUnusedParameters": true,
+    "noFallthroughCasesInSwitch": true,
+    "noImplicitReturns": true,
+    "noImplicitOverride": true

32-34: Consider adding explicit exclusions for test files.

The current configuration includes all files under src/**/* but doesn't explicitly exclude test files. This might lead to TypeScript attempting to compile test files during the production build.

Add an exclude section to prevent compiling test files:

   "include": [
     "src/**/*"
-  ]
+  ],
+  "exclude": [
+    "src/**/*.test.ts",
+    "src/**/*.spec.ts",
+    "src/**/__tests__/*"
+  ]
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 89dfb45 and 1f50cb1.

📒 Files selected for processing (1)
  • components/log-viewer-webui/server/tsconfig.json (1 hunks)
🔇 Additional comments (1)
components/log-viewer-webui/server/tsconfig.json (1)

2-13: LGTM! Compiler options are well configured for a Node.js server.

The basic compiler options are appropriately configured with modern ECMAScript targets and Node.js-specific module resolution settings.

@junhaoliao junhaoliao requested a review from haiqi96 December 31, 2024 19:20
@junhaoliao
Copy link
Member Author

@haiqi96 Could you help check if the CLP package changes make sense to you? Thanks

Copy link
Contributor

@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

🧹 Nitpick comments (6)
components/log-viewer-webui/server/src/typings/DbManager.ts (2)

7-11: Use explicit numerical values for the enum to avoid reordering issues
If the order of the constants changes, it can produce unexpected values. Defining explicit numeric values (e.g., SEARCH_OR_AGGREGATION = 0, EXTRACT_IR = 1, EXTRACT_JSON = 2) can help maintain stable references.


16-19: Efficient data structure
Object.freeze([...]) is fine for preventing modifications, but if you repeatedly check membership, consider using a Set for faster lookups.

components/log-viewer-webui/server/src/routes/query.ts (1)

8-9: Separate import statements for improved clarity
Consider moving QUERY_JOB_TYPE and EXTRACT_JOB_TYPES imports to the same line or grouping them in a single statement for clarity.

components/log-viewer-webui/server/src/DbManager.ts (3)

114-120: Reusability of connection strings
Creating the connection strings directly here is straightforward, but consider centralizing configuration logic if multiple plugins share them.


156-181: Separate config logic
You handle different job configurations based on the jobType. For increased maintainability, consider refactoring these conditionals into separate helper functions or a strategy pattern.


228-264: Infinite loop risk
The while(true) loop, combined with occasional status polling, can hang if the job never finishes. Although handled by repeated checks, you might want configurable timeouts to avoid indefinite loops.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 1f50cb1 and 13192bd.

📒 Files selected for processing (4)
  • components/log-viewer-webui/server/src/DbManager.ts (1 hunks)
  • components/log-viewer-webui/server/src/main.ts (1 hunks)
  • components/log-viewer-webui/server/src/routes/query.ts (1 hunks)
  • components/log-viewer-webui/server/src/typings/DbManager.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (4)
components/log-viewer-webui/server/src/main.ts (1)

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

components/log-viewer-webui/server/src/routes/query.ts (1)

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

components/log-viewer-webui/server/src/typings/DbManager.ts (1)

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

components/log-viewer-webui/server/src/DbManager.ts (1)

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

🔇 Additional comments (16)
components/log-viewer-webui/server/src/typings/DbManager.ts (4)

27-34: Enum organization
These statuses match the code well and provide clarity. This looks good.


39-43: Ease of further usage
When adding or removing statuses from QUERY_JOB_STATUS_WAITING_STATES, ensure existing logic in other files properly handles each possible status.


50-55: Descriptive naming
The QUERY_JOBS_TABLE_COLUMN_NAMES enum is straightforward, ensuring consistent usage of column names throughout the code.


58-63: Interface aligns with table schema
The property-index signatures match the intended column usage, promoting robust type checking against your table columns.

components/log-viewer-webui/server/src/routes/query.ts (5)

1-1: Import modules
The imports reflect the shift to TypeScript and schema validation with TypeBox. This enhances type safety in route definitions.

Also applies to: 4-6


17-19: Correct usage of typed Fastify instance
Using app.withTypeProvider<TypeBoxTypeProvider>() is a good approach for stricter type checking of schemas.


20-27: Comprehensive schema definitions
Defining request body schemas with TypeBox ensures minimal boilerplate while maintaining strong runtime validation.


28-28: Consistent usage of "false ==="
This check complies with the coding guidelines, avoiding the !expression pattern. Good job.


37-37: Robust error handling
Errors are thrown with meaningful messages and correct status codes, helping users debug invalid scenarios easily.

Also applies to: 43-43, 51-51, 56-56, 62-62

components/log-viewer-webui/server/src/main.ts (7)

1-5: DOTENV usage
Loading environment variables with dotenv is a standard practice, ensuring local overrides via .env files.


11-14: Use of a default Node environment
Defining a default environment (e.g., development) is a solid fallback strategy.


18-27: Logger configuration map
Using environment-based logger templates is clean and easily adjustable per environment.


35-41: Type guard usage
isKnownNodeEnv is well-structured, preventing invalid environment strings from slipping through.


43-51: Explicit interface for environment variables
Ensuring each required env var has a type reduces the risk of runtime errors due to missing configurations.


59-99: Graceful handling of missing environment variables
You log a helpful message if NODE_ENV is invalid and throw an error for other mandatory vars, striking a good balance of clarity and strictness.


104-125: Server initialization strategy
Wrapping the server startup in main() and calling it at the end makes your code more testable and maintainable.

/**
* Interval in milliseconds for polling the completion status of a job.
*/
const JOB_COMPLETION_STATUS_POLL_INTERVAL_MILLIS = 0.5;
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Clarify polling interval
The value 0.5 might be interpreted as half a millisecond (i.e., 0.5 ms), leading to extremely high polling frequency. If you intended half a second, consider using 500 (milliseconds).

Copy link
Contributor

@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)
components/log-viewer-webui/server/src/typings/DbManager.ts (1)

50-55: Consider specifying numeric values for the enum.

Setting explicit numeric values for the QUERY_JOBS_TABLE_COLUMN_NAMES enum might help prevent unintentional changes in values if the enum's order is ever altered. Although not required, it can be a small safeguard.

components/log-viewer-webui/server/src/DbManager.ts (1)

230-264: Consider adding a maximum wait time or retry limit.

The while (true) loop may run forever if a job never completes. Adding a timeout or a maximum number of retries could be beneficial to avoid indefinite polling.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 13192bd and 58a434c.

📒 Files selected for processing (4)
  • components/log-viewer-webui/server/src/DbManager.ts (1 hunks)
  • components/log-viewer-webui/server/src/main.ts (1 hunks)
  • components/log-viewer-webui/server/src/routes/query.ts (1 hunks)
  • components/log-viewer-webui/server/src/typings/DbManager.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (4)
components/log-viewer-webui/server/src/routes/query.ts (1)

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

components/log-viewer-webui/server/src/main.ts (1)

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

components/log-viewer-webui/server/src/typings/DbManager.ts (1)

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

components/log-viewer-webui/server/src/DbManager.ts (1)

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

📓 Learnings (1)
components/log-viewer-webui/server/src/main.ts (1)
Learnt from: junhaoliao
PR: y-scope/clp#647
File: components/log-viewer-webui/server/src/main.ts:8-8
Timestamp: 2024-12-31T19:19:55.032Z
Learning: When "moduleResolution": "node16" is used in a TypeScript project's tsconfig.json, the import file extension is typically expected to be ".js" (or ".cjs"/".mjs"), even if the source code is originally in TypeScript (.ts). This is a part of node16’s resolution behavior.
🔇 Additional comments (4)
components/log-viewer-webui/server/src/typings/DbManager.ts (1)

7-19: Enums and Sets are well-structured, good job!

Defining separate enums for job types and statuses, along with dedicated 'Set' constants, enhances code clarity and maintainability. Keep in mind to document any future additions for new job types or statuses in the same style.

components/log-viewer-webui/server/src/routes/query.ts (1)

30-30: Consistent boolean checks.

Using if (false === EXTRACT_JOB_TYPES.has(extractJobType)) aligns with your coding guideline of preferring false === expression. Great work maintaining consistency!

components/log-viewer-webui/server/src/main.ts (1)

89-94: Confirm environment configuration.

Ensuring the default environment is used if NODE_ENV is undefined or unrecognised is a neat approach. Please verify that the NODE_ENV is set correctly in your deployment environment, so you don't inadvertently run in "development" mode in production.

components/log-viewer-webui/server/src/DbManager.ts (1)

59-59: Clarify polling interval.

The value 0.5 implies a half-millisecond polling interval, which is extremely frequent and might degrade performance. If you intended half a second, use 500.

- |-
cd "server"
rsync -a \
package.json package-lock.json \
Copy link
Contributor

Choose a reason for hiding this comment

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

stupid question, don't we need to sync "src" as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

not at all. How TypeScript compilation works is that the compiler performs type-checking then transpiles the TypeScript code into JavaScript code.

After we run npm run build to perform the compilation, the JS code would have been the dist folder and the sources stored in src can be thrown away.

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.

2 participants