Skip to content

Conversation

@davemarco
Copy link
Contributor

@davemarco davemarco commented Jun 23, 2025

Description

PR moves sql query route into new fastify architecture. This is a chain of PRs which started with #1027

In addition, I just removed the querySql function out of dbManager. It can just be declared in route which in simpler.

I also renamed the route, and did some minor refactor.

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

Metadata tables load correctly

Summary by CodeRabbit

  • New Features

    • Introduced a new API endpoint for archive metadata SQL queries under /api/archive-metadata/sql.
  • Bug Fixes

    • Updated the client to use the new API endpoint for SQL queries.
  • Chores

    • Improved schema validation for SQL query requests.
    • Updated internal ESLint configuration for code quality.
    • Removed outdated SQL query route and related backend method.

@davemarco davemarco requested a review from junhaoliao June 23, 2025 20:37
@davemarco davemarco requested a review from a team as a code owner June 23, 2025 20:37
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 23, 2025

Walkthrough

A new Fastify API route for archive metadata SQL queries was introduced, replacing the previous /query/sql endpoint. The supporting schema and route handler were moved and refactored, and the database query method was removed from the DbManager class. The client API call was updated to use the new endpoint URL.

Changes

File(s) Change Summary
components/webui/server/eslint.config.mjs Updated ESLint "new-cap" rule exceptions to include "Type.Any".
components/webui/server/src/fastify-v2/schemas/archive-metadata.ts Added new SQL query schema definition and export.
components/webui/server/src/plugins/DbManager.ts Removed the queryMySql method from the DbManager class.
components/webui/server/src/routes/query.ts Removed the POST route handler for /query/sql.
components/webui/server/src/fastify-v2/routes/api/archive-metadata/index.ts Added new Fastify plugin providing a POST /sql endpoint for archive metadata SQL queries.
components/webui/client/src/api/sql/index.ts Updated API endpoint URL in querySql from "/query/sql" to "/api/archive-metadata/sql".

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant FastifyServer
    participant MySQLPool

    Client->>FastifyServer: POST /api/archive-metadata/sql { queryString }
    FastifyServer->>MySQLPool: Execute queryString
    MySQLPool-->>FastifyServer: Query result
    FastifyServer-->>Client: 200 OK + result
Loading

Suggested reviewers

  • junhaoliao

📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 65197de and 88fdee7.

📒 Files selected for processing (3)
  • components/webui/client/src/api/sql/index.ts (1 hunks)
  • components/webui/server/src/plugins/DbManager.ts (0 hunks)
  • components/webui/server/src/routes/query.ts (0 hunks)
💤 Files with no reviewable changes (2)
  • components/webui/server/src/routes/query.ts
  • components/webui/server/src/plugins/DbManager.ts
🧰 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>.

⚙️ Source: CodeRabbit Configuration File

List of files the instruction was applied to:

  • components/webui/client/src/api/sql/index.ts
🧠 Learnings (2)
📓 Common learnings
Learnt from: junhaoliao
PR: y-scope/clp#939
File: components/package-template/src/etc/clp-config.yml:64-64
Timestamp: 2025-06-22T04:01:43.409Z
Learning: The new webui architecture uses Fastify with Pino logging instead of the previous Winston-based logging system that was removed during the webui refactoring.
components/webui/client/src/api/sql/index.ts (1)
Learnt from: junhaoliao
PR: y-scope/clp#596
File: components/log-viewer-webui/client/src/api/query.js:16-23
Timestamp: 2024-11-21T15:51:33.203Z
Learning: In `components/log-viewer-webui/client/src/api/query.js`, the `ExtractJsonResp` type definition is accurate as-is and does not require modification. When suggesting changes to type definitions, ensure they align with the server-side definitions, referencing the source code if necessary.
🔇 Additional comments (1)
components/webui/client/src/api/sql/index.ts (1)

11-11: LGTM! Endpoint URL correctly updated for Fastify migration.

The endpoint URL change from /query/sql to /api/archive-metadata/sql properly aligns with the migration to the new Fastify architecture mentioned in the PR objectives.

✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in 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
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: 4

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d8ac122 and 5e88f4d.

📒 Files selected for processing (5)
  • components/log-viewer-webui/client/src/pages/IngestPage/sqlConfig.ts (1 hunks)
  • components/log-viewer-webui/server/src/fastify-v2/routes/api/archive-metadata/index.ts (1 hunks)
  • components/log-viewer-webui/server/src/fastify-v2/schemas/archive-metadata.ts (1 hunks)
  • components/log-viewer-webui/server/src/plugins/DbManager.ts (0 hunks)
  • components/log-viewer-webui/server/src/routes/query.ts (0 hunks)
