-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3da2cb8
commit 36c0081
Showing
9 changed files
with
100 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,46 @@ | ||
import logging | ||
import typing | ||
|
||
from pc.config import pg_dsn | ||
from tortoise import Tortoise | ||
import asyncpg | ||
|
||
log = logging.getLogger(__name__) | ||
from ..config import pg_host,pg_port,pg_user,pg_password,pg_database | ||
from .models import BrightnessObservation | ||
|
||
log = logging.getLogger(__name__) | ||
table = "brightness_observation" | ||
|
||
async def initialize_db(): | ||
log.info(f"initializing db at {pg_dsn}") | ||
await Tortoise.init( | ||
db_url=pg_dsn, | ||
modules={"models": ["pc.persistence.models"]} | ||
async def create_pool() -> typing.Optional[asyncpg.Pool]: | ||
pool = await asyncpg.create_pool( | ||
user=pg_user, | ||
password=pg_password, | ||
database=pg_database, | ||
host=pg_host, | ||
port=pg_port, | ||
min_size=1, | ||
max_size=10 | ||
) | ||
await Tortoise.generate_schemas() | ||
return pool | ||
|
||
async def create_brightness_table(pool: asyncpg.Pool): | ||
async with pool.acquire() as conn: | ||
await conn.execute( | ||
f""" | ||
CREATE TABLE IF NOT EXISTS {table} ( | ||
uuid UUID PRIMARY KEY, | ||
lat DOUBLE PRECISION NOT NULL, | ||
lon DOUBLE PRECISION NOT NULL, | ||
h3_id TEXT NOT NULL, | ||
mpsas DOUBLE PRECISION NOT NULL, | ||
timestamp_utc TIMESTAMPTZ NOT NULL | ||
); | ||
""" | ||
) | ||
|
||
|
||
async def insert_brightness_observation(pool, observation: BrightnessObservation): | ||
async with pool.acquire() as conn: | ||
await conn.execute(f""" | ||
INSERT INTO {table} (uuid, lat, lon, h3_id, mpsas, timestamp_utc) | ||
VALUES ($1, $2, $3, $4, $5, $6) | ||
""", observation.uuid, observation.lat, observation.lon, observation.h3_id, observation.mpsas, observation.timestamp_utc) | ||
log.info(f"Inserted observation: {observation}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
from tortoise import fields, models | ||
from pydantic import BaseModel | ||
from datetime import datetime | ||
|
||
|
||
class BrightnessObservation(models.Model): | ||
uuid = fields.CharField(primary_key=True, max_length=36) | ||
lat = fields.FloatField() | ||
lon = fields.FloatField() | ||
h3_id = fields.CharField(max_length=15) | ||
utc_iso = fields.CharField(max_length=32) | ||
mpsas = fields.FloatField() | ||
|
||
def __str__(self): | ||
return f"{self.__class__.__name__}(#{self.h3_id},{self.mpsas},{self.utc_iso})" | ||
class BrightnessObservation(BaseModel): | ||
uuid: str | ||
lat: float | ||
lon: float | ||
h3_id: str | ||
mpsas: float | ||
timestamp_utc: datetime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
aio-pika==9.4.2 | ||
asyncpg | ||
tortoise-orm[asyncpg]~=0.21.6 | ||
aio-pika~=9.4.2 | ||
asyncpg~=0.29.0 | ||
pydantic~=2.9.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,27 @@ | ||
from unittest import mock | ||
from unittest.mock import AsyncMock, patch | ||
|
||
import pytest | ||
import asyncpg | ||
from aio_pika import Message | ||
|
||
from pc.consumer.consumer import Consumer | ||
|
||
@pytest.fixture | ||
def consumer(): | ||
async def mock_asyncpg_pool(): | ||
with patch("asyncpg.create_pool") as mock_create_pool: | ||
mock_pool = AsyncMock() | ||
mock_create_pool.return_value = mock_pool | ||
|
||
mock_connection = AsyncMock() | ||
mock_pool.acquire.return_value.__aenter__.return_value = mock_connection | ||
yield mock_pool | ||
|
||
@pytest.fixture | ||
def consumer(mock_asyncpg_pool): | ||
amqp_url="amqp://localhost" | ||
prediction_queue="prediction" | ||
return Consumer(url=amqp_url, prediction_queue=prediction_queue,cycle_queue="") | ||
return Consumer(url=amqp_url, prediction_queue=prediction_queue,cycle_queue="",connection_pool=mock_asyncpg_pool) | ||
|
||
@pytest.mark.skip | ||
@pytest.mark.asyncio | ||
async def test_can_consume_message(consumer): | ||
pass | ||
async def test_consumer(consumer): | ||
assert consumer is not None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters