Skip to content

Commit 4da8125

Browse files
feat: migrate to new opening logging in the backend
1 parent c062cf6 commit 4da8125

File tree

3 files changed

+58
-126
lines changed

3 files changed

+58
-126
lines changed

src/api/opening/opening.ts

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,20 @@
11
import { buildUrl } from '../utils'
22

33
// API Types for opening drill logging
4-
export interface OpeningDrillSelection {
4+
5+
export interface LogOpeningDrillRequest {
56
opening_fen: string
67
side_played: string
7-
}
8-
9-
export interface SelectOpeningDrillsRequest {
10-
openings: OpeningDrillSelection[]
118
opponent: string
129
num_moves: number
13-
num_drills: number
14-
}
15-
16-
export interface SelectOpeningDrillsResponse {
17-
session_id: string
18-
}
19-
20-
export interface SubmitOpeningDrillRequest {
21-
session_id: string
22-
opening_fen: string
23-
side_played: string
2410
moves_played_uci: string[]
2511
}
2612

27-
// API function to log opening drill selections and start a session
28-
export const selectOpeningDrills = async (
29-
request: SelectOpeningDrillsRequest,
30-
): Promise<SelectOpeningDrillsResponse> => {
31-
const res = await fetch(buildUrl('opening/select_opening_drills'), {
32-
method: 'POST',
33-
headers: {
34-
Accept: 'application/json',
35-
'Content-Type': 'application/json',
36-
},
37-
body: JSON.stringify(request),
38-
})
39-
40-
if (res.status === 401) {
41-
throw new Error('Unauthorized')
42-
}
43-
44-
if (!res.ok) {
45-
throw new Error(`Failed to select opening drills: ${res.statusText}`)
46-
}
47-
48-
const data = await res.json()
49-
return data as SelectOpeningDrillsResponse
50-
}
51-
52-
// API function to submit a completed opening drill
53-
export const submitOpeningDrill = async (
54-
request: SubmitOpeningDrillRequest,
13+
// API function to log a completed opening drill
14+
export const logOpeningDrill = async (
15+
request: LogOpeningDrillRequest,
5516
): Promise<void> => {
56-
const res = await fetch(buildUrl('opening/record_opening_drill'), {
17+
const res = await fetch(buildUrl('opening/log_opening_drill'), {
5718
method: 'POST',
5819
headers: {
5920
Accept: 'application/json',
@@ -67,6 +28,6 @@ export const submitOpeningDrill = async (
6728
}
6829

6930
if (!res.ok) {
70-
throw new Error(`Failed to submit opening drill: ${res.statusText}`)
31+
throw new Error(`Failed to log opening drill: ${res.statusText}`)
7132
}
7233
}

src/components/Openings/OpeningSelectionModal.tsx

Lines changed: 29 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
trackDrillConfigurationCompleted,
2424
} from 'src/lib/analytics'
2525
import { MAIA_MODELS_WITH_NAMES } from 'src/constants/common'
26-
import { selectOpeningDrills } from 'src/api/opening'
2726

2827
type MobileTab = 'browse' | 'selected'
2928

@@ -1073,66 +1072,38 @@ export const OpeningSelectionModal: React.FC<Props> = ({
10731072
if (selections.length > 0) {
10741073
const drillSequence = generateDrillSequence(selections, drillCount)
10751074

1076-
try {
1077-
// Prepare API request data
1078-
const openings = selections.map((selection) => ({
1079-
opening_fen: selection.variation
1080-
? selection.variation.fen
1081-
: selection.opening.fen,
1082-
side_played: selection.playerColor,
1083-
}))
1084-
1085-
// Call the backend API to log opening selections and get session ID
1086-
const response = await selectOpeningDrills({
1087-
openings,
1088-
opponent: selectedMaiaVersion.id,
1089-
num_moves: targetMoveNumber,
1090-
num_drills: drillCount,
1091-
})
1092-
1093-
const configuration: DrillConfiguration = {
1094-
selections,
1095-
drillCount,
1096-
drillSequence,
1097-
sessionId: response.session_id,
1098-
}
1075+
const configuration: DrillConfiguration = {
1076+
selections,
1077+
drillCount,
1078+
drillSequence,
1079+
}
10991080

1100-
// Track drill configuration completion
1101-
const uniqueOpenings = new Set(selections.map((s) => s.opening.id)).size
1102-
const averageTargetMoves =
1103-
selections.reduce((sum, s) => sum + s.targetMoveNumber, 0) /
1104-
selections.length
1105-
const maiaVersionsUsed = [
1106-
...new Set(selections.map((s) => s.maiaVersion)),
1107-
]
1108-
const colorDistribution = selections.reduce(
1109-
(acc, s) => {
1110-
acc[s.playerColor]++
1111-
return acc
1112-
},
1113-
{ white: 0, black: 0 },
1114-
)
1081+
// Track drill configuration completion
1082+
const uniqueOpenings = new Set(selections.map((s) => s.opening.id)).size
1083+
const averageTargetMoves =
1084+
selections.reduce((sum, s) => sum + s.targetMoveNumber, 0) /
1085+
selections.length
1086+
const maiaVersionsUsed = [
1087+
...new Set(selections.map((s) => s.maiaVersion)),
1088+
]
1089+
const colorDistribution = selections.reduce(
1090+
(acc, s) => {
1091+
acc[s.playerColor]++
1092+
return acc
1093+
},
1094+
{ white: 0, black: 0 },
1095+
)
11151096

1116-
trackDrillConfigurationCompleted(
1117-
selections.length,
1118-
drillCount,
1119-
uniqueOpenings,
1120-
averageTargetMoves,
1121-
maiaVersionsUsed,
1122-
colorDistribution,
1123-
)
1097+
trackDrillConfigurationCompleted(
1098+
selections.length,
1099+
drillCount,
1100+
uniqueOpenings,
1101+
averageTargetMoves,
1102+
maiaVersionsUsed,
1103+
colorDistribution,
1104+
)
11241105

1125-
onComplete(configuration)
1126-
} catch (error) {
1127-
console.error('Failed to start drilling session:', error)
1128-
// Still allow the drill to start even if API call fails
1129-
const configuration: DrillConfiguration = {
1130-
selections,
1131-
drillCount,
1132-
drillSequence,
1133-
}
1134-
onComplete(configuration)
1135-
}
1106+
onComplete(configuration)
11361107
}
11371108
}
11381109

src/hooks/useOpeningDrillController/useOpeningDrillController.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useState, useMemo, useCallback, useEffect, useRef } from 'react'
22
import { Chess } from 'chess.ts'
33
import { getGameMove } from 'src/api/play/play'
4-
import { submitOpeningDrill } from 'src/api/opening'
4+
import { logOpeningDrill } from 'src/api/opening'
55
import { useTreeController } from '../useTreeController'
66
import { useLocalStorage } from '../useLocalStorage'
77
import {
@@ -530,21 +530,20 @@ export const useOpeningDrillController = (
530530
currentMove: 'Preparing analysis...',
531531
})
532532

533-
// Submit drill data to backend if session ID is available
534-
if (configuration.sessionId) {
535-
try {
536-
await submitOpeningDrill({
537-
session_id: configuration.sessionId,
538-
opening_fen: drillGame.selection.variation
539-
? drillGame.selection.variation.fen
540-
: drillGame.selection.opening.fen,
541-
side_played: drillGame.selection.playerColor,
542-
moves_played_uci: drillGame.moves,
543-
})
544-
} catch (error) {
545-
console.error('Failed to submit drill to backend:', error)
546-
// Continue even if backend submission fails
547-
}
533+
// Submit drill data to backend
534+
try {
535+
await logOpeningDrill({
536+
opening_fen: drillGame.selection.variation
537+
? drillGame.selection.variation.fen
538+
: drillGame.selection.opening.fen,
539+
side_played: drillGame.selection.playerColor,
540+
opponent: drillGame.selection.maiaVersion,
541+
num_moves: drillGame.selection.targetMoveNumber,
542+
moves_played_uci: drillGame.moves,
543+
})
544+
} catch (error) {
545+
console.error('Failed to log drill to backend:', error)
546+
// Continue even if backend submission fails
548547
}
549548

550549
// Ensure all positions in the drill are analyzed to sufficient depth
@@ -593,23 +592,24 @@ export const useOpeningDrillController = (
593592
setIsAnalyzingDrill(false)
594593
}
595594
},
596-
[currentDrillGame, evaluateDrillPerformance, configuration.sessionId],
595+
[currentDrillGame, evaluateDrillPerformance],
597596
)
598597

599598
const moveToNextDrill = useCallback(async () => {
600-
// Submit drill data to backend if session ID is available
601-
if (configuration.sessionId && currentDrillGame) {
599+
// Submit drill data to backend
600+
if (currentDrillGame) {
602601
try {
603-
await submitOpeningDrill({
604-
session_id: configuration.sessionId,
602+
await logOpeningDrill({
605603
opening_fen: currentDrillGame.selection.variation
606604
? currentDrillGame.selection.variation.fen
607605
: currentDrillGame.selection.opening.fen,
608606
side_played: currentDrillGame.selection.playerColor,
607+
opponent: currentDrillGame.selection.maiaVersion,
608+
num_moves: currentDrillGame.selection.targetMoveNumber,
609609
moves_played_uci: currentDrillGame.moves,
610610
})
611611
} catch (error) {
612-
console.error('Failed to submit drill to backend:', error)
612+
console.error('Failed to log drill to backend:', error)
613613
}
614614
}
615615

0 commit comments

Comments
 (0)