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(rfq-api): filter old quotes #2958

Merged
merged 6 commits into from
Aug 6, 2024
Merged

feat(rfq-api): filter old quotes #2958

merged 6 commits into from
Aug 6, 2024

Conversation

dwasse
Copy link
Collaborator

@dwasse dwasse commented Jul 30, 2024

Summary by CodeRabbit

  • New Features

    • Introduced a new configuration parameter for maximum quote age, allowing for better management of quote validity.
    • Added a filtering mechanism to ensure only recently updated quotes are included in API responses.
  • Tests

    • Added new tests to validate the functionality of the quote age filtering mechanism, ensuring accurate results across various scenarios.

@github-actions github-actions bot added go Pull requests that update Go code size/s labels Jul 30, 2024
Copy link
Contributor

coderabbitai bot commented Jul 30, 2024

Walkthrough

The recent changes introduce a new configuration parameter, MaxQuoteAge, enhancing quote management by specifying a maximum age for quotes. Functions have been added to filter quotes based on this age, improving API response quality. Additionally, dedicated test functions ensure robust validation of the filtering logic, refining the server's handling of quote validity.

Changes

Files Change Summary
services/rfq/api/config/config.go Added MaxQuoteAge to Config struct and GetMaxQuoteAge method for retrieving this value.
services/rfq/api/rest/export_test.go Introduced FilterQuoteAge function for testing quote age filtering.
services/rfq/api/rest/handler.go Added filterQuoteAge function in GetQuotes method to filter quotes based on age using maxAge.
services/rfq/api/rest/server_test.go Added TestFilterQuoteAge to validate filtering logic for quote age.
services/rfq/api/rest/suite_test.go Integrated MaxQuoteAge into SetupTest with a default of 15 minutes for quote validity.
.github/workflows/go.yml Modified GitHub Actions workflow to exclude the agents package from job execution.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant API
    participant Config
    participant QuoteDB

    Client->>API: Request Quotes
    API->>Config: Retrieve MaxQuoteAge
    API->>QuoteDB: Fetch Quotes
    API->>API: Filter Quotes by Age (using MaxQuoteAge)
    API-->>Client: Return Filtered Quotes
Loading

🐰 In fields so bright, where quotes do play,
A max age now guides them, keeping old ones at bay.
With new tools to filter, the fresh ones will stay,
Hopping with joy, our code's in ballet!
Let's cherish these changes, in code they will say! 🐇✨


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

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

The pull request introduces a feature to filter out old quotes in the RFQ API by adding a configurable maximum age parameter.

  • New Configuration Parameter: Added MaxQuoteAge in services/rfq/api/config/config.go to set the maximum age for quotes.
  • Filtering Function: Introduced filterQuoteAge in services/rfq/api/rest/handler.go to filter quotes based on MaxQuoteAge.
  • Testing Enhancements: Added services/rfq/api/rest/export_test.go to test filterQuoteAge independently.
  • Test Coverage: Updated services/rfq/api/rest/server_test.go and services/rfq/api/rest/suite_test.go to include tests for the new filtering functionality.

5 file(s) reviewed, 9 comment(s)
Edit PR Review Bot Settings

