|
|
|
@ -3,9 +3,9 @@ use std::sync::Arc; |
|
|
|
|
use crate::db::{PgRepo, UDB}; |
|
|
|
|
use crate::error::Error; |
|
|
|
|
use serde::Deserialize; |
|
|
|
|
use u_lib::jobs::join_payload; |
|
|
|
|
use u_lib::{ |
|
|
|
|
jobs::split_payload, |
|
|
|
|
api::retypes, |
|
|
|
|
jobs::{join_payload, split_payload}, |
|
|
|
|
messaging::{AsMsg, Reportable}, |
|
|
|
|
misc::OneOrVec, |
|
|
|
|
models::*, |
|
|
|
@ -24,7 +24,7 @@ pub struct GetJobQuery { |
|
|
|
|
pub struct Endpoints; |
|
|
|
|
|
|
|
|
|
impl Endpoints { |
|
|
|
|
pub async fn get_agents(repo: Arc<PgRepo>, id: Option<Id>) -> EndpResult<Vec<Agent>> { |
|
|
|
|
pub async fn get_agents(repo: Arc<PgRepo>, id: Option<Id>) -> EndpResult<retypes::GetAgents> { |
|
|
|
|
repo.interact(move |mut db| { |
|
|
|
|
Ok(match id { |
|
|
|
|
Some(id) => { |
|
|
|
@ -41,7 +41,11 @@ impl Endpoints { |
|
|
|
|
.map_err(From::from) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub async fn get_job(repo: Arc<PgRepo>, id: Id, params: GetJobQuery) -> EndpResult<FatJob> { |
|
|
|
|
pub async fn get_job( |
|
|
|
|
repo: Arc<PgRepo>, |
|
|
|
|
id: Id, |
|
|
|
|
params: GetJobQuery, |
|
|
|
|
) -> EndpResult<retypes::GetJob> { |
|
|
|
|
let Some(job) = repo.interact(move |mut db| db.get_job(id)).await? else { |
|
|
|
|
return Err(not_found()) |
|
|
|
|
}; |
|
|
|
@ -60,19 +64,25 @@ impl Endpoints { |
|
|
|
|
Ok(join_payload(job).map_err(Error::from)?) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub async fn get_jobs(repo: Arc<PgRepo>) -> EndpResult<Vec<JobModel>> { |
|
|
|
|
pub async fn get_jobs(repo: Arc<PgRepo>) -> EndpResult<retypes::GetJobs> { |
|
|
|
|
repo.interact(move |mut db| db.get_jobs()) |
|
|
|
|
.await |
|
|
|
|
.map_err(From::from) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub async fn get_agent_jobs(repo: Arc<PgRepo>, id: Option<Id>) -> EndpResult<Vec<AssignedJob>> { |
|
|
|
|
pub async fn get_agent_jobs( |
|
|
|
|
repo: Arc<PgRepo>, |
|
|
|
|
id: Option<Id>, |
|
|
|
|
) -> EndpResult<retypes::GetAgentJobs> { |
|
|
|
|
repo.interact(move |mut db| db.get_exact_jobs(id, false)) |
|
|
|
|
.await |
|
|
|
|
.map_err(From::from) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub async fn get_personal_jobs(repo: Arc<PgRepo>, id: Id) -> EndpResult<Vec<AssignedJob>> { |
|
|
|
|
pub async fn get_personal_jobs( |
|
|
|
|
repo: Arc<PgRepo>, |
|
|
|
|
id: Id, |
|
|
|
|
) -> EndpResult<retypes::GetPersonalJobs> { |
|
|
|
|
repo.transaction(move |mut db| { |
|
|
|
|
let agent = db.get_agent(id)?; |
|
|
|
|
match agent { |
|
|
|
@ -100,13 +110,19 @@ impl Endpoints { |
|
|
|
|
db.update_job_status(job.id, JobState::Running)?; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Ok(assigned_jobs) |
|
|
|
|
Ok(assigned_jobs |
|
|
|
|
.into_iter() |
|
|
|
|
.map(|j| AssignedJobById::from(&j)) |
|
|
|
|
.collect()) |
|
|
|
|
}) |
|
|
|
|
.await |
|
|
|
|
.map_err(From::from) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub async fn upload_jobs(repo: Arc<PgRepo>, msg: Vec<FatJob>) -> EndpResult<()> { |
|
|
|
|
pub async fn upload_jobs( |
|
|
|
|
repo: Arc<PgRepo>, |
|
|
|
|
msg: Vec<FatJob>, |
|
|
|
|
) -> EndpResult<retypes::UploadJobs> { |
|
|
|
|
let jobs = msg |
|
|
|
|
.into_iter() |
|
|
|
|
.map(|meta| Ok(split_payload(meta)?)) |
|
|
|
@ -119,7 +135,12 @@ impl Endpoints { |
|
|
|
|
|
|
|
|
|
pub async fn del(repo: Arc<PgRepo>, id: Id) -> EndpResult<()> { |
|
|
|
|
repo.transaction(move |mut db| { |
|
|
|
|
[UDB::del_agents, UDB::del_jobs, UDB::del_results] |
|
|
|
|
[ |
|
|
|
|
UDB::del_agents, |
|
|
|
|
UDB::del_jobs, |
|
|
|
|
UDB::del_results, |
|
|
|
|
UDB::del_payloads, |
|
|
|
|
] |
|
|
|
|
.iter() |
|
|
|
|
.map(|f| f(&mut db, &[id])) |
|
|
|
|
.collect::<Result<(), Error>>() |
|
|
|
@ -132,7 +153,7 @@ impl Endpoints { |
|
|
|
|
repo: Arc<PgRepo>, |
|
|
|
|
agent_id: Id, |
|
|
|
|
job_idents: Vec<String>, |
|
|
|
|
) -> EndpResult<Vec<Id>> { |
|
|
|
|
) -> EndpResult<retypes::SetJobs> { |
|
|
|
|
repo.transaction(move |mut db| { |
|
|
|
|
let assigned_job_idents = job_idents |
|
|
|
|
.into_iter() |
|
|
|
@ -162,7 +183,7 @@ impl Endpoints { |
|
|
|
|
repo: Arc<PgRepo>, |
|
|
|
|
msg: Data, |
|
|
|
|
agent_id: Id, |
|
|
|
|
) -> EndpResult<()> { |
|
|
|
|
) -> EndpResult<retypes::Report> { |
|
|
|
|
repo.transaction(move |mut db| { |
|
|
|
|
for entry in msg.into_vec() { |
|
|
|
|
match entry { |
|
|
|
@ -214,17 +235,23 @@ impl Endpoints { |
|
|
|
|
.map_err(From::from) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub async fn update_agent(repo: Arc<PgRepo>, agent: Agent) -> EndpResult<()> { |
|
|
|
|
pub async fn update_agent(repo: Arc<PgRepo>, agent: Agent) -> EndpResult<retypes::UpdateAgent> { |
|
|
|
|
repo.interact(move |mut db| db.upsert_agent(&agent)).await?; |
|
|
|
|
Ok(()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub async fn update_job(repo: Arc<PgRepo>, job: JobModel) -> EndpResult<()> { |
|
|
|
|
repo.interact(move |mut db| db.update_job(&job)).await?; |
|
|
|
|
pub async fn update_job(repo: Arc<PgRepo>, job: FatJob) -> EndpResult<retypes::UpdateJob> { |
|
|
|
|
let thin_job = split_payload(job).map_err(Error::from)?; |
|
|
|
|
|
|
|
|
|
repo.interact(move |mut db| db.update_job(&thin_job.job)) |
|
|
|
|
.await?; |
|
|
|
|
Ok(()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub async fn update_assigned_job(repo: Arc<PgRepo>, assigned: AssignedJob) -> EndpResult<()> { |
|
|
|
|
pub async fn update_assigned_job( |
|
|
|
|
repo: Arc<PgRepo>, |
|
|
|
|
assigned: AssignedJob, |
|
|
|
|
) -> EndpResult<retypes::UpdateResult> { |
|
|
|
|
repo.interact(move |mut db| db.update_result(&assigned)) |
|
|
|
|
.await?; |
|
|
|
|
Ok(()) |
|
|
|
|