File size: 10,890 Bytes
970eef1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import React, { useState, useEffect, useRef } from "react";
import {
  Box,
  Typography,
  CircularProgress,
  Alert,
  Paper,
  Divider,
  Button,
} from "@mui/material";
import AccessTimeIcon from "@mui/icons-material/AccessTime";
import LogDisplay from "./LogDisplay";
import { useNavigate } from "react-router-dom";

// Evaluation steps
const EVALUATION_STEPS = [
  "preparation",
  "model_evaluation",
  "results_compilation",
];

// Friendly step names for display
const STEP_LABELS = {
  preparation: "Preparation",
  model_evaluation: "Model Evaluation",
  results_compilation: "Results Compilation",
};

/**
 * Component to handle benchmark evaluation and display logs
 *
 * @param {Object} props - Component props
 * @param {string} props.sessionId - Session ID of the benchmark to evaluate
 * @param {Function} props.onComplete - Function to call when evaluation is complete
 * @returns {JSX.Element} Benchmark evaluation component
 */
const BenchmarkEvaluation = ({ sessionId, onComplete }) => {
  const [evaluating, setEvaluating] = useState(false);
  const [evaluationComplete, setEvaluationComplete] = useState(false);
  const [evaluationLogs, setEvaluationLogs] = useState([]);
  const [error, setError] = useState(null);
  const [currentPhase, setCurrentPhase] = useState("initializing");
  const [completedSteps, setCompletedSteps] = useState([]);
  const [activeStep, setActiveStep] = useState(0);
  const [elapsedTime, setElapsedTime] = useState(0);

  const pollingIntervalRef = useRef(null);
  const timerIntervalRef = useRef(null);
  const startTimeRef = useRef(null);

  const navigate = useNavigate();

  // Start evaluation when component mounts
  useEffect(() => {
    // Set start time
    startTimeRef.current = Date.now();

    // Start timer
    timerIntervalRef.current = setInterval(() => {
      const timeElapsed = Math.floor(
        (Date.now() - startTimeRef.current) / 1000
      );
      setElapsedTime(timeElapsed);
    }, 1000);

    startEvaluation();

    // Clean up intervals on unmount
    return () => {
      if (pollingIntervalRef.current) {
        clearInterval(pollingIntervalRef.current);
      }
      if (timerIntervalRef.current) {
        clearInterval(timerIntervalRef.current);
      }
    };
  }, []);

  // Determine current phase and completed steps from logs
  useEffect(() => {
    if (evaluationLogs.length === 0) return;

    // Check all logs for completed steps
    const newCompletedSteps = [...completedSteps];
    let newActiveStep = activeStep;

    evaluationLogs.forEach((log) => {
      // Detect completed steps (format: [SUCCESS] Stage completed: step_name)
      const match = log.match(/\[SUCCESS\] Stage completed: (\w+)/);
      if (match && match[1]) {
        const completedStep = match[1].trim();
        if (
          EVALUATION_STEPS.includes(completedStep) &&
          !newCompletedSteps.includes(completedStep)
        ) {
          newCompletedSteps.push(completedStep);
          // Set active step to index of next step
          const stepIndex = EVALUATION_STEPS.indexOf(completedStep);
          if (stepIndex >= 0 && stepIndex + 1 > newActiveStep) {
            newActiveStep = stepIndex + 1;
            if (newActiveStep >= EVALUATION_STEPS.length) {
              newActiveStep = EVALUATION_STEPS.length;
            }
          }
        }
      }
    });

    // Update state if there are new completed steps
    if (newCompletedSteps.length > completedSteps.length) {
      setCompletedSteps(newCompletedSteps);
      setActiveStep(newActiveStep);
    }

    // Check recent logs to determine current phase
    const recentLogs = evaluationLogs.slice(-10);

    // Detect completion conditions
    const isComplete =
      recentLogs.some((log) =>
        log.includes("[SUCCESS] Evaluation completed")
      ) ||
      completedSteps.includes("results_compilation") ||
      newCompletedSteps.includes("results_compilation");

    if (isComplete) {
      setCurrentPhase("complete");
      setEvaluationComplete(true);
      // Stop polling when evaluation is complete
      if (pollingIntervalRef.current) {
        clearInterval(pollingIntervalRef.current);
      }
      if (timerIntervalRef.current) {
        clearInterval(timerIntervalRef.current);
      }
      // Notify parent component that evaluation is complete
      if (onComplete) {
        onComplete({
          success: true,
          sessionId,
          logs: evaluationLogs,
        });
      }
    } else if (recentLogs.some((log) => log.includes("Comparing models"))) {
      setCurrentPhase("compiling_results");
    } else if (recentLogs.some((log) => log.includes("Starting evaluations"))) {
      setCurrentPhase("evaluating");
    } else if (recentLogs.some((log) => log.includes("Initialization"))) {
      setCurrentPhase("preparing");
    }
  }, [evaluationLogs, completedSteps, activeStep, sessionId, onComplete]);

  // Format elapsed time as HH:MM:SS
  const formatElapsedTime = () => {
    const hours = Math.floor(elapsedTime / 3600);
    const minutes = Math.floor((elapsedTime % 3600) / 60);
    const seconds = elapsedTime % 60;

    return [
      hours.toString().padStart(2, "0"),
      minutes.toString().padStart(2, "0"),
      seconds.toString().padStart(2, "0"),
    ].join(":");
  };

  // Start benchmark evaluation
  const startEvaluation = async () => {
    if (!sessionId) {
      setError("Missing session ID");
      return;
    }

    setEvaluating(true);
    setEvaluationLogs([]);
    setError(null);
    setCurrentPhase("initializing");
    setCompletedSteps([]);
    setActiveStep(0);

    try {
      // Call API to start evaluation
      const response = await fetch("http://localhost:3001/evaluate-benchmark", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          session_id: sessionId,
        }),
      });

      const result = await response.json();

      if (response.ok) {
        setEvaluationLogs(result.logs || []);

        // Set up polling to retrieve more logs
        pollingIntervalRef.current = setInterval(async () => {
          // Check if we're already done
          if (evaluationComplete) {
            clearInterval(pollingIntervalRef.current);
            return;
          }

          try {
            // Call API to get latest logs
            const logsResponse = await fetch(
              `http://localhost:3001/evaluation-logs/${sessionId}`
            );

            if (logsResponse.ok) {
              const logsResult = await logsResponse.json();

              // Update logs if there are new ones
              if (
                logsResult.logs &&
                logsResult.logs.length > evaluationLogs.length
              ) {
                setEvaluationLogs(logsResult.logs);
              }

              // Check if evaluation is complete
              if (logsResult.is_completed) {
                setEvaluationComplete(true);
                clearInterval(pollingIntervalRef.current);
              }
            }
          } catch (error) {
            console.log("Error polling logs:", error);
            // Don't stop polling on network errors
          }
        }, 2000); // Poll every 2 seconds
      } else {
        // Handle error
        setEvaluationLogs([`Error: ${result.error || "Unknown error"}`]);
        setError(result.error || "Benchmark evaluation failed");
      }
    } catch (error) {
      console.error("Error starting evaluation:", error);
      setEvaluationLogs([`Error: ${error.message || "Unknown error"}`]);
      setError("Error connecting to server");
    } finally {
      setEvaluating(false);
    }
  };

  // Get title based on current phase
  const getPhaseTitle = () => {
    switch (currentPhase) {
      case "initializing":
        return "Preparing evaluation...";
      case "preparing":
        return "Preparing models...";
      case "evaluating":
        return "Evaluating models...";
      case "compiling_results":
        return "Compiling results...";
      case "complete":
        return "Evaluation completed successfully!";
      default:
        return "Processing...";
    }
  };

  // Get current step info for display
  const getCurrentStepInfo = () => {
    const totalSteps = EVALUATION_STEPS.length;
    const currentStepIndex = activeStep;

    // If no active step yet
    if (currentStepIndex === 0 && completedSteps.length === 0) {
      return `Starting... (0%)`;
    }

    // If all steps completed
    if (currentStepIndex >= totalSteps) {
      return `Completed (100%)`;
    }

    // Calculate percentage
    const percentage = Math.round((currentStepIndex / totalSteps) * 100);

    // Get current step name
    const currentStepName =
      STEP_LABELS[EVALUATION_STEPS[currentStepIndex]] || "Processing";

    return `${currentStepName} (${percentage}%)`;
  };

  // Function to navigate to results page
  const viewResults = () => {
    navigate(`/evaluation-display?session=${sessionId}`);
  };

  return (
    <Paper
      elevation={3}
      sx={{
        p: 4,
        mt: 3,
        mb: 3,
        display: "flex",
        flexDirection: "column",
        alignItems: "center",
        justifyContent: "center",
        minHeight: 200,
      }}
    >
      {error ? (
        <Alert severity="error" sx={{ width: "100%" }}>
          {error}
        </Alert>
      ) : (
        <>
          {evaluationComplete ? (
            <>
              <Alert severity="success" sx={{ width: "100%", mb: 3 }}>
                Evaluation completed successfully!
              </Alert>
              <Button
                variant="contained"
                color="primary"
                onClick={viewResults}
                sx={{ mb: 3 }}
              >
                View Results Leaderboard
              </Button>
            </>
          ) : (
            <>
              <CircularProgress size={60} sx={{ mb: 2 }} />
              <Typography variant="h6" component="div" gutterBottom>
                {getPhaseTitle()}
              </Typography>

              {/* Step progress indicator */}
              <Typography variant="body1" color="text.secondary">
                {getCurrentStepInfo()}
              </Typography>

              {/* Timer display */}
              <Box
                sx={{
                  display: "flex",
                  alignItems: "center",
                  mt: 1,
                  color: "text.secondary",
                  opacity: 0.5,
                }}
              >
                <Typography variant="body2">{formatElapsedTime()}</Typography>
              </Box>
            </>
          )}
        </>
      )}

      {/* Use the LogDisplay component for logs */}
      <LogDisplay logs={evaluationLogs} height={300} />
    </Paper>
  );
};

export default BenchmarkEvaluation;