Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Nov 12, 2023
1 parent 8627ad8 commit 1f542fa
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 45 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 24 additions & 18 deletions server/main-api/src/entries/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
web::Query(args): web::Query<utils::LangQueryArgs>,
data: web::Data<crate::AppData>
data: web::Data<crate::AppData>,
) -> HttpResponse {
let (probable_id, redirect_url) = match get_alias_and_redirect(&data.db, &params.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, &params.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::<String>(&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::<String>(&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 }
Expand All @@ -47,21 +50,24 @@ 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
WHERE key = ? OR key = ?
"#,
query,
query
).fetch_one::<DBRoomKeyAlias>(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()
Expand Down
4 changes: 2 additions & 2 deletions server/main-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
}

Expand Down
22 changes: 14 additions & 8 deletions server/main-api/src/maps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DBRoomEntry, HttpResponse> {

async fn get_localised_data(
conn: &SqlitePool,
id: &str,
should_use_english: bool,
) -> Result<DBRoomEntry, HttpResponse> {
let result = if should_use_english {
sqlx::query!("SELECT * FROM en WHERE key = ?",id)
.fetch_all::<DBRoomEntry>(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::<DBRoomEntry>(conn).await?
sqlx::query_as!(DBRoomEntry, "SELECT * FROM de WHERE key = ?", id)
.fetch_all(conn)
.await
};

match result {
Expand Down Expand Up @@ -122,11 +127,12 @@ fn load_default_image() -> Vec<u8> {
#[get("/{id}")]
pub async fn maps_handler(
params: web::Path<String>,
web::Query(args): web::Query<utils::LangQueryArgs>,data: web::Data<crate::AppData>
web::Query(args): web::Query<utils::LangQueryArgs>,
data: web::Data<crate::AppData>,
) -> 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;
Expand Down
20 changes: 10 additions & 10 deletions server/main-api/src/maps/overlay_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 1f542fa

Please sign in to comment.