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

feat(ledger): add capability to sort transactions by timestamp #1606

Merged
merged 1 commit into from
Jul 19, 2024

Conversation

gfyrag
Copy link
Contributor

@gfyrag gfyrag commented Jul 15, 2024

Fixes ENG-1155

Add two query params on GET //transactions :

  • order == effective => indicate we want to sort transactions using effective dates
  • reverse == true => Indicate we want to reverse the sort order (works when sorting on ids or on timestamps)

@gfyrag gfyrag requested a review from a team as a code owner July 15, 2024 10:40
Copy link
Contributor

coderabbitai bot commented Jul 15, 2024

Walkthrough

The changes made introduce the ability to order transactions in the Ledger API by adding support for "order" and "reverse" query parameters. Adjustments span across query construction, API controllers, and related tests, ensuring transactions can be efficiently ordered and handled during pagination.

Changes

File Path Change Summary
components/.../controllers_transactions.go Modified getTransactions to support "order" and "reverse" query parameters
components/.../controllers_transactions_test.go Updated test construction for expectQuery to use NewGetTransactionsQuery
components/.../ledgerstore/transactions.go Added new import and method WithColumn to support column-based ordering
libs/go-libs/.../pagination.go Introduced method WithColumn for column pagination
libs/go-libs/.../pagination_column.go Enhanced pagination ID handling to support time.Time and libtime.Time
libs/go-libs/.../pagination_offset.go Removed redundant return statements in UsingOffset functions
releases/sdks/go/.speakeasy/gen.lock Updated docChecksum and added generated file for orders in the documentation
releases/sdks/go/docs/.../order.md Introduced new documentation section on Order values
releases/sdks/go/docs/.../v2listtransactionsrequest.md Added Order and Reverse parameters in the request documentation
releases/sdks/go/pkg/.../v2listtransactions.go Added Order type and fields for Order and Reverse in V2ListTransactionsRequest
tests/integration/.../ledger-list-transactions.go Replaced BeforeEach with JustBeforeEach, adjusted transaction and query setup

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant API as Ledger API
    participant Db as Database

    Client->>API: GET /transactions?order=effective&reverse=true
    API->>API: Parse query parameters
    API->>Db: Construct and execute GetTransactionsQuery
    Db-->>API: Return ordered transactions
    API-->>Client: Return response with ordered transactions
Loading

Assessment against linked issues

Objective Addressed Explanation
Support ordering transactions in Ledger API (ENG-1155)

Poem

In the ledger's quiet hum,
Transactions ordered, one by one,
With "order" and "reverse" in play,
They align by time of day.
Rabbits cheer, the code's now fun!


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6f8ffa7 and 833f7ec.

