-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from sam-atkins/feature/add-email-notifications
Feature/add email notifications
- Loading branch information
Showing
12 changed files
with
86 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,44 @@ | ||
""" | ||
Notification adapters. More a placeholder than anything else, just logs right now but | ||
could be extended to send emails, Slack messages, etc. | ||
Notification adapters. Small examples for logging and email but | ||
could be extended to send different emails or Slack messages, etc. | ||
""" | ||
import abc | ||
import logging.config | ||
import smtplib | ||
|
||
from slowking.domain import events | ||
from slowking.config import settings | ||
from slowking.domain import model | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class AbstractNotifications(abc.ABC): | ||
@abc.abstractmethod | ||
def send(self, event: events.Event, message: str): | ||
def send(self, benchmark: model.Benchmark, message: str): | ||
raise NotImplementedError | ||
|
||
|
||
class LogNotifications(AbstractNotifications): | ||
def send(self, event: events.Event, message: str): | ||
logger.info(f"Sending notification for event {event} with message: {message}") | ||
def send(self, benchmark: model.Benchmark, message: str): | ||
logger.info(f"Sending notification for benchmark {benchmark.name}") | ||
|
||
|
||
class EmailNotifications(AbstractNotifications): | ||
def __init__(self, smtp_host=settings.EMAIL_HOST, port=settings.EMAIL_PORT): | ||
self.server = smtplib.SMTP(smtp_host, port=port) | ||
self.server.noop() | ||
|
||
def send(self, benchmark: model.Benchmark, message: str) -> None: | ||
subject = f"Benchmark Report: {benchmark.name}" | ||
body = f"Report generated: {message}" | ||
msg = f"Subject: {subject}\n{body}" | ||
|
||
# Hard coded as an example, could be taken from the benchmark aggregate | ||
# the CreateBenchmark command could have an email field | ||
destination = ["hello@example.com"] | ||
|
||
self.server.sendmail( | ||
from_addr="benchmarks@slowking.com", | ||
to_addrs=destination, | ||
msg=msg, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters