Code review fixes for futures-odds page

- Remove unused upsertParticipantEVWithNormalization import
- Remove debug console.log statements left in save action
- Move normalizeName to module level (pure function, no component deps)
- Pre-compute normalized participant names once in findParticipantMatch
  instead of re-normalizing across three separate passes
- Fix array index key on unmatched list to use composite string key
- Drop unused result variable from upsertParticipantEV call

https://claude.ai/code/session_01JvgcqKSJZ36bEztfg6CkWP
This commit is contained in:
Claude 2026-02-19 22:30:28 +00:00
parent 7ce7b04fca
commit 82e501d6a5
No known key found for this signature in database

View file

@ -19,10 +19,7 @@ import {
calculateICMFromOdds, calculateICMFromOdds,
icmResultToArray, icmResultToArray,
} from '~/services/icm-calculator'; } from '~/services/icm-calculator';
import { import { upsertParticipantEV } from '~/models/participant-expected-value';
upsertParticipantEV,
upsertParticipantEVWithNormalization,
} from '~/models/participant-expected-value';
import { Loader2, Info, CheckCircle2, AlertCircle } from 'lucide-react'; import { Loader2, Info, CheckCircle2, AlertCircle } from 'lucide-react';
export async function loader({ params }: Route.LoaderArgs) { export async function loader({ params }: Route.LoaderArgs) {
@ -133,13 +130,6 @@ export async function action({ request, params }: Route.ActionArgs) {
// Save probabilities from preview (passed through form) // Save probabilities from preview (passed through form)
// This ensures saved values match exactly what user saw in preview // This ensures saved values match exactly what user saw in preview
for (const { participantId, odds } of futuresOdds) { for (const { participantId, odds } of futuresOdds) {
// Debug: check what's in form data
const probFirstRaw = formData.get(`prob_first_${participantId}`);
const probSecondRaw = formData.get(`prob_second_${participantId}`);
console.log(`[Futures Odds Save DEBUG] Looking for participant ${participantId}`);
console.log(` prob_first_${participantId} = ${probFirstRaw} (type: ${typeof probFirstRaw})`);
console.log(` prob_second_${participantId} = ${probSecondRaw} (type: ${typeof probSecondRaw})`);
// Read probabilities from form data (preview results) // Read probabilities from form data (preview results)
const probFirst = parseFloat(formData.get(`prob_first_${participantId}`) as string) || 0; const probFirst = parseFloat(formData.get(`prob_first_${participantId}`) as string) || 0;
const probSecond = parseFloat(formData.get(`prob_second_${participantId}`) as string) || 0; const probSecond = parseFloat(formData.get(`prob_second_${participantId}`) as string) || 0;
@ -150,11 +140,9 @@ export async function action({ request, params }: Route.ActionArgs) {
const probSeventh = parseFloat(formData.get(`prob_seventh_${participantId}`) as string) || 0; const probSeventh = parseFloat(formData.get(`prob_seventh_${participantId}`) as string) || 0;
const probEighth = parseFloat(formData.get(`prob_eighth_${participantId}`) as string) || 0; const probEighth = parseFloat(formData.get(`prob_eighth_${participantId}`) as string) || 0;
console.log(`[Futures Odds Save] ${participantId}: P(1st)=${probFirst}, P(2nd)=${probSecond}, P(3rd)=${probThird}, P(4th)=${probFourth}, P(5th)=${probFifth}, P(6th)=${probSixth}, P(7th)=${probSeventh}, P(8th)=${probEighth}, sum=${probFirst+probSecond+probThird+probFourth+probFifth+probSixth+probSeventh+probEighth}`);
// Use upsertParticipantEV (not WithNormalization) because ICM probabilities // Use upsertParticipantEV (not WithNormalization) because ICM probabilities
// are already correct - they sum to <1.0 for teams unlikely to finish top 8 // are already correct - they sum to <1.0 for teams unlikely to finish top 8
const result = await upsertParticipantEV({ await upsertParticipantEV({
participantId, participantId,
sportsSeasonId: sportsSeasonId, sportsSeasonId: sportsSeasonId,
probabilities: { probabilities: {
@ -171,11 +159,8 @@ export async function action({ request, params }: Route.ActionArgs) {
source: 'futures_odds', source: 'futures_odds',
sourceOdds: odds, sourceOdds: odds,
}); });
console.log(`[Futures Odds Save] Saved ${participantId}: DB probFirst=${result.probFirst}, EV=${result.expectedValue}`);
} }
console.log('[Futures Odds Save] All probabilities saved, redirecting...');
return redirect(`/admin/sports-seasons/${sportsSeasonId}/expected-values`); return redirect(`/admin/sports-seasons/${sportsSeasonId}/expected-values`);
} }
@ -189,6 +174,10 @@ export async function action({ request, params }: Route.ActionArgs) {
} }
} }
function normalizeName(name: string): string {
return name.toLowerCase().replace(/[^a-z0-9\s]/g, '').replace(/\s+/g, ' ').trim();
}
export default function AdminSportsSeasonFuturesOdds() { export default function AdminSportsSeasonFuturesOdds() {
const { sportsSeason, participants, existingOdds } = useLoaderData<typeof loader>(); const { sportsSeason, participants, existingOdds } = useLoaderData<typeof loader>();
const actionData = useActionData<ActionData>(); const actionData = useActionData<ActionData>();
@ -213,33 +202,28 @@ export default function AdminSportsSeasonFuturesOdds() {
unmatched: Array<{ inputName: string; odds: number }>; unmatched: Array<{ inputName: string; odds: number }>;
} | null>(null); } | null>(null);
function normalizeName(name: string): string {
return name.toLowerCase().replace(/[^a-z0-9\s]/g, '').replace(/\s+/g, ' ').trim();
}
function findParticipantMatch(inputName: string) { function findParticipantMatch(inputName: string) {
const normalizedInput = normalizeName(inputName); const normalizedInput = normalizeName(inputName);
// Pre-compute normalized names once for all three passes
const normalized = participants.map(p => ({ p, n: normalizeName(p.name) }));
// Exact match // Exact match
let match = participants.find(p => normalizeName(p.name) === normalizedInput); const exact = normalized.find(({ n }) => n === normalizedInput);
if (match) return match; if (exact) return exact.p;
// Contains match (one contains the other) // Contains match (one contains the other)
match = participants.find(p => { const contains = normalized.find(({ n }) => n.includes(normalizedInput) || normalizedInput.includes(n));
const normalizedP = normalizeName(p.name); if (contains) return contains.p;
return normalizedP.includes(normalizedInput) || normalizedInput.includes(normalizedP);
});
if (match) return match;
// Word-overlap match (at least half the significant words overlap) // Word-overlap match (at least half the significant words overlap)
const inputWords = normalizedInput.split(' ').filter(w => w.length > 2); const inputWords = normalizedInput.split(' ').filter(w => w.length > 2);
match = participants.find(p => { const overlap = normalized.find(({ n }) => {
const pWords = normalizeName(p.name).split(' ').filter(w => w.length > 2); const pWords = n.split(' ').filter(w => w.length > 2);
const overlap = inputWords.filter(w => pWords.includes(w)); const shared = inputWords.filter(w => pWords.includes(w));
return overlap.length > 0 && overlap.length >= Math.min(inputWords.length, pWords.length) * 0.5; return shared.length > 0 && shared.length >= Math.min(inputWords.length, pWords.length) * 0.5;
}); });
return match || null; return overlap?.p ?? null;
} }
function parseBulkText() { function parseBulkText() {
@ -349,7 +333,7 @@ export default function AdminSportsSeasonFuturesOdds() {
</div> </div>
<div className="rounded-md border border-amber-200 bg-amber-50 divide-y divide-amber-100 text-sm"> <div className="rounded-md border border-amber-200 bg-amber-50 divide-y divide-amber-100 text-sm">
{parseResults.unmatched.map((u, i) => ( {parseResults.unmatched.map((u, i) => (
<div key={i} className="flex justify-between px-3 py-1.5"> <div key={`${u.inputName}-${i}`} className="flex justify-between px-3 py-1.5">
<span>{u.inputName}</span> <span>{u.inputName}</span>
<span className="font-medium">{u.odds > 0 ? '+' : ''}{u.odds}</span> <span className="font-medium">{u.odds > 0 ? '+' : ''}{u.odds}</span>
</div> </div>