Files ignored due to path filters (3)
  • components/ledger/openapi.yaml is excluded by !**/*.yaml
  • components/ledger/openapi/v2.yaml is excluded by !**/*.yaml
  • releases/sdks/go/gen.yaml is excluded by !**/*.yaml
Files selected for processing (12)
  • components/ledger/internal/api/v2/controllers_transactions.go (1 hunks)
  • components/ledger/internal/api/v2/controllers_transactions_test.go (4 hunks)
  • components/ledger/internal/storage/ledgerstore/transactions.go (3 hunks)
  • libs/go-libs/bun/bunpaginate/pagination.go (1 hunks)
  • libs/go-libs/bun/bunpaginate/pagination_column.go (3 hunks)
  • libs/go-libs/bun/bunpaginate/pagination_offset.go (1 hunks)
  • releases/sdks/go/.speakeasy/gen.lock (4 hunks)
  • releases/sdks/go/docs/pkg/models/operations/order.md (1 hunks)
  • releases/sdks/go/docs/pkg/models/operations/v2listtransactionsrequest.md (1 hunks)
  • releases/sdks/go/formance.go (1 hunks)
  • releases/sdks/go/pkg/models/operations/v2listtransactions.go (4 hunks)
  • tests/integration/suite/ledger-list-transactions.go (11 hunks)
Files skipped from review due to trivial changes (3)
  • libs/go-libs/bun/bunpaginate/pagination_offset.go
  • releases/sdks/go/docs/pkg/models/operations/order.md
  • releases/sdks/go/formance.go
Additional comments not posted (28)
libs/go-libs/bun/bunpaginate/pagination_column.go (2)

9-9: Import Addition Approved

The import for time is necessary for handling time.Time types in the pagination logic.


64-84: Handling Different Pagination ID Types Approved

The code correctly handles various types for pagination IDs, including time.Time, libtime.Time, *BigInt, *big.Int, and int64. The use of big.NewInt ensures consistent conversion to big.Int.

libs/go-libs/bun/bunpaginate/pagination.go (1)

67-71: Addition of WithColumn Method Approved

The WithColumn method allows setting the column for pagination. The method is simple and follows the pattern of other With* methods in the file.

releases/sdks/go/pkg/models/operations/v2listtransactions.go (5)

14-18: Addition of Order Type and Constant Approved

The Order type and its constant OrderEffective are necessary for specifying the order of transactions.


20-22: Addition of ToPointer Method Approved

The ToPointer method is useful for converting an Order value to a pointer, which is often needed in Go for optional fields.


23-35: Addition of UnmarshalJSON Method Approved

The UnmarshalJSON method correctly handles the JSON unmarshalling for the Order type, ensuring that only valid values are accepted.


48-53: Addition of Order and Reverse Fields Approved

The addition of the Order and Reverse fields to the V2ListTransactionsRequest struct allows specifying the order and direction of the transactions.


95-100: Addition of GetOrder and GetReverse Methods Approved

The GetOrder and GetReverse methods provide convenient access to the Order and Reverse fields.

Also applies to: 116-121

components/ledger/internal/api/v2/controllers_transactions.go (1)

60-69: Handling of order and reverse Query Parameters Approved

The code correctly handles the order and reverse query parameters, setting the appropriate fields in the GetTransactionsQuery object.

releases/sdks/go/docs/pkg/models/operations/v2listtransactionsrequest.md (2)

12-12: Field Addition: Order

The Order field is correctly documented and integrated into the V2ListTransactionsRequest struct.


15-15: Field Addition: Reverse

The Reverse field is correctly documented and integrated into the V2ListTransactionsRequest struct.

components/ledger/internal/storage/ledgerstore/transactions.go (2)

166-166: Modification: buildTransactionQuery Method

The changes to the buildTransactionQuery method are correctly implemented to include the new column expression.


406-411: Method Addition: WithColumn

The WithColumn method is correctly implemented and integrated into the GetTransactionsQuery struct.

components/ledger/internal/api/v2/controllers_transactions_test.go (1)

Line range hint 570-717: Modification: TestGetTransactions Function

The changes to the TestGetTransactions function are correctly implemented to include the new expectQuery object.

tests/integration/suite/ledger-list-transactions.go (11)

8-8: Import Approved: sort package.

The addition of the sort package is necessary for sorting transactions by timestamp.


38-38: Change Approved: Use of JustBeforeEach.

The use of JustBeforeEach ensures that the ledger is created just before each test runs, which is appropriate for setup steps that should be executed in the latest possible moment.


54-63: Change Approved: Creating transactions with adjusted timestamps.

The logic for adjusting the timestamps ensures that transactions have different effective dates, which is necessary for testing the ordering functionality.


78-78: Change Approved: Setting transaction timestamp.

The Timestamp field of the transaction is set using a pointer to the adjusted txTimestamp, which is crucial for testing the sorting functionality.


136-160: Change Approved: Updating test setup for listing transactions.

The changes ensure that the request for listing transactions includes the necessary parameters for pagination and filtering.


171-182: Change Approved: Adding context for testing effective ordering.

The new context verifies that transactions are correctly ordered by their effective timestamp, which is the primary objective of the PR.


189-189: Change Approved: Ensuring cursor stability.

Creating a new transaction ensures the stability of the cursor when paginating through transactions, which is important for accurate pagination tests.


231-231: Change Approved: Following previous cursor.

The JustBeforeEach block ensures that the previous cursor is followed correctly, which is necessary for testing pagination in both directions.


260-260: Change Approved: Listing transactions with a single match filter.

The JustBeforeEach block sets up the necessary conditions for testing the listing of transactions with a specific filter, ensuring the correct functionality of the filtering feature.


302-302: Change Approved: Listing transactions with multiple match filters.

The JustBeforeEach block sets up the necessary conditions for testing the listing of transactions with multiple filters, ensuring the correct functionality of complex filtering scenarios.


360-360: Change Approved: Listing transactions with an invalid filter.

The JustBeforeEach block sets up the necessary conditions for testing the error handling when an invalid filter is used, ensuring robust validation.

releases/sdks/go/.speakeasy/gen.lock (3)

8-8: Release version update.

The releaseVersion is updated to 0.0.1. Ensure this version number is consistent across the codebase and documentation.

Verification successful

Release version update verified.

The releaseVersion is updated to 0.0.1 and this version number is consistent across the relevant parts of the codebase, specifically in releases/sdks/go/formance.go.

  • releases/sdks/go/formance.go: SDKVersion: "0.0.1"
  • releases/sdks/go/formance.go: UserAgent: "speakeasy-sdk/go 0.0.1 ..."
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the consistency of the release version across the codebase and documentation.

# Test: Search for the release version. Expect: Only occurrences of the new version.
rg --type go --type md '0.0.1'

Length of output: 4966


16-16: New feature added: downloadStreams.

The downloadStreams feature is added. Ensure this feature is appropriately documented and integrated.


9-9: Configuration checksum update.

The configChecksum is updated to a4b5c74b8763c6fdc98e6adfde02c782. Ensure this checksum is correctly generated and matches the current configuration.

Verification successful

Configuration checksum update verified.

The configChecksum value a4b5c74b8763c6fdc98e6adfde02c782 matches the checksum of the configuration file releases/sdks/go/gen.yaml.

  • releases/sdks/go/gen.yaml
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the configuration checksum.

# Test: Generate the configuration checksum and compare with the updated value.
# Note: Replace `<config-file>` with the actual configuration file path.
md5sum <config-file> | grep 'a4b5c74b8763c6fdc98e6adfde02c782'

Length of output: 199


Script:

#!/bin/bash
# Find potential configuration files in the repository
fd -e json -e yaml -e yml -e toml -e ini --type f

# Calculate the checksums of these files and compare with the given checksum
for file in $(fd -e json -e yaml -e yml -e toml -e ini --type f); do
  md5sum "$file"
done | grep 'a4b5c74b8763c6fdc98e6adfde02c782'

Length of output: 73500

@gfyrag gfyrag force-pushed the feat/ledger-timstamp-based-pagination branch from 833f7ec to 483e99b Compare July 15, 2024 11:17
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

Outside diff range, codebase verification and nitpick comments (2)
components/ledger/internal/api/v2/controllers_transactions.go (2)

62-64: Ensure consistency in handling query parameters.

The logic for setting the Column field based on the "order" parameter is correct. However, consider using constants or enums for the "order" values to avoid hardcoding strings.

- if r.URL.Query().Get("order") == "effective" {
+ const orderEffective = "effective"
+ if r.URL.Query().Get("order") == orderEffective {

65-67: Ensure consistency in handling query parameters.

The logic for setting the Order field based on the "reverse" parameter is correct. However, consider using constants or enums for the "reverse" values to avoid hardcoding strings.

- if r.URL.Query().Get("reverse") == "true" {
+ const reverseTrue = "true"
+ if r.URL.Query().Get("reverse") == reverseTrue {
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 833f7ec and 483e99b.

Files ignored due to path filters (3)
  • components/ledger/openapi.yaml is excluded by !**/*.yaml
  • components/ledger/openapi/v2.yaml is excluded by !**/*.yaml
  • releases/sdks/go/gen.yaml is excluded by !**/*.yaml
