From 5fa3fb8999f871a15ee7b4c783f6b39fe2ed13d3 Mon Sep 17 00:00:00 2001 From: "2569718930@qq.com" <2569718930@qq.com> Date: Thu, 11 Jun 2026 14:29:56 +0800 Subject: [PATCH] use lightweight MADIS health probe --- tests/test_ops_health_check.py | 47 ++++++++++++++++++++++++++++++++++ web/services/ops_api.py | 3 ++- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 tests/test_ops_health_check.py diff --git a/tests/test_ops_health_check.py b/tests/test_ops_health_check.py new file mode 100644 index 00000000..98138dfd --- /dev/null +++ b/tests/test_ops_health_check.py @@ -0,0 +1,47 @@ +from web.services import ops_api + + +class _Elapsed: + def total_seconds(self): + return 0.01 + + +class _Response: + ok = True + status_code = 200 + elapsed = _Elapsed() + + +def test_ops_health_check_uses_lightweight_head_for_madis(monkeypatch): + import requests + + head_calls = [] + + monkeypatch.setattr(ops_api, "_require_ops", lambda request: {}) + monkeypatch.setattr(ops_api, "_check_amsc_awos_health", lambda timeout=8: {"ok": True}) + monkeypatch.setattr(requests, "get", lambda *args, **kwargs: _Response()) + monkeypatch.setattr( + requests, + "head", + lambda url, **kwargs: head_calls.append((url, kwargs)) or _Response(), + ) + for name in ( + "SUPABASE_URL", + "SUPABASE_SERVICE_ROLE_KEY", + "KNMI_API_KEY", + "TELEGRAM_BOT_TOKEN", + "CWA_API_KEY", + "CWA_OPEN_DATA_AUTH", + "CWA_OPEN_DATA_API_KEY", + ): + monkeypatch.delenv(name, raising=False) + + result = ops_api.get_ops_health_check(object()) + + assert result["services"]["madis"]["ok"] is True + assert head_calls == [ + ( + "https://madis-data.ncep.noaa.gov/madisPublic1/data/LDAD/hfmetar/netCDF/", + {"timeout": 8, "allow_redirects": True}, + ) + ] diff --git a/web/services/ops_api.py b/web/services/ops_api.py index a01eb1e4..035250c8 100644 --- a/web/services/ops_api.py +++ b/web/services/ops_api.py @@ -2082,9 +2082,10 @@ def get_ops_health_check(request: Request) -> dict[str, Any]: # MADIS (NOAA) try: t0 = _time.perf_counter() - r = _r.get( + r = _r.head( "https://madis-data.ncep.noaa.gov/madisPublic1/data/LDAD/hfmetar/netCDF/", timeout=timeout, + allow_redirects=True, ) results["madis"] = { "ok": r.ok,