SIENTIAPDE-988

Enhance ingestor and manager classes with notification handling

- Integrated NotificationHandler into Ingestor, DataManager, IngestorManager, and OpcManager for improved error reporting and monitoring.
- Updated methods to send notifications on critical events such as Kafka publishing errors, OPC connection issues, and cycle count warnings.
- Refactored related tests to ensure coverage of new notification functionalities and validate integration with existing components.
- Improved logging and error handling across the system to enhance traceability and operational insights.
This commit is contained in:
vitor-aignosi
2025-04-30 19:28:43 -03:00
parent 3fc3753636
commit d00078d323
8 changed files with 423 additions and 168 deletions

View File

@@ -1,7 +1,7 @@
from unittest.mock import ANY, MagicMock, patch
from pytest import fixture
from kafka.errors import NoBrokersAvailable
from sientia_do.notifications.models import NotificationLevel
from ingestor.managers.data_manager import DataManager
@@ -10,7 +10,8 @@ from ingestor.managers.data_manager import DataManager
def data_manager(kafka):
return DataManager(
kafka_servers="localhost:9092",
logger=MagicMock()
logger=MagicMock(),
notification_handler=MagicMock()
)
@@ -20,7 +21,8 @@ def test___init___success(kafka):
data_manager = DataManager(
kafka_servers="localhost:9092",
logger=logger_mock
logger=logger_mock,
notification_handler=MagicMock()
)
kafka.assert_called_once_with(
@@ -46,7 +48,8 @@ def test___init___second_attempt(kafka):
data_manager = DataManager(
kafka_servers="localhost:9092",
logger=logger_mock
logger=logger_mock,
notification_handler=MagicMock()
)
kafka.assert_any_call(
@@ -79,7 +82,8 @@ def test___init___failure_max_attempts(kafka):
try:
DataManager(
kafka_servers="localhost:9092",
logger=logger_mock
logger=logger_mock,
notification_handler=MagicMock()
)
except NoBrokersAvailable as e:
assert str(
@@ -172,7 +176,8 @@ def test_publish(data_manager):
data_manager.kafka_producer.flush.assert_called_once()
def test_publish_error(data_manager):
@patch("ingestor.managers.data_manager.traceback")
def test_publish_error(traceback, data_manager):
topic = "test_topic"
data = {"key": "value"}
@@ -189,6 +194,10 @@ def test_publish_error(data_manager):
)
# Check if the error was logged
data_manager.logger.error.assert_called_once_with(
"Failed to publish message: Test error"
data_manager.notification_handler.build_and_send_notification.assert_called_once_with(
notification_id=f"KAFKA_PRODUCER_ERROR_{topic}",
message=f"Error publishing message to topic {topic}: Test error",
block="kafka_producer",
level=NotificationLevel.ERROR,
attachment_content=traceback.format_exc.return_value
)

View File

@@ -1,6 +1,6 @@
from unittest.mock import MagicMock, patch
from pytest import fixture
from sientia_do.notifications.models import NotificationLevel
from ingestor.managers.ingestor_manager import IngestorManager
@@ -16,14 +16,16 @@ def ingestor_manager(data_manager_mock, resource_manager_mock):
heartbeat_ttl=60,
pod_id="test_pod",
poll_interval=5,
logger=MagicMock()
logger=MagicMock(),
notification_handler=MagicMock()
)
@patch('ingestor.managers.ingestor_manager.OpcManager')
@patch('ingestor.managers.ingestor_manager.DataManager')
@patch('ingestor.managers.ingestor_manager.ResourceManager')
def test___init__(resource_manager_mock, data_manager_mock, opc_manager_mock):
@patch('ingestor.managers.ingestor_manager.NotificationHandler')
def test___init__(notification_handler_mock, resource_manager_mock, data_manager_mock, opc_manager_mock):
ingestor = IngestorManager(
kafka_servers="localhost:9092",
@@ -33,14 +35,15 @@ def test___init__(resource_manager_mock, data_manager_mock, opc_manager_mock):
heartbeat_ttl=60,
pod_id="test_pod",
poll_interval=5,
logger=MagicMock()
logger=MagicMock(),
notification_handler=MagicMock()
)
opc_manager_mock.assert_not_called()
data_manager_mock.assert_called_once_with(
"localhost:9092", ingestor.logger)
resource_manager_mock.assert_called_once_with(
"localhost", 6379, 60, 60, "test_pod")
"localhost", 6379, 60, 60, "test_pod", None, None)
assert ingestor.poll_interval == 5
assert ingestor.managed_tags == {}
@@ -76,7 +79,8 @@ def test_initialize_opc_from_config(opc_manager, ingestor_manager):
@patch('ingestor.managers.ingestor_manager.OpcManager')
def test_initialize_opc_from_config_exception(opc_manager, ingestor_manager):
@patch('ingestor.managers.ingestor_manager.traceback')
def test_initialize_opc_from_config_exception(traceback_mock, opc_manager, ingestor_manager):
server_config = {
'name': 'server1',
'url': 'opc.tcp://localhost:4840',
@@ -93,8 +97,15 @@ def test_initialize_opc_from_config_exception(opc_manager, ingestor_manager):
server_config, ingestor_manager.data_manager, ingestor_manager.logger)
assert result is None
ingestor_manager.logger.error.assert_called_once_with(
"Failed to initialize OpcManager: Initialization error")
traceback_mock.format_exc.assert_called_once()
ingestor_manager.notification_handler.build_and_send_notification.assert_called_once_with(
notification_id=f'OPC_CONNECTION_ERROR_{server_config["name"]}',
message='Error initializing OPC manager: Initialization error',
block="opc_manager",
level=NotificationLevel.ERROR,
attachment_content=traceback_mock.format_exc.return_value
)
@patch('ingestor.managers.ingestor_manager.OpcManager')
@@ -406,7 +417,8 @@ def test_manage_server(ingestor_manager):
'slot1', 'config1', ingestor_manager.poll_interval)
def test_manage_server_subscribe_failure(ingestor_manager):
@patch('ingestor.managers.ingestor_manager.traceback')
def test_manage_server_subscribe_failure(traceback_mock, ingestor_manager):
ingestor_manager.opc_managers = {
"server1": MagicMock(),
"server2": MagicMock()
@@ -431,9 +443,17 @@ def test_manage_server_subscribe_failure(ingestor_manager):
'slot1', 'config1', ingestor_manager.poll_interval)
ingestor_manager.opc_managers["server1"].unsubscribe.assert_called_once_with(
'slot1')
ingestor_manager.logger.error.assert_any_call(
"Failed to subscribe to tags from slot1:server1\n{'tags': 'config1'}: Subscription error"
traceback_mock.format_exc.assert_called_once()
ingestor_manager.notification_handler.build_and_send_notification.assert_called_once_with(
notification_id='OPC_SUBSCRIPTION_ERROR_slot1:server1',
message='Failed to subscribe to tags from slot1:server1\n{\'tags\': \'config1\'}: Subscription error',
block="opc_manager",
level=NotificationLevel.ERROR,
attachment_content=traceback_mock.format_exc.return_value
)
ingestor_manager.logger.warning.assert_any_call(
"Removing subscription from server server1 for slot slot1"
)
@@ -475,3 +495,129 @@ def test_subscribe_to_tags(ingestor_manager):
ingestor_manager.managed_tags['slot1'].pop.assert_called_once_with(
'server3', None)
def test_check_opc_servers_integrity_all_healthy(ingestor_manager):
# Setup mock OPC managers
opc_manager1 = MagicMock()
opc_manager1.check_cycles.return_value = None
opc_manager1.check_opc_listenning.return_value = False
opc_manager1.config = {"config": "config1"}
opc_manager2 = MagicMock()
opc_manager2.check_cycles.return_value = None
opc_manager2.check_opc_listenning.return_value = False
opc_manager2.config = {"config": "config2"}
ingestor_manager.opc_managers = {
"server1": opc_manager1,
"server2": opc_manager2
}
# Mock the initialize_opc_from_config method
ingestor_manager.initialize_opc_from_config = MagicMock()
# Call the method
ingestor_manager.check_opc_servers_integrity()
# Verify that check_cycles and check_opc_listenning were called for each server
opc_manager1.check_cycles.assert_called_once()
opc_manager1.check_opc_listenning.assert_called_once()
opc_manager2.check_cycles.assert_called_once()
opc_manager2.check_opc_listenning.assert_called_once()
# Verify that no reinitialization was needed
ingestor_manager.initialize_opc_from_config.assert_not_called()
def test_check_opc_servers_integrity_server_lost(ingestor_manager):
# Setup mock OPC manager that will be lost
opc_manager = MagicMock()
opc_manager.check_cycles.return_value = None
opc_manager.check_opc_listenning.return_value = True # Server is lost
opc_manager.config = {"config": "config1"}
ingestor_manager.opc_managers = {
"server1": opc_manager
}
# Mock the initialize_opc_from_config method to return a new manager
new_manager = MagicMock()
ingestor_manager.initialize_opc_from_config = MagicMock(
return_value=new_manager)
# Mock update_opc_servers and manage_server
ingestor_manager.update_opc_servers = MagicMock()
ingestor_manager.manage_server = MagicMock()
# Call the method
ingestor_manager.check_opc_servers_integrity()
# Verify that the lost server was disconnected
opc_manager.disconnect.assert_called_once()
# Verify that a new manager was initialized
ingestor_manager.initialize_opc_from_config.assert_called_once_with(
opc_manager.config, ingestor_manager.data_manager, ingestor_manager.logger
)
# Verify that the new manager was assigned
assert ingestor_manager.opc_managers["server1"] == new_manager
# Verify that update_opc_servers was called
ingestor_manager.update_opc_servers.assert_called_once()
def test_check_opc_servers_integrity_server_lost_with_tags(ingestor_manager):
# Setup mock OPC manager that will be lost
opc_manager = MagicMock()
opc_manager.check_cycles.return_value = None
opc_manager.check_opc_listenning.return_value = True # Server is lost
opc_manager.config = {"config": "config1"}
ingestor_manager.opc_managers = {
"server1": opc_manager
}
# Setup managed tags
ingestor_manager.managed_tags = {
"slot1": {
"server1": {
"config": "config1",
"tags": {"tag1": "value1"}
}
}
}
# Mock the initialize_opc_from_config method to return a new manager
new_manager = MagicMock()
ingestor_manager.initialize_opc_from_config = MagicMock(
return_value=new_manager)
# Mock update_opc_servers and manage_server
ingestor_manager.update_opc_servers = MagicMock()
ingestor_manager.manage_server = MagicMock()
# Call the method
ingestor_manager.check_opc_servers_integrity()
# Verify that the lost server was disconnected
opc_manager.disconnect.assert_called_once()
# Verify that a new manager was initialized
ingestor_manager.initialize_opc_from_config.assert_called_once_with(
opc_manager.config, ingestor_manager.data_manager, ingestor_manager.logger
)
# Verify that the new manager was assigned
assert ingestor_manager.opc_managers["server1"] == new_manager
# Verify that update_opc_servers was called
ingestor_manager.update_opc_servers.assert_called_once()
# Verify that manage_server was called with the correct tags
ingestor_manager.manage_server.assert_called_once_with(
"slot1", "server1",
{"config": "config1", "tags": {"tag1": "value1"}},
{"tag1": "value1"}
)

View File

@@ -4,6 +4,7 @@ from unittest.mock import MagicMock, patch
from pytest import fixture
from asyncua.crypto.security_policies import SecurityPolicyBasic256
import pytest
from ingestor.managers.opc_manager import OpcManager
from sientia_do.notifications.models import NotificationLevel
@@ -36,7 +37,7 @@ tags = {
def raw_opc_manager():
return OpcManager(
'TestConnector', 'opc.tcp://localhost:4840', MagicMock(),
MagicMock(), 'opc.tcp://localhost:4840'
MagicMock(), 'opc.tcp://localhost:4840', MagicMock()
)
@@ -240,82 +241,102 @@ def test_datachange_notification(opc_manager_subscribed):
assert opc_manager_subscribed.nodes['ns=3;i=1001']['cycle_rule']['cycle_count'] == 0
def test_check_cycles(opc_manager_subscribed):
handler = MagicMock()
opc_manager_subscribed.nodes = tags
opc_manager_subscribed.nodes['ns=3;i=1001']['cycle_rule'] = {
'cycle_increment': 1.0,
'cycle_count': 0
def test_check_cycles_no_notification(opc_manager):
# Setup: node with cycle_count just below threshold
opc_manager.nodes = {
'ns=3;i=1001': {
'tag_name': 'Counter',
'cycle_rule': {
'cycle_increment': 1.0,
'cycle_count': 3.0
}
}
}
opc_manager_subscribed.check_cycles(
{}, 'ns=3;i=1001', tags['ns=3;i=1001'], handler)
opc_manager.notification_handler.build_and_send_notification = MagicMock()
assert opc_manager_subscribed.nodes['ns=3;i=1001']['cycle_rule']['cycle_count'] == 1
opc_manager.check_cycles()
opc_manager_subscribed.nodes['ns=3;i=1001']['cycle_rule']['cycle_count'] = 0
opc_manager_subscribed.check_cycles({'ns=3;i=1001': {}},
'ns=3;i=1001', tags['ns=3;i=1001'], handler)
assert opc_manager_subscribed.nodes['ns=3;i=1001']['cycle_rule']['cycle_count'] == 0
opc_manager_subscribed.nodes['ns=3;i=1001']['cycle_rule']['cycle_count'] = 4
opc_manager_subscribed.check_cycles(
{}, 'ns=3;i=1001', tags['ns=3;i=1001'], handler)
assert opc_manager_subscribed.nodes['ns=3;i=1001']['cycle_rule']['cycle_count'] == 5
handler.assert_called_once_with('5.0 cycles without receive from ns=3;i=1001:Counter',
'TAG_ns=3;i=1001:Counter_LISTENNING_STOPPED', NotificationLevel.WARNING)
# After one increment, cycle_count = 4.0, still below threshold
assert opc_manager.nodes['ns=3;i=1001']['cycle_rule']['cycle_count'] == pytest.approx(
4.0)
opc_manager.notification_handler.build_and_send_notification.assert_not_called()
def test_check_opc_listenning_5_cycles(opc_manager_subscribed):
def test_check_cycles_triggers_notification(opc_manager):
# Setup: node with cycle_count just below threshold, increment will cross threshold
opc_manager.nodes = {
'ns=3;i=1001': {
'tag_name': 'Counter',
'cycle_rule': {
'cycle_increment': 2.5,
'cycle_count': 3.0
}
}
}
opc_manager.notification_handler.build_and_send_notification = MagicMock()
handler = MagicMock()
opc_manager_subscribed.nodes = tags
opc_manager_subscribed.non_receive_count = 4
opc_manager.check_cycles()
opc_manager_subscribed.check_opc_listenning(handler)
handler.assert_called_once_with(
f'5 cycles without receive from OPC TestConnector. Tags: {json.dumps(tags)}',
'OPC_LISTENNING_STOPPED__TestConnector',
NotificationLevel.ERROR
# After increment, cycle_count = 5.5, should trigger notification
assert opc_manager.nodes['ns=3;i=1001']['cycle_rule']['cycle_count'] == pytest.approx(
5.5)
opc_manager.notification_handler.build_and_send_notification.assert_called_once_with(
notification_id='TAG_ns=3;i=1001:Counter_LISTENNING_STOPPED',
message='5.5 cycles without receive from ns=3;i=1001:Counter',
block="opc_manager",
level=NotificationLevel.WARNING
)
def test_check_opc_listenning_no_cycles(opc_manager_subscribed):
handler = MagicMock()
opc_manager_subscribed.non_receive_count = 0
def test_check_opc_listenning_no_notification(opc_manager):
opc_manager.non_receive_count = 3
opc_manager.notification_handler.build_and_send_notification = MagicMock()
opc_manager_subscribed.check_opc_listenning(handler)
result = opc_manager.check_opc_listenning()
handler.assert_not_called()
assert opc_manager.non_receive_count == 4
opc_manager.notification_handler.build_and_send_notification.assert_not_called()
assert result is False
def test_check_opc_listenning_no_handler(opc_manager_subscribed):
opc_manager_subscribed.non_receive_count = 5
opc_manager_subscribed.check_opc_listenning(None)
def test_check_opc_listenning_warning_notification(opc_manager):
opc_manager.non_receive_count = 4
opc_manager.notification_handler.build_and_send_notification = MagicMock()
assert opc_manager_subscribed.non_receive_count == 6
result = opc_manager.check_opc_listenning()
def test_check_opc_listenning_15_cycles(opc_manager_subscribed):
handler = MagicMock()
opc_manager_subscribed.init_collector = MagicMock()
opc_manager_subscribed.non_receive_count = 14
opc_manager_subscribed.collect_period = 1000
opc_manager_subscribed.period = 500
opc_manager_subscribed.nodes = tags
opc_manager_subscribed.check_opc_listenning(handler)
handler.assert_any_call(
f'15 cycles without receive from OPC TestConnector. Tags: {json.dumps(tags)}',
'OPC_LISTENNING_STOPPED__TestConnector',
NotificationLevel.ERROR
assert opc_manager.non_receive_count == 5
opc_manager.notification_handler.build_and_send_notification.assert_called_once_with(
notification_id=f'OPC_LISTENNING_STOPPED__{opc_manager.name}',
message=f'5 cycles without receive from OPC {opc_manager.name}. Tags: {json.dumps(opc_manager.nodes)}',
block="opc_manager",
level=NotificationLevel.ERROR
)
handler.assert_any_call(
'Retrying to connect to server TestConnector',
'OPC_CONNECTION_RETRY__TestConnector',
NotificationLevel.ERROR
assert result is False
def test_check_opc_listenning_error_notification_and_retry(opc_manager):
opc_manager.non_receive_count = 14
opc_manager.notification_handler.build_and_send_notification = MagicMock()
result = opc_manager.check_opc_listenning()
assert opc_manager.non_receive_count == 15
# Should be called twice: once for 5, once for 15
assert opc_manager.notification_handler.build_and_send_notification.call_count == 2
calls = opc_manager.notification_handler.build_and_send_notification.call_args_list
# First call: 5 cycles warning
assert calls[0].kwargs == dict(
notification_id=f'OPC_LISTENNING_STOPPED__{opc_manager.name}',
message=f'15 cycles without receive from OPC {opc_manager.name}. Tags: {json.dumps(opc_manager.nodes)}',
block="opc_manager",
level=NotificationLevel.ERROR
)
opc_manager_subscribed.init_collector.assert_called_once_with(
tags, 1000, 500)
# Second call: 15 cycles retry
assert calls[1].kwargs == dict(
notification_id=f'OPC_CONNECTION_RETRY__{opc_manager.name}',
message=f'Retrying to connect to server {opc_manager.name}',
block="opc_manager",
level=NotificationLevel.ERROR
)
assert result is True

View File

@@ -1,4 +1,4 @@
from unittest.mock import MagicMock, patch
from unittest.mock import ANY, MagicMock, patch
from pytest import fixture
from ingestor.ingestor import Ingestor
@@ -6,21 +6,27 @@ from ingestor.ingestor import Ingestor
@patch("ingestor.ingestor.getenv")
@patch("ingestor.ingestor.Ingestor.init_logger")
def test___init__(init_logger, getenv):
@patch("ingestor.ingestor.NotificationHandler")
def test___init__(notification_handler, init_logger, getenv):
getenv.side_effect = [
"localhost:9092,localhost:35", # KAFKA_SERVERS
"localhost1", # REDIS_HOST
'63790', # REDIS_PORT
"user", # REDIS_USERNAME
"password", # REDIS_PASSWORD
'100', # LEASE_TTL
'200', # HEARTBEAT_TTL
"localhost1", # HOSTNAME
'50' # POLL_INTERVAL
]
ingestor = Ingestor()
getenv.assert_any_call("KAFKA_SERVERS", "localhost:9092")
getenv.assert_any_call("REDIS_HOST", "localhost")
getenv.assert_any_call("REDIS_PORT", 6379)
getenv.assert_any_call("REDIS_USERNAME", None)
getenv.assert_any_call("REDIS_PASSWORD", None)
getenv.assert_any_call("LEASE_TTL", 10)
getenv.assert_any_call("HEARTBEAT_TTL", 20)
getenv.assert_any_call("HOSTNAME", "localhost")
@@ -29,18 +35,30 @@ def test___init__(init_logger, getenv):
assert ingestor.kafka_servers == ["localhost:9092", "localhost:35"]
assert ingestor.redis_host == "localhost1"
assert ingestor.redis_port == 63790
assert ingestor.redis_username == "user"
assert ingestor.redis_password == "password"
assert ingestor.lease_ttl == 100
assert ingestor.heartbeat_ttl == 200
assert ingestor.pod_id == "localhost1"
assert ingestor.poll_interval == 50
init_logger.assert_called_once()
notification_handler.assert_called_once_with(
servers=["localhost:9092", "localhost:35"],
logger=ingestor.logger,
project_name="OPC_INGESTOR",
pipeline_name="-",
trigger_name="-",
model_name="-",
model="-"
)
@fixture
@patch("ingestor.ingestor.getenv")
@patch("ingestor.ingestor.Ingestor.init_logger")
def ingestor(init_logger, getenv):
@patch("ingestor.ingestor.NotificationHandler")
def ingestor(notification_handler, init_logger, getenv):
ing = Ingestor()
ing.logger = MagicMock()
@@ -104,7 +122,10 @@ def test_prepare_ingestor(ingestor_manager_mock, ingestor):
ingestor.heartbeat_ttl,
ingestor.pod_id,
ingestor.poll_interval,
ingestor.logger
ingestor.logger,
ingestor.redis_username,
ingestor.redis_password,
ingestor.notification_handler
)
ingestor_manager.declare_active.assert_called_once()
ingestor_manager.get_slot_leases.assert_called_once()
@@ -206,6 +227,8 @@ def test_loop(ingestor_manager_started):
return_value=["ingestor1", "ingestor2"])
ingestor_manager_started.ingestor_manager.get_number_of_slots = MagicMock(
return_value=5)
ingestor_manager_started.ingestor_manager.get_number_of_leases = MagicMock(
return_value=1)
ingestor_manager_started.loop()
@@ -217,7 +240,7 @@ def test_loop(ingestor_manager_started):
ingestor_manager_started.ingestor_manager.get_number_of_slots.return_value)
# Explanation: 5 - 2 = 3, 3 - 1 = 2
ingestor_manager_started.manage_leases.assert_called_once_with(
3, 2)
4, 3, 2)
ingestor_manager_started.ingestor_manager.update_slot_config.assert_called_once()
@@ -240,7 +263,7 @@ def test_loop_no_managed(ingestor_manager_started):
ingestor_manager_started.ingestor_manager.get_number_of_slots.return_value)
# Explanation: 5 - 2 = 3, 3 - 1 = 2
ingestor_manager_started.manage_leases.assert_called_once_with(
3, -1)
ANY, 3, -1)
ingestor_manager_started.ingestor_manager.update_slot_config.assert_called_once()
ingestor_manager_started.logger.info.assert_any_call(
"No slots acquired in this loop")