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

[OTE-543] Add leaderboard pnl table #1938

Merged
merged 7 commits into from
Jul 18, 2024
Merged

[OTE-543] Add leaderboard pnl table #1938

merged 7 commits into from
Jul 18, 2024

Conversation

affanv14
Copy link
Contributor

@affanv14 affanv14 commented Jul 17, 2024

Changelist

  • Adds leaderboard pnl table
  • Adds upsert and bulk upsert
  • Adds index for timespan and rank

Test Plan

  • Adds unit test

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

    • Introduced leaderboard PNL (Profit and Loss) handling with new rank and timespan functionalities.
    • Added support for querying leaderboard data based on rank and timespan.
  • Backend Enhancements

    • Created new database index to optimize leaderboard PNL querying by rank and timespan.
  • Models

    • Implemented LeaderboardPnlModel to manage leaderboard PNL data, including subaccount relations and validation schema.

Copy link

linear bot commented Jul 17, 2024

Copy link
Contributor

coderabbitai bot commented Jul 17, 2024

Walkthrough

The recent changes introduce enhancements for managing leaderboard Profit and Loss (PNL) data within the database. New enum values and interfaces are added for handling query configurations. A new migration script creates an index to optimize leaderboard PNL queries. Additionally, a LeaderboardPnlModel class defines the structure and relations for the leaderboard PNL data.

Changes

File Change Summary
...src/types/query-types.ts Added TIMESPAN and RANK to QueryableField enum and introduced LeaderboardPNLQueryConfig interface.
...src/db/migrations/...create_leaderboard_pnl_rank_timespan_index.ts Added migration script to create/drop an index on rank and timeSpan columns in leaderboard_pnl table.
...src/index.ts Exported LeaderboardPnlModel and LeaderboardPnlTable.
...src/models/leaderboard-pnl-model.ts Introduced LeaderboardPnlModel class extending BaseModel with table name, ID columns, relation mappings, JSON schema, and various properties.

Poem

A leaderboard to track,
PNL with perfect knack.
With ranks and spans in store,
Optimized, it shines much more.
A model stands so bright,
For data's guiding light.


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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 655b8fa and 8277fa4.

Files selected for processing (12)
  • indexer/packages/postgres/tests/helpers/constants.ts (2 hunks)
  • indexer/packages/postgres/tests/stores/leaderboard-pnl-table.test.ts (1 hunks)
  • indexer/packages/postgres/src/db/migrations/migration_files/20240717160024_create_leaderboard_pnl_table.ts (1 hunks)
  • indexer/packages/postgres/src/db/migrations/migration_files/20240717171246_create_leaderboard_pnl_rank_timespan_index.ts (1 hunks)
  • indexer/packages/postgres/src/helpers/db-helpers.ts (1 hunks)
  • indexer/packages/postgres/src/index.ts (2 hunks)
  • indexer/packages/postgres/src/models/leaderboard-pnl-model.ts (1 hunks)
  • indexer/packages/postgres/src/stores/leaderboard-pnl-table.ts (1 hunks)
  • indexer/packages/postgres/src/types/db-model-types.ts (1 hunks)
  • indexer/packages/postgres/src/types/index.ts (1 hunks)
  • indexer/packages/postgres/src/types/leaderboard-pnl-types.ts (1 hunks)
  • indexer/packages/postgres/src/types/query-types.ts (2 hunks)
Files skipped from review due to trivial changes (3)
  • indexer/packages/postgres/src/helpers/db-helpers.ts
  • indexer/packages/postgres/src/types/db-model-types.ts
  • indexer/packages/postgres/src/types/index.ts
Additional comments not posted (20)
indexer/packages/postgres/src/types/leaderboard-pnl-types.ts (1)

1-17: LGTM!

The interface and enum are well-defined and provide clear structure and type safety for leaderboard PNL data.

indexer/packages/postgres/src/db/migrations/migration_files/20240717171246_create_leaderboard_pnl_rank_timespan_index.ts (1)

1-17: LGTM!

The migration script is well-defined and handles index creation and deletion appropriately.

indexer/packages/postgres/src/db/migrations/migration_files/20240717160024_create_leaderboard_pnl_table.ts (1)

1-26: LGTM!

The migration script is well-defined and the table schema is appropriate for storing leaderboard PNL data.

indexer/packages/postgres/src/models/leaderboard-pnl-model.ts (1)

1-60: LGTM!

The model is well-defined and includes necessary configurations and validations.

indexer/packages/postgres/__tests__/stores/leaderboard-pnl-table.test.ts (4)

33-47: LGTM!

