Skip to content

Commit

Permalink
daemon/grpc: refactoring session exceptions into alerts, since they a…
Browse files Browse the repository at this point in the history
…re not exclusive for errors alone
  • Loading branch information
bharnden committed Nov 14, 2023
1 parent 76853b7 commit 4ca11c9
Show file tree
Hide file tree
Showing 16 changed files with 89 additions and 108 deletions.
2 changes: 1 addition & 1 deletion daemon/core/api/grpc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def get_session(self, session_id: int) -> wrappers.Session:
def alert(
self,
session_id: int,
level: wrappers.ExceptionLevel,
level: wrappers.AlertLevel,
source: str,
text: str,
node_id: int = None,
Expand Down
34 changes: 17 additions & 17 deletions daemon/core/api/grpc/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from core.api.grpc import core_pb2, grpcutils
from core.api.grpc.grpcutils import convert_link_data
from core.emulator.data import EventData, ExceptionData, LinkData, NodeData
from core.emulator.data import AlertData, EventData, LinkData, NodeData
from core.emulator.session import Session

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -61,22 +61,22 @@ def handle_session_event(event_data: EventData) -> core_pb2.Event:
return core_pb2.Event(session_event=session_event)


def handle_exception_event(exception_data: ExceptionData) -> core_pb2.Event:
def handle_alert_event(alert_data: AlertData) -> core_pb2.Event:
"""
Handle exception event when there is exception event
Handle alert data, when there is an alert event.
:param exception_data: exception data
:return: exception event
:param alert_data: alert data
:return: alert event
"""
exception_event = core_pb2.ExceptionEvent(
node_id=exception_data.node,
level=exception_data.level.value,
source=exception_data.source,
date=exception_data.date,
text=exception_data.text,
opaque=exception_data.opaque,
alert_event = core_pb2.AlertEvent(
node_id=alert_data.node,
level=alert_data.level.value,
source=alert_data.source,
date=alert_data.date,
text=alert_data.text,
opaque=alert_data.opaque,
)
return core_pb2.Event(exception_event=exception_event)
return core_pb2.Event(alert_event=alert_event)


class EventStreamer:
Expand Down Expand Up @@ -109,7 +109,7 @@ def add_handlers(self) -> None:
if core_pb2.EventType.LINK in self.event_types:
self.session.broadcast_manager.add_handler(LinkData, self.queue.put)
if core_pb2.EventType.EXCEPTION in self.event_types:
self.session.broadcast_manager.add_handler(ExceptionData, self.queue.put)
self.session.broadcast_manager.add_handler(AlertData, self.queue.put)
if core_pb2.EventType.SESSION in self.event_types:
self.session.broadcast_manager.add_handler(EventData, self.queue.put)

Expand All @@ -128,8 +128,8 @@ def process(self) -> Optional[core_pb2.Event]:
event = handle_link_event(data)
elif isinstance(data, EventData):
event = handle_session_event(data)
elif isinstance(data, ExceptionData):
event = handle_exception_event(data)
elif isinstance(data, AlertData):
event = handle_alert_event(data)
else:
logger.error("unknown event: %s", data)
except Empty:
Expand All @@ -149,6 +149,6 @@ def remove_handlers(self) -> None:
if core_pb2.EventType.LINK in self.event_types:
self.session.broadcast_manager.remove_handler(LinkData, self.queue.put)
if core_pb2.EventType.EXCEPTION in self.event_types:
self.session.broadcast_manager.remove_handler(ExceptionData, self.queue.put)
self.session.broadcast_manager.remove_handler(AlertData, self.queue.put)
if core_pb2.EventType.SESSION in self.event_types:
self.session.broadcast_manager.remove_handler(EventData, self.queue.put)
11 changes: 3 additions & 8 deletions daemon/core/api/grpc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,7 @@
from core.emane.modelmanager import EmaneModelManager
from core.emulator.coreemu import CoreEmu
from core.emulator.data import InterfaceData, LinkData, LinkOptions
from core.emulator.enumerations import (
EventTypes,
ExceptionLevels,
MessageFlags,
NodeTypes,
)
from core.emulator.enumerations import AlertLevels, EventTypes, MessageFlags, NodeTypes
from core.emulator.session import NT, Session
from core.errors import CoreCommandError, CoreError
from core.location.mobility import BasicRangeModel, Ns2ScriptedMobility
Expand Down Expand Up @@ -440,9 +435,9 @@ def SessionAlert(
self, request: core_pb2.SessionAlertRequest, context: ServicerContext
) -> core_pb2.SessionAlertResponse:
session = self.get_session(request.session_id, context)
level = ExceptionLevels(request.level)
level = AlertLevels(request.level)
node_id = request.node_id if request.node_id else None
session.broadcast_exception(level, request.source, request.text, node_id)
session.broadcast_alert(level, request.source, request.text, node_id)
return core_pb2.SessionAlertResponse(result=True)

def Events(self, request: core_pb2.EventsRequest, context: ServicerContext) -> None:
Expand Down
26 changes: 11 additions & 15 deletions daemon/core/api/grpc/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class LinkType(Enum):
WIRED = 1


class ExceptionLevel(Enum):
class AlertLevel(Enum):
DEFAULT = 0
FATAL = 1
ERROR = 2
Expand Down Expand Up @@ -292,23 +292,21 @@ def to_proto(self) -> core_pb2.SessionLocation:


@dataclass
class ExceptionEvent:
class AlertEvent:
session_id: int
node_id: int
level: ExceptionLevel
level: AlertLevel
source: str
date: str
text: str
opaque: str

@classmethod
def from_proto(
cls, session_id: int, proto: core_pb2.ExceptionEvent
) -> "ExceptionEvent":
return ExceptionEvent(
def from_proto(cls, session_id: int, proto: core_pb2.AlertEvent) -> "AlertEvent":
return AlertEvent(
session_id=session_id,
node_id=proto.node_id,
level=ExceptionLevel(proto.level),
level=AlertLevel(proto.level),
source=proto.source,
date=proto.date,
text=proto.text,
Expand Down Expand Up @@ -922,31 +920,29 @@ class Event:
session_event: SessionEvent = None
node_event: NodeEvent = None
link_event: LinkEvent = None
exception_event: ExceptionEvent = None
alert_event: AlertEvent = None

@classmethod
def from_proto(cls, proto: core_pb2.Event) -> "Event":
source = proto.source if proto.source else None
node_event = None
link_event = None
exception_event = None
alert_event = None
session_event = None
if proto.HasField("node_event"):
node_event = NodeEvent.from_proto(proto.node_event)
elif proto.HasField("link_event"):
link_event = LinkEvent.from_proto(proto.link_event)
elif proto.HasField("exception_event"):
exception_event = ExceptionEvent.from_proto(
proto.session_id, proto.exception_event
)
elif proto.HasField("alert_event"):
alert_event = AlertEvent.from_proto(proto.session_id, proto.alert_event)
elif proto.HasField("session_event"):
session_event = SessionEvent.from_proto(proto.session_event)
return Event(
session_id=proto.session_id,
source=source,
node_event=node_event,
link_event=link_event,
exception_event=exception_event,
alert_event=alert_event,
session_event=session_event,
)

Expand Down
4 changes: 2 additions & 2 deletions daemon/core/emulator/broadcast.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from collections.abc import Callable
from typing import TypeVar, Union

from core.emulator.data import EventData, ExceptionData, LinkData, NodeData
from core.emulator.data import AlertData, EventData, LinkData, NodeData
from core.errors import CoreError

T = TypeVar("T", bound=Union[EventData, ExceptionData, NodeData, LinkData])
T = TypeVar("T", bound=Union[EventData, AlertData, NodeData, LinkData])


class BroadcastManager:
Expand Down
11 changes: 3 additions & 8 deletions daemon/core/emulator/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@
import netaddr

from core import utils
from core.emulator.enumerations import (
EventTypes,
ExceptionLevels,
LinkTypes,
MessageFlags,
)
from core.emulator.enumerations import AlertLevels, EventTypes, LinkTypes, MessageFlags

if TYPE_CHECKING:
from core.nodes.base import CoreNode, NodeBase
Expand All @@ -29,10 +24,10 @@ class EventData:


@dataclass
class ExceptionData:
class AlertData:
node: int = None
session: int = None
level: ExceptionLevels = None
level: AlertLevels = None
source: str = None
date: str = None
text: str = None
Expand Down
4 changes: 2 additions & 2 deletions daemon/core/emulator/enumerations.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ def already_collected(self) -> bool:
return self.value >= self.DATACOLLECT_STATE.value


class ExceptionLevels(Enum):
class AlertLevels(Enum):
"""
Exception levels.
Alert levels.
"""

NONE = 0
Expand Down
25 changes: 10 additions & 15 deletions daemon/core/emulator/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,15 @@
from core.emulator.broadcast import BroadcastManager
from core.emulator.controlnets import ControlNetManager
from core.emulator.data import (
AlertData,
EventData,
ExceptionData,
InterfaceData,
LinkData,
LinkOptions,
NodeData,
)
from core.emulator.distributed import DistributedController
from core.emulator.enumerations import (
EventTypes,
ExceptionLevels,
MessageFlags,
NodeTypes,
)
from core.emulator.enumerations import AlertLevels, EventTypes, MessageFlags, NodeTypes
from core.emulator.hooks import HookManager
from core.emulator.links import CoreLink, LinkManager
from core.emulator.sessionconfig import SessionConfig
Expand Down Expand Up @@ -646,27 +641,27 @@ def broadcast_event(
)
self.broadcast_manager.send(event_data)

def broadcast_exception(
self, level: ExceptionLevels, source: str, text: str, node_id: int = None
def broadcast_alert(
self, level: AlertLevels, source: str, text: str, node_id: int = None
) -> None:
"""
Generate and broadcast an exception event.
Generate and broadcast an alert event.
:param level: exception level
:param level: alert level
:param source: source name
:param text: exception message
:param node_id: node related to exception, defaults to None
:param text: alert message
:param node_id: node related to alert, defaults to None
:return: nothing
"""
exception_data = ExceptionData(
alert_data = AlertData(
node=node_id,
session=self.id,
level=level,
source=source,
date=time.ctime(),
text=text,
)
self.broadcast_manager.send(exception_data)
self.broadcast_manager.send(alert_data)

def broadcast_node(
self,
Expand Down
10 changes: 5 additions & 5 deletions daemon/core/gui/coreclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

from core.api.grpc import client, core_pb2
from core.api.grpc.wrappers import (
AlertEvent,
ConfigOption,
EmaneModelConfig,
Event,
ExceptionEvent,
Link,
LinkEvent,
LinkType,
Expand Down Expand Up @@ -202,8 +202,8 @@ def handle_events(self, event: Event) -> None:
logger.warning("unknown session event: %s", session_event)
elif event.node_event:
self.app.after(0, self.handle_node_event, event.node_event)
elif event.exception_event:
self.handle_exception_event(event.exception_event)
elif event.alert_event:
self.handle_alert_event(event.alert_event)
else:
logger.info("unhandled event: %s", event)

Expand Down Expand Up @@ -308,8 +308,8 @@ def handle_throughputs(self, event: ThroughputsEvent) -> None:
def handle_cpu_event(self, event: core_pb2.CpuUsageEvent) -> None:
self.app.after(0, self.app.statusbar.set_cpu, event.usage)

def handle_exception_event(self, event: ExceptionEvent) -> None:
logger.info("exception event: %s", event)
def handle_alert_event(self, event: AlertEvent) -> None:
logger.info("alert event: %s", event)
self.app.statusbar.add_alert(event)

def update_session_title(self) -> None:
Expand Down
12 changes: 6 additions & 6 deletions daemon/core/gui/dialogs/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from tkinter import ttk
from typing import TYPE_CHECKING, Optional

from core.api.grpc.wrappers import ExceptionEvent, ExceptionLevel
from core.api.grpc.wrappers import AlertEvent, AlertLevel
from core.gui.dialogs.dialog import Dialog
from core.gui.themes import PADX, PADY
from core.gui.widgets import CodeText
Expand All @@ -19,7 +19,7 @@ def __init__(self, app: "Application") -> None:
super().__init__(app, "Alerts")
self.tree: Optional[ttk.Treeview] = None
self.codetext: Optional[CodeText] = None
self.alarm_map: dict[int, ExceptionEvent] = {}
self.alarm_map: dict[int, AlertEvent] = {}
self.draw()

def draw(self) -> None:
Expand Down Expand Up @@ -67,13 +67,13 @@ def draw(self) -> None:
)
self.alarm_map[insert_id] = exception

error_name = ExceptionLevel.ERROR.name
error_name = AlertLevel.ERROR.name
self.tree.tag_configure(error_name, background="#ff6666")
fatal_name = ExceptionLevel.FATAL.name
fatal_name = AlertLevel.FATAL.name
self.tree.tag_configure(fatal_name, background="#d9d9d9")
warning_name = ExceptionLevel.WARNING.name
warning_name = AlertLevel.WARNING.name
self.tree.tag_configure(warning_name, background="#ffff99")
notice_name = ExceptionLevel.NOTICE.name
notice_name = AlertLevel.NOTICE.name
self.tree.tag_configure(notice_name, background="#85e085")

yscrollbar = ttk.Scrollbar(frame, orient="vertical", command=self.tree.yview)
Expand Down
Loading

0 comments on commit 4ca11c9

Please sign in to comment.