use serde_json::{Map, Value};
use evalhub_schema::EVAL_SCHEMA;
use crate::run::materialise;
pub const V1_MIGRATION_META_KEY: &str = "v0.2.0_migration";
pub const UNRECORDED: &str = "unrecorded";
const EVAL_SCHEMA_1_0: &str = "evalhub.eval/1.0";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EvalSchema {
V1,
V2,
}
impl EvalSchema {
pub const fn id(self) -> &'static str {
match self {
EvalSchema::V1 => EVAL_SCHEMA_1_0,
EvalSchema::V2 => EVAL_SCHEMA,
}
}
}
pub fn declared_schema(body: &Value) -> Option<EvalSchema> {
match body.get("schema").and_then(Value::as_str)? {
EVAL_SCHEMA => Some(EvalSchema::V2),
EVAL_SCHEMA_1_0 => Some(EvalSchema::V1),
_ => None,
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct SplitV1 {
pub header: Value,
pub runs: Vec<(String, Value)>,
pub duplicate_run_ids: Vec<String>,
}
pub fn split_v1(body: &Value) -> SplitV1 {
let Some(obj) = body.as_object() else {
return SplitV1 {
header: body.clone(),
runs: Vec::new(),
duplicate_run_ids: Vec::new(),
};
};
let mut header_obj = obj.clone();
let runs_v1 = header_obj.remove("runs");
header_obj.insert("schema".to_string(), Value::String(EVAL_SCHEMA.to_string()));
let header = Value::Object(header_obj);
let mut runs: Vec<(String, Value)> = Vec::new();
let mut duplicate_run_ids: Vec<String> = Vec::new();
let elements = runs_v1
.as_ref()
.and_then(Value::as_array)
.map(Vec::as_slice)
.unwrap_or_default();
for element in elements {
let Some(element) = element.as_object() else {
continue;
};
let Some(run_id) = element.get("run_id").and_then(Value::as_str) else {
continue;
};
let run = convert_run(run_id, element, &header);
match runs.iter_mut().find(|(id, _)| id == run_id) {
Some(slot) => {
slot.1 = run;
if !duplicate_run_ids.iter().any(|d| d == run_id) {
duplicate_run_ids.push(run_id.to_string());
}
}
None => runs.push((run_id.to_string(), run)),
}
}
SplitV1 {
header,
runs,
duplicate_run_ids,
}
}
fn convert_run(run_id: &str, v1: &Map<String, Value>, header: &Value) -> Value {
let mut run = Map::new();
run.insert("run_id".to_string(), Value::String(run_id.to_string()));
let outcome = v1.get("outcome");
match outcome.and_then(Value::as_str) {
Some("pass" | "fail") => {
run.insert("status".to_string(), "ok".into());
}
Some("skipped") => {
run.insert("status".to_string(), "skipped".into());
run.insert("skip_reason".to_string(), UNRECORDED.into());
}
_ => {
run.insert("status".to_string(), "error".into());
let mut error = Map::new();
error.insert("kind".to_string(), UNRECORDED.into());
run.insert("error".to_string(), Value::Object(error));
}
}
for key in ["started_at", "ended_at", "calls", "artifacts"] {
if let Some(v) = v1.get(key) {
run.insert(key.to_string(), v.clone());
}
}
let mut named: Vec<&str> = Vec::new();
if let Some(c) = v1.get("calls").and_then(Value::as_str) {
named.push(c);
}
if let Some(artifacts) = v1.get("artifacts").and_then(Value::as_array) {
named.extend(artifacts.iter().filter_map(Value::as_str));
}
let mut copied: Vec<&str> = Vec::new();
let mut attachments: Vec<Value> = Vec::new();
if let Some(entries) = header.get("attachments").and_then(Value::as_array) {
for entry in entries {
let Some(path) = entry.get("path").and_then(Value::as_str) else {
continue;
};
if named.contains(&path) && !copied.contains(&path) {
copied.push(path);
attachments.push(entry.clone());
}
}
}
if !attachments.is_empty() {
run.insert("attachments".to_string(), Value::Array(attachments));
}
if let Some(outcome) = outcome {
let mut dropped = Map::new();
dropped.insert("outcome".to_string(), outcome.clone());
let mut meta = Map::new();
meta.insert(V1_MIGRATION_META_KEY.to_string(), Value::Object(dropped));
run.insert("meta".to_string(), Value::Object(meta));
}
let mut run = Value::Object(run);
materialise(&mut run, header);
run
}