-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Add serve_web function #11
Draft
dali99
wants to merge
2
commits into
DataTreehouse:main
Choose a base branch
from
dali99:sparql-web
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ pub mod rewriting; | |
pub mod sparql_database; | ||
mod sparql_result_to_polars; | ||
pub mod splitter; | ||
pub mod web; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use std::sync::Arc; | ||
|
||
use axum::response::Html; | ||
use axum::Form; | ||
use axum::{self, Router}; | ||
use axum::{extract::State, routing::get}; | ||
|
||
use oxigraph::sparql::{results::QueryResultsFormat, QueryResults, QuerySolutionIter}; | ||
use oxrdf::Variable; | ||
use serde::Deserialize; | ||
|
||
use crate::sparql_database::SparqlQueryable; | ||
|
||
#[derive(Clone)] | ||
struct AppState { | ||
sparql_engine: Arc<(dyn SparqlQueryable)>, | ||
} | ||
|
||
pub async fn launch_web(sparql_engine: Arc<(dyn SparqlQueryable)>, address: &str) { | ||
let state = AppState { sparql_engine }; | ||
|
||
let app: Router = Router::new() | ||
.route("/", get(|| async { "Hello, World!" })) | ||
.route("/query", get(get_query).post(post_query)) | ||
.with_state(state); | ||
|
||
let listener = tokio::net::TcpListener::bind(address).await.unwrap(); | ||
axum::serve(listener, app).await.unwrap(); | ||
} | ||
|
||
async fn get_query() -> Html<&'static str> { | ||
Html(include_str!("web/sparql.html")) | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
struct SparqlQuery { | ||
query: String, | ||
} | ||
|
||
async fn post_query(State(state): State<AppState>, Form(form): Form<SparqlQuery>) -> String { | ||
let sparql_engine = state.sparql_engine; | ||
|
||
let query = spargebra::Query::parse(&form.query, None).unwrap(); | ||
|
||
let query_result: QueryResults = match sparql_engine.execute(&query).await { | ||
Ok(v) => { | ||
let variables: Arc<[Variable]> = v.first().unwrap().variables().into(); | ||
let iter = v | ||
.into_iter() | ||
.map(|qs| Ok(qs.values().iter().map(|t| t.clone()).collect())); | ||
let qsi = QuerySolutionIter::new(variables, iter); | ||
qsi.into() | ||
dali99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
Err(_) => todo!(), | ||
}; | ||
|
||
let mut results = Vec::new(); | ||
query_result.write(&mut results, QueryResultsFormat::Json); | ||
|
||
String::from_utf8_lossy(&results).into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<!-- | ||
From https://github.com/oxigraph/oxigraph/blob/22d956823f6257b7840f2116858e1431393cdf84/cli/templates/query.html | ||
Licensed under Apache 2.0 or MIT | ||
Vincent Emonet | ||
Thomas Tanon | ||
--> | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Chrontext - Query</title> | ||
<link href="https://unpkg.com/@zazuko/yasgui@4/build/yasgui.min.css" rel="stylesheet" type="text/css" /> | ||
<script src="https://unpkg.com/@zazuko/yasgui@4/build/yasgui.min.js"></script> | ||
<link rel="icon" type="image/svg+xml" href="/logo.svg"> | ||
</head> | ||
<body> | ||
<div id="yasgui"></div> | ||
<script> | ||
const url = new URL(window.location.href.endsWith('/') ? window.location.href.slice(0, -1) : window.location.href); | ||
new Yasgui(document.getElementById("yasgui"), { | ||
requestConfig: { endpoint: url.origin + "/query" } | ||
}); | ||
</script> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason I might be missing as to maybe implementing this over
engine
instead ofSparqlQueryables
?I think this is only letting you run queries on some underlying sparql database, and I'm worried about how this might interact with the virtualization stuff.
the
Engine::query
return type(DataFrame, HashMap<String, RDFNodeType>, Vec<Context>)
is pretty complicated, so I need to take a deeper dive into how this worksThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be implemented over engine, yes.
The
DataFrame, HashMap<String, RDFNodeType>
representation is a column-based encoding of a result.For each variable, there is a column. The map holds the RDF type of the column. In case the variable has multiple types, there is a Struct-column with multiple columns for that type.
There is https://github.com/DataTreehouse/maplib/blob/main/lib/representation/src/polars_to_rdf.rs which maps the df and types to a row based result of the kind we need here. Might need a bit of cleaning up though, but should be fairly well tested.