💤 Files with no reviewable changes (2)
  • components/log-viewer-webui/server/src/plugins/DbManager.ts
  • components/log-viewer-webui/server/src/routes/query.ts
🧰 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>.

  • components/log-viewer-webui/client/src/pages/IngestPage/sqlConfig.ts
  • components/log-viewer-webui/server/src/fastify-v2/routes/api/archive-metadata/index.ts
  • components/log-viewer-webui/server/src/fastify-v2/schemas/archive-metadata.ts
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: lint-check (ubuntu-latest)
🔇 Additional comments (1)
components/log-viewer-webui/client/src/pages/IngestPage/sqlConfig.ts (1)

55-55: LGTM! Endpoint URL updated correctly for the new Fastify architecture.

The URL change from /query/sql to /api/archive-metadata/sql properly aligns with the new Fastify v2 API structure while maintaining the same function signature and request payload.

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

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 1fde043 and 5378604.

📒 Files selected for processing (5)
  • components/webui/client/src/pages/IngestPage/sqlConfig.ts (1 hunks)
  • components/webui/server/eslint.config.mjs (1 hunks)
  • components/webui/server/src/fastify-v2/schemas/archive-metadata.ts (1 hunks)
  • components/webui/server/src/plugins/DbManager.ts (0 hunks)
  • components/webui/server/src/routes/query.ts (0 hunks)
💤 Files with no reviewable changes (2)
  • components/webui/server/src/routes/query.ts
  • components/webui/server/src/plugins/DbManager.ts
🧰 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>.

⚙️ Source: CodeRabbit Configuration File

List of files the instruction was applied to:

  • components/webui/client/src/pages/IngestPage/sqlConfig.ts
  • components/webui/server/src/fastify-v2/schemas/archive-metadata.ts
🧠 Learnings (4)
📓 Common learnings
Learnt from: junhaoliao
PR: y-scope/clp#939
File: components/package-template/src/etc/clp-config.yml:64-64
Timestamp: 2025-06-22T04:01:43.409Z
Learning: The new webui architecture uses Fastify with Pino logging instead of the previous Winston-based logging system that was removed during the webui refactoring.
components/webui/server/eslint.config.mjs (1)
Learnt from: junhaoliao
PR: y-scope/clp#596
File: components/log-viewer-webui/client/src/api/query.js:16-23
Timestamp: 2024-11-21T15:51:33.203Z
Learning: In `components/log-viewer-webui/client/src/api/query.js`, the `ExtractJsonResp` type definition is accurate as-is and does not require modification. When suggesting changes to type definitions, ensure they align with the server-side definitions, referencing the source code if necessary.
components/webui/client/src/pages/IngestPage/sqlConfig.ts (1)
Learnt from: junhaoliao
PR: y-scope/clp#596
File: components/log-viewer-webui/client/src/api/query.js:16-23
Timestamp: 2024-11-21T15:51:33.203Z
Learning: In `components/log-viewer-webui/client/src/api/query.js`, the `ExtractJsonResp` type definition is accurate as-is and does not require modification. When suggesting changes to type definitions, ensure they align with the server-side definitions, referencing the source code if necessary.
components/webui/server/src/fastify-v2/schemas/archive-metadata.ts (1)
Learnt from: junhaoliao
PR: y-scope/clp#596
File: components/log-viewer-webui/client/src/api/query.js:16-23
Timestamp: 2024-11-21T15:51:33.203Z
Learning: In `components/log-viewer-webui/client/src/api/query.js`, the `ExtractJsonResp` type definition is accurate as-is and does not require modification. When suggesting changes to type definitions, ensure they align with the server-side definitions, referencing the source code if necessary.
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: lint-check (ubuntu-latest)
  • GitHub Check: lint-check (macos-latest)
🔇 Additional comments (2)
components/webui/client/src/pages/IngestPage/sqlConfig.ts (1)

49-49: Cannot locate new API endpoint on server

I wasn’t able to find an implementation of the /api/archive-metadata/sql route in the server code. Please verify that this endpoint is defined and registered in your Fastify routes, or adjust the client call to match the actual server path.

• Check under components/webui/server/ for the archive-metadata SQL route (e.g. in your routes or plugins files)
• Ensure the endpoint is registered with Fastify (e.g. fastify.post('/api/archive-metadata/sql', …))