@@ -25,6 +25,7 @@ type Config struct {
Bridges map[uint32]string `yaml:"bridges"`
Port string `yaml:"port"`
RelayAckTimeout time.Duration `yaml:"relay_ack_timeout"`
MaxQuoteAge time.Duration `yaml:"max_quote_age"`
Copy link

Choose a reason for hiding this comment

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

style: Ensure MaxQuoteAge is correctly set in all deployment environments to avoid unexpected behavior.

Comment on lines +43 to +49
// GetMaxQuoteAge returns the max quote age.
func (c Config) GetMaxQuoteAge() time.Duration {
if c.MaxQuoteAge == 0 {
return defaultMaxQuoteAge
}
return c.MaxQuoteAge
}
Copy link

Choose a reason for hiding this comment

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

check: Consider adding validation to ensure MaxQuoteAge is within a reasonable range to prevent misconfiguration.

Comment on lines +8 to +11
// FilterQuoteAge exports filterQuoteAge for testing.
func FilterQuoteAge(cfg config.Config, dbQuotes []*db.Quote) []*db.Quote {
return filterQuoteAge(cfg, dbQuotes)
}
Copy link

Choose a reason for hiding this comment

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

style: Consider adding a comment to clarify that this function is intended for testing purposes only.

"net/http"
"strconv"
"time"
Copy link

Choose a reason for hiding this comment

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

logic: Consider the performance impact of importing the time package and using time-based filtering on large datasets.

Comment on lines +215 to +217
// Filter quotes
dbQuotes = filterQuoteAge(h.cfg, dbQuotes)

Copy link

Choose a reason for hiding this comment

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

logic: Ensure filterQuoteAge is efficient and does not introduce significant latency, especially with large datasets.

Comment on lines +245 to +257
func filterQuoteAge(cfg config.Config, dbQuotes []*db.Quote) []*db.Quote {
maxAge := cfg.GetMaxQuoteAge()

thresh := time.Now().Add(-maxAge)
var filteredQuotes []*db.Quote
for _, quote := range dbQuotes {
if quote.UpdatedAt.After(thresh) {
filteredQuotes = append(filteredQuotes, quote)
}
}

return filteredQuotes
}
Copy link

Choose a reason for hiding this comment

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

style: Consider edge cases where UpdatedAt might not be set correctly, leading to potential data exclusion.

Comment on lines 211 to 225
func (c *ServerSuite) TestFilterQuoteAge() {
now := time.Now()

// insert quote outside age range
quotes := []*db.Quote{
&db.Quote{OriginChainID: 1, UpdatedAt: now.Add(-time.Hour)},
&db.Quote{OriginChainID: 2, UpdatedAt: now.Add(-time.Minute)},
}

filteredQuotes := rest.FilterQuoteAge(c.cfg, quotes)

// verify old quote is filtered out
c.Equal(1, len(filteredQuotes))
c.Equal(quotes[1], filteredQuotes[0])
}
Copy link

Choose a reason for hiding this comment

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

style: Ensure the FilterQuoteAge function handles edge cases, such as quotes updated exactly at the threshold.

@@ -76,7 +77,8 @@ func (c *ServerSuite) SetupTest() {
1: ethFastBridgeAddress.Hex(),
42161: arbFastBridgeAddress.Hex(),
},
Port: fmt.Sprintf("%d", port),
Port: fmt.Sprintf("%d", port),
MaxQuoteAge: 15 * time.Minute,
Copy link

Choose a reason for hiding this comment

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

style: Ensure that the MaxQuoteAge parameter is consistently used across all relevant parts of the codebase.

@@ -76,7 +77,8 @@ func (c *ServerSuite) SetupTest() {
1: ethFastBridgeAddress.Hex(),
42161: arbFastBridgeAddress.Hex(),
},
Port: fmt.Sprintf("%d", port),
Port: fmt.Sprintf("%d", port),
MaxQuoteAge: 15 * time.Minute,
Copy link

Choose a reason for hiding this comment

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

logic: Verify that the filtering mechanism for old quotes does not introduce performance bottlenecks.

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

Outside diff range, codebase verification and nitpick comments (2)
services/rfq/api/rest/handler.go (1)

245-257: Review the new function filterQuoteAge.

The function correctly filters quotes based on their age using the maxAge configuration. The logic is straightforward and handles the filtering efficiently.

However, consider adding logging for better traceability and debugging.

+ log.Printf("Filtering quotes older than %s", maxAge)
services/rfq/api/rest/server_test.go (1)

211-225: Review the new test function TestFilterQuoteAge.

The test function correctly tests the filtering logic by creating quotes with varying timestamps and asserting that only the most recent quote remains after filtering. The test covers the basic functionality.

Consider adding more edge cases, such as quotes exactly on the threshold.

+ // insert quote exactly on age range
+ quotes = append(quotes, &db.Quote{OriginChainID: 3, UpdatedAt: now.Add(-maxAge)})
+ filteredQuotes = rest.FilterQuoteAge(c.cfg, quotes)
+ c.Equal(2, len(filteredQuotes))
Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9f5bdc2 and a7fd85e.

Files selected for processing (5)
  • services/rfq/api/config/config.go (2 hunks)
  • services/rfq/api/rest/export_test.go (1 hunks)
  • services/rfq/api/rest/handler.go (3 hunks)
  • services/rfq/api/rest/server_test.go (2 hunks)
  • services/rfq/api/rest/suite_test.go (2 hunks)
Additional comments not posted (6)
services/rfq/api/config/config.go (2)

41-49: LGTM! Ensure the default value logic is correct.

The method GetMaxQuoteAge correctly returns the default value if MaxQuoteAge is not set. Ensure that this logic is tested.


28-28: Ensure correct configuration loading for MaxQuoteAge.

The new field MaxQuoteAge is added to the Config struct. Verify that this field is correctly loaded from the configuration file.

services/rfq/api/rest/suite_test.go (1)

81-81: LGTM! Ensure the new configuration parameter is tested.

The new configuration parameter MaxQuoteAge is added to the SetupTest function. Ensure that this parameter is tested in the test cases.

services/rfq/api/rest/handler.go (2)

7-7: Import addition approved.

The time package is necessary for the new filtering functionality.


215-217: Filtering logic integration approved.

The integration of the filterQuoteAge function into the GetQuotes method is correct and enhances the functionality by filtering out old quotes.

services/rfq/api/rest/server_test.go (1)

12-15: Import changes approved.

The explicit imports of the db and rest packages are necessary for the new test functionality.

Comment on lines +1 to +11
package rest

import (
"github.com/synapsecns/sanguine/services/rfq/api/config"
"github.com/synapsecns/sanguine/services/rfq/api/db"
)

// FilterQuoteAge exports filterQuoteAge for testing.
func FilterQuoteAge(cfg config.Config, dbQuotes []*db.Quote) []*db.Quote {
return filterQuoteAge(cfg, dbQuotes)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Add tests for FilterQuoteAge.

This file is exporting the filterQuoteAge function for testing purposes. Ensure that there are corresponding test cases to validate the filtering logic.

Do you want me to generate the unit testing code or open a GitHub issue to track this task?

Copy link

cloudflare-workers-and-pages bot commented Jul 30, 2024

Deploying sanguine-fe with  Cloudflare Pages  Cloudflare Pages

Latest commit: bf8b67b
Status: ✅  Deploy successful!
Preview URL: https://f79fd8be.sanguine-fe.pages.dev
Branch Preview URL: https://feat-old-api-quotes.sanguine-fe.pages.dev

View logs

@dwasse dwasse changed the title RFQ API: filter old quotes feat(rfq-api): filter old quotes Aug 6, 2024
Copy link

codecov bot commented Aug 6, 2024

Codecov Report

Attention: Patch coverage is 70.58824% with 5 lines in your changes missing coverage. Please review.

Project coverage is 25.30033%. Comparing base (248dc89) to head (bf8b67b).

Files Patch % Lines
services/rfq/api/config/config.go 0.00000% 5 Missing ⚠️
Additional details and impacted files
@@                 Coverage Diff                 @@
##              master       #2958         +/-   ##
===================================================
+ Coverage   24.94693%   25.30033%   +0.35340%     
===================================================
  Files            799         780         -19     
  Lines          57939       56770       -1169     
  Branches          82          82                 
===================================================
- Hits           14454       14363         -91     
+ Misses         42002       40922       -1080     
- Partials        1483        1485          +2     
Flag Coverage Δ
opbot 0.48900% <ø> (ø)
promexporter ?
rfq 21.86965% <70.58824%> (+0.11962%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a7fd85e and 327825d.

Files selected for processing (1)
  • services/rfq/api/rest/suite_test.go (2 hunks)
Additional comments not posted (2)
services/rfq/api/rest/suite_test.go (2)

82-82: Ensure consistent use of MaxQuoteAge.

Make sure that the MaxQuoteAge parameter is consistently used across all relevant parts of the codebase.


82-82: Verify performance impact of filtering mechanism.

Ensure that the filtering mechanism for old quotes does not introduce performance bottlenecks.

@github-actions github-actions bot added the M-ci Module: CI label Aug 6, 2024
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.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 327825d and 6ed3c5d.

Files selected for processing (1)
  • .github/workflows/go.yml (1 hunks)
Additional comments not posted (1)
.github/workflows/go.yml (1)

89-90: LGTM! Verify the impact of excluding the agents package.

The addition of the exclude key to exclude the agents package from the matrix of jobs is a valid change. Ensure that this exclusion does not inadvertently skip any necessary tests or checks for the agents package.

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 (1)
services/rfq/api/rest/server_test.go (1)

211-225: LGTM! But consider adding edge case tests and additional assertions.

The test function correctly verifies that old quotes are filtered out. However, consider adding edge cases such as:

  1. Quotes updated exactly at the threshold.
  2. Quotes with the same timestamp.
  3. Quotes with future timestamps.

Additionally, add assertions to verify the contents of the filtered quotes.

+	// insert quote at the threshold
+	quotes = append(quotes, &db.Quote{OriginChainID: 3, UpdatedAt: now.Add(-c.cfg.MaxQuoteAge)})
+	
+	// insert quote with future timestamp
+	quotes = append(quotes, &db.Quote{OriginChainID: 4, UpdatedAt: now.Add(time.Hour)})
+	
+	// insert quote with the same timestamp as the recent quote
+	quotes = append(quotes, &db.Quote{OriginChainID: 5, UpdatedAt: now.Add(-time.Minute)})

+	// verify filtered quotes
+	filteredQuotes := rest.FilterQuoteAge(c.cfg, quotes)
+	c.Equal(3, len(filteredQuotes))
+	c.Contains(filteredQuotes, quotes[1])
+	c.Contains(filteredQuotes, quotes[2])
+	c.Contains(filteredQuotes, quotes[4])
Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6ed3c5d and bf8b67b.

Files selected for processing (1)
  • services/rfq/api/rest/server_test.go (2 hunks)

@dwasse dwasse merged commit 2555452 into master Aug 6, 2024
30 checks passed
@dwasse dwasse deleted the feat/old-api-quotes branch August 6, 2024 19:53
Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

(updates since last review)

The pull request introduces a feature to filter out old quotes in the RFQ API by adding a configurable maximum age parameter. The recent changes focus on enhancing the filtering mechanism and updating the testing framework.

  • New Configuration Parameter: Added MaxQuoteAge in services/rfq/relayer/relconfig/config.go to set the maximum age for quotes.
  • Filtering Mechanism: Introduced filterQuoteAge in services/rfq/relayer/quoter/quoter.go to filter quotes based on MaxQuoteAge.
  • Test Enhancements: Updated services/rfq/relayer/quoter/suite_test.go to validate the new filtering functionality.
  • Utility Function: Replaced chain.IsGasToken with util.IsGasToken in services/rfq/relayer/relapi/handler.go for consistency.
  • Concurrency Handling: Added mutex in services/rfq/api/rest/suite_test.go to ensure thread safety when adding test backends.

85 file(s) reviewed, 2 comment(s)
Edit PR Review Bot Settings

@@ -77,7 +78,8 @@ func (c *ServerSuite) SetupTest() {
1: ethFastBridgeAddress.Hex(),
42161: arbFastBridgeAddress.Hex(),
},
Port: fmt.Sprintf("%d", port),
Port: fmt.Sprintf("%d", port),
MaxQuoteAge: 15 * time.Minute,
Copy link

Choose a reason for hiding this comment

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

style: Ensure that the MaxQuoteAge parameter is consistently used across all relevant parts of the codebase.

@@ -77,7 +78,8 @@ func (c *ServerSuite) SetupTest() {
1: ethFastBridgeAddress.Hex(),
42161: arbFastBridgeAddress.Hex(),
},
Port: fmt.Sprintf("%d", port),
Port: fmt.Sprintf("%d", port),
MaxQuoteAge: 15 * time.Minute,
Copy link

Choose a reason for hiding this comment

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

logic: Verify that the filtering mechanism for old quotes does not introduce performance bottlenecks.

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

(updates since last review)

The pull request introduces a feature to filter out old quotes in the RFQ API by adding a configurable maximum age parameter. The recent changes focus on enhancing the filtering mechanism and updating the testing framework.

  • New Configuration Parameter: Added MaxQuoteAge in services/rfq/relayer/relconfig/config.go to set the maximum age for quotes.
  • Filtering Mechanism: Introduced filterQuoteAge in services/rfq/relayer/quoter/quoter.go to filter quotes based on MaxQuoteAge.
  • Test Enhancements: Updated services/rfq/relayer/quoter/suite_test.go to validate the new filtering functionality.
  • Utility Function: Replaced chain.IsGasToken with util.IsGasToken in services/rfq/relayer/relapi/handler.go for consistency.
  • Concurrency Handling: Added mutex in services/rfq/api/rest/suite_test.go to ensure thread safety when adding test backends.

1 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings

Comment on lines +216 to +217
{OriginChainID: 1, UpdatedAt: now.Add(-time.Hour)},
{OriginChainID: 2, UpdatedAt: now.Add(-time.Minute)},
Copy link

Choose a reason for hiding this comment

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

style: Ensure the FilterQuoteAge function handles edge cases, such as quotes updated exactly at the threshold.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
go Pull requests that update Go code M-ci Module: CI size/s
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant