1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use super::job_status::JobStatus;
use crate::job::Job;
use crate::parameter::container::ParametersContainer;
use crate::parameter::Parameter;
use crate::parameter::ParameterValue;
use reqwest::Error;
use serde::Serialize;
use std::time::Instant;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct JobResult {
  destination_paths: Vec<String>,
  execution_duration: f64,
  job_id: u64,
  parameters: Vec<Parameter>,
  #[serde(skip_serializing, skip_deserializing, default = "default_instant")]
  start_instant: Instant,
  status: JobStatus,
}

fn default_instant() -> Instant {
  Instant::now()
}

impl JobResult {
  pub fn new(job_id: u64) -> JobResult {
    JobResult {
      destination_paths: vec![],
      execution_duration: 0.0,
      job_id,
      parameters: vec![],
      start_instant: Instant::now(),
      status: JobStatus::default(),
    }
  }

  pub fn with_status(mut self, status: JobStatus) -> Self {
    self.update_execution_duration();
    self.status = status;
    self
  }

  pub fn with_error(mut self, error: Error) -> Self {
    self.update_execution_duration();
    self.parameters.push(Parameter {
      id: "message".to_string(),
      kind: String::get_type_as_string(),
      store: None,
      default: None,
      value: serde_json::to_value(error.to_string()).ok(),
    });
    self
  }

  pub fn with_message(mut self, message: &str) -> Self {
    self.parameters.push(Parameter {
      id: "message".to_string(),
      kind: String::get_type_as_string(),
      store: None,
      default: None,
      value: serde_json::to_value(message.to_string()).ok(),
    });
    self
  }

  pub fn with_parameters(mut self, parameters: &mut Vec<Parameter>) -> Self {
    self.parameters.append(parameters);
    self
  }

  pub fn with_destination_paths(mut self, destination_paths: &mut Vec<String>) -> Self {
    self.destination_paths.append(destination_paths);
    self
  }

  pub fn with_json<T>(mut self, id: &str, serializable: &T) -> Result<Self, String>
  where
    T: Serialize + Sized,
  {
    let json_string = serde_json::to_string(serializable)
      .map_err(|error| format!("Unable to serialize object: {:?}", error))?;
    self.parameters.push(Parameter {
      id: id.to_string(),
      kind: String::get_type_as_string(),
      store: None,
      default: None,
      value: serde_json::to_value(json_string).ok(),
    });
    Ok(self)
  }

  pub fn get_job_id(&self) -> u64 {
    self.job_id
  }

  pub fn get_str_job_id(&self) -> String {
    self.job_id.to_string()
  }

  pub fn get_status(&self) -> &JobStatus {
    &self.status
  }

  pub fn get_execution_duration(&self) -> f64 {
    self.execution_duration
  }

  pub fn get_parameters(&self) -> &Vec<Parameter> {
    &self.parameters
  }

  pub fn get_destination_paths(&self) -> &Vec<String> {
    &self.destination_paths
  }

  pub fn update_execution_duration(&mut self) {
    self.execution_duration = self.start_instant.elapsed().as_secs_f64();
  }
}

impl From<Job> for JobResult {
  fn from(job: Job) -> JobResult {
    JobResult::new(job.job_id)
  }
}

impl From<&Job> for JobResult {
  fn from(job: &Job) -> JobResult {
    JobResult::new(job.job_id)
  }
}

impl ParametersContainer for JobResult {
  fn get_parameters(&self) -> &Vec<Parameter> {
    &self.parameters
  }
}

impl PartialEq for JobResult {
  fn eq(&self, other: &Self) -> bool {
    self.job_id == other.job_id
      && self.status == other.status
      && self.parameters == other.parameters
      && self.destination_paths == other.destination_paths
  }
}