Preserve WU trends with staged METAR history fallback
This commit is contained in:
@@ -685,8 +685,7 @@ export function getTemperatureChartData(
|
|||||||
observationCode === "cwa" ||
|
observationCode === "cwa" ||
|
||||||
observationCode === "noaa" ||
|
observationCode === "noaa" ||
|
||||||
observationCode === "wunderground";
|
observationCode === "wunderground";
|
||||||
const useSettlementObservationSource =
|
const useSettlementObservationSource = settlementSource;
|
||||||
settlementSource && observationCode !== "wunderground";
|
|
||||||
const officialObservationSource =
|
const officialObservationSource =
|
||||||
useSettlementObservationSource
|
useSettlementObservationSource
|
||||||
? detail.settlement_today_obs?.length
|
? detail.settlement_today_obs?.length
|
||||||
@@ -733,6 +732,8 @@ export function getTemperatureChartData(
|
|||||||
officialObservationSource.length > 0 &&
|
officialObservationSource.length > 0 &&
|
||||||
officialObservationSource.length < 3 &&
|
officialObservationSource.length < 3 &&
|
||||||
metarObservationSource.length >= 3;
|
metarObservationSource.length >= 3;
|
||||||
|
const usingMetarObservationSource =
|
||||||
|
!useSettlementObservationSource || shouldUseMetarFallback;
|
||||||
const observationSource = useSettlementObservationSource
|
const observationSource = useSettlementObservationSource
|
||||||
? shouldUseMetarFallback
|
? shouldUseMetarFallback
|
||||||
? metarObservationSource
|
? metarObservationSource
|
||||||
@@ -747,8 +748,10 @@ export function getTemperatureChartData(
|
|||||||
const observationDisplayTag =
|
const observationDisplayTag =
|
||||||
usingCurrentObservationFallback
|
usingCurrentObservationFallback
|
||||||
? String(currentFallbackTag).toUpperCase()
|
? String(currentFallbackTag).toUpperCase()
|
||||||
: observationCode === "wunderground"
|
: observationCode === "wunderground" && usingMetarObservationSource
|
||||||
? metarFallbackTag
|
? metarFallbackTag
|
||||||
|
: observationCode === "wunderground"
|
||||||
|
? `WU ${String(detail.risk?.icao || "").trim().toUpperCase() || "STATION"}`
|
||||||
: useSettlementObservationSource && shouldUseMetarFallback
|
: useSettlementObservationSource && shouldUseMetarFallback
|
||||||
? metarFallbackTag
|
? metarFallbackTag
|
||||||
: observationCode === "noaa"
|
: observationCode === "noaa"
|
||||||
|
|||||||
@@ -106,8 +106,6 @@ class MetarSourceMixin:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
url = "https://aviationweather.gov/api/data/metar"
|
url = "https://aviationweather.gov/api/data/metar"
|
||||||
history_hours = 24
|
|
||||||
|
|
||||||
def _request_metar_records(hours: int, timeout: float) -> list:
|
def _request_metar_records(hours: int, timeout: float) -> list:
|
||||||
response = self._http_get(
|
response = self._http_get(
|
||||||
url,
|
url,
|
||||||
@@ -122,23 +120,39 @@ class MetarSourceMixin:
|
|||||||
payload = response.json()
|
payload = response.json()
|
||||||
return payload if isinstance(payload, list) else []
|
return payload if isinstance(payload, list) else []
|
||||||
|
|
||||||
try:
|
data = []
|
||||||
data = _request_metar_records(
|
history_hours = 24
|
||||||
history_hours,
|
first_exc: Optional[httpx.HTTPError] = None
|
||||||
getattr(self, "metar_timeout_sec", self.timeout),
|
fallback_hours = [24, 12, 6, 2]
|
||||||
)
|
for index, hours in enumerate(fallback_hours):
|
||||||
except httpx.HTTPError as primary_exc:
|
timeout = (
|
||||||
history_hours = 2
|
getattr(self, "metar_timeout_sec", self.timeout)
|
||||||
logger.warning(
|
if index == 0
|
||||||
f"METAR {icao} 24h 请求失败,尝试 latest fallback: {primary_exc}"
|
else getattr(self, "metar_latest_timeout_sec", 2.5)
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
data = _request_metar_records(
|
data = _request_metar_records(hours, timeout)
|
||||||
history_hours,
|
history_hours = hours
|
||||||
getattr(self, "metar_latest_timeout_sec", 2.5),
|
if index > 0:
|
||||||
|
logger.warning(
|
||||||
|
"METAR {} {}h fallback returned {} records",
|
||||||
|
icao,
|
||||||
|
hours,
|
||||||
|
len(data),
|
||||||
|
)
|
||||||
|
break
|
||||||
|
except httpx.HTTPError as exc:
|
||||||
|
if first_exc is None:
|
||||||
|
first_exc = exc
|
||||||
|
logger.warning(
|
||||||
|
"METAR {} {}h 请求失败,尝试下一档 fallback: {}",
|
||||||
|
icao,
|
||||||
|
hours,
|
||||||
|
exc,
|
||||||
)
|
)
|
||||||
except httpx.HTTPError:
|
else:
|
||||||
raise primary_exc
|
if first_exc is not None:
|
||||||
|
raise first_exc
|
||||||
|
|
||||||
if not data:
|
if not data:
|
||||||
return None
|
return None
|
||||||
|
|||||||
Reference in New Issue
Block a user