-
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.
Merge pull request #93 from gamultong/feature/add-event-recorder
EventRecorder 추가
- Loading branch information
Showing
8 changed files
with
97 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .internal.event_broker import EventBroker, Receiver | ||
from .internal.event_recorder import EventRecorder | ||
from .internal.exceptions import NoMatchingReceiverException |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from message import Message | ||
from db import db | ||
from datetime import datetime | ||
import asyncio | ||
import json | ||
|
||
TABLE_NAME = "events" | ||
|
||
|
||
async def init_table(): | ||
await db.execute(f""" | ||
CREATE TABLE IF NOT EXISTS {TABLE_NAME}( | ||
event TEXT NOT NULL, | ||
timestamp FLOAT NOT NULL, | ||
header TEXT NOT NULL, | ||
payload TEXT NOT NULL | ||
)""") | ||
|
||
asyncio.run(init_table()) | ||
|
||
|
||
class EventRecorder: | ||
async def record(timestamp: datetime, msg: Message): | ||
header = json.dumps(obj=msg.header, sort_keys=True) | ||
payload = json.dumps(obj=msg.payload, default=lambda o: o.__dict__, sort_keys=True) | ||
|
||
await db.execute( | ||
f""" | ||
INSERT INTO {TABLE_NAME} (event, timestamp, header, payload) | ||
VALUES (:event, :timestamp, :header, :payload) | ||
""", | ||
{ | ||
"event": msg.event, | ||
"timestamp": timestamp.timestamp(), | ||
"header": header, | ||
"payload": payload | ||
} | ||
) |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import unittest | ||
|
||
from event import EventRecorder | ||
|
||
from message import Message | ||
from message.payload import ErrorEvent, ErrorPayload | ||
from .utils import clear_records | ||
|
||
from datetime import datetime | ||
|
||
|
||
class EventRecorderTestCase(unittest.IsolatedAsyncioTestCase): | ||
async def asyncSetUp(self): | ||
await clear_records() | ||
|
||
async def asyncTearDown(self): | ||
await clear_records() | ||
|
||
async def test_record(self): | ||
message = Message( | ||
event=ErrorEvent.ERROR, | ||
header={ | ||
"ayo": "pizza here", | ||
"thisisint": 1 | ||
}, | ||
payload=ErrorPayload(msg="heelo world") | ||
) | ||
|
||
timestamp = datetime.now() | ||
|
||
await EventRecorder.record(timestamp, message) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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,5 @@ | ||
from db import db | ||
|
||
|
||
async def clear_records(): | ||
await db.execute("DELETE FROM events") |
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