-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from casesandberg/feature/counts-on-market-and…
…-options Counts on market and options
- Loading branch information
Showing
32 changed files
with
432 additions
and
217 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
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
8 changes: 8 additions & 0 deletions
8
packages/database/migrations/20240904184941_cache_common_market_values/migration.sql
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,8 @@ | ||
-- AlterTable | ||
ALTER TABLE "Market" ADD COLUMN "commentCount" DECIMAL(65,30), | ||
ADD COLUMN "liquidityCount" DECIMAL(65,30), | ||
ADD COLUMN "uniquePromotersCount" DECIMAL(65,30), | ||
ADD COLUMN "uniqueTradersCount" DECIMAL(65,30); | ||
|
||
-- AlterTable | ||
ALTER TABLE "MarketOption" ADD COLUMN "probability" DECIMAL(65,30); |
18 changes: 18 additions & 0 deletions
18
...es/database/migrations/20240904224051_cache_common_market_values_as_numbers/migration.sql
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,18 @@ | ||
/* | ||
Warnings: | ||
- You are about to alter the column `commentCount` on the `Market` table. The data in that column could be lost. The data in that column will be cast from `Decimal(65,30)` to `Integer`. | ||
- You are about to alter the column `liquidityCount` on the `Market` table. The data in that column could be lost. The data in that column will be cast from `Decimal(65,30)` to `Integer`. | ||
- You are about to alter the column `uniquePromotersCount` on the `Market` table. The data in that column could be lost. The data in that column will be cast from `Decimal(65,30)` to `Integer`. | ||
- You are about to alter the column `uniqueTradersCount` on the `Market` table. The data in that column could be lost. The data in that column will be cast from `Decimal(65,30)` to `Integer`. | ||
- You are about to alter the column `probability` on the `MarketOption` table. The data in that column could be lost. The data in that column will be cast from `Decimal(65,30)` to `Integer`. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "Market" ALTER COLUMN "commentCount" SET DATA TYPE INTEGER, | ||
ALTER COLUMN "liquidityCount" SET DATA TYPE INTEGER, | ||
ALTER COLUMN "uniquePromotersCount" SET DATA TYPE INTEGER, | ||
ALTER COLUMN "uniqueTradersCount" SET DATA TYPE INTEGER; | ||
|
||
-- AlterTable | ||
ALTER TABLE "MarketOption" ALTER COLUMN "probability" SET DATA TYPE INTEGER; |
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
186 changes: 186 additions & 0 deletions
186
packages/database/scripts/backfill-counts-on-markets.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,186 @@ | ||
import { getMarketBalances } from '@play-money/finance/lib/getBalances' | ||
import { updateMarketOptionProbabilities } from '@play-money/markets/lib/updateMarketOptionProbabilities' | ||
import db from '../prisma' | ||
|
||
async function main() { | ||
try { | ||
const marketsWithoutProbabilities = await db.market.findMany({ | ||
where: { | ||
options: { | ||
some: { | ||
probability: null, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
console.log(`Found ${marketsWithoutProbabilities.length} markets without probabilities.`) | ||
|
||
for await (const market of marketsWithoutProbabilities) { | ||
try { | ||
const balances = await getMarketBalances({ marketId: market.id, accountId: market.ammAccountId }) | ||
|
||
await db.$transaction(async (tx) => { | ||
await updateMarketOptionProbabilities({ tx, balances, marketId: market.id }) | ||
}) | ||
console.log(`Successfully added probabilities to market with id: ${market.id}`) | ||
} catch (updateError) { | ||
const error = updateError as Error | ||
console.error(`Failed to add probabilities to id: ${market.id}. Error: ${error.message}`) | ||
} | ||
} | ||
|
||
const marketsWithoutLiquidity = await db.market.findMany({ | ||
where: { | ||
liquidityCount: null, | ||
}, | ||
}) | ||
|
||
console.log(`Found ${marketsWithoutLiquidity.length} markets without liquidity count.`) | ||
|
||
for await (const market of marketsWithoutLiquidity) { | ||
try { | ||
const data = await db.transactionEntry.aggregate({ | ||
_sum: { | ||
amount: true, | ||
}, | ||
where: { | ||
toAccountId: market.clearingAccountId, | ||
assetType: 'CURRENCY', | ||
assetId: 'PRIMARY', | ||
transaction: { | ||
marketId: market.id, | ||
}, | ||
}, | ||
}) | ||
|
||
await db.market.update({ | ||
where: { | ||
id: market.id, | ||
}, | ||
data: { | ||
liquidityCount: data._sum.amount?.toNumber(), | ||
}, | ||
}) | ||
console.log(`Successfully added liquidity count to market with id: ${market.id}`) | ||
} catch (updateError) { | ||
const error = updateError as Error | ||
console.error(`Failed to add liquidity count to id: ${market.id}. Error: ${error.message}`) | ||
} | ||
} | ||
|
||
const marketsWithoutUniqueTraders = await db.market.findMany({ | ||
where: { | ||
uniqueTradersCount: null, | ||
}, | ||
}) | ||
|
||
console.log(`Found ${marketsWithoutUniqueTraders.length} markets without unique traders count.`) | ||
|
||
for await (const market of marketsWithoutUniqueTraders) { | ||
try { | ||
const data = await db.transaction.groupBy({ | ||
by: ['initiatorId'], | ||
where: { | ||
marketId: market.id, | ||
type: 'TRADE_BUY', | ||
}, | ||
_count: true, | ||
}) | ||
|
||
await db.market.update({ | ||
where: { | ||
id: market.id, | ||
}, | ||
data: { | ||
uniqueTradersCount: data.length, | ||
}, | ||
}) | ||
console.log(`Successfully added unique traders count to market with id: ${market.id}`) | ||
} catch (updateError) { | ||
const error = updateError as Error | ||
console.error(`Failed to add unique traders count to id: ${market.id}. Error: ${error.message}`) | ||
} | ||
} | ||
|
||
const marketsWithoutUniquePromoters = await db.market.findMany({ | ||
where: { | ||
uniquePromotersCount: null, | ||
}, | ||
}) | ||
|
||
console.log(`Found ${marketsWithoutUniquePromoters.length} markets without unique promoters count.`) | ||
|
||
for await (const market of marketsWithoutUniquePromoters) { | ||
try { | ||
const data = await db.transaction.groupBy({ | ||
by: ['initiatorId'], | ||
where: { | ||
marketId: market.id, | ||
type: { | ||
in: ['LIQUIDITY_DEPOSIT', 'LIQUIDITY_INITIALIZE'], | ||
}, | ||
}, | ||
_count: true, | ||
}) | ||
|
||
await db.market.update({ | ||
where: { | ||
id: market.id, | ||
}, | ||
data: { | ||
uniquePromotersCount: data.length, | ||
}, | ||
}) | ||
console.log(`Successfully added unique promoters count to market with id: ${market.id}`) | ||
} catch (updateError) { | ||
const error = updateError as Error | ||
console.error(`Failed to add unique promoters count to id: ${market.id}. Error: ${error.message}`) | ||
} | ||
} | ||
|
||
const marketsWithoutCommentsCount = await db.market.findMany({ | ||
where: { | ||
commentCount: null, | ||
}, | ||
}) | ||
|
||
console.log(`Found ${marketsWithoutCommentsCount.length} markets without comments count.`) | ||
|
||
for await (const market of marketsWithoutCommentsCount) { | ||
try { | ||
const data = await db.comment.aggregate({ | ||
where: { | ||
entityId: market.id, | ||
}, | ||
_count: true, | ||
}) | ||
|
||
await db.market.update({ | ||
where: { | ||
id: market.id, | ||
}, | ||
data: { | ||
commentCount: data._count, | ||
}, | ||
}) | ||
console.log(`Successfully added comments count to market with id: ${market.id}`) | ||
} catch (updateError) { | ||
const error = updateError as Error | ||
console.error(`Failed to add comments count to id: ${market.id}. Error: ${error.message}`) | ||
} | ||
} | ||
} catch (fetchError) { | ||
const error = fetchError as Error | ||
console.error(`An error occurred while fetching markets: ${error.message}`) | ||
} finally { | ||
await db.$disconnect() | ||
console.log('Database connection closed.') | ||
} | ||
} | ||
|
||
main().catch((e) => { | ||
const error = e as Error | ||
console.error(`Unexpected error: ${error.message}`) | ||
process.exit(1) | ||
}) |
2 changes: 1 addition & 1 deletion
2
packages/database/zod/inputTypeSchemas/MarketOptionScalarFieldEnumSchema.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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { z } from 'zod'; | ||
|
||
export const MarketOptionScalarFieldEnumSchema = z.enum(['id','name','marketId','color','liquidityProbability','createdAt','updatedAt']); | ||
export const MarketOptionScalarFieldEnumSchema = z.enum(['id','name','marketId','color','liquidityProbability','createdAt','updatedAt','probability']); | ||
|
||
export default MarketOptionScalarFieldEnumSchema; |
2 changes: 1 addition & 1 deletion
2
packages/database/zod/inputTypeSchemas/MarketScalarFieldEnumSchema.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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { z } from 'zod'; | ||
|
||
export const MarketScalarFieldEnumSchema = z.enum(['id','question','description','slug','closeDate','resolvedAt','createdBy','tags','ammAccountId','clearingAccountId','createdAt','updatedAt']); | ||
export const MarketScalarFieldEnumSchema = z.enum(['id','question','description','slug','closeDate','resolvedAt','createdBy','tags','ammAccountId','clearingAccountId','createdAt','updatedAt','commentCount','uniqueTradersCount','uniquePromotersCount','liquidityCount']); | ||
|
||
export default MarketScalarFieldEnumSchema; |
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
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
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
Oops, something went wrong.