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

[CT-913] Fix issue where pnl wasn't being calculated for some subaccounts #1662

Merged
merged 2 commits into from
Jun 14, 2024

Conversation

dydxwill
Copy link
Contributor

@dydxwill dydxwill commented Jun 10, 2024

Changelist

Fix issue where pnl wasn't being calculated for some subaccounts

Test Plan

Tested in env

Author/Reviewer Checklist

  • If this PR has changes that result in a different app state given the same prior state and transaction list, manually add the state-breaking label.
  • If the PR has breaking postgres changes to the indexer add the indexer-postgres-breaking label.
  • If this PR isn't state-breaking but has changes that modify behavior in PrepareProposal or ProcessProposal, manually add the label proposal-breaking.
  • If this PR is one of many that implement a specific feature, manually label them all feature:[feature-name].
  • If you wish to for mergify-bot to automatically create a PR to backport your change to a release branch, manually add the label backport/[branch-name].
  • Manually add any of the following labels: refactor, chore, bug.

Summary by CodeRabbit

  • New Features

    • Enhanced functionality to provide additional information about the latest processed block time and the count of records.
  • Refactor

    • Updated methods to return both maxBlockTime and count for better data insights.
    • Improved logging to include more detailed information about subaccount processing.
  • Tests

    • Updated test cases to reflect changes in method returns.
    • Added a new test case for subaccount limit handling.

Copy link
Contributor

coderabbitai bot commented Jun 10, 2024

Walkthrough

The changes enhance the PnlTicksTable class by replacing the findLatestProcessedBlocktime method with findLatestProcessedBlocktimeAndCount, which now returns both the latest block time and the count of records. Corresponding updates were made across test files and the create-pnl-ticks task to accommodate this new functionality. This change allows for more detailed tracking and processing of PNL ticks, considering both time and the number of processed accounts.

Changes

Files Change Summary
indexer/.../pnl-ticks-table.test.ts Updated tests to reflect the new findLatestProcessedBlocktimeAndCount method, checking both maxBlockTime and count.
indexer/.../pnl-ticks-table.ts Modified findLatestProcessedBlocktime to findLatestProcessedBlocktimeAndCount, now returning an object with maxBlockTime and count, and updated the SQL query to retrieve both values.
indexer/.../create-pnl-ticks.test.ts Refactored tests to destructure maxBlockTime and count from findLatestProcessedBlocktimeAndCount(), updated variable usage, and added a new test case for subaccount limit.
indexer/.../create-pnl-ticks.ts Updated the runTask function to use maxBlockTime and count from findLatestProcessedBlocktimeAndCount(), and modified the logic to skip runs based on both time difference and the count of processed accounts.

Sequence Diagram(s)

sequenceDiagram
    participant TaskRunner
    participant PnlTicksTable
    participant Logger

    TaskRunner->>PnlTicksTable: findLatestProcessedBlocktimeAndCount()
    PnlTicksTable-->>TaskRunner: { maxBlockTime, count }
    TaskRunner->>Logger: Log { maxBlockTime, count }
    alt If conditions met
        TaskRunner->>PnlTicksTable: Process PNL Ticks
    else If conditions not met
        TaskRunner->>Logger: Skip processing
    end
Loading

Poem

In the land of code so bright,
PNL ticks take flight,
With time and count in hand,
They journey through the data land.
A rabbit's joy, a coder's glee,
Enhanced tracking, efficiency! 🐇💻✨


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

Outside diff range and nitpick comments (1)
indexer/services/roundtable/src/tasks/create-pnl-ticks.ts (1)

Line range hint 1-87: Overall, the runTask function appears well-structured and the changes align with the PR's objectives. However, consider adding error handling for the await calls within the Promise.all to prevent potential unhandled promise rejections.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5aa268e and 6c70714.

Files selected for processing (4)
  • indexer/packages/postgres/tests/stores/pnl-ticks-table.test.ts (1 hunks)
  • indexer/packages/postgres/src/stores/pnl-ticks-table.ts (1 hunks)
  • indexer/services/roundtable/tests/tasks/create-pnl-ticks.test.ts (2 hunks)
  • indexer/services/roundtable/src/tasks/create-pnl-ticks.ts (1 hunks)