Files selected for processing (12)
  • components/ledger/internal/api/v2/controllers_transactions.go (1 hunks)
  • components/ledger/internal/api/v2/controllers_transactions_test.go (4 hunks)
  • components/ledger/internal/storage/ledgerstore/transactions.go (3 hunks)
  • libs/go-libs/bun/bunpaginate/pagination.go (1 hunks)
  • libs/go-libs/bun/bunpaginate/pagination_column.go (3 hunks)
  • libs/go-libs/bun/bunpaginate/pagination_offset.go (1 hunks)
  • releases/sdks/go/.speakeasy/gen.lock (2 hunks)
  • releases/sdks/go/docs/pkg/models/operations/order.md (1 hunks)
  • releases/sdks/go/docs/pkg/models/operations/v2listtransactionsrequest.md (1 hunks)
  • releases/sdks/go/formance.go (1 hunks)
  • releases/sdks/go/pkg/models/operations/v2listtransactions.go (4 hunks)
  • tests/integration/suite/ledger-list-transactions.go (11 hunks)
Files skipped from review due to trivial changes (1)
  • releases/sdks/go/docs/pkg/models/operations/order.md
Files skipped from review as they are similar to previous changes (10)
  • components/ledger/internal/api/v2/controllers_transactions_test.go
  • components/ledger/internal/storage/ledgerstore/transactions.go
  • libs/go-libs/bun/bunpaginate/pagination.go
  • libs/go-libs/bun/bunpaginate/pagination_column.go
  • libs/go-libs/bun/bunpaginate/pagination_offset.go
  • releases/sdks/go/.speakeasy/gen.lock
  • releases/sdks/go/docs/pkg/models/operations/v2listtransactionsrequest.md
  • releases/sdks/go/formance.go
  • releases/sdks/go/pkg/models/operations/v2listtransactions.go
  • tests/integration/suite/ledger-list-transactions.go

