You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
2.6 KiB
106 lines
2.6 KiB
use u_lib::{ |
|
models::* |
|
}; |
|
use warp::{ |
|
Rejection, |
|
Reply, |
|
reply::with_status, |
|
http::StatusCode |
|
}; |
|
use crate::db::{ |
|
Storage |
|
}; |
|
|
|
pub async fn add_agent( |
|
msg: BaseMessage<'_, IAgent>, |
|
db: Storage) -> Result<impl Reply, Rejection> |
|
{ |
|
let result = db.lock() |
|
.unwrap() |
|
.new_agent(msg.into_item()); |
|
match result { |
|
Ok(_) => Ok(warp::reply::json( |
|
&RawMsg("Added".to_string()).as_message() |
|
)), |
|
Err(e) => Ok(warp::reply::json( //TODO: rejection |
|
&RawMsg("Already exist".to_string()).as_message() |
|
)) |
|
} |
|
} |
|
|
|
/* |
|
pub async fn report( |
|
msg: Payload<Vec<JobResult>>, |
|
db: Storage) -> Result<impl Reply, Rejection> |
|
{ |
|
let results = msg.item.into_inner(); |
|
let mut storage = db.results().await; |
|
results.into_iter().for_each(|new_result| { |
|
match storage.get_mut(&new_result.id) { |
|
Some(v) => v.push(new_result), |
|
None => storage.insert(new_result.id, vec![new_result]) |
|
} |
|
}); |
|
Ok(with_status(warp::reply(), StatusCode::OK)) |
|
} |
|
|
|
pub async fn get_job_results( |
|
uid: Uuid, |
|
db: Storage) -> Result<impl Reply, Rejection> |
|
{ |
|
let storage = db.results().await; |
|
match storage.get(&uid) { |
|
Some(v) => Ok(warp::reply::json( |
|
&BaseMessage::new(v.clone()) |
|
)), |
|
None => Err(warp::reject()) |
|
} |
|
} |
|
|
|
pub async fn get_jobs( |
|
db: Storage) -> Result<impl Reply, Rejection> |
|
{ |
|
let mut clients = db.clients().await; |
|
let cli = clients.get_mut(&msg.id).unwrap(); |
|
cli.jobs.iter_mut().for_each(|job: &mut JobMeta| { |
|
if job.state == JobState::Queued { |
|
job.state = JobState::Pending |
|
} |
|
}); |
|
Ok(warp::reply::json( |
|
&BaseMessage::new(cli.jobs.clone()) |
|
)) |
|
} |
|
|
|
pub async fn set_jobs( |
|
uid: Option<Uuid>, |
|
msg: BaseMessage<'_, ItemWrap<JobMetaStorage>>, |
|
db: Storage) -> Result<impl Reply, Rejection> |
|
{ |
|
let mut clients = db.clients().await; |
|
let cli = clients.get_mut(&uid.unwrap_or(msg.id)).unwrap(); |
|
msg.item.0.into_iter().for_each(|(uuid, job)| { |
|
match cli.jobs.get_mut(&uuid) { |
|
Some(cli_job) => *cli_job = job, |
|
None => cli.jobs.push(job) |
|
}; |
|
}); |
|
Ok(()) |
|
|
|
} |
|
*/ |
|
pub async fn get_agents(db: Storage) -> Result<impl Reply, Rejection> { |
|
let result = db.lock().unwrap().get_agents(); |
|
match result { |
|
Ok(r) => Ok(warp::reply::json( |
|
&r.into_message() |
|
)), |
|
Err(e) => { |
|
Err(warp::reject()) |
|
} |
|
} |
|
} |
|
|
|
pub async fn dummy() -> Result<impl Reply, Rejection> { |
|
Ok(String::from("ok")) |
|
}
|
|
|