Defer decision ID fetch to after metadata message
Decision IDs are now loaded inside the streaming generators after the metadata message is yielded, eliminating a blocking DB query from the pre-stream path (~100-200ms improvement to TTFB).
This commit is contained in:
parent
e99006e2f9
commit
e53b1e120a
2 changed files with 71 additions and 26 deletions
|
|
@ -315,3 +315,44 @@ class TestStreamingEndpoint:
|
|||
from api.app import FIRST_BATCH_SIZE
|
||||
assert len(batch_messages[0]["features"]) <= FIRST_BATCH_SIZE
|
||||
|
||||
def test_streaming_still_filters_disliked(self, client):
|
||||
"""Test that disliked listings are filtered even with deferred decision loading."""
|
||||
with patch("api.app.get_cached_count", return_value=None), \
|
||||
patch("api.app._get_user_id_safe", return_value=42), \
|
||||
patch("api.app.ListingRepository") as MockRepo, \
|
||||
patch("api.app.DecisionRepository"), \
|
||||
patch("api.app.decision_service") as mock_ds:
|
||||
|
||||
mock_ds.get_disliked_ids.return_value = {2}
|
||||
|
||||
mock_instance = MagicMock()
|
||||
mock_instance.count_listings.return_value = 2
|
||||
mock_instance.stream_listings_optimized.return_value = iter([
|
||||
{
|
||||
'id': 1, 'price': 2000.0, 'number_of_bedrooms': 2,
|
||||
'square_meters': 50.0, 'longitude': -0.1, 'latitude': 51.5,
|
||||
'photo_thumbnail': None, 'last_seen': datetime.now(),
|
||||
'agency': None, 'price_history_json': '[]', 'available_from': None,
|
||||
},
|
||||
{
|
||||
'id': 2, 'price': 2500.0, 'number_of_bedrooms': 2,
|
||||
'square_meters': 60.0, 'longitude': -0.12, 'latitude': 51.51,
|
||||
'photo_thumbnail': None, 'last_seen': datetime.now(),
|
||||
'agency': None, 'price_history_json': '[]', 'available_from': None,
|
||||
},
|
||||
])
|
||||
MockRepo.return_value = mock_instance
|
||||
|
||||
response = client.get("/api/listing_geojson/stream?listing_type=RENT&limit=10")
|
||||
messages = [json.loads(line) for line in response.text.strip().split("\n")]
|
||||
|
||||
all_features = []
|
||||
for m in messages:
|
||||
if m["type"] == "batch":
|
||||
all_features.extend(m["features"])
|
||||
|
||||
feature_ids = [f["properties"]["id"] for f in all_features]
|
||||
assert 2 not in feature_ids
|
||||
assert 1 in feature_ids
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue