From a29ebfae27be6475f70928636529a01a3616dc64 Mon Sep 17 00:00:00 2001 From: "2569718930@qq.com" <2569718930@qq.com> Date: Tue, 12 May 2026 21:15:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=8A=E6=97=A5=E5=AE=9E=E6=B5=8B=E6=9C=80?= =?UTF-8?q?=E9=AB=98=E6=94=B9=E7=94=A8=20airport=5Fcurrent.max=5Fso=5Ffar?= =?UTF-8?q?=EF=BC=88METAR=E5=8E=86=E5=8F=B2=E8=A7=82=E6=B5=8B=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 项目已有 airport_current 字段存储 METAR/AMOS 当天最高温及时间, 无需从 obs_log 自建。obs_log 保留期恢复为 2 小时。 Tested: pytest 176 passed, ruff check 通过 --- src/database/db_manager.py | 2 +- src/utils/telegram_push.py | 34 ++++++++++------------------------ 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/src/database/db_manager.py b/src/database/db_manager.py index d3198b16..28447e66 100644 --- a/src/database/db_manager.py +++ b/src/database/db_manager.py @@ -1861,7 +1861,7 @@ class DBManager: temp_c, wind_kt, pressure_hpa, str(obs_time)), ) conn.execute( - "DELETE FROM airport_obs_log WHERE created_at < datetime('now', '-24 hours')" + "DELETE FROM airport_obs_log WHERE created_at < datetime('now', '-2 hours')" ) conn.commit() diff --git a/src/utils/telegram_push.py b/src/utils/telegram_push.py index 103806f2..5a8d5f1a 100644 --- a/src/utils/telegram_push.py +++ b/src/utils/telegram_push.py @@ -763,38 +763,24 @@ def _build_airport_status_message( lines.append(f"当前实测:{station_temp:.1f}°C") if deb_pred is not None: lines.append(f"今日DEB预报最高:{deb_pred:.1f}°C") - max_so_far, max_temp_time = _get_airport_daily_high(city) + max_so_far, max_temp_time = _get_airport_daily_high(city_weather) if max_so_far is not None: time_str = f"({max_temp_time})" if max_temp_time else "" lines.append(f"今日实测最高:{max_so_far:.1f}°C{time_str}") return "\n".join(lines) -def _get_airport_daily_high(city: str): - """Get today's observed high from airport_obs_log for this city.""" - icao = HIGH_FREQ_AIRPORT_ICAO.get(city, "") - if not icao: - return None, None - try: - from src.database.db_manager import DBManager - db = DBManager() - obs = db.get_airport_obs_recent(icao, minutes=1440) # last 24h - if not obs: - return None, None - best = max(obs, key=lambda r: r.get("temp_c") or -999) - temp_c = best.get("temp_c") - obs_time = best.get("obs_time") or "" - if temp_c is None: - return None, None +def _get_airport_daily_high(city_weather: Dict[str, Any]): + """Get today's observed high from METAR/AMOS airport history.""" + airport = city_weather.get("airport_current") or {} + max_so_far = airport.get("max_so_far") + max_time = airport.get("max_temp_time") + if max_so_far is not None: try: - from datetime import datetime - dt = datetime.fromisoformat(str(obs_time)) - time_str = dt.strftime("%H:%M") + max_so_far = round(float(max_so_far), 1) except Exception: - time_str = str(obs_time)[:5] if obs_time else "" - return round(float(temp_c), 1), time_str - except Exception: - return None, None + max_so_far = None + return max_so_far, max_time # Per-city push interval matching native data refresh rate (seconds)