Skip to content

Commit

Permalink
Add functional redis push and pull!
Browse files Browse the repository at this point in the history
  • Loading branch information
Fireye04 committed Nov 21, 2024
1 parent c6734d2 commit 1cd1934
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
25 changes: 11 additions & 14 deletions src/sasquatchbackpack/commands/usgs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""USGS CLI."""

import asyncio
from datetime import timedelta

import click
Expand Down Expand Up @@ -214,19 +213,17 @@ def test_redis() -> None:

# 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"],
),
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,
)

erm.get(record["value"]["id"])
27 changes: 17 additions & 10 deletions src/sasquatchbackpack/schemas/usgs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""USGS Schemas."""

import asyncio

import redis.asyncio as redis
from dataclasses_avroschema.pydantic import AvroBaseModel
from pydantic import Field
Expand All @@ -10,11 +12,13 @@ class EarthquakeSchema(AvroBaseModel):
"""Collection of earthquakes near the summit."""

timestamp: int
id: str = Field(metadata={"description": "unique earthquake id"})
latitude: float = Field(metadata={"units": "degree"})
longitude: float = Field(metadata={"units": "degree"})
depth: float = Field(metadata={"units": "km"})
magnitude: float = Field(metadata={"units": "u.richter_magnitudes"})
id: str = Field(description="unique earthquake id")
latitude: float = Field(json_schema_extra={"units": "degree"})
longitude: float = Field(json_schema_extra={"units": "degree"})
depth: float = Field(json_schema_extra={"units": "km"})
magnitude: float = Field(
json_schema_extra={"units": "u.richter_magnitudes"}
)

class Meta:
"""Schema metadata."""
Expand All @@ -32,14 +36,17 @@ def __init__(self, address: str) -> None:
self.model = PydanticRedisStorage(
datatype=EarthquakeSchema, redis=connection
)
self.loop = asyncio.new_event_loop()

async def store(self, key: str, item: EarthquakeSchema) -> bool:
def store(self, key: str, item: EarthquakeSchema) -> None:
if self.model is None:
raise RuntimeError("Model is undefined.")
await self.model.store(key, item)
return True
self.loop.run_until_complete(self.model.store(key, item))

async def get(self, key: str) -> EarthquakeSchema:
def get(self, key: str) -> EarthquakeSchema:
if self.model is None:
raise RuntimeError("Model is undefined.")
return await self.model.get(key)
target = self.loop.run_until_complete(self.model.get(key))
if target is None:
raise LookupError(f"Entry with key of {key} could not be found")
return target

0 comments on commit 1cd1934

Please sign in to comment.