components/webui/server/src/fastify-v2/schemas/archive-metadata.ts (1)

1-14: Import path verification successful

Verified that components/webui/server/src/fastify-v2/schemas/common.ts exports StringSchema and that importing from ./common.js matches the pattern used in search.ts. No further changes required.

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: 3

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 5378604 and 644cc26.

📒 Files selected for processing (1)
  • components/webui/server/src/fastify-v2/routes/api/archive-metadata/index.ts (1 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>.

⚙️ Source: CodeRabbit Configuration File

List of files the instruction was applied to:

  • components/webui/server/src/fastify-v2/routes/api/archive-metadata/index.ts
🧠 Learnings (2)
📓 Common learnings
Learnt from: junhaoliao
PR: y-scope/clp#939
File: components/package-template/src/etc/clp-config.yml:64-64
Timestamp: 2025-06-22T04:01:43.409Z
Learning: The new webui architecture uses Fastify with Pino logging instead of the previous Winston-based logging system that was removed during the webui refactoring.
components/webui/server/src/fastify-v2/routes/api/archive-metadata/index.ts (1)
Learnt from: davemarco
PR: y-scope/clp#1015
File: components/log-viewer-webui/server/src/routes/static.ts:65-70
Timestamp: 2025-06-16T13:05:27.349Z
Learning: In components/log-viewer-webui/server/src/routes/static.ts, when decorateReply is set to true in fastifyStatic configuration, the reply.sendFile() method automatically uses the root directory configured in the static plugin registration, eliminating the need to pass the root directory as a second parameter.
🔇 Additional comments (1)
components/webui/server/src/fastify-v2/routes/api/archive-metadata/index.ts (1)

39-39: LGTM!

The export follows standard Fastify plugin conventions.

schema: {
body: SqlSchema,
response: {
[StatusCodes.OK]: Type.Any(),
Copy link
Contributor

@coderabbitai coderabbitai bot Jul 2, 2025

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

Consider improving response type safety.

Using Type.Any() for the response schema eliminates type safety benefits. Consider defining a more specific schema based on expected query results.

-[StatusCodes.OK]: Type.Any(),
+[StatusCodes.OK]: Type.Object({
+    // Define expected response structure based on your use case
+    rows: Type.Array(Type.Any()),
+    fields: Type.Optional(Type.Array(Type.Any())),
+}),
🤖 Prompt for AI Agents
In components/webui/server/src/fastify-v2/routes/api/archive-metadata/index.ts
at line 24, the response schema uses Type.Any(), which removes type safety.
Replace Type.Any() with a more specific TypeBox schema that accurately
represents the expected structure of the query results to improve type safety
and validation.

Copy link
Member

Choose a reason for hiding this comment

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

quick question: why do we want to specify Any() here? would it be beneficial to list out what fields are available in the response (as some documentation of the response schema)?

Copy link
Contributor

Choose a reason for hiding this comment

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

@junhaoliao You raise an excellent point about the trade-offs between flexibility and type safety/documentation.

The current Type.Any() approach has both pros and cons:

Why Type.Any() might be used here:

  • This endpoint accepts arbitrary SQL queries, so the response structure is inherently dynamic
  • Different queries return vastly different structures (SELECT vs INSERT/UPDATE/DELETE results)
  • Maximum flexibility for any SQL query type

Benefits of defining specific response fields:

  • Better API documentation and developer experience
  • Some level of type safety and validation
  • Clearer expectations for API consumers

Suggested middle-ground approach:

[StatusCodes.OK]: Type.Object({
    data: Type.Any(), // The actual query results
    queryType: Type.Optional(Type.String()), // e.g., "SELECT", "INSERT", etc.
    rowCount: Type.Optional(Type.Number()),
    fields: Type.Optional(Type.Array(Type.Object({
        name: Type.String(),
        type: Type.String(),
    }))),
}),

This provides:

  1. Structure - Consistent response wrapper
  2. Documentation - Clear API contract
  3. Flexibility - data field can still accommodate any query result
  4. Metadata - Additional info about the query execution

The challenge is that MySQL query results have different shapes (result sets vs execution metadata), so some level of Any() typing may be unavoidable while still providing useful structure and documentation.

What's your preference for this API design?


🧠 Learnings used
Learnt from: junhaoliao
PR: y-scope/clp#596
File: components/log-viewer-webui/client/src/api/query.js:16-23
Timestamp: 2024-11-21T15:51:33.203Z
Learning: In `components/log-viewer-webui/client/src/api/query.js`, the `ExtractJsonResp` type definition is accurate as-is and does not require modification. When suggesting changes to type definitions, ensure they align with the server-side definitions, referencing the source code if necessary.

Learnt from: junhaoliao
PR: y-scope/clp#596
File: components/log-viewer-webui/client/src/api/query.js:35-41
Timestamp: 2024-11-19T19:52:43.429Z
Learning: For internal APIs in `components/log-viewer-webui/client/src/api/query.js`, runtime validation of parameters may not be necessary since the APIs are not exposed to end users, and JsDoc type annotations may be sufficient.

Learnt from: junhaoliao
PR: y-scope/clp#939
File: components/package-template/src/etc/clp-config.yml:64-64
Timestamp: 2025-06-22T04:01:43.409Z
Learning: The new webui architecture uses Fastify with Pino logging instead of the previous Winston-based logging system that was removed during the webui refactoring.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Similiarly to what the AI responded, the response will be different for types of queries see resultheader or a custom row. Typing of response is not exactly clear with arbitrary query

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Copy link
Member

Choose a reason for hiding this comment

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

Right, looking at https://sidorares.github.io/node-mysql2/docs/examples/queries/simple-queries/select#queryoptions , i was thinking that the return type can be written as Type.Array(Type.Any()) if I understand correctly.

Anyways as you suggested, it probably won't make a difference given we can't specify the element's types. This is fine to leave as-is until we come up with better API endpoints testing / documentation infra like Swagger

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes i think since its generic there is not much we can do for now, but the client can specify typing

hoophalab
hoophalab previously approved these changes Jul 7, 2025
Copy link
Contributor

@hoophalab hoophalab left a comment

Choose a reason for hiding this comment

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

This PR looks good to me for now, but let's merge this after #1050 and $1042 to avoid issues. I'll take a look again after those two are merged.

@hoophalab hoophalab dismissed their stale review July 7, 2025 04:47

withdraw

@davemarco
Copy link
Contributor Author

@hoophalab - i merged main into this, please rereview

@davemarco davemarco requested a review from hoophalab July 10, 2025 15:03
@davemarco davemarco changed the title refactor(new-webui): Migrate archive metadata SQL query route to new Fastify architecture. refactor(webui): Migrate archive metadata SQL query route to new Fastify architecture. Jul 10, 2025
@davemarco
Copy link
Contributor Author

@hoophalab - i merged main again

Copy link
Contributor

@hoophalab hoophalab left a comment

Choose a reason for hiding this comment

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

lgtm. We need write permission from @junhaoliao. Also, any suggestions on the pr title?

Validations:

  1. run clp-text and clp-json
  2. compress several files
  3. all cards displayed correctly

async (req, reply) => {
const {queryString} = req.body;
const [result] = await mysqlConnectionPool.query(queryString);
reply.code(StatusCodes.OK);
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel we don't actually need explicit statuscode, but feel free to keep it here.

Copy link
Contributor Author

@davemarco davemarco Jul 10, 2025

Choose a reason for hiding this comment

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

in the search routes i am explicity using fancier positive status codes(not always OK) like NO CONTENT, and CREATE. I like the explicit status code since it tells dev that something must be returned to client. I would prefer to leave it here

Copy link
Contributor

Choose a reason for hiding this comment

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

sounds fairly reasonable. Let's keep it here.

@davemarco
Copy link
Contributor Author

@junhaoliao tagging for write access

Copy link
Member

@junhaoliao junhaoliao left a comment

Choose a reason for hiding this comment

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

the PR title lgtm; deferring to @hoophalab 's review

this is a separate request - we might want to update the target path at

target: "http://localhost:3000/",
to be more specific once we're done moving all API routes to /api

schema: {
body: SqlSchema,
response: {
[StatusCodes.OK]: Type.Any(),
Copy link
Member

Choose a reason for hiding this comment

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

Right, looking at https://sidorares.github.io/node-mysql2/docs/examples/queries/simple-queries/select#queryoptions , i was thinking that the return type can be written as Type.Array(Type.Any()) if I understand correctly.

Anyways as you suggested, it probably won't make a difference given we can't specify the element's types. This is fine to leave as-is until we come up with better API endpoints testing / documentation infra like Swagger

@davemarco
Copy link
Contributor Author

davemarco commented Jul 10, 2025

this is a separate request - we might want to update the target path at

makes sense to update this when finished this refactor

@davemarco davemarco merged commit fada2e1 into y-scope:main Jul 10, 2025
8 checks passed
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.

3 participants