Tweaking find all metars
This commit is contained in:
@@ -14,7 +14,7 @@ use crate::metars::MetarCheck;
|
||||
|
||||
const TABLE_NAME: &str = "metars";
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Metar {
|
||||
pub icao: String,
|
||||
pub raw_text: String,
|
||||
@@ -60,7 +60,7 @@ pub struct Metar {
|
||||
pub density_altitude: Option<f64>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub enum ReportModifier {
|
||||
#[serde(rename = "AUTO")]
|
||||
Auto,
|
||||
@@ -88,7 +88,7 @@ impl Display for ReportModifier {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct RunwayVisualRange {
|
||||
pub runway: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
@@ -110,7 +110,7 @@ impl Default for RunwayVisualRange {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub enum AutomatedStationType {
|
||||
#[serde(rename = "AO1")]
|
||||
WithoutPrecipitationDiscriminator,
|
||||
@@ -141,7 +141,7 @@ impl Display for AutomatedStationType {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Remarks {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub peak_wind: Option<PeakWind>,
|
||||
@@ -165,7 +165,7 @@ pub struct Remarks {
|
||||
pub sky_condition_at_secondary_location_not_available: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PeakWind {
|
||||
pub degrees: i32,
|
||||
pub speed: i32,
|
||||
@@ -190,7 +190,7 @@ impl Default for Remarks {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SkyCondition {
|
||||
pub sky_cover: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
@@ -209,7 +209,7 @@ impl Default for SkyCondition {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub enum FlightCategory {
|
||||
VFR,
|
||||
MVFR,
|
||||
@@ -993,10 +993,9 @@ impl Metar {
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn find_all(
|
||||
pub async fn find_all_distinct(
|
||||
client: &Client,
|
||||
icao_list: &Vec<String>,
|
||||
_force: &bool,
|
||||
icao_list: &Vec<String>
|
||||
) -> ApiResult<Vec<Self>> {
|
||||
if icao_list.is_empty() {
|
||||
return Ok(Vec::new());
|
||||
@@ -1017,9 +1016,9 @@ impl Metar {
|
||||
|
||||
let current_time = Utc::now().timestamp();
|
||||
let time_offset = env::var("API_METAR_TIME_OFFSET")
|
||||
.unwrap_or("3000".to_string())
|
||||
.unwrap_or("1800".to_string())
|
||||
.parse::<i64>()
|
||||
.unwrap_or(3000);
|
||||
.unwrap_or(1800);
|
||||
let short_time_offset: i64 = 300;
|
||||
|
||||
// Setup metars and missing metar structures
|
||||
@@ -1042,8 +1041,8 @@ impl Metar {
|
||||
};
|
||||
// If the metar was cached more than short_time_offset minutes ago, refresh it
|
||||
if refresh_seconds >= short_time_offset {
|
||||
log::trace!("{} METAR data is outdated, refreshing now", &icao);
|
||||
missing_metar_icaos.push(icao);
|
||||
log::trace!("{} METAR data is outdated, marked for refresh", &icao);
|
||||
missing_metar_icaos.push(icao.clone());
|
||||
}
|
||||
// Otherwise return outdated data and wait
|
||||
else {
|
||||
@@ -1058,7 +1057,7 @@ impl Metar {
|
||||
// Otherwise add the metar to the vector
|
||||
else {
|
||||
found_metar_icaos.insert(icao.clone());
|
||||
let metar_check = MetarCheck::new(icao, true);
|
||||
let metar_check = MetarCheck::new(icao, true).await;
|
||||
metar_check.insert(time_offset as u64).await?;
|
||||
metars.push(Metar::from_db(metar_row)?);
|
||||
}
|
||||
@@ -1092,21 +1091,29 @@ impl Metar {
|
||||
|
||||
if remote_metars.len() > 0 {
|
||||
// Insert missing METARs
|
||||
for remote_metar in &remote_metars {
|
||||
found_metar_icaos.insert(remote_metar.icao.to_string());
|
||||
let metar_check = MetarCheck::new(remote_metar.icao.clone(), true);
|
||||
metar_check.insert(time_offset as u64).await?;
|
||||
for remote_metar in remote_metars.clone() {
|
||||
remote_metar.insert().await?;
|
||||
found_metar_icaos.insert(remote_metar.icao.to_string());
|
||||
let mut metar_check = MetarCheck::new(remote_metar.icao.clone(), true).await;
|
||||
metar_check.last_metar = Some(remote_metar);
|
||||
metar_check.insert(time_offset as u64).await?;
|
||||
}
|
||||
metars.append(&mut remote_metars)
|
||||
metars.append(&mut remote_metars);
|
||||
}
|
||||
|
||||
// Update still missing metars
|
||||
let mut still_missing_metar_icaos: Vec<String> = vec![];
|
||||
// let mut still_missing_metar_icaos: Vec<String> = vec![];
|
||||
for difference in found_metar_icaos.symmetric_difference(&requested_icaos) {
|
||||
still_missing_metar_icaos.push(difference.to_string());
|
||||
let metar_check = MetarCheck::new(difference.to_string(), false);
|
||||
metar_check.insert(short_time_offset as u64).await?
|
||||
// still_missing_metar_icaos.push(difference.to_string());
|
||||
let metar_check = MetarCheck::new(difference.to_string(), false).await;
|
||||
metar_check.insert(short_time_offset as u64).await?;
|
||||
// Only add cached metar data if it's less than 4 hours old
|
||||
if let Some(last_metar) = metar_check.last_metar {
|
||||
let four_hours_ago = Utc::now() - chrono::Duration::hours(4);
|
||||
if last_metar.observation_time < four_hours_ago {
|
||||
metars.push(last_metar);
|
||||
}
|
||||
}
|
||||
}
|
||||
// if !still_missing_metar_icaos.is_empty() {
|
||||
// log::trace!("Still missing METAR data from {:?}", still_missing_metar_icaos);
|
||||
|
||||
Reference in New Issue
Block a user