-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix daily changed profit & optimize scheduled profit periods and hist…
…ories
- Loading branch information
1 parent
314245d
commit c6e6e70
Showing
10 changed files
with
312 additions
and
285 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
backend/api/src/get-daily-changed-metrics-and-contracts.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,81 @@ | ||
import { APIHandler } from './helpers/endpoint' | ||
import { updateUserMetricPeriods } from 'shared/update-user-metric-periods' | ||
import { DAY_MS } from 'common/util/time' | ||
import { getUnresolvedStatsForToken } from 'shared/update-user-portfolio-histories-core' | ||
import { sortBy, uniqBy } from 'lodash' | ||
import { MarketContract } from 'common/contract' | ||
|
||
export const getDailyChangedMetricsAndContracts: APIHandler< | ||
'get-daily-changed-metrics-and-contracts' | ||
> = async (props, auth) => { | ||
const { limit } = props | ||
const userId = auth.uid | ||
|
||
const since = Date.now() - DAY_MS | ||
// First update the user's metrics | ||
const { metricsByUser, contractsById } = await updateUserMetricPeriods( | ||
[userId], | ||
since, | ||
true | ||
) | ||
const manaStats = getUnresolvedStatsForToken( | ||
'MANA', | ||
metricsByUser[userId], | ||
contractsById | ||
) | ||
const cashStats = getUnresolvedStatsForToken( | ||
'CASH', | ||
metricsByUser[userId], | ||
contractsById | ||
) | ||
const manaMetrics = metricsByUser[userId].filter( | ||
(m) => contractsById[m.contractId]?.token === 'MANA' | ||
) | ||
const cashMetrics = metricsByUser[userId].filter( | ||
(m) => contractsById[m.contractId]?.token === 'CASH' | ||
) | ||
const topManaMetrics = sortBy( | ||
manaMetrics, | ||
(m) => -(m.from?.day.profit ?? 0) | ||
).slice(0, limit / 2) | ||
const bottomManaMetrics = sortBy( | ||
manaMetrics, | ||
(m) => m.from?.day.profit ?? 0 | ||
).slice(0, limit / 2) | ||
const topCashMetrics = sortBy( | ||
cashMetrics, | ||
(m) => -(m.from?.day.profit ?? 0) | ||
).slice(0, limit / 2) | ||
const bottomCashMetrics = sortBy( | ||
cashMetrics, | ||
(m) => m.from?.day.profit ?? 0 | ||
).slice(0, limit / 2) | ||
const uniqueContractIds = uniqBy( | ||
[ | ||
...topManaMetrics, | ||
...bottomManaMetrics, | ||
...topCashMetrics, | ||
...bottomCashMetrics, | ||
], | ||
'contractId' | ||
).map((m) => m.contractId) | ||
const contracts = Object.values(contractsById).filter((c) => | ||
uniqueContractIds.includes(c.id) | ||
) as MarketContract[] | ||
|
||
return { | ||
manaMetrics: uniqBy( | ||
topManaMetrics.concat(bottomManaMetrics), | ||
(m) => m.contractId | ||
), | ||
cashMetrics: uniqBy( | ||
topCashMetrics.concat(bottomCashMetrics), | ||
(m) => m.contractId | ||
), | ||
contracts, | ||
manaProfit: manaStats.dailyProfit, | ||
cashProfit: cashStats.dailyProfit, | ||
manaInvestmentValue: manaStats.value, | ||
cashInvestmentValue: cashStats.value, | ||
} | ||
} |
58 changes: 0 additions & 58 deletions
58
backend/api/src/get-user-contract-metrics-with-contracts-direct.ts
This file was deleted.
Oops, something went wrong.
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.