Reduce polling and batch observation writes

This commit is contained in:
2569718930@qq.com
2026-06-08 15:12:41 +08:00
parent 96dde3b50e
commit 8f6925108c
12 changed files with 281 additions and 24 deletions
+26
View File
@@ -22,6 +22,32 @@ def test_backend_shared_timing_helper_avoids_sensitive_identity_fields():
assert "email" not in source
def test_server_timing_recorder_warns_on_slow_stages(monkeypatch):
import web.services.request_timing as request_timing
warnings = []
monkeypatch.setenv("POLYWEATHER_SLOW_STAGE_LOG_MS", "0")
monkeypatch.setattr(
request_timing.logger,
"warning",
lambda *args, **kwargs: warnings.append(args),
)
recorder = request_timing.ServerTimingRecorder(
None,
log_name="unit_timing",
prefix="unit",
state_attr="unit_server_timing",
)
recorder.measure("sqlite_query", lambda: None)
recorder.finish(outcome="ok", status_code=200)
assert warnings
assert warnings[0][0] == "slow_request_timing log_name={} outcome={} status_code={} slow_stages={}"
assert warnings[0][1] == "unit_timing"
assert "sqlite_query" in warnings[0][4]
def test_city_detail_batch_response_includes_backend_server_timing(monkeypatch):
city_api._CITY_DETAIL_BATCH_RESPONSE_CACHE.clear()
city_api._CITY_DETAIL_BATCH_RESPONSE_CACHE_TS.clear()
+35
View File
@@ -288,3 +288,38 @@ def test_ephemeral_observation_log_writes_skip_sqlite_lock(monkeypatch, tmp_path
target_runway_max=24.0,
otime_utc="2026-06-08T04:00:00Z",
)
def test_airport_obs_batch_writes_share_one_sqlite_transaction(monkeypatch, tmp_path):
from src.database.db_manager import DBManager
db = DBManager(str(tmp_path / "polyweather-airport-obs-batch.db"))
original_get_connection = db._get_connection
connection_calls = {"count": 0}
def counting_connection():
connection_calls["count"] += 1
return original_get_connection()
monkeypatch.setattr(db, "_get_connection", counting_connection)
db.append_airport_obs_batch(
[
{
"icao": "ZBAA",
"city": "beijing",
"temp_c": 24.0,
"obs_time": "2026-06-08T04:00:00Z",
},
{
"icao": "ZBAD",
"city": "beijing",
"temp_c": 25.0,
"obs_time": "2026-06-08T04:00:00Z",
},
]
)
assert connection_calls["count"] == 1
assert [row["temp_c"] for row in db.get_airport_obs_recent("ZBAA", minutes=180)] == [24.0]
assert [row["temp_c"] for row in db.get_airport_obs_recent("ZBAD", minutes=180)] == [25.0]
+51
View File
@@ -1959,6 +1959,57 @@ def test_auth_me_uses_subscription_window_as_required_subscription_gate(monkeypa
assert payload["subscription_queued_days"] == 30
def test_auth_me_entitlement_scope_reuses_subscription_window_cache(monkeypatch):
monkeypatch.setattr(web_core.SUPABASE_ENTITLEMENT, "enabled", True)
monkeypatch.setattr(web_core.SUPABASE_ENTITLEMENT, "require_subscription", False)
monkeypatch.setattr(web_core, "_SUPABASE_AUTH_REQUIRED", False)
monkeypatch.setattr(routes, "_resolve_weekly_profile", lambda request: {"weekly_points": 0, "weekly_rank": None})
monkeypatch.setattr(routes, "_resolve_auth_points", lambda request: 0)
def _bind_identity(request):
request.state.auth_user_id = "user-1"
request.state.auth_email = "user@example.com"
calls = []
def _subscription_window(
user_id,
respect_requirement=False,
bypass_cache=False,
unknown_on_error=False,
):
calls.append(
{
"user_id": user_id,
"bypass_cache": bypass_cache,
"unknown_on_error": unknown_on_error,
}
)
return {
"current": {
"plan_code": "pro_monthly",
"starts_at": "2026-03-22T00:00:00+00:00",
"expires_at": "2026-04-21T00:00:00+00:00",
},
"rows": [],
}
monkeypatch.setattr(routes, "_assert_entitlement", lambda request: None)
monkeypatch.setattr(routes, "_bind_optional_supabase_identity", _bind_identity)
monkeypatch.setattr(routes.SUPABASE_ENTITLEMENT, "get_subscription_window", _subscription_window)
response = client.get("/api/auth/me?scope=entitlement")
assert response.status_code == 200
assert calls == [
{
"user_id": "user-1",
"bypass_cache": False,
"unknown_on_error": True,
}
]
def test_auth_me_preserves_unknown_subscription_window(monkeypatch):
monkeypatch.setattr(web_core.SUPABASE_ENTITLEMENT, "enabled", True)
monkeypatch.setattr(web_core.SUPABASE_ENTITLEMENT, "require_subscription", False)