Cleanup, refactored, various changes

This commit is contained in:
2023-12-01 16:47:44 -05:00
parent c3b9f0d649
commit 1feb713a47
13 changed files with 60 additions and 69 deletions

View File

@@ -34817,6 +34817,7 @@
},
{
"icao": "KIAH",
"full_name": "George Bush Intercontinental Airport",
"category": "large_airport",
"point":
{
@@ -36735,23 +36736,6 @@
"iata_code": "",
"local_code": "K19"
},
{
"icao": "KK20",
"category": "small_airport",
"full_name": "Wendell H Ford Airport",
"point":
{
"y": 37.384838,
"x": -83.259662
},
"elevation_ft": 1256,
"iso_country": "US",
"iso_region": "US-KY",
"municipality": "Chavies",
"gps_code": "",
"iata_code": "",
"local_code": "CPF"
},
{
"icao": "KK22",
"category": "small_airport",

View File

@@ -14,7 +14,6 @@ pub struct Airport {
pub icao: String,
pub category: String,
pub full_name: String,
pub point: Point,
pub elevation_ft: Option<i32>,
pub iso_country: String,
pub iso_region: String,
@@ -22,6 +21,7 @@ pub struct Airport {
pub gps_code: String,
pub iata_code: String,
pub local_code: String,
pub point: Point,
pub tower: Option<bool>,
}
@@ -31,13 +31,13 @@ impl Into<QueryAirport> for Airport {
icao: self.icao.clone(),
category: self.category.clone(),
full_name: self.full_name.clone(),
point: self.point.clone(),
iso_country: self.iso_country.clone(),
iso_region: self.iso_region.clone(),
municipality: self.municipality.clone(),
gps_code: self.gps_code.clone(),
iata_code: self.iata_code.clone(),
local_code: self.local_code.clone(),
point: self.point.clone(),
data: match serde_json::to_value(&self) {
Ok(d) => d,
Err(err) => {

View File

@@ -1,4 +1,4 @@
use crate::{error_handler::ServiceError, airports::QueryAirport};
use crate::{error_handler::ServiceError, airports::{QueryAirport, Airport}};
use diesel::{r2d2::ConnectionManager, PgConnection};
use redis::{Client as RedisClient, aio::Connection as RedisConnection};
use serde::{Deserialize, Serialize};
@@ -63,10 +63,10 @@ pub fn import_data() -> i32 {
let path = "airport-codes.json";
debug!("Importing data from {}", path);
let contents: String = std::fs::read_to_string(path).expect("Failed to read file");
let airports: Vec<QueryAirport> = serde_json::from_str(&contents).expect("JSON was not well formed.");
let airports: Vec<Airport> = serde_json::from_str(&contents).expect("JSON was not well formed.");
let mut count = 0;
for airport in airports {
match QueryAirport::insert(airport) {
match QueryAirport::insert(airport.into()) {
Ok(_) => count += 1,
Err(err) => error!("Error inserting airport; {}", err)
};

View File

@@ -176,7 +176,8 @@ impl Metar {
if metar_parts[0] == "AUTO" {
metar.quality_control_flags.auto = Some(true);
metar_parts.remove(0);
} else if metar_parts[0] == "COR" {
}
if metar_parts[0] == "COR" {
metar.quality_control_flags.corrected = Some(true);
metar_parts.remove(0);
}
@@ -270,7 +271,10 @@ impl Metar {
}
// Weather Phenomena
let wx_re = regex::Regex::new(r"^(?:[+-]|VC|MI|PR|BC|DR|BL|SH|TS|FZ)?(?:DZ|RA|SN|SG|IC|PL|GR|GS|UP|BR|FG|FU|VA|DU|SA|HZ|PY|PO|SQ|FC|SS|DS)$").unwrap();
let wx_intensity = "(?:[+-]|VC)?";
let wx_descriptor = "(?:MI|PR|BC|DR|BL|SH|TS|FZ)?";
let wx_precipitation = "(?:DZ|RA|SN|SG|IC|PL|GR|GS|UP|BR|FG|FU|VA|DU|SA|HZ|PY|PO|SQ|FC|SS|DS)?";
let wx_re = regex::Regex::new(&format!(r"^{}{}{}$", wx_intensity, wx_descriptor, wx_precipitation)).unwrap();
while wx_re.is_match(metar_parts[0]) {
metar.weather_phenomena.push(metar_parts[0].to_string());
metar_parts.remove(0);
@@ -396,7 +400,7 @@ impl Metar {
}
// Flight Category
if metar.visibility_statute_mi.is_none() || metar.sky_condition.is_empty() {
if metar.visibility_statute_mi.is_none() && metar.sky_condition.is_empty() {
metar.flight_category = FlightCategory::UNKN;
} else {
let visibility = match &metar.visibility_statute_mi {
@@ -407,7 +411,7 @@ impl Metar {
v.parse::<f64>().unwrap()
}
}
None => 0.0
None => 5.0 // Assume VFR if no visibility is present
};
let ceiling = match metar.sky_condition.first() {
Some(s) => {

View File

@@ -55,9 +55,10 @@ pub fn update_airports() {
}
}
debug!("METAR update complete");
// Sleep until the observation time is 1 hour old
// Sleep until the earliest observation time is 1 hour old
// Bounded by 1 and 3600 seconds
let now = chrono::Utc::now().timestamp();
let sleep_time = now - (observation_time + (3600));
let sleep_time = std::cmp::min(std::cmp::max(1, now - (observation_time + (3600))), 3600);
debug!("Next update in {} seconds", sleep_time);
sleep(Duration::from_secs(sleep_time as u64)).await;
}