chiral_common/job/
status.rs

1//! Job Status
2//! 
3
4use serde::{Serialize, Deserialize};
5use crate::traits::{Serialization, SerializedFormat};
6use chiral_derive::Serialization;
7
8#[derive(Serialize, Deserialize, Serialization, Debug, Clone, PartialEq, Eq, Copy)]  
9pub enum StatusLabel {
10    Created,
11    Processing,
12    Completed,
13    Cancelled,
14    ErrorJobIDNotFound
15}
16
17impl std::fmt::Display for StatusLabel {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        match self {
20            Self::Created => write!(f, "CREATED"),
21            Self::Processing => write!(f, "PROCESSING"),
22            Self::Completed => write!(f, "COMPLETED"),
23            Self::Cancelled => write!(f, "CANCELLED"),
24            Self::ErrorJobIDNotFound => write!(f, "ERROR_ID_NOT_FOUND")
25        }
26    }
27}
28
29type CountTaskWaiting = super::DividendSize;
30type CountTaskProcessing = super::DividendSize;
31type CountTaskCompleted = super::DividendSize;
32type ProgressTuple = (CountTaskWaiting, CountTaskProcessing, CountTaskCompleted);
33
34/// Status
35#[derive(Serialize, Deserialize, Serialization, Debug, Clone, PartialEq, Eq)] 
36pub struct Status {
37    label: StatusLabel,
38    progress: Option<ProgressTuple>
39}
40
41impl Status {
42    pub fn new(label: StatusLabel, progress: Option<(CountTaskWaiting, CountTaskProcessing, CountTaskCompleted)>) -> Self {
43        Self { label, progress }
44    }
45
46    pub fn is_created(&self) -> bool { self.label == StatusLabel::Created }
47    pub fn is_processing(&self) -> bool { self.label == StatusLabel::Processing }
48    pub fn is_completed(&self) -> bool { self.label == StatusLabel::Completed }
49    pub fn is_error_job_not_found(&self) -> bool { self.label == StatusLabel::ErrorJobIDNotFound }
50
51    pub fn count_completed(&self) -> (usize, usize) {
52        match self.progress {
53            Some((w, p, c)) => (c, w + p + c), 
54            None => (0, 0)
55        }
56    }
57    
58    pub fn get_progress(&self) -> f32 {
59        match self.progress {
60            Some((w, p, c)) => {
61                if w + p + c == 0 {
62                    0.0
63                } else {
64                    c as f32 / (w + p + c) as f32
65                }
66            },
67            None => 0.0
68        }
69    }
70
71    pub fn assign_task(&mut self, dividends: usize) {
72        match &self.label {
73            StatusLabel::Created => {
74                self.label = StatusLabel::Processing;
75                self.progress = Some((dividends - 1, 1, 0));
76            },
77            StatusLabel::Processing => {
78                let (w, p, c) = self.progress.expect("job in process should not have null progress");
79                self.progress = Some((w - 1, p + 1, c))
80            },
81            _ => {
82                crate::logging::warn("job status: should not reach here");
83            }
84        }
85    }
86
87    pub fn complete_task(&mut self, dividends: usize) {
88        match &self.label {
89            StatusLabel::Processing => {
90                let (w, p, c) = self.progress.expect("job in process should not have null progress");
91                self.progress = Some((w, p - 1, c + 1));
92                if c + 1 == dividends {
93                    self.label = StatusLabel::Completed;
94                } 
95            },
96            _ => {
97                crate::logging::warn("job status: should not reach here");
98            }
99        };
100    }
101}
102
103impl std::fmt::Display for Status {
104    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
105        let progress_str = match self.progress {
106            Some(p) => format!("waiting: {}, processing: {}, completed: {}", p.0, p.1, p.2),
107            None => format!("")
108        };
109        write!(f, "{}", format!("{} {}", self.label, progress_str))
110    }
111}