The test case is well-structured and correctly verifies the creation of multiple LeaderboardPNL records.


49-67: LGTM!

The test case is well-structured and correctly verifies the retrieval of a LeaderboardPNL record using subaccountId and timeSpan.


69-83: LGTM!

The test case is well-structured and correctly verifies the upsert operation for a LeaderboardPNL record.


85-98: LGTM!

The test case is well-structured and correctly verifies the bulk upsert operation for multiple LeaderboardPNL records.

indexer/packages/postgres/src/index.ts (2)

20-20: LGTM!

The export for LeaderboardPNLModel is correctly added.


44-44: LGTM!

The export for LeaderboardPNLTable is correctly added.

indexer/packages/postgres/src/stores/leaderboard-pnl-table.ts (4)

27-83: LGTM!

The findAll function is well-structured and includes necessary checks and query setups.


85-94: LGTM!

The create function is well-structured and correctly uses the insert method.


96-106: LGTM!

The upsert function is well-structured and correctly uses the upsert method.


108-146: LGTM!

The bulkUpsert function is well-structured and includes necessary checks and query setups.

indexer/packages/postgres/src/types/query-types.ts (2)

89-90: LGTM!

The additions to the QueryableField enum are correctly added.


319-323: LGTM!

The LeaderboardPNLQueryConfig interface is well-defined and correctly extends QueryConfig.

indexer/packages/postgres/__tests__/helpers/constants.ts (4)

862-868: LGTM!

The defaultLeaderboardPnlOneDay object is correctly defined with appropriate values.


870-876: LGTM!

The defaultLeaderboardPnl2OneDay object is correctly defined with appropriate values.


878-884: LGTM!

The defaultLeaderboardPnl1AllTime object is correctly defined with appropriate values.


886-892: LGTM!

The defaultLeaderboardPnlOneDayToUpsert object is correctly defined with appropriate values.

Comment on lines 29 to 31
it('Successfully creates a LeaderboardPNL', async () => {
await LeaderboardPNLTable.create(defaultLeaderboardPnlOneDay);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Add assertions to verify the creation.

The test case should include assertions to verify that the LeaderboardPNL record was created successfully.

-  await LeaderboardPNLTable.create(defaultLeaderboardPnlOneDay);
+  const result = await LeaderboardPNLTable.create(defaultLeaderboardPnlOneDay);
+  expect(result).toEqual(expect.objectContaining(defaultLeaderboardPnlOneDay));
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
it('Successfully creates a LeaderboardPNL', async () => {
await LeaderboardPNLTable.create(defaultLeaderboardPnlOneDay);
});
it('Successfully creates a LeaderboardPNL', async () => {
const result = await LeaderboardPNLTable.create(defaultLeaderboardPnlOneDay);
expect(result).toEqual(expect.objectContaining(defaultLeaderboardPnlOneDay));
});

table.integer('rank').notNullable();
table.primary(['subaccountId', 'timeSpan']);
});

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: no new line

