Skip to content

Commit

Permalink
Fix mertrics on mc
Browse files Browse the repository at this point in the history
  • Loading branch information
IanPhilips committed Nov 29, 2024
1 parent 1323201 commit e657611
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
7 changes: 6 additions & 1 deletion backend/api/src/get-user-contract-metrics-with-contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createSupabaseDirectClient } from 'shared/supabase/init'
import { ContractMetric } from 'common/contract-metric'
import { Contract } from 'common/contract'
import { calculateUpdatedMetricsForContracts } from 'common/calculate-metrics'
import { mapValues } from 'lodash'

const bodySchema = z
.object({
Expand Down Expand Up @@ -42,7 +43,11 @@ export const getusercontractmetricswithcontracts = MaybeAuthedEndpoint(
(row) => row as { contract: Contract; metrics: ContractMetric[] }
)

const { metricsByContract } = calculateUpdatedMetricsForContracts(results)
const { metricsByContract: allMetrics } =
calculateUpdatedMetricsForContracts(results)
const metricsByContract = mapValues(allMetrics, (metrics) =>
metrics.find((m) => m.answerId === null)
)

return {
metricsByContract,
Expand Down
11 changes: 5 additions & 6 deletions common/src/calculate-metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ export const calculateUpdatedMetricsForContracts = (
metrics: ContractMetric[]
}[]
) => {
const metricsByContract: Dictionary<Omit<ContractMetric, 'id'>> = {}
const metricsByContract: Dictionary<Omit<ContractMetric, 'id'>[]> = {}
const contracts: Contract[] = []

for (const { contract, metrics } of contractsWithMetrics) {
Expand All @@ -567,10 +567,9 @@ export const calculateUpdatedMetricsForContracts = (
// For binary markets, update metrics with current probability
const metric = metrics.find((m) => m.answerId === null)
if (metric) {
metricsByContract[contractId] = calculateProfitMetricsWithProb(
contract.prob,
metric
)
metricsByContract[contractId] = [
calculateProfitMetricsWithProb(contract.prob, metric),
]
}
} else if (contract.mechanism === 'cpmm-multi-1') {
// For multiple choice markets, update each answer's metrics and compute summary
Expand All @@ -595,7 +594,7 @@ export const calculateUpdatedMetricsForContracts = (
updatedAnswerMetrics.forEach((m) =>
applyMetricToSummary(m, summaryMetric, true)
)
metricsByContract[contractId] = summaryMetric
metricsByContract[contractId] = [...updatedAnswerMetrics, summaryMetric]
}
}

Expand Down
5 changes: 1 addition & 4 deletions web/hooks/use-saved-contract-metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ export const useSavedContractMetrics = (
const { metricsByContract } = calculateUpdatedMetricsForContracts([
{ contract, metrics },
])
return metrics.map((metric) => ({
...metricsByContract[metric.contractId],
id: metric.id,
}))
return metricsByContract[contract.id] as ContractMetric[]
}

const refreshMyMetrics = useEvent(async () => {
Expand Down

0 comments on commit e657611

Please sign in to comment.