Skip to content

Commit bacc814

Browse files
Yuri ZmytrakovYuri Zmytrakov
authored andcommitted
fix: implement recommendations
1 parent cb7ee7c commit bacc814

File tree

12 files changed

+249
-153
lines changed

12 files changed

+249
-153
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ repos:
3131
]
3232
additional_dependencies: [
3333
"types-attrs",
34-
"types-requests"
34+
"types-requests",
35+
"types-redis"
3536
]
3637
- repo: https://github.com/PyCQA/pydocstyle
3738
rev: 6.1.1

Makefile

Lines changed: 1 addition & 13 deletions
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-elasticsearch test-datetime-filtering-es test-opensearch test-datetime-filtering-os
8686

8787
.PHONY: run-database-es
8888
run-database-es:
@@ -118,15 +118,3 @@ docs-image:
118118
docs: docs-image
119119
docker compose -f compose.docs.yml \
120120
run docs
121-
122-
.PHONY: test-redis-es
123-
test-redis-es:
124-
docker compose -f compose-redis.yml up -d
125-
-$(run_es) /bin/bash -c 'export REDIS_ENABLE=true REDIS_HOST=redis REDIS_PORT=6379 && ./scripts/wait-for-it-es.sh elasticsearch:9200 && cd stac_fastapi/tests/ && pytest redis/ -v'
126-
docker compose -f compose-redis.yml down
127-
128-
.PHONY: test-redis-os
129-
test-redis-os:
130-
docker compose -f compose-redis.yml up -d
131-
-$(run_os) /bin/bash -c 'export REDIS_ENABLE=true REDIS_HOST=redis REDIS_PORT=6379 && ./scripts/wait-for-it-es.sh opensearch:9202 && cd stac_fastapi/tests/ && pytest redis/ -v'
132-
docker compose -f compose-redis.yml down

compose-redis.yml

Lines changed: 0 additions & 13 deletions
This file was deleted.

compose.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ services:
2323
- BACKEND=elasticsearch
2424
- DATABASE_REFRESH=true
2525
- ENABLE_COLLECTIONS_SEARCH_ROUTE=true
26+
- REDIS_ENABLE=true
27+
- REDIS_HOST=redis
28+
- REDIS_PORT=6379
2629
ports:
2730
- "8080:8080"
2831
volumes:
@@ -31,6 +34,7 @@ services:
3134
- ./esdata:/usr/share/elasticsearch/data
3235
depends_on:
3336
- elasticsearch
37+
- redis
3438
command:
3539
bash -c "./scripts/wait-for-it-es.sh es-container:9200 && python -m stac_fastapi.elasticsearch.app"
3640

@@ -58,6 +62,9 @@ services:
5862
- BACKEND=opensearch
5963
- STAC_FASTAPI_RATE_LIMIT=200/minute
6064
- ENABLE_COLLECTIONS_SEARCH_ROUTE=true
65+
- REDIS_ENABLE=true
66+
- REDIS_HOST=redis
67+
- REDIS_PORT=6379
6168
ports:
6269
- "8082:8082"
6370
volumes:
@@ -66,6 +73,7 @@ services:
6673
- ./osdata:/usr/share/opensearch/data
6774
depends_on:
6875
- opensearch
76+
- redis
6977
command:
7078
bash -c "./scripts/wait-for-it-es.sh os-container:9202 && python -m stac_fastapi.opensearch.app"
7179

@@ -96,3 +104,14 @@ services:
96104
- ./opensearch/snapshots:/usr/share/opensearch/snapshots
97105
ports:
98106
- "9202:9202"
107+
108+
redis:
109+
image: redis:7-alpine
110+
hostname: redis
111+
ports:
112+
- "6379:6379"
113+
volumes:
114+
- redis_test_data:/data
115+
command: redis-server
116+
volumes:
117+
redis_test_data:

mypy.ini

Lines changed: 0 additions & 3 deletions
This file was deleted.

stac_fastapi/core/setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"jsonschema~=4.0.0",
2121
"slowapi~=0.1.9",
2222
]
23+
extra_reqs = {"redis": ["redis~=6.4.0"]}
2324

2425
setup(
2526
name="stac_fastapi_core",
@@ -43,4 +44,5 @@
4344
packages=find_namespace_packages(),
4445
zip_safe=False,
4546
install_requires=install_requires,
47+
extras_require=extra_reqs,
4648
)

stac_fastapi/core/stac_fastapi/core/core.py

Lines changed: 16 additions & 54 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 redis_pagination_links
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
@@ -270,6 +270,7 @@ async def all_collections(
270270
A Collections object containing all the collections in the database and links to various resources.
271271
"""
272272
base_url = str(request.base_url)
273+
redis_enable = get_bool_env("REDIS_ENABLE", default=False)
273274

274275
# Get the global limit from environment variable
275276
global_limit = None
@@ -329,20 +330,6 @@ async def all_collections(
329330
if parsed_sort:
330331
sort = parsed_sort
331332

332-
current_url = str(request.url)
333-
redis_enable = get_bool_env("REDIS_ENABLE", default=False)
334-
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-
346333
# Convert q to a list if it's a string
347334
q_list = None
348335
if q is not None:
@@ -441,21 +428,13 @@ async def all_collections(
441428
},
442429
]
443430

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-
)
431+
if redis_enable:
432+
await redis_pagination_links(
433+
current_url=str(request.url),
434+
token=token,
435+
next_token=next_token,
436+
links=links,
437+
)
459438

460439
if next_token:
461440
next_link = PagingLinks(next=next_token, request=request).link_next()
@@ -775,9 +754,8 @@ async def post_search(
775754
HTTPException: If there is an error with the cql2_json filter.
776755
"""
777756
base_url = str(request.base_url)
778-
redis_enable = get_bool_env("REDIS_ENABLE", default=False)
779-
780757
search = self.database.make_search()
758+
redis_enable = get_bool_env("REDIS_ENABLE", default=False)
781759

782760
if search_request.ids:
783761
search = self.database.apply_ids_filter(
@@ -902,28 +880,12 @@ async def post_search(
902880
links.extend(collection_links)
903881

904882
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-
)
883+
await redis_pagination_links(
884+
current_url=str(request.url),
885+
token=token_param,
886+
next_token=next_token,
887+
links=links,
888+
)
927889

928890
return stac_types.ItemCollection(
929891
type="FeatureCollection",

0 commit comments

Comments
 (0)