Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

95 bugfix/form not hyperlinked #96

Merged
merged 9 commits into from
Oct 26, 2023
1 change: 1 addition & 0 deletions node_monitor/load_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
DB_PASSWORD = os.environ.get('DB_PASSWORD', '')
DB_NAME = os.environ.get('DB_NAME', '')
DB_PORT = os.environ.get('DB_PORT', '')
FEEDBACK_FORM = os.environ.get('FEEDBACK_FORM', '')
louisevelayo marked this conversation as resolved.
Show resolved Hide resolved


## Pre-flight check
Expand Down
7 changes: 5 additions & 2 deletions node_monitor/node_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from node_monitor.node_monitor_helpers.get_compromised_nodes import \
get_compromised_nodes
import node_monitor.node_monitor_helpers.messages as messages
import node_monitor.load_config as c

Seconds = int
Principal = str
Expand Down Expand Up @@ -107,7 +108,8 @@ def broadcast_alerts(self) -> None:
channels = self.node_provider_db.get_channels_as_dict()
for node_provider_id, nodes in self.actionables.items():
preferences = subscribers[node_provider_id]
subject, message = messages.nodes_down_message(nodes, node_labels)
subject, message = messages.nodes_down_message(
nodes, node_labels, c.FEEDBACK_FORM)
# - - - - - - - - - - - - - - - - -
if preferences['notify_email'] == True:
recipients = email_recipients[node_provider_id]
Expand Down Expand Up @@ -142,7 +144,8 @@ def broadcast_status_report(self) -> None:
# - - - - - - - - - - - - - - - - -
for node_provider_id, nodes in reportable_nodes.items():
preferences = subscribers[node_provider_id]
subject, message = messages.nodes_status_message(nodes, node_labels)
subject, message = messages.nodes_status_message(
nodes, node_labels, c.FEEDBACK_FORM)
# - - - - - - - - - - - - - - - - -
if preferences['notify_email'] == True:
recipients = email_recipients[node_provider_id]
Expand Down
11 changes: 7 additions & 4 deletions node_monitor/node_monitor_helpers/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import List, Dict, Tuple

import node_monitor.ic_api as ic_api
import node_monitor.load_config as c

# Forgive me Lord Guido, for I have broken PEP8.
Principal = str
Expand Down Expand Up @@ -53,7 +54,8 @@ def detailnodes(nodes: List[ic_api.Node],


def nodes_down_message(nodes: List[ic_api.Node],
labels: Dict[Principal, str]) -> Tuple[str, str]:
labels: Dict[Principal, str],
feedback_form: str) -> Tuple[str, str]:
"""Returns a message that describes the nodes that are down, in the
format of an email or message for a comprable communication channel.
"""
Expand All @@ -74,13 +76,14 @@ def _make_subject() -> str:
f"\n"
f"Node Monitor by Aviate Labs\n"
f"Report Generated: {datetime.utcnow().isoformat()} UTC\n"
f"Help us serve you better! Provide your feedback!\n")
f"Help us serve you better! Provide your feedback here: {feedback_form}\n")
return (subject, message)



def nodes_status_message(nodes: List[ic_api.Node],
labels: Dict[Principal, str]) -> Tuple[str, str]:
labels: Dict[Principal, str],
feedback_form: str) -> Tuple[str, str]:
"""Returns a message that describes the status of all nodes, in the
format of an email or message for a comprable communication channel.
"""
Expand Down Expand Up @@ -125,5 +128,5 @@ def _render_frac(numerator: int, denominator: int) -> str:
f"Thanks for reviewing today's report. We'll be back tomorrow!\n"
f"Node Monitor by Aviate Labs.\n"
f"Report generated: {datetime.utcnow().isoformat()} UTC\n"
f"Help us serve you better! Provide your feedback!\n")
f"Help us serve you better! Provide your feedback here: {feedback_form} \n")
louisevelayo marked this conversation as resolved.
Show resolved Hide resolved
return (subject, message)
7 changes: 5 additions & 2 deletions tests/test_bot_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def test_send_emails_network():
subnet_id = 'fake_subnet_id',
)
fakelabel = {'fake_node_id': 'fake_label'}
fakeurl = 'https://forms.gle/thisisfake'