@gfyrag gfyrag force-pushed the feat/ledger-timstamp-based-pagination branch from 483e99b to 73075a5 Compare July 15, 2024 11:22
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 483e99b and 73075a5.

Files ignored due to path filters (2)
  • components/ledger/openapi.yaml is excluded by !**/*.yaml
  • components/ledger/openapi/v2.yaml is excluded by !**/*.yaml
Files selected for processing (11)
  • components/ledger/internal/api/v2/controllers_transactions.go (1 hunks)
  • components/ledger/internal/api/v2/controllers_transactions_test.go (4 hunks)
  • components/ledger/internal/storage/ledgerstore/transactions.go (3 hunks)
  • libs/go-libs/bun/bunpaginate/pagination.go (1 hunks)
  • libs/go-libs/bun/bunpaginate/pagination_column.go (3 hunks)
  • libs/go-libs/bun/bunpaginate/pagination_offset.go (1 hunks)
  • releases/sdks/go/.speakeasy/gen.lock (2 hunks)
  • releases/sdks/go/docs/pkg/models/operations/order.md (1 hunks)
  • releases/sdks/go/docs/pkg/models/operations/v2listtransactionsrequest.md (1 hunks)
  • releases/sdks/go/pkg/models/operations/v2listtransactions.go (4 hunks)
  • tests/integration/suite/ledger-list-transactions.go (11 hunks)
Files skipped from review as they are similar to previous changes (10)
  • components/ledger/internal/api/v2/controllers_transactions_test.go
  • components/ledger/internal/storage/ledgerstore/transactions.go
  • libs/go-libs/bun/bunpaginate/pagination.go
  • libs/go-libs/bun/bunpaginate/pagination_column.go
  • libs/go-libs/bun/bunpaginate/pagination_offset.go
  • releases/sdks/go/.speakeasy/gen.lock
  • releases/sdks/go/docs/pkg/models/operations/order.md
  • releases/sdks/go/docs/pkg/models/operations/v2listtransactionsrequest.md
  • releases/sdks/go/pkg/models/operations/v2listtransactions.go
  • tests/integration/suite/ledger-list-transactions.go
Additional comments not posted (4)
components/ledger/internal/api/v2/controllers_transactions.go (4)

60-61: Initialize the query object correctly.

The initialization of q with ledgerstore.NewGetTransactionsQuery(*options) seems correct.


62-64: Ensure correct column assignment for ordering.

The condition checks if the "order" query parameter is set to "effective" and assigns the "timestamp" column for ordering. This looks correct.


65-67: Verify the order direction assignment.

The condition checks if the "reverse" query parameter is set to "true" and assigns ascending order. Ensure this logic aligns with the intended behavior.

Is the ascending order intended when "reverse" is set to true? Typically, "reverse" implies descending order.


68-69: Pointer conversion for the query object.

The conversion of the query object to a pointer using pointer.For(q) is correct.

@gfyrag gfyrag added this pull request to the merge queue Jul 19, 2024
Merged via the queue into main with commit 7a8cb98 Jul 19, 2024
11 checks passed
@gfyrag gfyrag deleted the feat/ledger-timstamp-based-pagination branch July 19, 2024 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants