chiral_common/job/
result.rs

1//! Job Result
2//! 
3
4use serde::{Serialize, Deserialize};
5use crate::traits::{Serialization, SerializedFormat};
6use chiral_derive::Serialization;
7
8/// Task Result: a job is divided into several tasks
9#[derive(Serialize, Deserialize, Serialization, Debug, Clone, PartialEq, Eq)] 
10pub struct TaskResult {
11    output: SerializedFormat,
12    error: Option<String>,
13    duration_init: std::time::Duration,
14    duration_whole: Option<std::time::Duration>
15}
16
17impl TaskResult {
18    pub fn new(output: SerializedFormat, error: Option<String>, duration_init: std::time::Duration) -> Self {
19        Self { output, error, duration_init, duration_whole: None }
20    }
21
22    pub fn set_whole_duration(&mut self,  duration_whole: std::time::Duration) {
23        self.duration_whole = Some(duration_whole);
24    }
25
26    pub fn get_output(&self) -> &SerializedFormat {
27        &self.output
28    }
29}
30
31#[derive(Serialize, Deserialize, Serialization, Debug, Clone, PartialEq, Eq)] 
32pub struct Result {
33    req: super::requirement::Requirement,
34    results: Vec<Option<TaskResult>>
35}
36
37impl Result {
38    pub fn null_string() -> String { "".to_string() }
39
40    pub fn new(req: super::requirement::Requirement, dividends: super::DividendSize) -> Self {
41        Self {
42            req,
43            results: vec![None; dividends]
44        }
45    }
46
47    pub fn get_req(&self) -> &super::requirement::Requirement {
48        &self.req
49    }
50
51    pub fn set(&mut self, idx: super::DividendSize, result: TaskResult) {
52        self.results[idx] = Some(result);
53    }
54
55    pub fn count_completed_tasks(&self) -> usize {
56        self.results.iter().filter(|r| r.is_some()).count()
57    }
58
59    pub fn save_report(&self, job_id: super::ID, filepath: &std::path::PathBuf) -> std::io::Result<u64>  {
60        let opk = self.req.get_opk();
61        let output_sers = self.results.iter()
62            .filter(|op_tr| op_tr.is_some())
63            .map(|some_tr| some_tr.as_ref().unwrap().get_output().to_owned())
64            .collect();
65
66        opk.report_save(job_id, self.req.get_dsk().to_owned(), self.get_req().get_ji(), &output_sers, filepath) 
67    }
68
69    pub fn get_outputs(&self) -> Vec<SerializedFormat> {
70        self.results.iter()
71            .filter(|r| r.is_some())
72            .map(|r| r.to_owned().unwrap().get_output().to_owned())
73            .collect()
74    }
75}