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

fix: missing filter while listing logs #707

Merged
merged 1 commit into from
Feb 25, 2025

Conversation

gfyrag
Copy link
Contributor

@gfyrag gfyrag commented Feb 25, 2025

No description provided.

@gfyrag gfyrag requested a review from a team as a code owner February 25, 2025 10:22
Copy link

coderabbitai bot commented Feb 25, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

🗂️ Base branches to auto review (1)
  • main

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

This pull request makes changes to several components of the codebase. In the Earthfile, the build step for the client generation is commented out, leaving the openapi build active for the pre-commit target. In the API package and scripting logic, variable handling is adjusted to use pointers and type switches for improved data formatting. Several SQL query functions in the ledgerstore have been modified to include point-in-time filters, new conditions, and enhanced error handling. Additionally, multiple new test cases have been added across packages to verify the updated functionality and ensure correct handling of edge cases.

Changes

File(s) Change Summary
Earthfile Commented out the BUILD +generate-client target in the pre-commit process, effectively disabling the client build step.
internal/api/v2/utils.go, internal/script.go In utils.go, updated the getPITFilter function to declare pit as a pointer for nilability; in script.go, introduced a type switch in the ToCore method for type-specific formatting of the amount field.
internal/storage/ledgerstore/accounts.go, internal/storage/ledgerstore/balances.go, internal/storage/ledgerstore/logs.go Refined SQL query construction: added date filtering using PIT and a new first_usage case in accounts queries, replaced a stored procedure call with a custom query in balance retrieval (including enhanced error handling), and updated the logs query builder to include conditions for both date and id (with a debug print).
internal/script_test.go, internal/storage/ledgerstore/accounts_test.go, internal/storage/ledgerstore/balances_test.go, internal/storage/ledgerstore/logs_test.go Added new test cases to validate script conversion, account filtering using PIT, balance retrieval for non-existent accounts, and logs retrieval with an id condition.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Earthfile
    participant BuildEngine

    User->>Earthfile: Trigger pre-commit
    Earthfile->>BuildEngine: Execute BUILD +openapi
    Earthfile-->>BuildEngine: (generate-client step is skipped)
    BuildEngine-->>Earthfile: Return build results
    Earthfile->>User: Pre-commit complete
Loading

Possibly related PRs

Suggested reviewers

  • flemzord
  • paul-nicolas

Poem

I'm a little bunny, hopping in code,
Skipping steps that no longer explode.
The client build is napped for a while, 😴
Each query refined with a meticulous style.
Leaping through tests, I cheer with delight,
In this clean, refactored code day and night!
🐇💻 Hop on, coders, everything's all right!


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.

@gfyrag gfyrag changed the base branch from main to release/v2.1 February 25, 2025 10:22
@gfyrag gfyrag enabled auto-merge (rebase) February 25, 2025 10:23
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 (3)
internal/script.go (1)

35-40: Improved type handling for numeric values, but potential issues with precision.

The changes to ToCore method add proper type-specific handling for the amount field, which aligns with the PR objective to resolve incompatibility issues when handling large numbers. However, there are two potential concerns:

  1. Converting float64 to int will truncate decimal places, potentially causing data loss
  2. Very large float64 values might exceed the range of int type (which is platform-dependent), leading to overflow

Consider handling decimal places and potential overflow:

case float64:
-   s.Script.Vars[k] = fmt.Sprintf("%s %d", v["asset"], int(amount))
+   // Preserve precision and handle potential overflow
+   s.Script.Vars[k] = fmt.Sprintf("%s %.0f", v["asset"], amount)
internal/script_test.go (1)

19-47: Good test coverage, but missing potential edge cases.

The test cases effectively verify the conversion functionality for both large float64 and string-based big integers, which is great. This helps ensure the PR objective of fixing incompatibility with large numbers is met.

Consider adding these additional test cases:

  1. A number with decimal places to verify truncation behavior
  2. A negative number to ensure sign is preserved
  3. Edge cases like zero, extremely large values, or invalid inputs
testCases := []testCase{
    // existing test cases...
+   {
+       name: "decimal number conversion",
+       inputVars: map[string]any{
+           "amount": map[string]any{
+               "asset":  "USD",
+               "amount": 123.45,
+           },
+       },
+       expected: map[string]string{
+           "amount": "USD 123",
+       },
+   },
+   {
+       name: "negative number conversion",
+       inputVars: map[string]any{
+           "amount": map[string]any{
+               "asset":  "USD",
+               "amount": -500,
+           },
+       },
+       expected: map[string]string{
+           "amount": "USD -500",
+       },
+   },
}
internal/storage/ledgerstore/accounts.go (1)

128-141: DRY opportunity for balance-based filtering.
The logic here mirrors the approach in the balanceRegex case. If these two paths share the same concept, consider consolidating them to reduce duplication and facilitate maintenance.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 3c008ce and 4355a93.

