use serde::{Deserialize, Serialize};
use crate::events::{LearnStatsOutcome, LearningEvent};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LearnStatsRecord {
pub timestamp_ms: u64,
pub scenario: String,
pub session_id: String,
pub stats_json: String,
pub outcome: LearnStatsOutcome,
pub total_ticks: u64,
pub total_actions: u64,
}
impl From<&LearningEvent> for LearnStatsRecord {
fn from(event: &LearningEvent) -> Self {
match event {
LearningEvent::LearnStatsSnapshot {
timestamp_ms,
scenario,
session_id,
stats_json,
outcome,
total_ticks,
total_actions,
} => Self {
timestamp_ms: *timestamp_ms,
scenario: scenario.clone(),
session_id: session_id.clone(),
stats_json: stats_json.clone(),
outcome: outcome.clone(),
total_ticks: *total_ticks,
total_actions: *total_actions,
},
_ => panic!("LearnStatsRecord::from() requires LearnStatsSnapshot event"),
}
}
}
impl LearnStatsRecord {
pub fn is_success(&self) -> bool {
matches!(self.outcome, LearnStatsOutcome::Success { .. })
}
pub fn score(&self) -> f64 {
match &self.outcome {
LearnStatsOutcome::Success { score } => *score,
LearnStatsOutcome::Timeout { partial_score } => partial_score.unwrap_or(0.0),
LearnStatsOutcome::Failure { .. } => 0.0,
}
}
}