diff --git a/server/.sqlx/query-4824bfa1ca8109255aa81c5b9744c4222a914ce2d09a112c4dda259c425b3587.json b/server/.sqlx/query-4824bfa1ca8109255aa81c5b9744c4222a914ce2d09a112c4dda259c425b3587.json new file mode 100644 index 000000000..0db008ca7 --- /dev/null +++ b/server/.sqlx/query-4824bfa1ca8109255aa81c5b9744c4222a914ce2d09a112c4dda259c425b3587.json @@ -0,0 +1,62 @@ +{ + "db_name": "SQLite", + "query": "SELECT * FROM de WHERE key = ?", + "describe": { + "columns": [ + { + "name": "key", + "ordinal": 0, + "type_info": "Text" + }, + { + "name": "name", + "ordinal": 1, + "type_info": "Text" + }, + { + "name": "tumonline_room_nr", + "ordinal": 2, + "type_info": "Int64" + }, + { + "name": "type", + "ordinal": 3, + "type_info": "Text" + }, + { + "name": "type_common_name", + "ordinal": 4, + "type_info": "Text" + }, + { + "name": "lat", + "ordinal": 5, + "type_info": "Float" + }, + { + "name": "lon", + "ordinal": 6, + "type_info": "Float" + }, + { + "name": "data", + "ordinal": 7, + "type_info": "Text" + } + ], + "parameters": { + "Right": 1 + }, + "nullable": [ + false, + false, + true, + false, + false, + false, + false, + false + ] + }, + "hash": "4824bfa1ca8109255aa81c5b9744c4222a914ce2d09a112c4dda259c425b3587" +} diff --git a/server/.sqlx/query-84ee8b091b1e17632363a2c7e4efdc195d4716f7857a99494918905ee5bfb868.json b/server/.sqlx/query-84ee8b091b1e17632363a2c7e4efdc195d4716f7857a99494918905ee5bfb868.json new file mode 100644 index 000000000..7863b66c5 --- /dev/null +++ b/server/.sqlx/query-84ee8b091b1e17632363a2c7e4efdc195d4716f7857a99494918905ee5bfb868.json @@ -0,0 +1,62 @@ +{ + "db_name": "SQLite", + "query": "SELECT * FROM en WHERE key = ?", + "describe": { + "columns": [ + { + "name": "key", + "ordinal": 0, + "type_info": "Text" + }, + { + "name": "name", + "ordinal": 1, + "type_info": "Text" + }, + { + "name": "tumonline_room_nr", + "ordinal": 2, + "type_info": "Int64" + }, + { + "name": "type", + "ordinal": 3, + "type_info": "Text" + }, + { + "name": "type_common_name", + "ordinal": 4, + "type_info": "Text" + }, + { + "name": "lat", + "ordinal": 5, + "type_info": "Float" + }, + { + "name": "lon", + "ordinal": 6, + "type_info": "Float" + }, + { + "name": "data", + "ordinal": 7, + "type_info": "Text" + } + ], + "parameters": { + "Right": 1 + }, + "nullable": [ + false, + false, + true, + false, + false, + false, + false, + false + ] + }, + "hash": "84ee8b091b1e17632363a2c7e4efdc195d4716f7857a99494918905ee5bfb868" +} diff --git a/server/.sqlx/query-c407c4d9fe1ce6bd90db434750df722d58068d3187a54cdf95dd8361db2d4c26.json b/server/.sqlx/query-c407c4d9fe1ce6bd90db434750df722d58068d3187a54cdf95dd8361db2d4c26.json new file mode 100644 index 000000000..8dd5185d2 --- /dev/null +++ b/server/.sqlx/query-c407c4d9fe1ce6bd90db434750df722d58068d3187a54cdf95dd8361db2d4c26.json @@ -0,0 +1,32 @@ +{ + "db_name": "SQLite", + "query": "\n SELECT key, visible_id, type\n FROM aliases\n WHERE key = ? OR key = ?\n ", + "describe": { + "columns": [ + { + "name": "key", + "ordinal": 0, + "type_info": "Text" + }, + { + "name": "visible_id", + "ordinal": 1, + "type_info": "Text" + }, + { + "name": "type", + "ordinal": 2, + "type_info": "Text" + } + ], + "parameters": { + "Right": 2 + }, + "nullable": [ + false, + false, + false + ] + }, + "hash": "c407c4d9fe1ce6bd90db434750df722d58068d3187a54cdf95dd8361db2d4c26" +} diff --git a/server/.sqlx/query-ca4c11b14efa6564387df4dbc475539734b181798e2dba867f40bc460c7b9a3a.json b/server/.sqlx/query-ca4c11b14efa6564387df4dbc475539734b181798e2dba867f40bc460c7b9a3a.json new file mode 100644 index 000000000..13d4ee508 --- /dev/null +++ b/server/.sqlx/query-ca4c11b14efa6564387df4dbc475539734b181798e2dba867f40bc460c7b9a3a.json @@ -0,0 +1,20 @@ +{ + "db_name": "SQLite", + "query": "SELECT data FROM en WHERE key = ?", + "describe": { + "columns": [ + { + "name": "data", + "ordinal": 0, + "type_info": "Text" + } + ], + "parameters": { + "Right": 1 + }, + "nullable": [ + false + ] + }, + "hash": "ca4c11b14efa6564387df4dbc475539734b181798e2dba867f40bc460c7b9a3a" +} diff --git a/server/.sqlx/query-fe271671edc17aafa97e4da7ea8d7097d4186c3eb97649a45abc378fc8a2a83b.json b/server/.sqlx/query-fe271671edc17aafa97e4da7ea8d7097d4186c3eb97649a45abc378fc8a2a83b.json new file mode 100644 index 000000000..3809e3cac --- /dev/null +++ b/server/.sqlx/query-fe271671edc17aafa97e4da7ea8d7097d4186c3eb97649a45abc378fc8a2a83b.json @@ -0,0 +1,20 @@ +{ + "db_name": "SQLite", + "query": "SELECT data FROM de WHERE key = ?", + "describe": { + "columns": [ + { + "name": "data", + "ordinal": 0, + "type_info": "Text" + } + ], + "parameters": { + "Right": 1 + }, + "nullable": [ + false + ] + }, + "hash": "fe271671edc17aafa97e4da7ea8d7097d4186c3eb97649a45abc378fc8a2a83b" +} diff --git a/server/main-api/src/entries/get.rs b/server/main-api/src/entries/get.rs index c71db8ebb..7d884329e 100644 --- a/server/main-api/src/entries/get.rs +++ b/server/main-api/src/entries/get.rs @@ -2,33 +2,36 @@ use crate::models::DBRoomKeyAlias; use crate::utils; use actix_web::{get, web, HttpResponse}; use log::error; -use sqlx::{Executor}; +use sqlx::SqlitePool; #[get("/api/get/{id}")] pub async fn get_handler( params: web::Path, web::Query(args): web::Query, - data: web::Data + data: web::Data, ) -> HttpResponse { - let (probable_id, redirect_url) = match get_alias_and_redirect(&data.db, ¶ms.into_inner()).await { - Some(alias_and_redirect) => alias_and_redirect, - None => return HttpResponse::NotFound().body("Not found"), - }; + let (probable_id, redirect_url) = + match get_alias_and_redirect(&data.db, ¶ms.into_inner()).await { + Some(alias_and_redirect) => alias_and_redirect, + None => return HttpResponse::NotFound().body("Not found"), + }; let result = match args.should_use_english() { true => { - sqlx::query!("SELECT data FROM en WHERE key = ?",probable_id) - .fetch_one::(&data.db).await + sqlx::query_scalar!("SELECT data FROM en WHERE key = ?", probable_id) + .fetch_optional(&data.db) + .await } false => { - sqlx::query!("SELECT data FROM de WHERE key = ?",probable_id) - .fetch_one::(&data.db).await + sqlx::query_scalar!("SELECT data FROM de WHERE key = ?", probable_id) + .fetch_optional(&data.db) + .await } }; match result { - Ok(d) => match d.len() { - 0 => HttpResponse::NotFound().body("Not found"), - _ => { - let mut response_json = d[0].clone(); + Ok(d) => match d { + None => HttpResponse::NotFound().body("Not found"), + Some(d) => { + let mut response_json = d.clone(); // We don not want to serialise this data at any point in the server. // This just flows through the server, but adding redirect_url to the response is necessary response_json.pop(); // remove last } @@ -47,8 +50,9 @@ pub async fn get_handler( } } -async fn get_alias_and_redirect<'a, Conn: Executor<'a>>(conn: Conn, query: &str) -> Option<(String, String)> { - let result=sqlx::query!( +async fn get_alias_and_redirect(conn: &SqlitePool, query: &str) -> Option<(String, String)> { + let result = sqlx::query_as!( + DBRoomKeyAlias, r#" SELECT key, visible_id, type FROM aliases @@ -56,12 +60,14 @@ async fn get_alias_and_redirect<'a, Conn: Executor<'a>>(conn: Conn, query: &str) "#, query, query - ).fetch_one::(conn).await?; + ) + .fetch_all(conn) + .await; match result { Ok(d) => { let redirect_url = match d.len() { 0 => return None, // not key or alias - 1 => extract_redirect_exact_match(&d[0].type_, &d[0].visible_id), + 1 => extract_redirect_exact_match(&d[0].r#type, &d[0].visible_id), _ => { let keys = d .clone() diff --git a/server/main-api/src/main.rs b/server/main-api/src/main.rs index c4c3e5da7..63a4ebf44 100644 --- a/server/main-api/src/main.rs +++ b/server/main-api/src/main.rs @@ -2,9 +2,9 @@ use actix_cors::Cors; use actix_web::{get, middleware, web, App, HttpResponse, HttpServer}; use actix_web_prom::PrometheusMetricsBuilder; use log::{debug, error, info}; +use sqlx::prelude::*; use sqlx::sqlite::SqlitePoolOptions; use sqlx::SqlitePool; -use sqlx::prelude::*; use std::collections::HashMap; use structured_logger::async_json::new_writer; use structured_logger::Builder; @@ -19,7 +19,7 @@ mod utils; const MAX_JSON_PAYLOAD: usize = 1024 * 1024; // 1 MB #[derive(Clone, Debug)] -struct AppData { +pub struct AppData { db: SqlitePool, } diff --git a/server/main-api/src/maps/mod.rs b/server/main-api/src/maps/mod.rs index a6f6f5580..f234db364 100644 --- a/server/main-api/src/maps/mod.rs +++ b/server/main-api/src/maps/mod.rs @@ -27,14 +27,19 @@ pub fn configure(cfg: &mut web::ServiceConfig) { } } -async fn get_localised_data(conn:&SqlitePool,id: &str, should_use_english: bool) -> Result { - +async fn get_localised_data( + conn: &SqlitePool, + id: &str, + should_use_english: bool, +) -> Result { let result = if should_use_english { - sqlx::query!("SELECT * FROM en WHERE key = ?",id) - .fetch_all::(conn).await? + sqlx::query_as!(DBRoomEntry, "SELECT * FROM en WHERE key = ?", id) + .fetch_all(conn) + .await } else { - sqlx::query!("SELECT * FROM de WHERE key = ?",id) - .fetch_all::(conn).await? + sqlx::query_as!(DBRoomEntry, "SELECT * FROM de WHERE key = ?", id) + .fetch_all(conn) + .await }; match result { @@ -122,11 +127,12 @@ fn load_default_image() -> Vec { #[get("/{id}")] pub async fn maps_handler( params: web::Path, - web::Query(args): web::Query,data: web::Data + web::Query(args): web::Query, + data: web::Data, ) -> HttpResponse { let start_time = Instant::now(); let id = params.into_inner(); - let data = match get_localised_data(&data.db,&id, args.should_use_english()).await { + let data = match get_localised_data(&data.db, &id, args.should_use_english()).await { Ok(data) => data, Err(e) => { return e; diff --git a/server/main-api/src/maps/overlay_map.rs b/server/main-api/src/maps/overlay_map.rs index 84f49e3b1..847541a2d 100644 --- a/server/main-api/src/maps/overlay_map.rs +++ b/server/main-api/src/maps/overlay_map.rs @@ -4,14 +4,14 @@ use futures::{stream::FuturesUnordered, StreamExt}; use log::warn; pub(crate) struct OverlayMapTask { - pub(crate) x: f32, - pub(crate) y: f32, + pub(crate) x: f64, + pub(crate) y: f64, pub(crate) z: u32, } impl OverlayMapTask { pub fn with(entry: &DBRoomEntry) -> Self { - let zoom = match entry.type_.as_str() { + let zoom = match entry.r#type.as_str() { "campus" => 14, "area" | "site" => 15, "building" | "joined_building" => 16, @@ -77,11 +77,11 @@ impl OverlayMapTask { } } -fn lat_lon_z_to_xyz(lat_deg: f32, lon_deg: f32, zoom: u32) -> (f32, f32, u32) { +fn lat_lon_z_to_xyz(lat_deg: f64, lon_deg: f64, zoom: u32) -> (f64, f64, u32) { let lat_rad = lat_deg.to_radians(); - let n = 2_u32.pow(zoom) as f32; + let n = 2_u32.pow(zoom) as f64; let xtile = (lon_deg + 180.0) / 360.0 * n; - let ytile = (1.0 - lat_rad.tan().asinh() / std::f32::consts::PI) / 2.0 * n; + let ytile = (1.0 - lat_rad.tan().asinh() / std::f64::consts::PI) / 2.0 * n; (xtile, ytile, zoom) } @@ -109,16 +109,16 @@ mod overlay_tests { #[test] fn test_lat_lon_z_to_xyz() { let (x, y, _) = lat_lon_z_to_xyz(52.520_008, 13.404_954, 17); - assert_eq!(x, 70416.59_f32); - assert_eq!(y, 42985.734_f32); + assert_eq!(x, 70416.59_f64); + assert_eq!(y, 42985.734_f64); } #[test] fn test_lat_lon_no_zoom_mut() { for x in -5..5 { - let x = x as f32; + let x = x as f64; for y in -5..5 { - let y = y as f32; + let y = y as f64; for z in 0..20 { let (_, _, zg) = lat_lon_z_to_xyz(x + y / 100.0, y, z); assert_eq!(z, zg); diff --git a/server/main-api/src/models.rs b/server/main-api/src/models.rs index 1785f77c0..af8ad25ff 100644 --- a/server/main-api/src/models.rs +++ b/server/main-api/src/models.rs @@ -1,13 +1,12 @@ - #[derive(Debug, Clone)] pub struct DBRoomEntry { pub key: String, pub name: String, - pub tumonline_room_nr: Option, - pub type_: String, + pub tumonline_room_nr: Option, + pub r#type: String, pub type_common_name: String, - pub lat: f32, - pub lon: f32, + pub lat: f64, + pub lon: f64, pub data: String, } @@ -15,5 +14,5 @@ pub struct DBRoomEntry { pub struct DBRoomKeyAlias { pub key: String, pub visible_id: String, - pub type_: String, + pub r#type: String, } diff --git a/server/main-api/src/utils.rs b/server/main-api/src/utils.rs index 24d6d1786..98219e562 100644 --- a/server/main-api/src/utils.rs +++ b/server/main-api/src/utils.rs @@ -9,4 +9,4 @@ impl LangQueryArgs { pub fn should_use_english(&self) -> bool { self.lang.as_ref().map_or(false, |c| c == "en") } -} \ No newline at end of file +}