⛔ Files ignored due to path filters (1)
  • docker-compose.yml is excluded by !**/*.yml
📒 Files selected for processing (10)
  • Earthfile (1 hunks)
  • internal/api/v2/utils.go (1 hunks)
  • internal/script.go (1 hunks)
  • internal/script_test.go (1 hunks)
  • internal/storage/ledgerstore/accounts.go (2 hunks)
  • internal/storage/ledgerstore/accounts_test.go (3 hunks)
  • internal/storage/ledgerstore/balances.go (1 hunks)
  • internal/storage/ledgerstore/balances_test.go (1 hunks)
  • internal/storage/ledgerstore/logs.go (2 hunks)
  • internal/storage/ledgerstore/logs_test.go (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • Earthfile
🔇 Additional comments (10)
internal/script_test.go (1)

10-64: Well-structured test with parallel execution.

The test is well-structured using table-driven testing patterns, with clear test cases and good use of parallel test execution. The approach of testing both float64 and big integer string conversions is thorough.

internal/storage/ledgerstore/balances_test.go (1)

21-32: Good addition of test for zero balance on non-existent accounts.

This test properly validates that the store returns a zero balance instead of an error when querying for a non-existent account, aligning with the PR objective to "fix the ledger store to return a zero balance for non-existing accounts".

internal/storage/ledgerstore/logs_test.go (1)

293-298: Good addition of test case for filtering logs by ID.

This test case properly validates the functionality of retrieving logs with ID less than 3, which helps ensure the filtering capability works as expected. This aligns with the PR objective to fix the missing filter while listing logs.

internal/storage/ledgerstore/logs.go (1)

75-76: Good enhancement to support filtering by ID.

The addition of id as a valid filter key properly extends the query building functionality to support filtering logs by their ID, which was mentioned in the PR objectives.

internal/storage/ledgerstore/accounts_test.go (2)

10-11: Appropriate addition of pointer package import.

Good addition of the pointer package import, which is used in the new test case.


190-201: Good test case for balance filtering with PIT.

This test case properly validates that filtering accounts by balance works correctly when combined with a point-in-time (PIT) filter, which addresses the PR objective about "invalid balance filter when using point-in-time (PIT) queries".

internal/api/v2/utils.go (1)

50-62:

✅ Verification successful

Use caution with nil pointer usage in PIT.
Now that the function stores pit as a pointer, there may be scenarios where pit remains nil. Although the downstream code often checks if q.PIT != nil && !q.PIT.IsZero(), please verify that every place referencing PIT is robust against a potential nil value and won’t cause unexpected panics or logic errors.

Feel free to run the script below to search for all references to PIT and usage patterns across the codebase for potential nil-dereferences:


🏁 Script executed:

#!/bin/bash
# Description: Locate references to PIT and check for nil handling
rg -A 5 "q\.PIT" 

Length of output: 4668


PIT nil pointer handling confirmed

After searching across the codebase, all references to q.PIT—notably in internal/storage/ledgerstore/balances.go and internal/storage/ledgerstore/accounts.go—consistently check for a nil value (using patterns like if q.PIT != nil and if q.PIT != nil && !q.PIT.IsZero()). This indicates that the new usage of a pointer for pit is properly guarded and should not cause unexpected panics or logic errors downstream.

No further modifications are necessary.

internal/storage/ledgerstore/balances.go (1)

149-165: Inline SQL query for balance retrieval looks good.
Switching from the stored function to a direct inline SQL query makes the logic more explicit and predictable. Returning zero when no record is found (line 164-165) also clearly fulfills the requirement of avoiding errors for non-existent accounts.

internal/storage/ledgerstore/accounts.go (2)

110-126: Verify the usage of insertion_date for PIT filtering.
The added dateFilter condition uses insertion_date < ? for filtering when PIT is provided. Ensure that you indeed want to reference insertion_date rather than effective_date, since other parts of the code sometimes rely on effective_date as a point-in-time reference.


152-153: New first_usage filter support.
This addition helps filter queries by the first_usage column. It looks good. However, confirm that the chosen operator is compatible with all comparison modes you intend to allow (for instance $match, $exists, etc., if relevant).

fmt.Println(selectQuery.String())

Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Remove debugging print statement.

This fmt.Println statement appears to be a debugging leftover and should be removed from the production code.

-		fmt.Println(selectQuery.String())
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
fmt.Println(selectQuery.String())

@@ -83,6 +83,7 @@ func (store *Store) logsQueryBuilder(q PaginatedQueryOptions[any]) func(*bun.Sel
}
selectQuery = selectQuery.Where(subQuery, args...)
}
fmt.Println(selectQuery.String())
Copy link
Contributor

Choose a reason for hiding this comment

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

coderabbit is right on this one, we should remove the query print !

@gfyrag gfyrag force-pushed the hotfix/list-logs-missing-filter branch from 4355a93 to 64dd9a4 Compare February 25, 2025 10:38
@gfyrag gfyrag merged commit 6f5e5c7 into release/v2.1 Feb 25, 2025
7 checks passed
@gfyrag gfyrag deleted the hotfix/list-logs-missing-filter branch February 25, 2025 12:59
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