Skip to content

Commit

Permalink
Send query to reveaal and insert result in database
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderManich committed Dec 4, 2023
1 parent 9abfd2c commit 3351455
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Ecdar-ProtoBuf
Submodule Ecdar-ProtoBuf updated 2 files
+10 −0 api.proto
+1 −0 services.proto
102 changes: 91 additions & 11 deletions src/api/ecdar_api.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use super::server::server::{
ecdar_api_auth_server::EcdarApiAuth,
ecdar_api_server::EcdarApi,
ecdar_backend_server::EcdarBackend,
get_auth_token_request::{user_credentials, UserCredentials},
CreateAccessRequest, CreateModelRequest, CreateModelResponse, CreateQueryRequest,
CreateUserRequest, DeleteAccessRequest, DeleteModelRequest, DeleteQueryRequest,
GetAuthTokenRequest, GetAuthTokenResponse, GetModelRequest, GetModelResponse,
ListModelsInfoResponse, Query, QueryRequest, QueryResponse, SimulationStartRequest,
SimulationStepRequest, SimulationStepResponse, UpdateAccessRequest, UpdateModelRequest,
UpdateQueryRequest, UpdateUserRequest, UserTokenResponse,
use super::{
reveaal_context::ReveaalContext,

Check warning on line 2 in src/api/ecdar_api.rs

View workflow job for this annotation

GitHub Actions / Clippy lint and check

unused import: `reveaal_context::ReveaalContext`

warning: unused import: `reveaal_context::ReveaalContext` --> src/api/ecdar_api.rs:2:5 | 2 | reveaal_context::ReveaalContext, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default

Check warning on line 2 in src/api/ecdar_api.rs

View workflow job for this annotation

GitHub Actions / Clippy lint and check

unused import: `reveaal_context::ReveaalContext`

warning: unused import: `reveaal_context::ReveaalContext` --> src/api/ecdar_api.rs:2:5 | 2 | reveaal_context::ReveaalContext, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
server::server::{
ecdar_api_auth_server::EcdarApiAuth,
ecdar_api_server::EcdarApi,
ecdar_backend_server::EcdarBackend,
get_auth_token_request::{user_credentials, UserCredentials},
CreateAccessRequest, CreateModelRequest, CreateModelResponse, CreateQueryRequest,
CreateUserRequest, DeleteAccessRequest, DeleteModelRequest, DeleteQueryRequest,
GetAuthTokenRequest, GetAuthTokenResponse, GetModelRequest, GetModelResponse,
ListModelsInfoResponse, Query, QueryRequest, QueryResponse, SendQueryRequest,
SendQueryResponse, SimulationStartRequest, SimulationStepRequest, SimulationStepResponse,
UpdateAccessRequest, UpdateModelRequest, UpdateQueryRequest, UpdateUserRequest,
UserTokenResponse,
},
};
use crate::api::context_collection::ContextCollection;
use crate::api::{
Expand Down Expand Up @@ -701,6 +705,82 @@ impl EcdarApi for ConcreteEcdarApi {
},
}
}

/// Sends a query to be run on Reveaal.
/// After query is run the result is stored in the database.
///
/// Returns the response that is received from Reveaal.
async fn send_query(
&self,
request: Request<SendQueryRequest>,
) -> Result<Response<SendQueryResponse>, Status> {
let message = request.get_ref();

let uid = request.uid().unwrap();

// Verify user access
self.contexts
.access_context
.get_access_by_uid_and_model_id(uid, message.model_id)
.await
.map_err(|err| Status::new(Code::Internal, err.to_string()))?
.ok_or_else(|| {
Status::new(Code::PermissionDenied, "User does not have access to model")
})?;

// Get model from database
let model = self
.contexts
.model_context
.get_by_id(message.model_id)
.await
.map_err(|err| Status::new(Code::Internal, err.to_string()))?
.ok_or_else(|| Status::new(Code::NotFound, "Model not found"))?;

// Get query from database
let query = self
.contexts
.query_context
.get_by_id(message.id)
.await
.map_err(|err| Status::new(Code::Internal, err.to_string()))?
.ok_or_else(|| Status::new(Code::NotFound, "Query not found"))?;

// Construct query request to send to Reveaal
let query_request = Request::new(QueryRequest {
user_id: uid,
query_id: message.id,
query: query.string.clone(),
components_info: serde_json::from_value(model.components_info).unwrap(),
settings: Default::default(),
});

// Run query on Reveaal
let query_result = self
.contexts
.reveaal_context
.send_query(query_request)
.await?;

// Update query result in database
self.contexts
.query_context
.update(query::Model {
id: query.id,
string: query.string.clone(),
result: Some(
serde_json::to_value(query_result.get_ref().result.clone().unwrap()).unwrap(),
),
outdated: false,
model_id: query.model_id,
})
.await
.map_err(|err| Status::new(Code::Internal, err.to_string()))?;

Ok(Response::new(SendQueryResponse {
response: Some(query_result.into_inner()),
}))
}
}

async fn get_auth_find_user_helper(
Expand Down
109 changes: 109 additions & 0 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,116 @@ fn main() {
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute("ModelInfo", "#[derive(sea_orm::FromQueryResult)]")
.type_attribute("Error", "#[derive(serde::Serialize, serde::Deserialize)]")
.type_attribute(
"ComponentsNotInCache",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute("Success", "#[derive(serde::Serialize, serde::Deserialize)]")
.type_attribute(
"ModelFailure",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute(
"ParsingError",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute(
"ReachabilityPath",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute(
"RefinementFailure",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute(
"ReachabilityFailure",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute(
"ImplementationFailure",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute(
"DeterminismFailure",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute(
"ConsistencyFailure",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute("Path", "#[derive(serde::Serialize, serde::Deserialize)]")
.type_attribute(
"ActionFailure",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute(
"StateAction",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute(
"RefinementStateFailure",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute(
"ActionSet",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute(
"Decision",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute("Edge", "#[derive(serde::Serialize, serde::Deserialize)]")
.type_attribute(
"ComponentInstance",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute(
"Disjunction",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute(
"LocationTree",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute("State", "#[derive(serde::Serialize, serde::Deserialize)]")
.type_attribute(
"BinaryLocationOperator",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute(
"LeafLocation",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute(
"Conjunction",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute(
"Constraint",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute("Clock", "#[derive(serde::Serialize, serde::Deserialize)]")
.type_attribute(
"ZeroClock",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute(
"SystemClock",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.enum_attribute("clock", "#[derive(serde::Serialize, serde::Deserialize)]")
.enum_attribute(
"node_type",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute(
"ComponentClock",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.enum_attribute("failure", "#[derive(serde::Serialize, serde::Deserialize)]")
.enum_attribute("rep", "#[derive(serde::Serialize, serde::Deserialize)]")
.enum_attribute("result", "#[derive(serde::Serialize, serde::Deserialize)]")
.compile(&["Ecdar-ProtoBuf/services.proto"], &["Ecdar-ProtoBuf/"])
.unwrap();

Expand Down

0 comments on commit 3351455

Please sign in to comment.