-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add postgresql_telemetry_sink adapter
- Loading branch information
1 parent
fedea57
commit d0aafc2
Showing
32 changed files
with
293 additions
and
972 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
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
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
5 changes: 1 addition & 4 deletions
5
...rchestrator/edge_orchestrator/infrastructure/metadata_storage/mongodb_metadata_storage.py
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
66 changes: 66 additions & 0 deletions
66
...orchestrator/edge_orchestrator/infrastructure/telemetry_sink/postgresql_telemetry_sink.py
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from datetime import datetime | ||
from random import randrange | ||
from urllib.parse import urlparse | ||
from uuid import uuid4 | ||
|
||
import psycopg2 | ||
import time | ||
from typing import Dict | ||
|
||
from edge_orchestrator import logger | ||
from edge_orchestrator.domain.ports.telemetry_sink import TelemetrySink | ||
|
||
|
||
class PostgresTelemetrySink(TelemetrySink): | ||
|
||
def __init__(self, connection_url: str, timeout: int = 30, interval: int = 2): | ||
self.connection_url = connection_url | ||
self._connection = None | ||
self._timeout = timeout | ||
self._interval = interval | ||
self._device_id = f'device_{randrange(42)}' | ||
|
||
@property | ||
def connection(self): | ||
if self._connection: | ||
return self._connection | ||
|
||
result = urlparse(self.connection_url) | ||
username, password, hostname, port = result.username, result.password, result.hostname, result.port | ||
database = result.path[1:] | ||
|
||
nb_retry = self._timeout // self._interval | ||
for i in range(nb_retry): | ||
try: | ||
self._connection = psycopg2.connect(dbname=database, user=username, password=password, | ||
host=hostname, port=port) | ||
logger.debug(f'Telemetry Postgres DB took ‘{i * self._interval}‘sec to start and be migrated') | ||
return self._connection | ||
except psycopg2.OperationalError: | ||
time.sleep(self._interval) | ||
else: | ||
raise TimeoutError(f'Unable to connect to Telemetry Postgres DB using {self.connection_url} after {self._timeout:.0f} seconds') # noqa | ||
|
||
async def send(self, message: Dict): | ||
try: | ||
_id = uuid4().__str__() | ||
device_id = self._device_id | ||
decision = message['decision'] | ||
timestamp = datetime.now() | ||
item_id = message['item_id'] | ||
config = message['config'] | ||
|
||
self._insert_message(_id, device_id, decision, timestamp, item_id, config) | ||
except psycopg2.DatabaseError as e: | ||
logger.error(f'Message was not correctly inserted into telemetry table : {e}') | ||
|
||
def _insert_message(self, _id: str, device_id: str, decision: str, timestamp: datetime, item_id: str, | ||
config: str): | ||
with self.connection.cursor() as curs: | ||
curs.execute( | ||
'INSERT INTO iothub.telemetry ' | ||
'(id, device_id, business_decision, timestamp, item_id, config) VALUES (%s, %s, %s, %s, %s, %s)', | ||
(_id, device_id, decision, timestamp, item_id, config) | ||
) | ||
self.connection.commit() | ||
logger.warning(f'Telemetry message for item ‘{item_id}‘ stored with id ‘{_id}‘') |
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
Oops, something went wrong.