From e657611d3a890559dc013684f3b67c6a8e457421 Mon Sep 17 00:00:00 2001 From: Ian Philips Date: Fri, 29 Nov 2024 08:57:34 -0800 Subject: [PATCH] Fix mertrics on mc --- .../src/get-user-contract-metrics-with-contracts.ts | 7 ++++++- common/src/calculate-metrics.ts | 11 +++++------ web/hooks/use-saved-contract-metrics.ts | 5 +---- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/backend/api/src/get-user-contract-metrics-with-contracts.ts b/backend/api/src/get-user-contract-metrics-with-contracts.ts index 9bc77ea0c4..6d9db51178 100644 --- a/backend/api/src/get-user-contract-metrics-with-contracts.ts +++ b/backend/api/src/get-user-contract-metrics-with-contracts.ts @@ -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({ @@ -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, diff --git a/common/src/calculate-metrics.ts b/common/src/calculate-metrics.ts index 5225f0da63..87bba3329b 100644 --- a/common/src/calculate-metrics.ts +++ b/common/src/calculate-metrics.ts @@ -555,7 +555,7 @@ export const calculateUpdatedMetricsForContracts = ( metrics: ContractMetric[] }[] ) => { - const metricsByContract: Dictionary> = {} + const metricsByContract: Dictionary[]> = {} const contracts: Contract[] = [] for (const { contract, metrics } of contractsWithMetrics) { @@ -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 @@ -595,7 +594,7 @@ export const calculateUpdatedMetricsForContracts = ( updatedAnswerMetrics.forEach((m) => applyMetricToSummary(m, summaryMetric, true) ) - metricsByContract[contractId] = summaryMetric + metricsByContract[contractId] = [...updatedAnswerMetrics, summaryMetric] } } diff --git a/web/hooks/use-saved-contract-metrics.ts b/web/hooks/use-saved-contract-metrics.ts index 1cc5b32b79..aa2b48c90a 100644 --- a/web/hooks/use-saved-contract-metrics.ts +++ b/web/hooks/use-saved-contract-metrics.ts @@ -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 () => {