Fixed docker issue temporarily
This commit is contained in:
@@ -3,6 +3,7 @@ use crate::{error::ApiResult, db};
|
||||
use chrono::{DateTime, Datelike, Utc};
|
||||
use std::collections::HashSet;
|
||||
use redis::{AsyncCommands, RedisResult};
|
||||
use reqwest::Client;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::db::redis_async_connection;
|
||||
|
||||
@@ -845,8 +846,8 @@ impl Metar {
|
||||
missing_metar_icaos
|
||||
}
|
||||
|
||||
async fn get_remote_metars(icaos: &[&str]) -> ApiResult<Vec<Metar>> {
|
||||
let gov_api_url = std::env::var("GOV_API_URL").expect("GOV_API_URL must be set");
|
||||
async fn get_remote_metars(client: &Client, icaos: &[&str]) -> ApiResult<Vec<Metar>> {
|
||||
let base_url = std::env::var("AVIATION_WEATHER_URL").expect("GOV_API_URL must be set");
|
||||
// Query the remote API for the missing METAR data 10 at a time
|
||||
let icao_chunks = icaos
|
||||
.chunks(10)
|
||||
@@ -854,14 +855,14 @@ impl Metar {
|
||||
.collect::<Vec<String>>();
|
||||
let mut metars: Vec<Metar> = vec![];
|
||||
for icao_chunk in icao_chunks {
|
||||
let url = format!("{}/metar.php?ids={}", gov_api_url, icao_chunk);
|
||||
let mut m = match reqwest::get(url).await {
|
||||
let url = format!("{}/metar?ids={}&order=id", base_url, icao_chunk);
|
||||
let mut m = match client.get(url).send().await {
|
||||
Ok(r) => {
|
||||
// Check if the status code is 200
|
||||
if r.status() != 200 {
|
||||
return Err(Error::new(
|
||||
500,
|
||||
format!("Unable to get METAR request: {}", r.status()),
|
||||
format!("Request returned status {}", r.status()),
|
||||
));
|
||||
}
|
||||
match r.text().await {
|
||||
@@ -876,20 +877,10 @@ impl Metar {
|
||||
Err(err) => return Err(err),
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
return Err(Error::new(
|
||||
500,
|
||||
format!("Unable to parse METAR request: {}", err),
|
||||
))
|
||||
}
|
||||
Err(err) => return Err(Error::new(500, format!("METAR parse failed: {}", err))),
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
return Err(Error::new(
|
||||
500,
|
||||
format!("Unable to get METAR request: {}", err),
|
||||
))
|
||||
}
|
||||
Err(err) => return Err(err.into()),
|
||||
};
|
||||
metars.append(&mut m);
|
||||
}
|
||||
@@ -911,7 +902,11 @@ impl Metar {
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn find_all(icao_list: &Vec<String>) -> ApiResult<Vec<Self>> {
|
||||
pub async fn find_all(
|
||||
client: &Client,
|
||||
icao_list: &Vec<String>,
|
||||
force: &bool,
|
||||
) -> ApiResult<Vec<Self>> {
|
||||
if icao_list.is_empty() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
@@ -937,17 +932,21 @@ impl Metar {
|
||||
if !missing_icao_list.is_empty() {
|
||||
let mut updated_missing_icao_list: Vec<&str> = Vec::new();
|
||||
for icao in &missing_icao_list {
|
||||
let result: RedisResult<Option<bool>> = conn.get(icao).await;
|
||||
match result {
|
||||
Ok(Some(value)) => {
|
||||
if value {
|
||||
if *force {
|
||||
updated_missing_icao_list.push(icao);
|
||||
} else {
|
||||
let result: RedisResult<Option<bool>> = conn.get(icao).await;
|
||||
match result {
|
||||
Ok(Some(value)) => {
|
||||
if value {
|
||||
updated_missing_icao_list.push(icao);
|
||||
}
|
||||
}
|
||||
Ok(None) => {
|
||||
updated_missing_icao_list.push(icao);
|
||||
}
|
||||
Err(err) => return Err(err.into()),
|
||||
}
|
||||
Ok(None) => {
|
||||
updated_missing_icao_list.push(icao);
|
||||
}
|
||||
Err(err) => return Err(err.into()),
|
||||
}
|
||||
}
|
||||
if !updated_missing_icao_list.is_empty() {
|
||||
@@ -955,7 +954,7 @@ impl Metar {
|
||||
"Retrieving missing METAR data for {:?}",
|
||||
updated_missing_icao_list
|
||||
);
|
||||
let mut missing_icao_list = Self::get_remote_metars(&updated_missing_icao_list)
|
||||
let mut missing_icao_list = Self::get_remote_metars(client, &updated_missing_icao_list)
|
||||
.await
|
||||
.unwrap_or_else(|err| {
|
||||
log::warn!("Unable to get remote METAR data; {}", err);
|
||||
|
||||
@@ -2,14 +2,16 @@ use crate::metars::Metar;
|
||||
use actix_web::{get, web, HttpResponse, HttpRequest};
|
||||
use log::error;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::AppState;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct FindAllParameters {
|
||||
icaos: Option<String>,
|
||||
force: Option<bool>,
|
||||
}
|
||||
|
||||
#[get("metars")]
|
||||
async fn find_all(req: HttpRequest) -> HttpResponse {
|
||||
async fn find_all(data: web::Data<AppState>, req: HttpRequest) -> HttpResponse {
|
||||
let parameters = web::Query::<FindAllParameters>::from_query(req.query_string()).unwrap();
|
||||
let icao_option = ¶meters.icaos;
|
||||
let icao_string = match icao_option {
|
||||
@@ -17,8 +19,10 @@ async fn find_all(req: HttpRequest) -> HttpResponse {
|
||||
None => return HttpResponse::UnprocessableEntity().body("Missing icaos parameter"),
|
||||
};
|
||||
let icaos: Vec<String> = icao_string.split(',').map(|s| s.to_string()).collect();
|
||||
let force = ¶meters.force.unwrap_or(false);
|
||||
|
||||
let metars = match Metar::find_all(&icaos).await {
|
||||
let client = &data.client;
|
||||
let metars = match Metar::find_all(client, &icaos, force).await {
|
||||
Ok(a) => a,
|
||||
Err(err) => {
|
||||
error!("{}", err);
|
||||
|
||||
Reference in New Issue
Block a user