70 lines
2.4 KiB
Rust
70 lines
2.4 KiB
Rust
use serde_json::Value;
|
|
|
|
pub(crate) fn normalize_task_status(status: &str) -> String {
|
|
match status.trim().to_ascii_lowercase().as_str() {
|
|
"waiting" | "pending" | "queued" => "waiting".to_string(),
|
|
"generating" | "running" | "processing" => "generating".to_string(),
|
|
"done" | "finished" | "completed" | "success" | "succeeded" => "done".to_string(),
|
|
"failed" | "error" | "canceled" | "cancelled" => "failed".to_string(),
|
|
_ => "unknown".to_string(),
|
|
}
|
|
}
|
|
|
|
pub(crate) fn extract_job_statuses(
|
|
payload: &Value,
|
|
) -> Vec<shared_contracts::hyper3d::Hyper3dJobStatusPayload> {
|
|
let Some(array) = super::parsing::find_first_array_by_keys(payload, &["jobs", "tasks"]) else {
|
|
return Vec::new();
|
|
};
|
|
|
|
array
|
|
.iter()
|
|
.filter_map(|value| {
|
|
let status = super::parsing::find_first_string_by_keys(value, &["status", "state"])
|
|
.map(|value| normalize_task_status(&value))?;
|
|
Some(shared_contracts::hyper3d::Hyper3dJobStatusPayload {
|
|
uuid: super::parsing::find_first_string_by_keys(
|
|
value,
|
|
&["uuid", "task_uuid", "taskUuid"],
|
|
),
|
|
progress: super::parsing::find_first_f64_by_keys(
|
|
value,
|
|
&["progress", "percentage"],
|
|
)
|
|
.map(|value| value as f32),
|
|
message: super::parsing::find_first_string_by_keys(
|
|
value,
|
|
&["message", "detail", "error"],
|
|
),
|
|
status,
|
|
})
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
pub(crate) fn resolve_hyper3d_overall_status(
|
|
payload: &Value,
|
|
jobs: &[shared_contracts::hyper3d::Hyper3dJobStatusPayload],
|
|
) -> String {
|
|
if !jobs.is_empty() {
|
|
if jobs.iter().any(|job| job.status == "failed") {
|
|
return "failed".to_string();
|
|
}
|
|
if jobs.iter().all(|job| job.status == "done") {
|
|
return "done".to_string();
|
|
}
|
|
if jobs.iter().any(|job| job.status == "generating") {
|
|
return "generating".to_string();
|
|
}
|
|
if jobs.iter().any(|job| job.status == "waiting") {
|
|
return "waiting".to_string();
|
|
}
|
|
return "unknown".to_string();
|
|
}
|
|
normalize_task_status(
|
|
super::parsing::find_first_string_by_key(payload, "status")
|
|
.as_deref()
|
|
.unwrap_or("unknown"),
|
|
)
|
|
}
|