use super::{JobMeta, JobState, JobType}; #[cfg(not(target_arch = "wasm32"))] use crate::config::get_self_uid; #[cfg(feature = "server")] use crate::models::schema::*; #[cfg(feature = "server")] use diesel::{Identifiable, Insertable, Queryable}; use serde::{Deserialize, Serialize}; use std::{borrow::Cow, time::SystemTime}; use uuid::Uuid; #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] #[cfg_attr( feature = "server", derive(Queryable, Identifiable, Insertable, AsChangeset), table_name = "results" )] pub struct AssignedJob { pub agent_id: Uuid, pub alias: Option, pub created: SystemTime, pub id: Uuid, pub job_id: Uuid, pub result: Option>, pub state: JobState, pub exec_type: JobType, pub retcode: Option, pub updated: SystemTime, } #[derive(Serialize, Deserialize, Clone, Copy)] pub struct AssignedJobById { pub agent_id: Uuid, pub id: Uuid, pub job_id: Uuid, } impl From<(&JobMeta, AssignedJobById)> for AssignedJob { fn from((meta, pj): (&JobMeta, AssignedJobById)) -> Self { AssignedJob { id: pj.id, agent_id: pj.agent_id, job_id: pj.job_id, alias: meta.alias.clone(), exec_type: meta.exec_type, ..Default::default() } } } impl From<&JobMeta> for AssignedJob { fn from(meta: &JobMeta) -> Self { AssignedJob { job_id: meta.id, agent_id: get_self_uid(), alias: meta.alias.clone(), exec_type: meta.exec_type, ..Default::default() } } } impl Default for AssignedJobById { fn default() -> Self { Self { agent_id: get_self_uid(), id: Uuid::new_v4(), job_id: Uuid::nil(), } } } impl Default for AssignedJob { fn default() -> Self { Self { agent_id: Uuid::nil(), alias: None, created: SystemTime::now(), id: Uuid::new_v4(), job_id: Uuid::nil(), result: None, state: JobState::Queued, retcode: None, updated: SystemTime::now(), exec_type: JobType::default(), } } } impl AssignedJob { pub fn to_raw_result(&self) -> &[u8] { match self.result.as_ref() { Some(r) => r, None => b"No data yet", } } pub fn to_str_result(&self) -> Cow<'_, str> { String::from_utf8_lossy(self.to_raw_result()) } pub fn set_result(&mut self, result: &S) { self.result = Some(serde_json::to_vec(result).unwrap()); } pub fn touch(&mut self) { self.updated = SystemTime::now() } }