Skip to content

Commit

Permalink
Merge 621ee37 into 232d47d
Browse files Browse the repository at this point in the history
  • Loading branch information
adybbroe committed Sep 8, 2023
2 parents 232d47d + 621ee37 commit 8c1fbfc
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 9 deletions.
25 changes: 18 additions & 7 deletions activefires_pp/fire_notifications.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2021, 2022 Adam Dybbroe
# Copyright (c) 2021 - 2023 Adam Dybbroe

# Author(s):

Expand All @@ -20,8 +20,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Creating and sending notifications for detected forest fires.
"""
"""Creating and sending notifications for detected forest fires."""

import socket
from netrc import netrc
Expand Down Expand Up @@ -56,8 +55,10 @@


class RecipientDataStruct(object):
def __init__(self):
"""A data structure to control the list of configured recipients."""

def __init__(self):
"""Initialize the recipient data structure."""
self.recipients_with_attachment = []
self.recipients_without_attachment = []
self.recipients_all = []
Expand Down Expand Up @@ -140,7 +141,6 @@ def _setup_and_start_communication(self):

def _set_options_from_config(self, config):
"""From the configuration on disk set the option dictionary, holding all metadata for processing."""

for item in config:
self.options[item] = config[item]

Expand Down Expand Up @@ -184,13 +184,26 @@ def run(self):
LOG.debug("Message type not supported: %s", str(msg.type))
continue

if not self._product_name_supported(msg):
continue

output_msg = self.notify_end_users(msg)
if output_msg:
LOG.debug("Sending message: %s", str(output_msg))
self.publisher.send(str(output_msg))
else:
LOG.debug("No message to send")

def _product_name_supported(self, incoming_msg):
"""Check that the product name is supported via the configuration."""
product_name = incoming_msg.data.get('product')
product_list = self.options.get('products')
if product_list and product_name and product_name not in product_list:
LOG.info('Product %s will not generate a notification!', product_name)
return False

return True

def notify_end_users(self, msg):
"""Send notifications to configured end users (mail and text messages)."""
LOG.debug("Start sending notifications to configured end users.")
Expand Down Expand Up @@ -218,7 +231,6 @@ def notify_end_users(self, msg):

def _send_notifications_with_attachments(self, server, recipients, full_message, filename, platform_name):
"""Send notifications with attachments."""

notification = MIMEMultipart()
notification['From'] = self.sender
if platform_name:
Expand Down Expand Up @@ -252,7 +264,6 @@ def _send_notifications_with_attachments(self, server, recipients, full_message,

def _send_notifications_without_attachments(self, server, recipients, sub_messages, platform_name):
"""Send notifications without attachments."""

for submsg in sub_messages:
notification = MIMEMultipart()
notification['From'] = self.sender
Expand Down
58 changes: 56 additions & 2 deletions activefires_pp/tests/test_fire_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from unittest.mock import patch
import yaml
import io
# from posttroll.message import Message
from posttroll.message import Message

from activefires_pp.fire_notifications import EndUserNotifier
from activefires_pp.fire_notifications import EndUserNotifierRegional
Expand All @@ -40,6 +40,10 @@
publish_topic: VIIRS/L2/MSB/National
subscribe_topics: VIIRS/L2/Fires/PP/National
products:
- afimg
- afimg_some_other_geoid
smtp_server: smtp.mydomain.se
domain: mydomain.se
Expand Down Expand Up @@ -102,6 +106,8 @@

NATIONAL_TEST_MESSAGE = """pytroll://VIIRS/L2/Fires/PP/National file safusr.u@lxserv1043.smhi.se 2021-04-19T11:16:49.519087 v1.01 application/json {"start_time": "2021-04-16T12:29:53", "end_time": "2021-04-16T12:31:18", "orbit_number": 1, "platform_name": "NOAA-20", "sensor": "viirs", "data_processing_level": "2", "variant": "DR", "orig_orbit_number": 17666, "uri": "ssh://lxserv1043.smhi.se//san1/polar_out/direct_readout/viirs_active_fires/filtered/AFIMG_j01_d20210416_t122953.geojson", "uid": "AFIMG_j01_d20210416_t122953.geojson", "type": "GEOJSON-filtered", "format": "geojson", "product": "afimg"}""" # noqa

