Skip to content

Commit

Permalink
Functional redis calls
Browse files Browse the repository at this point in the history
Asyncio bugs to be fixed in later commit
  • Loading branch information
Fireye04 committed Nov 15, 2024
1 parent b5464aa commit e791d3b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
36 changes: 18 additions & 18 deletions src/sasquatchbackpack/commands/usgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
)
15 changes: 7 additions & 8 deletions src/sasquatchbackpack/schemas/usgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit e791d3b

Please sign in to comment.