Additional comments not posted (6)
indexer/services/roundtable/src/tasks/create-pnl-ticks.ts (3)

20-29: The destructuring assignment has been updated to accommodate the new method findLatestProcessedBlocktimeAndCount. Ensure that all usages of maxBlockTime and count are consistent throughout the codebase.


39-40: The condition for skipping a run has been updated to include a check on the count of accounts processed. This is a logical change given the PR's objectives. Ensure that the values for PNL_TICK_UPDATE_INTERVAL_MS and PNL_TICK_MAX_ACCOUNTS_PER_RUN are configured appropriately in the environment.


44-45: The log message has been updated to include information about subaccounts processing. This enhances transparency and debugging capabilities. Ensure that the log levels are appropriately set for production environments.

indexer/packages/postgres/src/stores/pnl-ticks-table.ts (1)

215-232: The implementation of findLatestProcessedBlocktimeAndCount correctly fetches the maximum block time and count from the pnl_ticks table. Ensure that the SQL query is optimized for performance, especially if the pnl_ticks table grows significantly in size.

indexer/packages/postgres/__tests__/stores/pnl-ticks-table.test.ts (1)

187-207: The test cases for findLatestProcessedBlocktimeAndCount correctly assert the maxBlockTime and count values. Ensure that edge cases, such as when the pnl_ticks table is empty, are also covered to prevent potential issues in production.

indexer/services/roundtable/__tests__/tasks/create-pnl-ticks.test.ts (1)

Line range hint 280-315: The test case for handling the PNL_TICK_UPDATE_INTERVAL_MS condition correctly simulates the scenario where the update interval has not been reached. Ensure that the mock implementations for Date.now and DateTime.utc are correctly reset after each test to prevent side effects in other tests.

@dydxwill dydxwill changed the title Fix issue where pnl wasn't being calculated for some subaccounts [CT-913] Fix issue where pnl wasn't being calculated for some subaccounts Jun 10, 2024
Copy link

linear bot commented Jun 10, 2024

@@ -305,7 +312,7 @@ describe('create-pnl-ticks', () => {
expect(pnlTicks.length).toEqual(1);
expect(logger.info).toHaveBeenCalledWith(
expect.objectContaining({
message: 'Skipping run because update interval has not been reached',
message: 'Skipping run because update interval has not been reached and all subaccounts have been processed',
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: create separate test to check the count condition

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

export async function findLatestProcessedBlocktime(): Promise<string> {
export async function findLatestProcessedBlocktimeAndCount(): Promise<{
maxBlockTime: string,
count: number,
Copy link
Contributor

Choose a reason for hiding this comment

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

im a little confused, why are we counting the number or rows in pnl_ticks? How does that relate to "all subaccounts have been processed"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This function is used to determine whether or not to run the pnl task.

Previously, we were looking at the last block time in the pnl table to determine whether or not to run the pnl task. This worked, but then we imposed a limit on the number of subaccounts/run to compute pnl for. What happened was we started to skip computing pnl on certain subaccounts because we were hitting the limit/run. So, this PR changes this check to see if the last run computed pnl for LIMIT # of subaccounts, and if so, proceed with the task & check the pnl timestamps of the subaccounts individually.

Copy link
Contributor

Choose a reason for hiding this comment

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

were we already checking the pnl timestamps fo subaccounts individually and reprocessing data? I don't see that logic

Copy link
Contributor Author

Choose a reason for hiding this comment

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

...getAccountsToUpdate(accountToLastUpdatedBlockTime, blockTime),

@dydxwill dydxwill requested a review from Christopher-Li June 11, 2024 16:45
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 6c70714 and 9b17a06.

Files selected for processing (1)
  • indexer/services/roundtable/tests/tasks/create-pnl-ticks.test.ts (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • indexer/services/roundtable/tests/tasks/create-pnl-ticks.test.ts

@dydxwill dydxwill merged commit 0162e47 into main Jun 14, 2024
11 checks passed
@dydxwill dydxwill deleted the wl/pnlfix3 branch June 14, 2024 18:07
@dydxwill
Copy link
Contributor Author

@Mergifyio backport release/indexer/v5.x

Copy link
Contributor

mergify bot commented Jun 27, 2024

backport release/indexer/v5.x

✅ Backports have been created

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

2 participants