## Init the authenticated email bot instance
email_bot = EmailBot(c.EMAIL_USERNAME, c.EMAIL_PASSWORD)
Expand All @@ -63,11 +64,13 @@ def test_send_emails_network():
## Send out nodes_down_message and nodes_status_message. We test both.
## We append time to the subject to act as an identifier for the test.
recipients = ['[email protected]']
subject1, message1 = messages.nodes_down_message([fakenode], fakelabel)
subject1, message1 = messages.nodes_down_message(
[fakenode], fakelabel, fakeurl)
subject1 = str(f'{time.time()} - {subject1}')
email_bot.send_emails(recipients, subject1, message1)

subject2, message2 = messages.nodes_status_message([fakenode], fakelabel)
subject2, message2 = messages.nodes_status_message(
[fakenode], fakelabel, fakeurl)
subject2 = str(f'{time.time()} - {subject2}')
email_bot.send_emails(recipients, subject2, message2)

Expand Down
34 changes: 29 additions & 5 deletions tests/test_bot_slack.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import pytest
from unittest.mock import patch

import node_monitor.load_config as c
from node_monitor.bot_slack import SlackBot
import node_monitor.load_config as c
import node_monitor.node_monitor_helpers.messages as messages
import node_monitor.ic_api as ic_api


@patch("slack_sdk.WebClient")
Expand All @@ -24,13 +26,35 @@ def test_send_message(mock_web_client):
def test_send_message_slack():
"""Send a real test message to a Slack workspace"""
slack_bot = SlackBot(c.TOKEN_SLACK)

## Create a fake node model
fakenode = ic_api.Node(
dc_id = 'fake_dc_id',
dc_name = 'fake_dc_name',
node_id = 'fake_node_id',
node_operator_id = 'fake_node_operator_id',
node_provider_id = 'fake_node_provider_id',
node_provider_name = 'fake_node_provider_name',
owner = 'fake_owner',
region = 'fake_region',
status = 'DOWN',
subnet_id = 'fake_subnet_id',
)
fakelabel = {'fake_node_id': 'fake_label'}
fakeurl = 'https://forms.gle/thisisfake'

slack_channel_name = "node-monitor"
message = "🔬 Hello from test_send_message_slack()"
subject1, message1 = messages.nodes_down_message(
[fakenode], fakelabel, fakeurl)
subject2, message2 = messages.nodes_status_message(
[fakenode], fakelabel, fakeurl)

# SlackBot.send_message() returns an error without raising an exception
# to prevent NodeMonitor from crashing if the message fails to send.
# Instead, we raise it here.
err = slack_bot.send_message(slack_channel_name, message)
if err is not None:
raise err
err1 = slack_bot.send_message(slack_channel_name, message1)
err2 = slack_bot.send_message(slack_channel_name, message2)
if err1 is not None:
raise err1
if err2 is not None:
raise err2
34 changes: 30 additions & 4 deletions tests/test_bot_telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from unittest.mock import patch

import node_monitor.load_config as c
import node_monitor.node_monitor_helpers.messages as messages
import node_monitor.ic_api as ic_api
from node_monitor.bot_telegram import TelegramBot

@patch("requests.get")
Expand All @@ -25,8 +27,32 @@ def test_send_message(mock_get):
def test_send_live_message():
telegram_bot = TelegramBot(c.TOKEN_TELEGRAM)
chat_id = "-1001925583150"
message = "Test message"

err = telegram_bot.send_message(chat_id, message)
if err is not None:
raise err
## Create a fake node model
fakenode = ic_api.Node(
dc_id = 'fake_dc_id',
dc_name = 'fake_dc_name',
node_id = 'fake_node_id',
node_operator_id = 'fake_node_operator_id',
node_provider_id = 'fake_node_provider_id',
node_provider_name = 'fake_node_provider_name',
owner = 'fake_owner',
region = 'fake_region',
status = 'DOWN',
subnet_id = 'fake_subnet_id',
)
fakelabel = {'fake_node_id': 'fake_label'}
fakeurl = 'https://forms.gle/thisisfake'

subject1, message1 = messages.nodes_down_message(
[fakenode], fakelabel, fakeurl)
subject2, message2 = messages.nodes_status_message(
[fakenode], fakelabel, fakeurl)

err1 = telegram_bot.send_message(chat_id, message1)
err2 = telegram_bot.send_message(chat_id, message2)
if err1 is not None:
raise err1
if err2 is not None:
raise err2

Loading