Skip to content

Commit d86149e

Browse files
Yuri ZmytrakovYuri Zmytrakov
authored andcommitted
dummy
1 parent cb7ee7c commit d86149e

File tree

4 files changed

+63
-56
lines changed

4 files changed

+63
-56
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ test-datetime-filtering-os:
8282
docker compose down
8383

8484
.PHONY: test
85-
test: test-elasticsearch test-datetime-filtering-es test-opensearch test-datetime-filtering-os test-redis-es test-redis-os
85+
test: test-opensearch
8686

8787
.PHONY: run-database-es
8888
run-database-es:

stac_fastapi/core/stac_fastapi/core/core.py

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from stac_fastapi.core.base_settings import ApiBaseSettings
2525
from stac_fastapi.core.datetime_utils import format_datetime_range
2626
from stac_fastapi.core.models.links import PagingLinks
27-
from stac_fastapi.core.redis_utils import connect_redis, get_prev_link, save_self_link
27+
from stac_fastapi.core.redis_utils import _handle_pagination_via_redis
2828
from stac_fastapi.core.serializers import CollectionSerializer, ItemSerializer
2929
from stac_fastapi.core.session import Session
3030
from stac_fastapi.core.utilities import filter_fields, get_bool_env
@@ -329,20 +329,8 @@ async def all_collections(
329329
if parsed_sort:
330330
sort = parsed_sort
331331

332-
current_url = str(request.url)
333332
redis_enable = get_bool_env("REDIS_ENABLE", default=False)
334333

335-
redis = None
336-
if redis_enable:
337-
try:
338-
redis = await connect_redis()
339-
logger.info("Redis connection established successfully")
340-
except Exception as e:
341-
redis = None
342-
logger.warning(
343-
f"Redis connection failed, continuing without Redis: {e}"
344-
)
345-
346334
# Convert q to a list if it's a string
347335
q_list = None
348336
if q is not None:
@@ -441,21 +429,7 @@ async def all_collections(
441429
},
442430
]
443431

444-
if redis_enable and redis:
445-
if next_token:
446-
await save_self_link(redis, next_token, current_url)
447-
448-
prev_link = await get_prev_link(redis, token)
449-
if prev_link:
450-
links.insert(
451-
0,
452-
{
453-
"rel": "prev",
454-
"type": "application/json",
455-
"method": "GET",
456-
"href": prev_link,
457-
},
458-
)
432+
_handle_pagination_via_redis(redis_enable, next_token, token, request, links)
459433

460434
if next_token:
461435
next_link = PagingLinks(next=next_token, request=request).link_next()
@@ -901,29 +875,9 @@ async def post_search(
901875
)
902876
links.extend(collection_links)
903877

904-
if redis_enable:
905-
redis = None
906-
try:
907-
redis = await connect_redis()
908-
logger.info("Redis connection established successfully")
909-
self_link = str(request.url)
910-
await save_self_link(redis, next_token, self_link)
911-
912-
prev_link = await get_prev_link(redis, token_param)
913-
if prev_link:
914-
links.insert(
915-
0,
916-
{
917-
"rel": "prev",
918-
"type": "application/json",
919-
"method": "GET",
920-
"href": prev_link,
921-
},
922-
)
923-
except Exception as e:
924-
logger.warning(
925-
f"Redis connection failed, continuing without Redis: {e}"
926-
)
878+
_handle_pagination_via_redis(
879+
redis_enable, next_token, token_param, request, links
880+
)
927881

928882
return stac_types.ItemCollection(
929883
type="FeatureCollection",

stac_fastapi/core/stac_fastapi/core/redis_utils.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
"""Utilities for connecting to and managing Redis connections."""
22

3-
from typing import Optional
3+
import logging
4+
from typing import Dict, List, Optional
45

6+
from fastapi import Request
57
from pydantic_settings import BaseSettings
68
from redis import asyncio as aioredis
79
from redis.asyncio.sentinel import Sentinel
810

11+
logger = logging.getLogger(__name__)
12+
913
redis_pool: Optional[aioredis.Redis] = None
1014

1115

@@ -122,3 +126,38 @@ async def get_prev_link(redis: aioredis.Redis, token: Optional[str]) -> Optional
122126
if not token:
123127
return None
124128
return await redis.get(f"nav:self:{token}")
129+
130+
131+
async def _handle_pagination_via_redis(
132+
redis_enable: bool,
133+
next_token: Optional[str],
134+
token_param: Optional[str],
135+
request: Request,
136+
links: List[Dict],
137+
) -> None:
138+
"""Handle Redis connection and operations for pagination links."""
139+
if not redis_enable:
140+
return
141+
142+
redis = None
143+
try:
144+
redis = await connect_redis()
145+
logger.info("Redis connection established successfully")
146+
147+
if redis and next_token:
148+
self_link = str(request.url)
149+
await save_self_link(redis, next_token, self_link)
150+
151+
prev_link = await get_prev_link(redis, token_param)
152+
if prev_link:
153+
links.insert(
154+
0,
155+
{
156+
"rel": "prev",
157+
"type": "application/json",
158+
"method": "GET",
159+
"href": prev_link,
160+
},
161+
)
162+
except Exception as e:
163+
logger.warning(f"Redis connection failed, continuing without Redis: {e}")

stac_fastapi/tests/redis/test_redis_pagination.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ async def test_search_pagination_uses_redis_cache(
1010
app_client, txn_client, load_test_data
1111
):
1212
"""Test Redis caching and navigation for the /search endpoint."""
13-
13+
import os
14+
print(f"REDIS_ENABLE: {os.getenv('REDIS_ENABLE')}")
15+
1416
collection = load_test_data("test_collection.json")
1517
collection_id = f"test-pagination-collection-{uuid.uuid4()}"
1618
collection["id"] = collection_id
@@ -22,27 +24,39 @@ async def test_search_pagination_uses_redis_cache(
2224
item["collection"] = collection_id
2325
await create_item(txn_client, item)
2426

27+
# First request - should save to Redis
2528
resp = await app_client.post(
2629
"/search", json={"collections": [collection_id], "limit": 1}
2730
)
2831
resp_json = resp.json()
32+
print(f"First response links: {[link['rel'] for link in resp_json['links']]}")
2933

3034
next_link = next(
3135
(link for link in resp_json["links"] if link["rel"] == "next"), None
3236
)
37+
assert next_link is not None, "No next link found in first response"
3338
next_token = next_link["body"]["token"]
39+
print(f"Next token: {next_token}")
3440

35-
# Expect the previous link on the second page to be retrieved from Redis cache
41+
# Second request - should retrieve from Redis
3642
resp2 = await app_client.post(
3743
"/search",
3844
json={"collections": [collection_id], "limit": 1, "token": next_token},
3945
)
4046
resp2_json = resp2.json()
47+
print(f"Second response links: {[link['rel'] for link in resp2_json['links']]}")
4148

4249
prev_link = next(
4350
(link for link in resp2_json["links"] if link["rel"] == "prev"), None
4451
)
45-
assert prev_link is not None
52+
53+
if prev_link is None:
54+
print("DEBUG: No prev link found. Checking all links:")
55+
for link in resp2_json["links"]:
56+
print(f" - {link['rel']}: {link.get('href', 'No href')}")
57+
58+
assert prev_link is not None, "No prev link found - Redis may not be working"
59+
print(f"Prev link found: {prev_link['href']}")
4660

4761

4862
@pytest.mark.asyncio

0 commit comments

Comments
 (0)