From e791d3b41401d06b979932617ca94d1cdee1f5cd Mon Sep 17 00:00:00 2001 From: Kai Koehler Date: Thu, 14 Nov 2024 17:29:59 -0700 Subject: [PATCH] Functional redis calls Asyncio bugs to be fixed in later commit --- src/sasquatchbackpack/commands/usgs.py | 36 +++++++++++++------------- src/sasquatchbackpack/schemas/usgs.py | 15 +++++------ 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/sasquatchbackpack/commands/usgs.py b/src/sasquatchbackpack/commands/usgs.py index 3541836..28bab70 100644 --- a/src/sasquatchbackpack/commands/usgs.py +++ b/src/sasquatchbackpack/commands/usgs.py @@ -202,8 +202,6 @@ def usgs_earthquake_data( def test_redis() -> None: """Test redis implementation.""" erm = schemas.EarthquakeRedisManager(address="redis://localhost:6379/0") - erm.start_redis() - config = scripts.USGSConfig( timedelta(days=10), DEFAULT_RADIUS, @@ -214,19 +212,21 @@ def test_redis() -> None: records = source.get_records() - for record in records: - # Using earthquake id as redis key - - asyncio.run( - erm.store( - record["value"]["id"], - schemas.EarthquakeSchema( - timestamp=record["value"]["timestamp"], - id=record["value"]["id"], - latitude=record["value"]["latitude"], - longitude=record["value"]["longitude"], - depth=record["value"]["depth"], - magnitude=record["value"]["depth"], - ), - ) - ) + # for record in records: + # Using earthquake id as redis key + + record = records[0] + asyncio.run( + erm.store( + record["value"]["id"], + schemas.EarthquakeSchema( + timestamp=record["value"]["timestamp"], + id=record["value"]["id"], + latitude=record["value"]["latitude"], + longitude=record["value"]["longitude"], + depth=record["value"]["depth"], + magnitude=record["value"]["depth"], + ), + ), + debug=True, + ) diff --git a/src/sasquatchbackpack/schemas/usgs.py b/src/sasquatchbackpack/schemas/usgs.py index 004b843..d9261cb 100644 --- a/src/sasquatchbackpack/schemas/usgs.py +++ b/src/sasquatchbackpack/schemas/usgs.py @@ -27,20 +27,19 @@ class EarthquakeRedisManager: """Manage redis for USGS.""" def __init__(self, address: str) -> None: - self.model = None self.address = address - - def start_redis(self) -> None: - redis_client = redis.Redis.from_url(self.address) + connection = redis.from_url(self.address) self.model = PydanticRedisStorage( - datatype=EarthquakeSchema, redis=redis_client + datatype=EarthquakeSchema, redis=connection ) - async def store(self, key: str, item: EarthquakeSchema) -> None: + async def store(self, key: str, item: EarthquakeSchema) -> bool: if self.model is None: - return - + raise RuntimeError("Model is undefined.") await self.model.store(key, item) + return True async def get(self, key: str) -> EarthquakeSchema: + if self.model is None: + raise RuntimeError("Model is undefined.") return await self.model.get(key)