Skip to content

Commit

Permalink
Recalculate user portfolios prior to mana supply
Browse files Browse the repository at this point in the history
  • Loading branch information
IanPhilips committed Jul 9, 2024
1 parent 5dbc839 commit e443eef
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion backend/api/src/get-mana-supply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { getManaSupply as fetchManaSupply } from 'shared/mana-supply'
import { APIHandler } from './helpers/endpoint'

export const getManaSupply: APIHandler<'get-mana-supply'> = async () => {
return await fetchManaSupply()
return await fetchManaSupply(false)
}
3 changes: 2 additions & 1 deletion backend/scheduler/src/jobs/update-stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,21 +220,22 @@ export const updateStatsCore = async () => {
.valueOf()

log('Fetching data for stats update...')
await calculateManaStats(startOfDayAgo, 1)
const [
dailyBets,
dailyContracts,
dailyComments,
dailyNewUsers,
dailyManaSales,
feedConversionScores,
_,
] = await Promise.all([
getDailyBets(pg, start, numberOfDays),
getDailyContracts(pg, start, numberOfDays),
getDailyComments(pg, start, numberOfDays),
getDailyNewUsers(pg, start, numberOfDays),
getSales(pg, start, numberOfDays),
getFeedConversionScores(pg, start, numberOfDays),
calculateManaStats(startOfDayAgo, 1),
])
logMemory()

Expand Down
2 changes: 1 addition & 1 deletion backend/scripts/get-mana-supply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getManaSupply } from 'shared/mana-supply'
if (require.main === module) {
runScript(async () => {
log('Getting mana supply...')
const manaSupply = await getManaSupply()
const manaSupply = await getManaSupply(false)
console.log(manaSupply)
})
}
2 changes: 1 addition & 1 deletion backend/shared/src/calculate-mana-stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const calculateManaStats = async (
)
}
const now = new Date().toISOString()
const ms = await getManaSupply()
const ms = await getManaSupply(true)
await pg.none(
`insert into mana_supply_stats (start_time, end_time, total_value, balance, spice_balance, investment_value, loan_total, amm_liquidity)
values ($1, $2, $3, $4, $5, $6, $7, $8)`,
Expand Down
25 changes: 24 additions & 1 deletion backend/shared/src/mana-supply.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
import { createSupabaseDirectClient } from 'shared/supabase/init'
import { chunk } from 'lodash'
import { updateUserMetricsCore } from 'shared/update-user-metrics-core'
import { log } from 'shared/utils'

export const getManaSupply = async () => {
export const getManaSupply = async (recalculateAllUserPortfolios: boolean) => {
const pg = createSupabaseDirectClient()
if (recalculateAllUserPortfolios) {
const allBetUserIds = await pg.map(
`
select distinct u.id from users u
join user_contract_metrics ucm on u.id = ucm.user_id
join contracts c on ucm.contract_id = c.id
where u.data->'lastBetTime' is not null
and c.resolution_time is null;
`,
[],
(r) => r.id as string
)
const chunks = chunk(allBetUserIds, 1000)
let processed = 0
for (const userIds of chunks) {
await updateUserMetricsCore(userIds)
processed += userIds.length
log(`Processed ${processed} of ${allBetUserIds.length} users`)
}
}
const userPortfolio = await pg.one(
`
select
Expand Down

0 comments on commit e443eef

Please sign in to comment.