Refactor, fixed search
This commit is contained in:
@@ -2,6 +2,7 @@ use crate::db;
|
||||
use crate::error_handler::CustomError;
|
||||
use crate::schema::airports;
|
||||
use diesel::prelude::*;
|
||||
use log::trace;
|
||||
use postgis_diesel::types::*;
|
||||
use postgis_diesel::functions::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -23,7 +24,8 @@ pub struct Airport {
|
||||
pub point: Point
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Queryable)]
|
||||
#[derive(Serialize, Deserialize, Queryable, QueryableByName)]
|
||||
#[diesel(table_name = airports)]
|
||||
pub struct Airports {
|
||||
pub icao: String,
|
||||
pub id: i32,
|
||||
@@ -41,32 +43,28 @@ pub struct Airports {
|
||||
}
|
||||
|
||||
impl Airports {
|
||||
pub fn find_all(bounds: Option<Polygon<Point>>, category: Option<String>, limit: i32, page: i32) -> Result<Vec<Self>, CustomError> {
|
||||
pub fn get_all(bounds: Option<Polygon<Point>>, category: Option<String>, filter: Option<String>, limit: i32, page: i32) -> Result<Vec<Self>, CustomError> {
|
||||
let mut conn = db::connection()?;
|
||||
let airports;
|
||||
if let Some(category) = category {
|
||||
airports = airports::table
|
||||
.limit(limit as i64)
|
||||
.filter(airports::id.gt(page * limit).and(match bounds {
|
||||
Some(b) => st_contains(b, airports::point),
|
||||
None => {
|
||||
let polygon: Polygon<Point> = Polygon::new(Some(4326));
|
||||
st_contains(polygon, airports::point)
|
||||
}
|
||||
}).and(airports::category.eq(category))).load::<Airports>(&mut conn)?;
|
||||
} else {
|
||||
airports = airports::table
|
||||
.order(airports::category.asc())
|
||||
.limit(limit as i64)
|
||||
.filter(airports::id.gt(page * limit).and(match bounds {
|
||||
Some(b) => st_contains(b, airports::point),
|
||||
None => {
|
||||
let polygon: Polygon<Point> = Polygon::new(Some(4326));
|
||||
st_contains(polygon, airports::point)
|
||||
}
|
||||
}))
|
||||
.load::<Airports>(&mut conn)?;
|
||||
}
|
||||
let mut query = airports::table
|
||||
.limit(limit as i64)
|
||||
.into_boxed();
|
||||
query = query.filter(airports::id.gt(page * limit));
|
||||
|
||||
if let Some(bounds) = bounds {
|
||||
query = query.filter(st_contains(bounds, airports::point));
|
||||
}
|
||||
if let Some(category) = category {
|
||||
query = query.filter(airports::category.eq(category));
|
||||
}
|
||||
if let Some(filter) = filter {
|
||||
query = query.filter(airports::icao
|
||||
.ilike(format!("%{}%", filter))
|
||||
.or(airports::full_name.ilike(format!("%{}%", filter)))
|
||||
)
|
||||
}
|
||||
let debug = diesel::debug_query::<diesel::pg::Pg, _>(&query);
|
||||
trace!("{}", debug);
|
||||
let airports: Vec<Airports> = query.order(airports::category.asc()).load::<Airports>(&mut conn)?;
|
||||
Ok(airports)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,63 +1,115 @@
|
||||
use crate::{airports::{Airport, Airports}, db};
|
||||
use crate::{airports::{Airport, Airports}, db::{self, Metadata}};
|
||||
use actix_web::{delete, get, post, put, web, HttpResponse, HttpRequest};
|
||||
use log::error;
|
||||
use log::{error, warn};
|
||||
use postgis_diesel::types::{Polygon, Point};
|
||||
use serde::{Serialize, Deserialize};
|
||||
use serde_json::json;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct FindAllParams {
|
||||
ne_lat: f64,
|
||||
ne_lon: f64,
|
||||
sw_lat: f64,
|
||||
sw_lon: f64,
|
||||
struct GetAllParameters {
|
||||
filter: Option<String>,
|
||||
bounds: Option<String>,
|
||||
category: Option<String>,
|
||||
limit: i32,
|
||||
page: i32
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
struct Coordinate {
|
||||
lon: f64,
|
||||
lat: f64
|
||||
#[get("/import")]
|
||||
async fn import() -> HttpResponse {
|
||||
db::import_data();
|
||||
HttpResponse::Ok().body({})
|
||||
}
|
||||
|
||||
#[get("/setup")]
|
||||
async fn setup() -> HttpResponse {
|
||||
db::import_data();
|
||||
HttpResponse::Ok().finish()
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct AirportsResponse {
|
||||
pub data: Vec<Airports>,
|
||||
pub meta: Metadata
|
||||
}
|
||||
|
||||
#[get("/airports")]
|
||||
async fn find_all(req: HttpRequest) -> HttpResponse {
|
||||
let params = web::Query::<FindAllParams>::from_query(req.query_string()).unwrap();
|
||||
let mut polygon: Polygon<Point> = Polygon::new(Some(4326));
|
||||
polygon.add_point(Point { x: params.sw_lon, y: params.sw_lat, srid: Some(4326) });
|
||||
polygon.add_point(Point { x: params.ne_lon, y: params.sw_lat, srid: Some(4326) });
|
||||
polygon.add_point(Point { x: params.ne_lon, y: params.ne_lat, srid: Some(4326) });
|
||||
polygon.add_point(Point { x: params.sw_lon, y: params.ne_lat, srid: Some(4326) });
|
||||
polygon.add_point(Point { x: params.sw_lon, y: params.sw_lat, srid: Some(4326) });
|
||||
async fn get_all(req: HttpRequest) -> HttpResponse {
|
||||
let params = web::Query::<GetAllParameters>::from_query(req.query_string()).unwrap();
|
||||
let polygon: Option<Polygon<Point>> = match ¶ms.bounds {
|
||||
Some(b) => {
|
||||
let bounds: Vec<&str> = b.split(",").collect();
|
||||
if bounds.len() != 4 {
|
||||
warn!("Expected 4 bounds, received {}: {}", bounds.len(), b);
|
||||
return HttpResponse::UnprocessableEntity().body(format!("Received {}; expected NE_LAT,NE_LON,SW_LAT,SW_LON", b))
|
||||
}
|
||||
let ne_lat = match bounds[0].parse::<f64>() {
|
||||
Ok(b) => b,
|
||||
Err(err) => {
|
||||
warn!("{}", err);
|
||||
return HttpResponse::UnprocessableEntity().body(format!("{}", err))
|
||||
}
|
||||
};
|
||||
let ne_lon = match bounds[1].parse::<f64>() {
|
||||
Ok(b) => b,
|
||||
Err(err) => {
|
||||
warn!("{}", err);
|
||||
return HttpResponse::UnprocessableEntity().body(format!("{}", err))
|
||||
}
|
||||
};
|
||||
let sw_lat = match bounds[2].parse::<f64>() {
|
||||
Ok(b) => b,
|
||||
Err(err) => {
|
||||
warn!("{}", err);
|
||||
return HttpResponse::UnprocessableEntity().body(format!("{}", err))
|
||||
}
|
||||
};
|
||||
let sw_lon = match bounds[3].parse::<f64>() {
|
||||
Ok(b) => b,
|
||||
Err(err) => {
|
||||
warn!("{}", err);
|
||||
return HttpResponse::UnprocessableEntity().body(format!("{}", err))
|
||||
}
|
||||
};
|
||||
let mut polygon: Polygon<Point> = Polygon::new(Some(4326));
|
||||
polygon.add_point(Point { x: sw_lon, y: sw_lat, srid: Some(4326) });
|
||||
polygon.add_point(Point { x: ne_lon, y: sw_lat, srid: Some(4326) });
|
||||
polygon.add_point(Point { x: ne_lon, y: ne_lat, srid: Some(4326) });
|
||||
polygon.add_point(Point { x: sw_lon, y: ne_lat, srid: Some(4326) });
|
||||
polygon.add_point(Point { x: sw_lon, y: sw_lat, srid: Some(4326) });
|
||||
Some(polygon)
|
||||
},
|
||||
None => None
|
||||
};
|
||||
let category = match ¶ms.category {
|
||||
Some(c) => Some(c.to_string()),
|
||||
None => None
|
||||
};
|
||||
let filter = match ¶ms.filter {
|
||||
Some(f) => Some(f.to_string()),
|
||||
None => None
|
||||
};
|
||||
|
||||
match web::block(move || Airports::find_all(Some(polygon), category, params.limit, params.page)).await.unwrap() {
|
||||
Ok(a) => HttpResponse::Ok().json(a),
|
||||
match web::block(move || Airports::get_all(polygon, category, filter, params.limit, params.page)).await.unwrap() {
|
||||
Ok(a) => HttpResponse::Ok().json(AirportsResponse {
|
||||
data: a,
|
||||
meta: Metadata { page: 0, limit: 0, pages: 0, total: 0 }
|
||||
}),
|
||||
Err(err) => {
|
||||
error!("{}", err);
|
||||
HttpResponse::InternalServerError().finish()
|
||||
err.to_http_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct AirportResponse {
|
||||
pub data: Airports,
|
||||
pub meta: Metadata
|
||||
}
|
||||
|
||||
#[get("/airports/{icao}")]
|
||||
async fn find(icao: web::Path<String>) -> HttpResponse {
|
||||
async fn get(icao: web::Path<String>) -> HttpResponse {
|
||||
match Airports::find(icao.into_inner()) {
|
||||
Ok(a) => HttpResponse::Ok().json(a),
|
||||
Ok(a) => HttpResponse::Ok().json(AirportResponse {
|
||||
data: a,
|
||||
meta: Metadata { page: 0, limit: 0, pages: 0, total: 0 }
|
||||
}),
|
||||
Err(err) => {
|
||||
error!("{}", err);
|
||||
HttpResponse::InternalServerError().finish()
|
||||
err.to_http_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -65,10 +117,10 @@ async fn find(icao: web::Path<String>) -> HttpResponse {
|
||||
#[post("/airports")]
|
||||
async fn create(airport: web::Json<Airport>) -> HttpResponse {
|
||||
match Airports::create(airport.into_inner()) {
|
||||
Ok(a) => HttpResponse::Ok().json(a),
|
||||
Ok(a) => HttpResponse::Created().json(a),
|
||||
Err(err) => {
|
||||
error!("{}", err);
|
||||
HttpResponse::InternalServerError().finish()
|
||||
err.to_http_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,7 +131,7 @@ async fn update(id: web::Path<i32>, airport: web::Json<Airport>) -> HttpResponse
|
||||
Ok(a) => HttpResponse::Ok().json(a),
|
||||
Err(err) => {
|
||||
error!("{}", err);
|
||||
HttpResponse::InternalServerError().finish()
|
||||
err.to_http_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -87,19 +139,19 @@ async fn update(id: web::Path<i32>, airport: web::Json<Airport>) -> HttpResponse
|
||||
#[delete("/airports/{id}")]
|
||||
async fn delete(id: web::Path<i32>) -> HttpResponse {
|
||||
match Airports::delete(id.into_inner()) {
|
||||
Ok(a) => HttpResponse::Ok().json(json!({ "deleted": a })),
|
||||
Ok(_) => HttpResponse::NoContent().finish(),
|
||||
Err(err) => {
|
||||
error!("{}", err);
|
||||
HttpResponse::InternalServerError().finish()
|
||||
err.to_http_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init_routes(config: &mut web::ServiceConfig) {
|
||||
config.service(find_all);
|
||||
config.service(find);
|
||||
config.service(get_all);
|
||||
config.service(get);
|
||||
config.service(create);
|
||||
config.service(update);
|
||||
config.service(delete);
|
||||
config.service(setup);
|
||||
config.service(import);
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
use crate::{error_handler::CustomError, airports::{Airport, Airports}};
|
||||
use diesel::{r2d2::ConnectionManager, PgConnection};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::diesel_migrations::MigrationHarness;
|
||||
use lazy_static::lazy_static;
|
||||
use log::{error, debug, info};
|
||||
@@ -50,4 +51,18 @@ pub fn import_data() {
|
||||
};
|
||||
}
|
||||
debug!("Import complete");
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Metadata {
|
||||
pub page: i32,
|
||||
pub limit: i32,
|
||||
pub pages: i32,
|
||||
pub total: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
pub struct Coordinate {
|
||||
pub lon: f64,
|
||||
pub lat: f64
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::{HttpResponse, ResponseError};
|
||||
use diesel::result::Error as DieselError;
|
||||
use log::warn;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use std::fmt;
|
||||
@@ -18,6 +19,17 @@ impl CustomError {
|
||||
error_message,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_http_response(&self) -> HttpResponse {
|
||||
let status_code = match StatusCode::from_u16(self.error_status_code) {
|
||||
Ok(s) => s,
|
||||
Err(err) => {
|
||||
warn!("{}", err);
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
}
|
||||
};
|
||||
HttpResponse::build(status_code).body(self.error_message.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for CustomError {
|
||||
|
||||
@@ -1,162 +0,0 @@
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use log::warn;
|
||||
use std::io::BufRead;
|
||||
use quick_xml::{Reader, events::{Event, BytesStart}, Writer, de::Deserializer};
|
||||
use serde::Deserialize;
|
||||
|
||||
pub struct Airport {
|
||||
pub name: String,
|
||||
pub icao: String
|
||||
}
|
||||
|
||||
impl Airport {
|
||||
pub fn new(name: String, icao: String) -> Airport {
|
||||
Airport { name, icao }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct WeatherError(pub String);
|
||||
|
||||
impl fmt::Display for WeatherError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for WeatherError {}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct Metar {
|
||||
pub raw_text: String,
|
||||
pub station_id: String,
|
||||
pub observation_time: String,
|
||||
pub latitude: f32,
|
||||
pub longitude: f32,
|
||||
pub temp_c: f32,
|
||||
pub dewpoint_c: f32,
|
||||
pub wind_dir_degrees: i32,
|
||||
pub wind_speed_kt: i32,
|
||||
pub visibility_statute_mi: String,
|
||||
pub altim_in_hg: f32,
|
||||
pub sea_level_pressure_mb: Option<f32>,
|
||||
pub quality_control_flags: Option<QualityControlFlags>,
|
||||
pub wx_string: Option<String>,
|
||||
// pub sky_con dition: Option<Vec<String>>, // TODO work on attributes
|
||||
pub flight_category: String,
|
||||
pub three_hr_pressure_tendency_mb: Option<f32>,
|
||||
pub metar_type: String,
|
||||
#[serde(rename = "maxT_c")]
|
||||
pub max_t_c: Option<f32>,
|
||||
#[serde(rename = "minT_c")]
|
||||
pub min_t_c: Option<f32>,
|
||||
pub precip_in: Option<f32>,
|
||||
pub elevation_m: i32
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct QualityControlFlags {
|
||||
pub auto: Option<bool>,
|
||||
pub auto_station: Option<bool>
|
||||
}
|
||||
|
||||
impl Metar {
|
||||
pub fn parse(input: String) -> Result<Vec<Metar>, WeatherError> {
|
||||
if input.is_empty() {
|
||||
return Err(WeatherError("Input is empty".to_string()))
|
||||
}
|
||||
|
||||
let mut reader = Reader::from_str(&input);
|
||||
let mut buf = Vec::new();
|
||||
let mut junk_buf: Vec<u8> = Vec::new();
|
||||
|
||||
loop {
|
||||
match reader.read_event_into(&mut buf) {
|
||||
Err(e) => panic!("Error at position: {}: {:?}", reader.buffer_position(), e),
|
||||
Ok(Event::Eof) => break,
|
||||
Ok(Event::Start(e)) => {
|
||||
match e.name().as_ref() {
|
||||
b"METAR" => {
|
||||
let metar_bytes = Metar::read_to_end_into_buffer(&mut reader, &e, &mut junk_buf).unwrap();
|
||||
let str = std::str::from_utf8(&metar_bytes).unwrap();
|
||||
let mut deserializer = Deserializer::from_str(str);
|
||||
let metar = Metar::deserialize(&mut deserializer).unwrap();
|
||||
println!("{:#?}", metar);
|
||||
},
|
||||
_ => ()
|
||||
}
|
||||
},
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(vec![])
|
||||
}
|
||||
|
||||
// https://capnfabs.net/posts/parsing-huge-xml-quickxml-rust-serde/
|
||||
pub fn read_to_end_into_buffer<R: BufRead>(reader: &mut Reader<R>, start_tag: &BytesStart, junk_buf: &mut Vec<u8>) -> Result<Vec<u8>, quick_xml::Error> {
|
||||
let mut depth = 0;
|
||||
let mut output_buf: Vec<u8> = Vec::new();
|
||||
let mut w = Writer::new(&mut output_buf);
|
||||
let tag_name = start_tag.name();
|
||||
w.write_event(Event::Start(start_tag.clone()))?;
|
||||
loop {
|
||||
junk_buf.clear();
|
||||
let event = reader.read_event_into(junk_buf)?;
|
||||
w.write_event(&event)?;
|
||||
|
||||
match event {
|
||||
Event::Start(e) if e.name() == tag_name => depth += 1,
|
||||
Event::End(e) if e.name() == tag_name => {
|
||||
if depth == 0 {
|
||||
return Ok(output_buf);
|
||||
}
|
||||
depth -= 1;
|
||||
}
|
||||
Event::Eof => {
|
||||
panic!("EOF")
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Weather {
|
||||
pub base_url: String
|
||||
}
|
||||
|
||||
impl Weather {
|
||||
pub async fn metar(&mut self, airports: Vec<Airport>) -> Vec<Metar> {
|
||||
let mut station_icaos: Vec<&str> = vec![];
|
||||
for station in airports.iter() {
|
||||
station_icaos.push(&station.icao);
|
||||
}
|
||||
let station_string = station_icaos.join(",");
|
||||
let url = format!("{}/metar.php?ids={}&format=xml", self.base_url, station_string);
|
||||
|
||||
let metars: Vec<Metar> = match reqwest::get(url).await {
|
||||
Ok(r) => match r.text().await {
|
||||
Ok(r) => {
|
||||
match Metar::parse(r) {
|
||||
Ok(m) => m,
|
||||
Err(err) => {
|
||||
warn!("{}", err);
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(err) => {
|
||||
warn!("Unable to parse METAR request: {}", err);
|
||||
vec![]
|
||||
}
|
||||
},
|
||||
Err(err) => {
|
||||
warn!("Unable to get METAR request: {}", err);
|
||||
vec![]
|
||||
}
|
||||
};
|
||||
return metars;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,32 @@
|
||||
use crate::error_handler::CustomError;
|
||||
use crate::{error_handler::CustomError, db::Metadata};
|
||||
use crate::metars::Metars;
|
||||
use actix_web::{get, web, HttpResponse, Responder};
|
||||
use log::error;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct MetarsResponse {
|
||||
pub data: Vec<Metars>,
|
||||
pub meta: Metadata
|
||||
}
|
||||
|
||||
#[get("metars/{ids}")]
|
||||
async fn get_all(ids: web::Path<String>) -> impl Responder {
|
||||
let airports = web::block(|| Ok::<_, CustomError>(async {Metars::get_all(ids.into_inner()).await}))
|
||||
let airports = match web::block(|| Ok::<_, CustomError>(async {Metars::get_all(ids.into_inner()).await}))
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.await
|
||||
.unwrap();
|
||||
HttpResponse::Ok().json(airports)
|
||||
.await {
|
||||
Ok(a) => a,
|
||||
Err(err) => {
|
||||
error!("{}", err);
|
||||
return err.to_http_response();
|
||||
}
|
||||
};
|
||||
HttpResponse::Ok().json(MetarsResponse {
|
||||
data: airports,
|
||||
meta: Metadata { page: 0, limit: 0, pages: 0, total: 0 }
|
||||
})
|
||||
}
|
||||
|
||||
pub fn init_routes(config: &mut web::ServiceConfig) {
|
||||
|
||||
Reference in New Issue
Block a user