-
Notifications
You must be signed in to change notification settings - Fork 24
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
Provider implements a include filter to define relevant notifications #91
Merged
chadell
merged 14 commits into
develop
from
enable-filtering-relevant-notifications-per-provider
Oct 1, 2021
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
bc39866
Provider implements a include filter to define relevant notifications
chadell fa72249
Add support for a exclude_filter
chadell 93006a2
Update circuit_maintenance_parser/provider.py
chadell ce9a06f
Add suggestions from code review
chadell ee1a942
Fix current tests
chadell ee94559
update changelog and readme
chadell 077a00b
Add testing and fixing logic thanks to tests
chadell f013d18
Merge branch 'develop' into enable-filtering-relevant-notifications-p…
chadell a657eb0
Simplify Lumen include filter
chadell 674d12b
Improve logging messages for filtering actions
chadell 25ce02b
Improve changelog
chadell b6c1e18
Merge branch 'enable-filtering-relevant-notifications-per-provider' o…
chadell d0d9d66
Add extra logging case when filtering fails
chadell ae6e561
Update circuit_maintenance_parser/provider.py
chadell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
"""Constants used in the library.""" | ||
|
||
EMAIL_HEADER_SUBJECT = "email-header-subject" | ||
EMAIL_HEADER_DATE = "email-header-date" |
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,8 +1,9 @@ | ||
"""Definition of Provider class as the entry point to the library.""" | ||
import logging | ||
import re | ||
import traceback | ||
|
||
from typing import Iterable, List | ||
from typing import Iterable, List, Dict | ||
|
||
from pydantic import BaseModel | ||
|
||
|
@@ -13,6 +14,7 @@ | |
from circuit_maintenance_parser.parser import ICal, EmailDateParser | ||
from circuit_maintenance_parser.errors import ProcessorError, ProviderError | ||
from circuit_maintenance_parser.processor import CombinedProcessor, SimpleProcessor, GenericProcessor | ||
from circuit_maintenance_parser.constants import EMAIL_HEADER_SUBJECT | ||
|
||
from circuit_maintenance_parser.parsers.aquacomms import HtmlParserAquaComms1, SubjectParserAquaComms1 | ||
from circuit_maintenance_parser.parsers.aws import SubjectParserAWS1, TextParserAWS1 | ||
|
@@ -50,6 +52,14 @@ class GenericProvider(BaseModel): | |
that will be used. Default: `[SimpleProcessor(data_parsers=[ICal])]`. | ||
_default_organizer (optional): Defines a default `organizer`, an email address, to be used to create a | ||
`Maintenance` in absence of the information in the original notification. | ||
_include_filter (optional): Dictionary that defines matching string per data type to take a notification into | ||
account. | ||
_exclude_filter (optional): Dictionary that defines matching string per data type to NOT take a notification | ||
into account. | ||
glennmatthews marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Notes: | ||
- If a notification matches both, the `_include_filter` and `_exclude_filter`, the second takes precedence and | ||
chadell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
the notification will be filtered out. | ||
|
||
Examples: | ||
>>> GenericProvider() | ||
|
@@ -59,12 +69,44 @@ class GenericProvider(BaseModel): | |
_processors: List[GenericProcessor] = [SimpleProcessor(data_parsers=[ICal])] | ||
_default_organizer: str = "unknown" | ||
|
||
_include_filter: Dict[str, List[str]] = {} | ||
_exclude_filter: Dict[str, List[str]] = {} | ||
|
||
def include_filter_check(self, data: NotificationData) -> bool: | ||
"""If `_include_filter` is defined, it verifies that the matching criteria is met.""" | ||
if self._include_filter: | ||
return self.filter_check(self._include_filter, data) | ||
return True | ||
|
||
def exclude_filter_check(self, data: NotificationData) -> bool: | ||
"""If `_exclude_filter` is defined, it verifies that the matching criteria is met.""" | ||
if self._exclude_filter: | ||
return self.filter_check(self._exclude_filter, data) | ||
return False | ||
|
||
@staticmethod | ||
def filter_check(filter_dict: Dict, data: NotificationData) -> bool: | ||
"""Generic filter check.""" | ||
for data_part in data.data_parts: | ||
glennmatthews marked this conversation as resolved.
Show resolved
Hide resolved
|
||
filter_data_type = data_part.type | ||
if filter_data_type not in filter_dict: | ||
continue | ||
|
||
data_part_content = data_part.content.decode() | ||
if any(re.search(filter_re, data_part_content) for filter_re in filter_dict[filter_data_type]): | ||
return True | ||
chadell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return False | ||
|
||
def get_maintenances(self, data: NotificationData) -> Iterable[Maintenance]: | ||
"""Main entry method that will use the defined `_processors` in order to extract the `Maintenances` from data.""" | ||
provider_name = self.__class__.__name__ | ||
error_message = "" | ||
related_exceptions = [] | ||
|
||
if self.exclude_filter_check(data) or not self.include_filter_check(data): | ||
return [] | ||
chadell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for processor in self._processors: | ||
try: | ||
return processor.process(data, self.get_extended_data()) | ||
|
@@ -172,6 +214,8 @@ class HGC(GenericProvider): | |
class Lumen(GenericProvider): | ||
"""Lumen provider custom class.""" | ||
|
||
_include_filter = {EMAIL_HEADER_SUBJECT: ["Scheduled Maintenance Window"]} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at more examples that I have locally, I think just "Scheduled Maintenance" would be better here. I see some subject lines like |
||
|
||
_processors: List[GenericProcessor] = [ | ||
CombinedProcessor(data_parsers=[EmailDateParser, HtmlParserLumen1]), | ||
] | ||
|
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Scheduled Maintenance Window |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to flesh this out with an example or two - it's not obvious from just reading this how a valid
_include_filter
would be structured.