Skip to content

Add typings result #458

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

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open

Add typings result #458

wants to merge 4 commits into from

Conversation

cristianpb
Copy link
Member

@cristianpb cristianpb commented Mar 26, 2025

  • Add typings
  • Add ensure conditions for artillery to spot http error codes when overloading the service

Summary by CodeRabbit

  • New Features

    • Enhanced error responses for standard and bulk requests, now providing clear timing, status, and diagnostic details.
    • Introduced new interfaces for better data structure definitions related to request and response handling.
    • Added new plugin configurations for improved error handling and response validation in performance testing scenarios.
  • Refactor

    • Streamlined internal data formatting to ensure consistent output without altering end-user functionality.
    • Improved type definitions for request and response functions, enhancing clarity and maintainability.
    • Updated naming conventions in aggregation requests for better clarity.

Copy link

coderabbitai bot commented Mar 26, 2025

Walkthrough

The changes update internal backend logic for type conversion and response structure. In the aggregation controller, the conversion of the delay variable now uses the String() function instead of a type assertion. New interfaces and enhanced type definitions are introduced to support both single and bulk request responses, with a revised aggregation property format and additional optional fields. The run request functions also update their return types and streamline error response formats for improved consistency in error handling.

Changes

File(s) Summary of Changes
backend/src/controllers/aggregation.controller.ts Updated the method signature of streamAggs to specify types for response and requestInput, changed the handling of the delay variable to use String(delay), added error handling for result.error, and replaced all myBuckets references with bucketResults.
backend/src/models/result.ts Updated ResultRawES interface with an index signature, replaced buckets with nested bucketResults containing buckets and after_key, and added optional properties status, statusText, and error.
backend/src/runRequest.ts Modified the return types of runBulkRequest; updated error response structure to include top-level took, empty hits, and moved error details to top-level properties.
backend/src/buildRequest.ts Renamed aggregation property from myBuckets to bucketResults in buildAggregation function to align with updated aggregation structure.
backend/Makefile Added new environment variable PERF_UTILS for performance utility scripts; removed CSV preprocessing command in the test-perf-v1 target.
backend/tests/performance/scenarios/test-backend-v1.yml Added expect and ensure plugin configurations for error reporting and thresholds; swapped firstName and lastName field order in payload; enabled CSV delimiter; added expect and capture directives in scenario flows for GET and POST requests.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant API as runRequest/runBulkRequest
    participant ES as Data Source

    Client->>API: Submit request (query/scroll or bulk body)
    API->>ES: Process request/query
    alt Success
        ES-->>API: Return valid response data
        API-->>Client: Respond with structured data (RequestResult/BulkRequestResult)
    else Error
        ES-->>API: Return error details
        API-->>Client: Respond with structured error { took, hits:[], status, statusText, error }
    end
Loading

Poem

Hop, hop—I'm CodeRabbit on the run,
Converting delays with a touch of fun.
New interfaces and buckets now in bloom,
Error handling's fixed to clear the gloom.
In backend bytes, my code hops free—
A joyful dance in our tech harmony! 🐇✨


📜 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 93a9bb7 and c8235c7.

📒 Files selected for processing (6)
  • Makefile (1 hunks)
  • backend/src/buildRequest.ts (1 hunks)
  • backend/src/controllers/aggregation.controller.ts (6 hunks)
  • backend/src/models/result.ts (1 hunks)
  • backend/src/runRequest.ts (2 hunks)
  • backend/tests/performance/scenarios/test-backend-v1.yml (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (6)
  • Makefile
  • backend/src/buildRequest.ts
  • backend/src/models/result.ts
  • backend/src/controllers/aggregation.controller.ts
  • backend/tests/performance/scenarios/test-backend-v1.yml
  • backend/src/runRequest.ts
✨ 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

@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)
backend/src/runRequest.ts (1)

96-96: Consider typing the body parameter

While updating the return type to Promise<BulkRequestResult> is good, the body parameter is still typed as any. Consider creating a specific interface for this parameter as well.

