1
0
mirror of https://github.com/ellmau/adf-obdd.git synced 2025-12-20 09:39:38 +01:00

WIP: return immediately for solving and add get endpoint

This commit is contained in:
monsterkrampe 2023-03-31 16:27:45 +02:00
parent ba752514ce
commit a0ad716b28
No known key found for this signature in database
GPG Key ID: B8ADC1F5A5CE5057
5 changed files with 119 additions and 40 deletions

23
Cargo.lock generated
View File

@ -283,6 +283,7 @@ dependencies = [
"adf_bdd",
"argon2",
"env_logger 0.9.3",
"futures-util",
"log",
"mongodb",
"names",
@ -1143,9 +1144,9 @@ dependencies = [
[[package]]
name = "futures-core"
version = "0.3.27"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86d7a0c1aa76363dac491de0ee99faf6941128376f1cf96f07db7603b7de69dd"
checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c"
[[package]]
name = "futures-executor"
@ -1160,19 +1161,19 @@ dependencies = [
[[package]]
name = "futures-io"
version = "0.3.27"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89d422fa3cbe3b40dca574ab087abb5bc98258ea57eea3fd6f1fa7162c778b91"
checksum = "4fff74096e71ed47f8e023204cfd0aa1289cd54ae5430a9523be060cdb849964"
[[package]]
name = "futures-macro"
version = "0.3.27"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3eb14ed937631bd8b8b8977f2c198443447a8355b6e3ca599f38c975e5a963b6"
checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
"syn 2.0.8",
]
[[package]]
@ -1183,15 +1184,15 @@ checksum = "ec93083a4aecafb2a80a885c9de1f0ccae9dbd32c2bb54b0c3a65690e0b8d2f2"
[[package]]
name = "futures-task"
version = "0.3.27"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd65540d33b37b16542a0438c12e6aeead10d4ac5d05bd3f805b8f35ab592879"
checksum = "76d3d132be6c0e6aa1534069c705a74a5997a356c0dc2f86a47765e5617c5b65"
[[package]]
name = "futures-util"
version = "0.3.27"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ef6b17e481503ec85211fed8f39d1970f128935ca1f814cd32ac4a6842e84ab"
checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533"
dependencies = [
"futures-core",
"futures-io",

View File

@ -24,6 +24,7 @@ actix-identity = "0.5.2"
argon2 = "0.5.0"
actix-session = { version="0.7.2", features = ["cookie-session"] }
names = "0.14.0"
futures-util = "0.3.28"
[features]
cors_for_local_development = []

View File

@ -1,16 +1,18 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, RwLock};
use actix_identity::Identity;
use actix_web::rt::spawn;
use actix_web::rt::task::spawn_blocking;
use actix_web::rt::time::timeout;
use actix_web::{post, put, web, HttpMessage, HttpRequest, HttpResponse, Responder};
use actix_web::{get, post, put, web, HttpMessage, HttpRequest, HttpResponse, Responder};
use adf_bdd::datatypes::adf::VarContainer;
use adf_bdd::datatypes::{BddNode, Term, Var};
use mongodb::bson::doc;
use mongodb::bson::{to_bson, Bson};
use names::{Generator, Name};
use serde::{Deserialize, Serialize};
use futures_util::FutureExt;
use adf_bdd::adf::{Adf, DoubleLabeledGraph};
use adf_bdd::adfbiodivine::Adf as BdAdf;
@ -22,13 +24,13 @@ use crate::user::{username_exists, User};
type Ac = Vec<Term>;
type AcDb = Vec<String>;
#[derive(Clone, Deserialize, Serialize)]
#[derive(Copy, Clone, Deserialize, Serialize)]
pub(crate) enum Parsing {
Naive,
Hybrid,
}
#[derive(Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
#[derive(Copy, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
pub(crate) enum Strategy {
Ground,
Complete,
@ -175,6 +177,32 @@ async fn adf_problem_exists(
.is_some()
}
#[derive(Serialize)]
struct AdfProblemInfo {
name: String,
code: String,
parsing_used: Parsing,
acs_per_strategy: AcsPerStrategy,
running_tasks: Vec<Task>,
}
impl AdfProblemInfo {
fn from_adf_prob_and_tasks(adf: AdfProblem, tasks: &HashSet<RunningInfo>) -> Self {
AdfProblemInfo {
name: adf.name.clone(),
code: adf.code,
parsing_used: adf.parsing_used,
acs_per_strategy: adf.acs_per_strategy,
running_tasks: tasks
.iter()
.filter_map(|t| {
(t.adf_name == adf.name && t.username == adf.username).then_some(t.task)
})
.collect(),
}
}
}
#[post("/add")]
async fn add_adf_problem(
req: HttpRequest,
@ -274,6 +302,8 @@ async fn add_adf_problem(
let adf_problem_input_clone = adf_problem_input.clone();
let username_clone = username.clone();
let problem_name_clone = problem_name.clone();
// TODO: change this: we want to return a response from the request whil the computation is running
let adf_res = timeout(
COMPUTE_TIME,
spawn_blocking(move || {
@ -322,7 +352,10 @@ async fn add_adf_problem(
Ok(Err(err)) => HttpResponse::InternalServerError().body(err.to_string()),
Ok(Ok(Err(err))) => HttpResponse::InternalServerError().body(err.to_string()),
Ok(Ok(Ok((adf, ac_and_graph)))) => {
let acs = AcsPerStrategy { parse_only: Some(vec![ac_and_graph]), ..Default::default()};
let acs = AcsPerStrategy {
parse_only: Some(vec![ac_and_graph]),
..Default::default()
};
let adf_problem: AdfProblem = AdfProblem {
name: problem_name,
@ -336,7 +369,7 @@ async fn add_adf_problem(
let result = adf_coll.insert_one(&adf_problem, None).await;
match result {
Ok(_) => HttpResponse::Ok().json(adf_problem), // TODO: return name of problem here (especially since it may be generated)
Ok(_) => HttpResponse::Ok().json(AdfProblemInfo::from_adf_prob_and_tasks(adf_problem, &HashSet::new())), // there should be no tasks running here
Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
}
}
@ -400,7 +433,9 @@ async fn solve_adf_problem(
let username_clone = username.clone();
let problem_name_clone = problem_name.clone();
let strategy_clone = adf_problem_input.strategy.clone();
let acs_and_graphs_res = timeout(
// TODO: change this: we want to return a response from the request whil the computation is running
let acs_and_graphs_fut = timeout(
COMPUTE_TIME,
spawn_blocking(move || {
let running_info = RunningInfo {
@ -456,26 +491,68 @@ async fn solve_adf_problem(
acs_and_graphs
}),
)
.await;
);
match acs_and_graphs_res {
Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
Ok(Err(err)) => HttpResponse::InternalServerError().body(err.to_string()),
Ok(Ok(acs_and_graphs)) => {
let result = adf_coll.update_one(doc! { "name": &problem_name, "username": &username }, match adf_problem_input.strategy {
Strategy::Complete => doc! { "$set": { "acs_per_strategy.complete": Some(&acs_and_graphs) } },
Strategy::Ground => doc! { "$set": { "acs_per_strategy.ground": Some(&acs_and_graphs) } },
Strategy::Stable => doc! { "$set": { "acs_per_strategy.stable": Some(&acs_and_graphs) } },
Strategy::StableCountingA => doc! { "$set": { "acs_per_strategy.stable_counting_a": Some(&acs_and_graphs) } },
Strategy::StableCountingB => doc! { "$set": { "acs_per_strategy.stable_counting_b": Some(&acs_and_graphs) } },
Strategy::StableNogood => doc! { "$set": { "acs_per_strategy.stable_nogood": Some(&acs_and_graphs) } },
}, None).await;
spawn(acs_and_graphs_fut.then(move |acs_and_graphs_res| async move {
match acs_and_graphs_res {
// TODO: error states should somehow be set in the database since the status codes are irrelevant in this future
Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
Ok(Err(err)) => HttpResponse::InternalServerError().body(err.to_string()),
Ok(Ok(acs_and_graphs)) => {
let result = adf_coll.update_one(doc! { "name": problem_name, "username": username }, match adf_problem_input.strategy {
Strategy::Complete => doc! { "$set": { "acs_per_strategy.complete": Some(&acs_and_graphs) } },
Strategy::Ground => doc! { "$set": { "acs_per_strategy.ground": Some(&acs_and_graphs) } },
Strategy::Stable => doc! { "$set": { "acs_per_strategy.stable": Some(&acs_and_graphs) } },
Strategy::StableCountingA => doc! { "$set": { "acs_per_strategy.stable_counting_a": Some(&acs_and_graphs) } },
Strategy::StableCountingB => doc! { "$set": { "acs_per_strategy.stable_counting_b": Some(&acs_and_graphs) } },
Strategy::StableNogood => doc! { "$set": { "acs_per_strategy.stable_nogood": Some(&acs_and_graphs) } },
}, None).await;
match result {
Ok(_) => HttpResponse::Ok().json(acs_and_graphs),
Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
match result {
Ok(_) => HttpResponse::Ok().json(acs_and_graphs),
Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
}
}
}
}
};
}));
HttpResponse::Ok().body("Parsing started...")
}
#[get("/{problem_name}")]
async fn get_adf_problem(
app_state: web::Data<AppState>,
identity: Option<Identity>,
path: web::Path<String>,
) -> impl Responder {
let problem_name = path.into_inner();
let adf_coll: mongodb::Collection<AdfProblem> = app_state
.mongodb_client
.database(DB_NAME)
.collection(ADF_COLL);
let username = match identity.map(|id| id.id()) {
None => {
return HttpResponse::Unauthorized().body("You need to login to get an ADF problem.")
}
Some(Err(err)) => return HttpResponse::InternalServerError().body(err.to_string()),
Some(Ok(username)) => username,
};
let adf_problem = match adf_coll
.find_one(doc! { "name": &problem_name, "username": &username }, None)
.await
{
Err(err) => return HttpResponse::InternalServerError().body(err.to_string()),
Ok(None) => {
return HttpResponse::NotFound()
.body(format!("ADF problem with name {problem_name} not found."))
}
Ok(Some(prob)) => prob,
};
HttpResponse::Ok().json(AdfProblemInfo::from_adf_prob_and_tasks(
adf_problem,
&app_state.currently_running.lock().unwrap(),
))
}

View File

@ -3,6 +3,7 @@ use std::sync::Mutex;
use std::time::Duration;
use mongodb::Client;
use serde::Serialize;
use crate::adf::Strategy;
@ -16,7 +17,7 @@ pub(crate) const DB_NAME: &str = "adf-obdd";
pub(crate) const USER_COLL: &str = "users";
pub(crate) const ADF_COLL: &str = "adf-problems";
#[derive(Clone, PartialEq, Eq, Hash)]
#[derive(Copy, Clone, PartialEq, Eq, Hash, Serialize)]
pub(crate) enum Task {
Parse,
Solve(Strategy),

View File

@ -280,8 +280,7 @@ async fn update_user(
Some(id) => match id.id() {
Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
Ok(username) => {
if user.username != username && username_exists(&user_coll, &user.username).await
{
if user.username != username && username_exists(&user_coll, &user.username).await {
return HttpResponse::Conflict()
.body("Username is already taken. Please pick another one!");
}