NATIONAL_TEST_MESSAGE2 = """pytroll://VIIRS/L2/Fires/PP/National file safusr.u@lxserv1043.smhi.se 2021-04-19T11:16:49.519087 v1.01 application/json {"start_time": "2021-04-16T12:29:53", "end_time": "2021-04-16T12:31:18", "orbit_number": 1, "platform_name": "NOAA-20", "sensor": "viirs", "data_processing_level": "2", "variant": "DR", "orig_orbit_number": 17666, "uri": "ssh://lxserv1043.smhi.se//san1/polar_out/direct_readout/viirs_active_fires/filtered/AFIMG_j01_d20210416_t122953.geojson", "uid": "AFIMG_j01_d20210416_t122953_somegeoid.geojson", "type": "GEOJSON-filtered", "format": "geojson", "product": "afimg_somegeoid"}""" # noqa


class MyNetrcMock(object):
"""Mocking the handling of secrets via the .netrc file."""
Expand All @@ -118,6 +124,52 @@ def authenticators(self, host):
class TestNotifyEndUsers(unittest.TestCase):
"""Test notifications on National fires."""

@patch('activefires_pp.fire_notifications.netrc')
@patch('activefires_pp.fire_notifications.socket.gethostname')
@patch('activefires_pp.fire_notifications.read_config')
@patch('activefires_pp.fire_notifications.EndUserNotifier._setup_and_start_communication')
def test_check_incoming_message_product_name_ok(self, setup_comm, read_config, gethostname, netrc):
"""Test the incoming message for the 'right' product (name)."""
secrets = MyNetrcMock()
netrc.return_value = secrets
gethostname.return_value = 'default'

myconfigfile = "/my/config/file/path"
natstream = io.StringIO(NAT_CONFIG)

read_config.return_value = yaml.load(natstream, Loader=yaml.UnsafeLoader)

this = EndUserNotifier(myconfigfile)

input_msg = Message.decode(rawstr=NATIONAL_TEST_MESSAGE)

result = this._product_name_supported(input_msg)

assert result is True

@patch('activefires_pp.fire_notifications.netrc')
@patch('activefires_pp.fire_notifications.socket.gethostname')
@patch('activefires_pp.fire_notifications.read_config')
@patch('activefires_pp.fire_notifications.EndUserNotifier._setup_and_start_communication')
def test_check_incoming_message_product_name_not_ok(self, setup_comm, read_config, gethostname, netrc):
"""Test the incoming message for the 'right' product (name)."""
secrets = MyNetrcMock()
netrc.return_value = secrets
gethostname.return_value = 'default'

myconfigfile = "/my/config/file/path"
natstream = io.StringIO(NAT_CONFIG)

read_config.return_value = yaml.load(natstream, Loader=yaml.UnsafeLoader)

this = EndUserNotifier(myconfigfile)

input_msg = Message.decode(rawstr=NATIONAL_TEST_MESSAGE2)

result = this._product_name_supported(input_msg)

assert result is False

@patch('activefires_pp.fire_notifications.netrc')
@patch('activefires_pp.fire_notifications.socket.gethostname')
@patch('activefires_pp.fire_notifications.read_config')
Expand All @@ -137,7 +189,9 @@ def test_get_options_national_filtering(self, setup_comm, read_config, gethostna

expected = {'publish_topic': 'VIIRS/L2/MSB/National',
'subscribe_topics': ['VIIRS/L2/Fires/PP/National'],
'smtp_server': 'smtp.mydomain.se', 'domain': 'mydomain.se', 'sender': 'active-fires@mydomain.se',
'products': ['afimg', 'afimg_some_other_geoid'],
'smtp_server': 'smtp.mydomain.se',
'domain': 'mydomain.se', 'sender': 'active-fires@mydomain.se',
'recipients': ['recipient1@recipients.se', 'recipient2@recipients.se', 'recipient3@recipients.se'],
'recipients_attachment': ['recipient1@recipients.se', 'recipient2@recipients.se'],
'subject': 'My subject', 'max_number_of_fires_in_sms': 3,
Expand Down
4 changes: 4 additions & 0 deletions examples/fire_notifier.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
publish_topic: VIIRS/L2/MSB/National
subscribe_topics: VIIRS/L2/Fires/PP/National

products:
- afimg
- afimg_some_other_geoid

smtp_server: smtp.mydomain.se

domain: mydomain.se
Expand Down

0 comments on commit 8c1fbfc

Please sign in to comment.