use std::collections::BTreeMap;
use hatchet_proto::v1;
use serde::de::DeserializeOwned;
use crate::Task;
use crate::error::{Error, Result};
use crate::runs::filter::RunStatus;
#[derive(Clone, Debug)]
pub struct RunDetails {
run_id: String,
status: RunStatus,
done: bool,
is_evicted: bool,
input: Vec<u8>,
additional_metadata: Vec<u8>,
tasks: BTreeMap<String, TaskRunDetail>,
}
#[derive(Clone, Debug)]
pub struct TaskRunDetail {
external_id: String,
readable_id: String,
status: RunStatus,
error: Option<String>,
output: Option<Vec<u8>>,
is_evicted: bool,
}
impl RunDetails {
pub(crate) fn from_proto(run_id: &str, response: v1::GetRunDetailsResponse) -> Result<Self> {
let unknown = |status: i32| Error::UnknownRunStatus {
run_id: run_id.to_string(),
status,
};
let mut tasks = BTreeMap::new();
for detail in response.task_runs.into_values() {
let task = TaskRunDetail {
readable_id: detail.readable_id.clone(),
external_id: detail.external_id,
status: RunStatus::from_proto(detail.status)
.ok_or_else(|| unknown(detail.status))?,
error: detail.error.filter(|error| !error.is_empty()),
output: detail.output,
is_evicted: detail.is_evicted,
};
tasks.insert(detail.readable_id, task);
}
Ok(Self {
run_id: run_id.to_string(),
status: RunStatus::from_proto(response.status)
.ok_or_else(|| unknown(response.status))?,
done: response.done,
is_evicted: response.is_evicted,
input: response.input,
additional_metadata: response.additional_metadata,
tasks,
})
}
pub fn run_id(&self) -> &str {
&self.run_id
}
pub fn status(&self) -> RunStatus {
self.status
}
pub fn is_done(&self) -> bool {
self.done
}
pub fn is_evicted(&self) -> bool {
self.is_evicted
}
pub fn input<T: DeserializeOwned>(&self) -> Result<T> {
self.decode(&self.input, "input")
}
pub fn additional_metadata<T: DeserializeOwned>(&self) -> Result<T> {
self.decode(&self.additional_metadata, "additional metadata")
}
pub fn task<I, O>(&self, task: &Task<I, O>) -> Option<&TaskRunDetail> {
self.task_by_name(task.name())
}
pub fn task_by_name(&self, name: &str) -> Option<&TaskRunDetail> {
self.tasks.get(name).or_else(|| {
self.tasks
.values()
.find(|task| task.readable_id.eq_ignore_ascii_case(name))
})
}
pub fn tasks(&self) -> impl Iterator<Item = (&str, &TaskRunDetail)> {
self.tasks.iter().map(|(name, task)| (name.as_str(), task))
}
pub fn output_of<I, O: DeserializeOwned>(&self, task: &Task<I, O>) -> Result<O> {
let detail = self.task(task).ok_or_else(|| Error::RunOutput {
run_id: self.run_id.clone(),
reason: format!("the run has no task {:?}", task.name()),
})?;
let output = detail.output.as_deref().ok_or_else(|| Error::RunOutput {
run_id: self.run_id.clone(),
reason: format!("task {:?} recorded no output", detail.readable_id),
})?;
self.decode(
output,
&format!("the output of task {:?}", detail.readable_id),
)
}
fn decode<T: DeserializeOwned>(&self, bytes: &[u8], what: &str) -> Result<T> {
if bytes.is_empty() {
return Err(Error::RunOutput {
run_id: self.run_id.clone(),
reason: format!("the engine recorded no {what} for this run"),
});
}
serde_json::from_slice(bytes).map_err(|error| Error::RunOutput {
run_id: self.run_id.clone(),
reason: format!("{what}: {error}"),
})
}
}
impl TaskRunDetail {
pub fn id(&self) -> &str {
&self.external_id
}
pub fn name(&self) -> &str {
&self.readable_id
}
pub fn status(&self) -> RunStatus {
self.status
}
pub fn error(&self) -> Option<&str> {
self.error.as_deref()
}
pub fn is_evicted(&self) -> bool {
self.is_evicted
}
pub fn output<T: DeserializeOwned>(&self) -> Result<T> {
let output = self.output.as_deref().ok_or_else(|| Error::RunOutput {
run_id: self.external_id.clone(),
reason: format!("task {:?} recorded no output", self.readable_id),
})?;
serde_json::from_slice(output).map_err(|error| Error::RunOutput {
run_id: self.external_id.clone(),
reason: format!("task {:?}: {error}", self.readable_id),
})
}
}