-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OTE-543] Add leaderboard pnl table (#1938)
Co-authored-by: Christopher-Li <Christopher-Li@users.noreply.github.com>
- Loading branch information
1 parent
6da7590
commit cdb8a78
Showing
12 changed files
with
415 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
indexer/packages/postgres/__tests__/stores/leaderboard-pnl-table.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { LeaderboardPnlFromDatabase } from '../../src/types'; | ||
import * as LeaderboardPnlTable from '../../src/stores/leaderboard-pnl-table'; | ||
import { clearData, migrate, teardown } from '../../src/helpers/db-helpers'; | ||
import { | ||
defaultLeaderboardPnl2OneDay, | ||
defaultLeaderboardPnlOneDay, | ||
defaultLeaderboardPnl1AllTime, | ||
defaultLeaderboardPnlOneDayToUpsert, | ||
} from '../helpers/constants'; | ||
import { seedData } from '../helpers/mock-generators'; | ||
|
||
describe('LeaderboardPnl store', () => { | ||
beforeEach(async () => { | ||
await seedData(); | ||
}); | ||
|
||
beforeAll(async () => { | ||
await migrate(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await clearData(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await teardown(); | ||
}); | ||
|
||
it('Successfully creates a LeaderboardPnl', async () => { | ||
await LeaderboardPnlTable.create(defaultLeaderboardPnlOneDay); | ||
}); | ||
|
||
it('Successfully creates multiple LeaderboardPnls', async () => { | ||
await Promise.all([ | ||
LeaderboardPnlTable.create(defaultLeaderboardPnlOneDay), | ||
LeaderboardPnlTable.create(defaultLeaderboardPnl2OneDay), | ||
LeaderboardPnlTable.create(defaultLeaderboardPnl1AllTime), | ||
]); | ||
|
||
const leaderboardPnls: LeaderboardPnlFromDatabase[] = await LeaderboardPnlTable.findAll( | ||
{}, | ||
[], | ||
); | ||
|
||
expect(leaderboardPnls.length).toEqual(3); | ||
}); | ||
|
||
it('Successfully finds LeaderboardPnl with subaccountId and timespan', async () => { | ||
await Promise.all([ | ||
LeaderboardPnlTable.create(defaultLeaderboardPnlOneDay), | ||
LeaderboardPnlTable.create(defaultLeaderboardPnl2OneDay), | ||
LeaderboardPnlTable.create(defaultLeaderboardPnl1AllTime), | ||
]); | ||
|
||
const leaderboardPnl: LeaderboardPnlFromDatabase[] = await LeaderboardPnlTable.findAll( | ||
{ | ||
subaccountId: [defaultLeaderboardPnlOneDay.subaccountId], | ||
timeSpan: [defaultLeaderboardPnlOneDay.timeSpan], | ||
}, | ||
[], | ||
); | ||
|
||
expect(leaderboardPnl.length).toEqual(1); | ||
expect(leaderboardPnl[0]).toEqual(expect.objectContaining(defaultLeaderboardPnlOneDay)); | ||
}); | ||
|
||
it('Successfully upserts a LeaderboardPnl', async () => { | ||
await LeaderboardPnlTable.upsert(defaultLeaderboardPnlOneDay); | ||
|
||
await LeaderboardPnlTable.upsert(defaultLeaderboardPnlOneDayToUpsert); | ||
|
||
const leaderboardPnls: LeaderboardPnlFromDatabase[] = await LeaderboardPnlTable.findAll( | ||
{}, | ||
[], | ||
); | ||
|
||
expect(leaderboardPnls.length).toEqual(1); | ||
expect(leaderboardPnls[0]).toEqual( | ||
expect.objectContaining(defaultLeaderboardPnlOneDayToUpsert)); | ||
}); | ||
|
||
it('Successfully bulk upserts LeaderboardPnls', async () => { | ||
await LeaderboardPnlTable.bulkUpsert( | ||
[defaultLeaderboardPnlOneDay, defaultLeaderboardPnl2OneDay]); | ||
|
||
const leaderboardPnls: LeaderboardPnlFromDatabase[] = await LeaderboardPnlTable.findAll( | ||
{}, | ||
[], | ||
); | ||
|
||
expect(leaderboardPnls.length).toEqual(2); | ||
expect(leaderboardPnls[0]).toEqual(expect.objectContaining(defaultLeaderboardPnlOneDay)); | ||
expect(leaderboardPnls[1]).toEqual(expect.objectContaining(defaultLeaderboardPnl2OneDay)); | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
...postgres/src/db/migrations/migration_files/20240717160024_create_leaderboard_pnl_table.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import * as Knex from 'knex'; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
return knex.schema.createTable('leaderboard_pnl', (table) => { | ||
table.uuid('subaccountId').notNullable().references('id').inTable('subaccounts'); | ||
table.enum( | ||
'timeSpan', | ||
[ | ||
'ONE_DAY', | ||
'SEVEN_DAYS', | ||
'THIRTY_DAYS', | ||
'ONE_YEAR', | ||
'ALL_TIME', | ||
], | ||
); | ||
table.string('pnl').notNullable(); | ||
table.string('currentEquity').notNullable(); | ||
table.integer('rank').notNullable(); | ||
table.primary(['subaccountId', 'timeSpan']); | ||
}); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
return knex.schema.dropTable('leaderboard_pnl'); | ||
} |
17 changes: 17 additions & 0 deletions
17
...b/migrations/migration_files/20240717171246_create_leaderboard_pnl_rank_timespan_index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as Knex from 'knex'; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
await knex.raw(` | ||
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"; | ||
`); | ||
} | ||
|
||
export const config = { | ||
transaction: false, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
indexer/packages/postgres/src/models/leaderboard-pnl-model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import path from 'path'; | ||
|
||
import { Model } from 'objection'; | ||
|
||
import { NumericPattern } from '../lib/validators'; | ||
import UpsertQueryBuilder from '../query-builders/upsert'; | ||
import BaseModel from './base-model'; | ||
|
||
export default class LeaderboardPnlModel extends BaseModel { | ||
|
||
static get tableName() { | ||
return 'leaderboard_pnl'; | ||
} | ||
|
||
static get idColumn() { | ||
return ['subaccountId', 'timeSpan']; | ||
} | ||
|
||
static relationMappings = { | ||
subaccount: { | ||
relation: Model.BelongsToOneRelation, | ||
modelClass: path.join(__dirname, 'subaccount-model'), | ||
join: { | ||
from: 'leaderboard_pnl.subaccountId', | ||
to: 'subaccounts.id', | ||
}, | ||
}, | ||
}; | ||
|
||
static get jsonSchema() { | ||
return { | ||
type: 'object', | ||
required: [ | ||
'subaccountId', | ||
'timeSpan', | ||
'pnl', | ||
'currentEquity', | ||
'rank', | ||
], | ||
properties: { | ||
subaccountId: { type: 'string' }, | ||
timeSpan: { type: 'string' }, | ||
pnl: { type: 'string', pattern: NumericPattern }, | ||
currentEquity: { type: 'string', pattern: NumericPattern }, | ||
rank: { type: 'integer' }, | ||
}, | ||
}; | ||
} | ||
|
||
subaccountId!: string; | ||
|
||
timeSpan!: string; | ||
|
||
QueryBuilderType!: UpsertQueryBuilder<this>; | ||
|
||
pnl!: string; | ||
|
||
currentEquity!: string; | ||
|
||
rank!: number; | ||
} |
Oops, something went wrong.