Skip to content

Commit

Permalink
feat(message-replay): add docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
DlieBG committed Nov 27, 2024
1 parent cfc5886 commit 95f55d0
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 137 deletions.
26 changes: 22 additions & 4 deletions src/adapters/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
from src.models.route_update import RouteUpdate
from src.models.route_update import ChangeType
from pymongo import MongoClient
from datetime import datetime
from typing import Optional
from bson import ObjectId
from datetime import datetime
from collections import OrderedDict
import os

class MongoDBAdapter:
Expand Down Expand Up @@ -224,9 +223,28 @@ def on_update(message: RouteUpdate):
}
statistics_announce = statistics_collection.update_one(statistics_filter, new_values, upsert=True)

class MongoDBLogLoader:
'''
This class is responsible for loading messages from the MongoDB Log.
Author:
Sebastian Forstner <sef9869@thi.de>
'''
@staticmethod
def load_messages(timestamp_start: datetime, timestamp_end: datetime) -> list[dict]:
'''
Loads messages from the MongoDB Log.
Author:
Sebastian Forstner <sef9869@thi.de>
class DB_logoutput:
def load_messages(timestamp_start: datetime, timestamp_end: datetime):
Args:
timestamp_start (datetime): The start timestamp.
timestamp_end (datetime): The end timestamp.
Returns:
list[dict]: The loaded messages.
'''
try:
'''Connects to MongoDB-Container running with Docker'''
database_client = MongoClient(
Expand Down
38 changes: 29 additions & 9 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
Benedikt Schwering <bes9584@thi.de>
Sebastian Forstner <sef9869@thi.de>
'''
from src.adapters.mongodb import MongoDBAdapter, DB_logoutput
from src.adapters.mongodb import MongoDBAdapter, MongoDBLogLoader
import src.services.mrt_simulation as mrt_simulation_service
from src.adapters.rabbitmq import RabbitMQAdapter
from src.parsers.dbreverse import ReverseParser
from src.parsers.reverse import ReverseParser
import src.services.exabgp as exabgp_service
from datetime import timedelta, datetime
from src.parsers.rib import RibParser
Expand Down Expand Up @@ -367,28 +367,48 @@ def rib_load(no_rabbitmq_direct: bool, rabbitmq_grouped: int, no_mongodb_log: bo
'-b',
type=float,
default=None,
help='Starttime of replay as timestamp',
help='Starttime of replay as timestamp.',
)
@click.option(
'--end-timestamp',
'-e',
type=float,
default=None,
help='Endtime of replay as timestamp',
help='Endtime of replay as timestamp.',
)
@click.option(
'--start-time',
'-r',
type=str,
help='Starttime of replay as time; in format (T is a set character): YYYY-MM-DDThh:mm:ss',
help='Starttime of replay as time; in format (T is a set character): YYYY-MM-DDThh:mm:ss.',
)
@click.option(
'--end-time',
'-f',
type=str,
help='Endtime of replay as time; in format (T is a set character): YYYY-MM-DDThh:mm:ss',
help='Endtime of replay as time; in format (T is a set character): YYYY-MM-DDThh:mm:ss.',
)
def message_replay(no_rabbitmq_direct: bool, rabbitmq_grouped: int, no_mongodb_log: bool, no_mongodb_state: bool, no_mongodb_statistics: bool, clear_mongodb: bool, playback_speed: int, playback_interval: int, start_timestamp: float, end_timestamp: float, start_time: str, end_time: str):
'''
Message replay command for replaying BGP messages from Database log.
Author:
Sebastian Forstner <sef9869@thi.de>
Args:
no_rabbitmq_direct (bool): Disable direct RabbitMQ direct queue..
rabbitmq_grouped (int): Queue group interval in minutes.
no_mongodb_log (bool): Disable logging to MongoDB.
no_mongodb_state (bool): Disable state storage to MongoDB.
no_mongodb_statistics (bool): Disable statistics storage to MongoDB.
clear_mongodb (bool): Clear MongoDB collections.
playback_speed (int): Playback speed in multiples of real time.
playback_interval (int): Playback interval in minutes.
start_timestamp (float): Starttime of replay as timestamp.
end_timestamp (float): Endtime of replay as timestamp.
start_time (str): Starttime of replay as time; in format (T is a set character): YYYY-MM-DDThh:mm:ss.
end_time (str): Endtime of replay as time; in format (T is a set character): YYYY-MM-DDThh:mm:ss.
'''
parser = ReverseParser()

if not no_rabbitmq_direct or rabbitmq_grouped:
Expand All @@ -405,13 +425,13 @@ def message_replay(no_rabbitmq_direct: bool, rabbitmq_grouped: int, no_mongodb_l
if start_timestamp and end_timestamp:
start_time = datetime.fromtimestamp(start_timestamp)
end_time = datetime.fromtimestamp(end_timestamp)
new_messages = DB_logoutput.load_messages(timestamp_start = start_time, timestamp_end = end_time)
new_messages = MongoDBLogLoader.load_messages(timestamp_start = start_time, timestamp_end = end_time)
elif start_time and end_time:
time_start = datetime.fromisoformat(start_time)
time_end = datetime.fromisoformat(end_time)
new_messages = DB_logoutput.load_messages(timestamp_start = time_start, timestamp_end = time_end)
new_messages = MongoDBLogLoader.load_messages(timestamp_start = time_start, timestamp_end = time_end)
else:
new_messages = DB_logoutput.load_messages(timestamp_start = None, timestamp_end = None)
new_messages = MongoDBLogLoader.load_messages(timestamp_start = None, timestamp_end = None)

# Copy messages in local list to avoid deleting them if -c is set; new_messages is corsor pointing to db
all_messages: list[OrderedDict] = []
Expand Down
124 changes: 0 additions & 124 deletions src/parsers/dbreverse.py

This file was deleted.

Loading

0 comments on commit 95f55d0

Please sign in to comment.