|
|
@ -3,7 +3,7 @@ use diesel::{pg::PgConnection, prelude::*, result::Error as DslError, Connection |
|
|
|
use u_lib::db::PgAsyncPool; |
|
|
|
use u_lib::db::PgAsyncPool; |
|
|
|
use u_lib::models::{schema, Agent, AssignedJob, JobState, ThinJobMeta}; |
|
|
|
use u_lib::models::{schema, Agent, AssignedJob, JobState, ThinJobMeta}; |
|
|
|
use u_lib::platform::Platform; |
|
|
|
use u_lib::platform::Platform; |
|
|
|
use uuid::Uuid; |
|
|
|
use u_lib::types::Id; |
|
|
|
|
|
|
|
|
|
|
|
type Result<T> = std::result::Result<T, Error>; |
|
|
|
type Result<T> = std::result::Result<T, Error>; |
|
|
|
|
|
|
|
|
|
|
@ -47,7 +47,7 @@ pub struct UDB<'c> { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl UDB<'_> { |
|
|
|
impl UDB<'_> { |
|
|
|
pub fn insert_jobs(&mut self, job_metas: &[ThinJobMeta]) -> Result<Vec<Uuid>> { |
|
|
|
pub fn insert_jobs(&mut self, job_metas: &[ThinJobMeta]) -> Result<Vec<Id>> { |
|
|
|
use schema::jobs; |
|
|
|
use schema::jobs; |
|
|
|
|
|
|
|
|
|
|
|
diesel::insert_into(jobs::table) |
|
|
|
diesel::insert_into(jobs::table) |
|
|
@ -57,7 +57,7 @@ impl UDB<'_> { |
|
|
|
.map_err(with_err_ctx("Can't insert jobs")) |
|
|
|
.map_err(with_err_ctx("Can't insert jobs")) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn get_job(&mut self, id: Uuid) -> Result<Option<ThinJobMeta>> { |
|
|
|
pub fn get_job(&mut self, id: Id) -> Result<Option<ThinJobMeta>> { |
|
|
|
use schema::jobs; |
|
|
|
use schema::jobs; |
|
|
|
|
|
|
|
|
|
|
|
jobs::table |
|
|
|
jobs::table |
|
|
@ -94,7 +94,7 @@ impl UDB<'_> { |
|
|
|
.do_update() |
|
|
|
.do_update() |
|
|
|
.set(agent) |
|
|
|
.set(agent) |
|
|
|
.execute(self.conn) |
|
|
|
.execute(self.conn) |
|
|
|
.map_err(with_err_ctx(format!("Can't insert agent {agent:x?}")))?; |
|
|
|
.map_err(with_err_ctx(format!("Can't insert agent {agent:?}")))?; |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -104,11 +104,11 @@ impl UDB<'_> { |
|
|
|
diesel::insert_into(results::table) |
|
|
|
diesel::insert_into(results::table) |
|
|
|
.values(result) |
|
|
|
.values(result) |
|
|
|
.execute(self.conn) |
|
|
|
.execute(self.conn) |
|
|
|
.map_err(with_err_ctx(format!("Can't insert result {result:x?}")))?; |
|
|
|
.map_err(with_err_ctx(format!("Can't insert result {result:?}")))?; |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn get_agent(&mut self, id: Uuid) -> Result<Option<Agent>> { |
|
|
|
pub fn get_agent(&mut self, id: Id) -> Result<Option<Agent>> { |
|
|
|
use schema::agents; |
|
|
|
use schema::agents; |
|
|
|
|
|
|
|
|
|
|
|
agents::table |
|
|
|
agents::table |
|
|
@ -126,7 +126,7 @@ impl UDB<'_> { |
|
|
|
.map_err(with_err_ctx(format!("Can't get agents"))) |
|
|
|
.map_err(with_err_ctx(format!("Can't get agents"))) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn update_job_status(&mut self, id: Uuid, status: JobState) -> Result<()> { |
|
|
|
pub fn update_job_status(&mut self, id: Id, status: JobState) -> Result<()> { |
|
|
|
use schema::results; |
|
|
|
use schema::results; |
|
|
|
|
|
|
|
|
|
|
|
diesel::update(results::table) |
|
|
|
diesel::update(results::table) |
|
|
@ -138,7 +138,7 @@ impl UDB<'_> { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//TODO: filters possibly could work in a wrong way, check
|
|
|
|
//TODO: filters possibly could work in a wrong way, check
|
|
|
|
pub fn get_exact_jobs(&mut self, id: Option<Uuid>, personal: bool) -> Result<Vec<AssignedJob>> { |
|
|
|
pub fn get_exact_jobs(&mut self, id: Option<Id>, personal: bool) -> Result<Vec<AssignedJob>> { |
|
|
|
use schema::results; |
|
|
|
use schema::results; |
|
|
|
|
|
|
|
|
|
|
|
let mut q = results::table.into_boxed(); |
|
|
|
let mut q = results::table.into_boxed(); |
|
|
@ -163,7 +163,7 @@ impl UDB<'_> { |
|
|
|
Ok(result) |
|
|
|
Ok(result) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn set_jobs_for_agent(&mut self, agent_id: Uuid, job_ids: &[Uuid]) -> Result<Vec<Uuid>> { |
|
|
|
pub fn set_jobs_for_agent(&mut self, agent_id: Id, job_ids: &[Id]) -> Result<Vec<Id>> { |
|
|
|
use schema::{jobs, results}; |
|
|
|
use schema::{jobs, results}; |
|
|
|
|
|
|
|
|
|
|
|
let agent_platform = match self.get_agent(agent_id)? { |
|
|
|
let agent_platform = match self.get_agent(agent_id)? { |
|
|
@ -178,7 +178,7 @@ impl UDB<'_> { |
|
|
|
let jobs_meta = jobs::table |
|
|
|
let jobs_meta = jobs::table |
|
|
|
.select((jobs::id, jobs::alias, jobs::platform)) |
|
|
|
.select((jobs::id, jobs::alias, jobs::platform)) |
|
|
|
.filter(jobs::id.eq_any(job_ids)) |
|
|
|
.filter(jobs::id.eq_any(job_ids)) |
|
|
|
.load::<(Uuid, Option<String>, String)>(self.conn) |
|
|
|
.load::<(Id, Option<String>, String)>(self.conn) |
|
|
|
.map_err(with_err_ctx(format!("Can't find jobs {job_ids:?}")))?; |
|
|
|
.map_err(with_err_ctx(format!("Can't find jobs {job_ids:?}")))?; |
|
|
|
|
|
|
|
|
|
|
|
for meta in &jobs_meta { |
|
|
|
for meta in &jobs_meta { |
|
|
@ -210,7 +210,7 @@ impl UDB<'_> { |
|
|
|
Ok(job_requests.iter().map(|aj| aj.id).collect()) |
|
|
|
Ok(job_requests.iter().map(|aj| aj.id).collect()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn del_jobs(&mut self, ids: &[Uuid]) -> Result<usize> { |
|
|
|
pub fn del_jobs(&mut self, ids: &[Id]) -> Result<usize> { |
|
|
|
use schema::jobs; |
|
|
|
use schema::jobs; |
|
|
|
|
|
|
|
|
|
|
|
let mut affected = 0; |
|
|
|
let mut affected = 0; |
|
|
@ -224,7 +224,7 @@ impl UDB<'_> { |
|
|
|
Ok(affected) |
|
|
|
Ok(affected) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn del_results(&mut self, ids: &[Uuid]) -> Result<usize> { |
|
|
|
pub fn del_results(&mut self, ids: &[Id]) -> Result<usize> { |
|
|
|
use schema::results; |
|
|
|
use schema::results; |
|
|
|
|
|
|
|
|
|
|
|
let mut affected = 0; |
|
|
|
let mut affected = 0; |
|
|
@ -238,7 +238,7 @@ impl UDB<'_> { |
|
|
|
Ok(affected) |
|
|
|
Ok(affected) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn del_agents(&mut self, ids: &[Uuid]) -> Result<usize> { |
|
|
|
pub fn del_agents(&mut self, ids: &[Id]) -> Result<usize> { |
|
|
|
use schema::agents; |
|
|
|
use schema::agents; |
|
|
|
|
|
|
|
|
|
|
|
let mut affected = 0; |
|
|
|
let mut affected = 0; |
|
|
@ -255,13 +255,13 @@ impl UDB<'_> { |
|
|
|
pub fn update_agent(&mut self, agent: &Agent) -> Result<()> { |
|
|
|
pub fn update_agent(&mut self, agent: &Agent) -> Result<()> { |
|
|
|
agent |
|
|
|
agent |
|
|
|
.save_changes::<Agent>(self.conn) |
|
|
|
.save_changes::<Agent>(self.conn) |
|
|
|
.map_err(with_err_ctx(format!("Can't update agent {agent:x?}")))?; |
|
|
|
.map_err(with_err_ctx(format!("Can't update agent {agent:?}")))?; |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn update_job(&mut self, job: &ThinJobMeta) -> Result<()> { |
|
|
|
pub fn update_job(&mut self, job: &ThinJobMeta) -> Result<()> { |
|
|
|
job.save_changes::<ThinJobMeta>(self.conn) |
|
|
|
job.save_changes::<ThinJobMeta>(self.conn) |
|
|
|
.map_err(with_err_ctx(format!("Can't update job {job:x?}")))?; |
|
|
|
.map_err(with_err_ctx(format!("Can't update job {job:?}")))?; |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -272,7 +272,7 @@ impl UDB<'_> { |
|
|
|
); |
|
|
|
); |
|
|
|
result |
|
|
|
result |
|
|
|
.save_changes::<AssignedJob>(self.conn) |
|
|
|
.save_changes::<AssignedJob>(self.conn) |
|
|
|
.map_err(with_err_ctx(format!("Can't update result {result:x?}")))?; |
|
|
|
.map_err(with_err_ctx(format!("Can't update result {result:?}")))?; |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|