Comment on lines 5 to 12
CREATE INDEX CONCURRENTLY IF NOT EXISTS "leaderboard_pnl_rank_timespan_index" ON leaderboard_pnl("rank", "timeSpan");
`);
}

export async function down(knex: Knex): Promise<void> {
await knex.raw(`
DROP INDEX CONCURRENTLY IF EXISTS "leaderboard_pnl_rank_timespan_index";
`);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: spacing

@@ -17,6 +17,7 @@ export { default as TransferModel } from './models/transfer-model';
export { default as TradingRewardModel } from './models/trading-reward-model';
export { default as TradingRewardAggregationModel } from './models/trading-reward-aggregation-model';
export { default as SubaccountUsernamesModel } from './models/subaccount-usernames-model';
export { default as LeaderboardPNLModel } from './models/leaderboard-pnl-model';
Copy link
Contributor

Choose a reason for hiding this comment

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

we refer to pnl in the codebase as Pnl, lets keep that heuristic and rename to LeaderboardPnlModel, and update accordingly for other objects

'timeSpan',
'pnl',
'currentEquity',
'rank'],
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: spacing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

spacing looks right to me?

leaderboardPNLObjects: LeaderboardPNLCreateObject[],
options: Options = { txId: undefined },
): Promise<void> {

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: remove new line

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 8277fa4 and 5ac0e08.

Files selected for processing (10)
  • indexer/packages/postgres/tests/helpers/constants.ts (2 hunks)
  • indexer/packages/postgres/tests/stores/leaderboard-pnl-table.test.ts (1 hunks)
  • indexer/packages/postgres/src/db/migrations/migration_files/20240717160024_create_leaderboard_pnl_table.ts (1 hunks)
  • indexer/packages/postgres/src/db/migrations/migration_files/20240717171246_create_leaderboard_pnl_rank_timespan_index.ts (1 hunks)
  • indexer/packages/postgres/src/index.ts (2 hunks)
  • indexer/packages/postgres/src/models/leaderboard-pnl-model.ts (1 hunks)
  • indexer/packages/postgres/src/stores/leaderboard-pnl-table.ts (1 hunks)
  • indexer/packages/postgres/src/types/db-model-types.ts (1 hunks)
  • indexer/packages/postgres/src/types/leaderboard-pnl-types.ts (1 hunks)
  • indexer/packages/postgres/src/types/query-types.ts (2 hunks)
Files skipped from review as they are similar to previous changes (10)
  • indexer/packages/postgres/tests/helpers/constants.ts
  • indexer/packages/postgres/tests/stores/leaderboard-pnl-table.test.ts
  • indexer/packages/postgres/src/db/migrations/migration_files/20240717160024_create_leaderboard_pnl_table.ts
  • indexer/packages/postgres/src/db/migrations/migration_files/20240717171246_create_leaderboard_pnl_rank_timespan_index.ts
  • indexer/packages/postgres/src/index.ts
  • indexer/packages/postgres/src/models/leaderboard-pnl-model.ts
  • indexer/packages/postgres/src/stores/leaderboard-pnl-table.ts
  • indexer/packages/postgres/src/types/db-model-types.ts
  • indexer/packages/postgres/src/types/leaderboard-pnl-types.ts
  • indexer/packages/postgres/src/types/query-types.ts

Co-authored-by: Christopher-Li <Christopher-Li@users.noreply.github.com>
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 5ac0e08 and 00732f4.

Files selected for processing (3)
  • indexer/packages/postgres/src/db/migrations/migration_files/20240717171246_create_leaderboard_pnl_rank_timespan_index.ts (1 hunks)
  • indexer/packages/postgres/src/index.ts (2 hunks)
  • indexer/packages/postgres/src/models/leaderboard-pnl-model.ts (1 hunks)
Files skipped from review as they are similar to previous changes (3)
  • indexer/packages/postgres/src/db/migrations/migration_files/20240717171246_create_leaderboard_pnl_rank_timespan_index.ts
  • indexer/packages/postgres/src/index.ts
  • indexer/packages/postgres/src/models/leaderboard-pnl-model.ts

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 00732f4 and 1666e98.

Files selected for processing (1)
  • indexer/packages/postgres/src/models/leaderboard-pnl-model.ts (1 hunks)
Additional comments not posted (7)
indexer/packages/postgres/src/models/leaderboard-pnl-model.ts (7)

9-9: Class declaration looks good.

The class LeaderboardPnlModel correctly extends BaseModel.


11-13: Method tableName looks good.

The method correctly returns the table name as leaderboard_pnl.


15-17: Method idColumn looks good.

The method correctly returns the primary key columns as subaccountId and timeSpan.


19-28: Method relationMappings looks good.

The method correctly defines a BelongsToOneRelation with the subaccount model.


30-47: Method jsonSchema looks good.

The method correctly defines the schema validation for the model.


50-60: Properties are correctly defined.

The properties subaccountId, timeSpan, QueryBuilderType, pnl, currentEquity, and rank are correctly defined with appropriate type annotations.


1-7: Ensure all imported modules are used.

Verify that all imported modules are necessary and used in the file.

Verification successful

All imported modules are used correctly.

The path, Model, NumericPattern, UpsertQueryBuilder, and BaseModel modules are all utilized in the leaderboard-pnl-model.ts file.

  • path: Used in modelClass: path.join(__dirname, 'subaccount-model').
  • Model: Used in relation: Model.BelongsToOneRelation.
  • NumericPattern: Used in pnl: { type: 'string', pattern: NumericPattern } and currentEquity: { type: 'string', pattern: NumericPattern }.
  • UpsertQueryBuilder: Used in QueryBuilderType!: UpsertQueryBuilder<this>;.
  • BaseModel: Used in export default class LeaderboardPnlModel extends BaseModel.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that all imported modules are used in the file.

# Test: Search for the usage of each imported module. Expect: Each module should be referenced in the file.
rg --type ts 'path|Model|NumericPattern|UpsertQueryBuilder|BaseModel' indexer/packages/postgres/src/models/leaderboard-pnl-model.ts

Length of output: 680

@affanv14 affanv14 merged commit cdb8a78 into main Jul 18, 2024
11 checks passed
@affanv14 affanv14 deleted the affan/leaderboard-table branch July 18, 2024 20:18
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