-export const runBulkRequest = async (body: any): Promise<BulkRequestResult> => {
+export const runBulkRequest = async (body: BulkRequestBody): Promise<BulkRequestResult> => {

Where BulkRequestBody could be defined in your models directory.

backend/src/models/result.ts (1)

161-162: Consider more specific typing if possible

While an index signature ([key: string]: any) provides flexibility for additional properties, it reduces type safety. If you know the specific properties that might be present, consider defining them explicitly.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 3e86c11 and c6986be.

📒 Files selected for processing (3)
  • backend/src/controllers/aggregation.controller.ts (1 hunks)
  • backend/src/models/result.ts (3 hunks)
  • backend/src/runRequest.ts (4 hunks)
🔇 Additional comments (11)
backend/src/controllers/aggregation.controller.ts (1)

216-216: Type safety improvement in delay conversion

The change from using a type assertion (delay as string) to using the String() function is a good improvement. The String() function ensures proper runtime conversion of the value to a string, while type assertions only affect TypeScript's type checking without changing the runtime behavior.

backend/src/runRequest.ts (5)

4-4: Good addition of typed interfaces

Using specific interfaces RequestResult and BulkRequestResult instead of relying on any types improves type safety and code readability.


15-15: Improved return type specificity

Updating the return type from Promise<any> to Promise<RequestResult> enhances type safety and makes the function's contract clearer.


57-70: Better error response structure

The updated error response structure provides a more consistent format that matches the success response, which makes error handling easier for consumers of the API.


77-89: Consistent error handling pattern

This error response follows the same pattern, reinforcing consistency throughout the codebase.


108-120: Consistent error response in bulk requests

The error response for bulk requests now follows the same structure as single requests, which is good for consistency.

backend/src/models/result.ts (5)

140-148: Good addition of specific response interfaces

The new RequestResult and BulkRequestResult interfaces properly define the structure of responses from runRequest and runBulkRequest functions, improving type safety and documentation.


164-168: Improved aggregation structure typing

The more specific structure for the myBuckets property improves type safety and makes the code more self-documenting.


169-171: Good addition of error properties

These optional properties align well with the updated error response structure in runRequest.ts, ensuring consistency between interfaces and implementation.


174-177: Well-defined BucketItem interface

Creating a specific interface for bucket items rather than using a generic type improves code clarity and type safety.


209-209: Fixed interface closure

This appears to be fixing the proper closure of the ResultRawHit interface, ensuring syntactic correctness.

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 (4)
backend/src/models/result.ts (4)

140-148: Add JSDoc comments to the new interfaces

The new RequestResult and BulkRequestResult interfaces will be useful for type safety, but they lack documentation. Consider adding JSDoc comments to explain their purpose and provide examples, similar to what's done for other interfaces in this file.

+/**
+ * Interface for single request response
+ * @tsoaModel
+ * @example
+ * {
+ *   "data": {
+ *     "took": 10,
+ *     "hits": { ... }
+ *   }
+ * }
+ */
 export interface RequestResult {
   data: ResultRawES;
 }

+/**
+ * Interface for bulk request response containing multiple result sets
+ * @tsoaModel
+ * @example
+ * {
+ *   "data": {
+ *     "responses": [
+ *       {
+ *         "took": 10,
+ *         "hits": { ... }
+ *       }
+ *     ]
+ *   }
+ * }
+ */
 export interface BulkRequestResult {
   data: {
     responses: ResultRawES[];
   }
 }

161-161: Consider using more specific typing for index signature

While [key: string]: any provides flexibility, it can hide potential type errors. If possible, it would be better to use a more specific type than any or to enumerate the known properties instead.


164-167: Reconsider the naming of "myBuckets"

The property name "myBuckets" isn't a conventional naming pattern and may be confusing. Consider using a more descriptive and conventional name like "bucketResults" or simply "results".

-    myBuckets: {
+    bucketResults: {
       buckets: BucketItem[];
       after_key: number;
     }

169-171: Consider extending error handling

Adding status, statusText, and error properties to ResultRawES is a good start for error handling. However, you might want to consider creating a separate interface for error responses to maintain separation of concerns.

 export interface ResultRawES {
   '_scroll_id'?: string;
   took: number;
   hits: {
     total: {
       value: number;
     }
     'max_score': number;
     hits:  ResultRawHit[]
   };
   aggregations?: {
     [key: string]: any;
     'doc_count_error_upper_bound': number;
     'sum_other_doc_count': number;
     myBuckets: {
       buckets: BucketItem[];
       after_key: number;
     }
   }
   status?: number;
   statusText?: string;
   error?: boolean;
 }

+/**
+ * Interface for error responses from Elasticsearch
+ */
+export interface ESErrorResponse {
+  status: number;
+  statusText: string;
+  error: boolean;
+  message?: string;
+}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c6986be and b4239ef.

📒 Files selected for processing (3)
  • backend/src/controllers/aggregation.controller.ts (4 hunks)
  • backend/src/models/result.ts (3 hunks)
  • backend/src/runRequest.ts (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • backend/src/runRequest.ts
  • backend/src/controllers/aggregation.controller.ts
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: 🐳 Build docker image
🔇 Additional comments (1)
backend/src/models/result.ts (1)

174-177: Good addition of the BucketItem interface

The BucketItem interface provides good type safety for the aggregation results. Consider adding a JSDoc comment to explain its purpose and possibly expanding it with additional fields if needed.

+/**
+ * Interface for aggregation bucket items returned by Elasticsearch
+ */
 interface BucketItem {
   key: string;
   doc_count: number;
 }

@cristianpb cristianpb force-pushed the feat/typings-result branch from ab41908 to 63bec12 Compare May 27, 2025 19:24
@rhanka
Copy link
Member

rhanka commented May 27, 2025

🎉 Snyk checks have passed. No issues have been found so far.

security/snyk check is complete. No issues have been found. (View Details)

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

🧹 Nitpick comments (1)
backend/tests/performance/scenarios/test-backend-v1.yml (1)

57-63: Align POST flow ordering for readability

To mirror the GET flow and improve consistency, consider moving the expect block above capture in the POST step:

- capture:
-   - json: '$'
-     as: resultPost
- expect:
-   - statusCode: 200
-   - contentType: json
+ expect:
+   - statusCode: 200
+   - contentType: json
+ capture:
+   - json: '$'
+     as: resultPost
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ab41908 and 63bec12.

📒 Files selected for processing (6)
  • Makefile (1 hunks)
  • backend/src/buildRequest.ts (1 hunks)
  • backend/src/controllers/aggregation.controller.ts (6 hunks)
  • backend/src/models/result.ts (3 hunks)
  • backend/src/runRequest.ts (4 hunks)
  • backend/tests/performance/scenarios/test-backend-v1.yml (2 hunks)
✅ Files skipped from review due to trivial changes (1)
  • backend/src/buildRequest.ts
🚧 Files skipped from review as they are similar to previous changes (4)
  • Makefile
  • backend/src/runRequest.ts
  • backend/src/controllers/aggregation.controller.ts
  • backend/src/models/result.ts
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: 🐳 Build docker image
🔇 Additional comments (3)
backend/tests/performance/scenarios/test-backend-v1.yml (3)

2-9: Verify plugin configuration & threshold semantics

Ensure that the expect and ensure plugins are installed and properly loaded by Artillery. Double-check that:

  • maxErrorRate: 1 (1%) and max: 500 requests align with your SLA.
  • p95: 200 is interpreted in milliseconds by the ensure plugin.

Run a quick local run to confirm no plugin load errors and that thresholds behave as expected.


29-34: Confirm CSV header alignment with fields mapping

You’ve swapped lastName and firstName in the payload. Verify that names.csv’s header row actually lists the columns in this order. If the CSV hasn’t changed, the mapping will be misaligned, causing incorrect substitutions at runtime.


44-49: Validate GET request assertions

The new expect checks for status 200 and JSON content type, plus capturing the full response as resultGet, are well-placed to catch failures early. Nice addition.

skipHeader: true
#delimiter: ";"
delimiter: ";"
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Ensure correct CSV parsing settings

The comment indicates skipHeader isn’t working. According to Artillery’s payload docs, the key should be skipFirstRow rather than skipHeader. Without this change, the header row may still be injected as data. For example:

 payload:
   - path: names.csv
     fields: [...]
-    skipHeader: true
+    skipFirstRow: true
     delimiter: ";"
🤖 Prompt for AI Agents
In backend/tests/performance/scenarios/test-backend-v1.yml around lines 37 to
38, the CSV parsing setting uses the incorrect key `skipHeader`. Replace
`skipHeader` with `skipFirstRow` to correctly skip the header row during CSV
parsing as per Artillery's payload documentation, ensuring the header is not
injected as data.

@cristianpb cristianpb force-pushed the feat/typings-result branch from afabbe2 to d8d5d32 Compare June 2, 2025 20:57
@cristianpb cristianpb force-pushed the feat/typings-result branch from 5231229 to 93a9bb7 Compare June 11, 2025 21:09
@cristianpb cristianpb force-pushed the feat/typings-result branch from 93a9bb7 to c8235c7 Compare June 12, 2025 09:47
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