-
-
Notifications
You must be signed in to change notification settings - Fork 63
implemented classify transform #82
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ed36b23
implemented classify transform
ErikBjare 672d8eb
fixed bug and Python 3.5 compatibility
ErikBjare 6ffe444
prefixed computed data fields with '$'
ErikBjare 16bd745
fixed classify behavior for non-category tags
ErikBjare cf90135
fixed test
ErikBjare d4db04d
split classify up into categorize and tag
ErikBjare 776ea7d
removed Python 3.5 support (now Python 3.6+)
ErikBjare ac92b89
fixed behavior for the case where regex pattern is an empty string
ErikBjare 95eed67
fixed Python 3.6 on Appveyor
ErikBjare 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 hidden or 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 |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ language: python | |
| python: | ||
| # - "3.3" | ||
| # - "3.4" | ||
| - "3.5" | ||
| # - "3.5" | ||
| - "3.6" | ||
|
|
||
| services: | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,52 @@ | ||
| from typing import Pattern, List, Iterable, Tuple, Dict, Optional | ||
| from functools import reduce | ||
| import re | ||
|
|
||
| from aw_core import Event | ||
|
|
||
|
|
||
| Tag = str | ||
| Category = List[str] | ||
|
|
||
|
|
||
| class Rule: | ||
| regex: Optional[Pattern] | ||
|
|
||
| def __init__(self, rules: Dict[str, str]): | ||
| if "regex" in rules: | ||
| self.regex = re.compile(rules["regex"]) if rules["regex"] else None | ||
|
|
||
| def match(self, e: Event): | ||
| for val in e.data.values(): | ||
| if isinstance(val, str): | ||
| if self.regex and self.regex.search(val): | ||
| return True | ||
| return False | ||
|
|
||
|
|
||
| def categorize(events: List[Event], classes: List[Tuple[Category, Rule]]): | ||
| return [_categorize_one(e, classes) for e in events] | ||
|
|
||
|
|
||
| def _categorize_one(e: Event, classes: List[Tuple[Category, Rule]]) -> Event: | ||
| e.data["$category"] = _pick_category([_cls for _cls, rule in classes if rule.match(e)]) | ||
| return e | ||
|
|
||
|
|
||
| def tag(events: List[Event], classes: List[Tuple[Tag, Rule]]): | ||
| return [_tag_one(e, classes) for e in events] | ||
|
|
||
|
|
||
| def _tag_one(e: Event, classes: List[Tuple[Tag, Rule]]) -> Event: | ||
| e.data["$tags"] = [_cls for _cls, rule in classes if rule.match(e)] | ||
| return e | ||
|
|
||
|
|
||
| def _pick_category(tags: Iterable[Category]) -> Category: | ||
| return reduce(_pick_deepest_cat, tags, ["Uncategorized"]) | ||
|
|
||
|
|
||
| def _pick_deepest_cat(t1: Category, t2: Category) -> Category: | ||
| # t1 will be the accumulator when used in reduce | ||
| # Always bias against t1, since it could be "Uncategorized" | ||
| return t2 if len(t2) >= len(t1) else t1 | ||
This file contains hidden or 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 hidden or 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,31 +1,23 @@ | ||
| import logging | ||
| from datetime import datetime, timedelta | ||
| from typing import List, Dict, Optional, Any | ||
| from copy import copy, deepcopy | ||
| import operator | ||
| from functools import reduce | ||
| from collections import defaultdict | ||
| from typing import List | ||
|
|
||
| from urllib.parse import urlparse | ||
|
|
||
| from aw_core.models import Event | ||
| from aw_core import TimePeriod | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| from urllib.parse import urlparse | ||
|
|
||
| def split_url_events(events): | ||
| def split_url_events(events: List[Event]) -> List[Event]: | ||
| for event in events: | ||
| if "url" in event.data: | ||
| url = event.data["url"] | ||
| parsed_url = urlparse(url) | ||
| event.data["protocol"] = parsed_url.scheme | ||
| event.data["domain"] = parsed_url.netloc | ||
| if event.data["domain"][:4] == "www.": | ||
| event.data["domain"] = event.data["domain"][4:] | ||
| event.data["path"] = parsed_url.path | ||
| event.data["params"] = parsed_url.params | ||
| event.data["options"] = parsed_url.query | ||
| event.data["identifier"] = parsed_url.fragment | ||
| event.data["$protocol"] = parsed_url.scheme | ||
| event.data["$domain"] = parsed_url.netloc[4:] if parsed_url.netloc[:4] == "www." else parsed_url.netloc | ||
| event.data["$path"] = parsed_url.path | ||
| event.data["$params"] = parsed_url.params | ||
| event.data["$options"] = parsed_url.query | ||
| event.data["$identifier"] = parsed_url.fragment | ||
| # TODO: Parse user, port etc aswell | ||
| return events |
This file contains hidden or 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
Oops, something went wrong.
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.
Spaghetti code, first you check if "regex" is in rules and then again you check if regex is in rules.
Would make more sense to write
Oh, now that I think of it it's even incorrect as well as "regex" is impossible to not be set and it is possible to set rules["regex"] = False which would be silly.
Uh oh!
There was an error while loading. Please reload this page.
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.
It's not actually, the check is for when the regex string is an empty string, which would erroneously match everything. And regex might not be set at all, in the future another rule might be used instead.
It could be replaced with
rules.get("regex", None) or Nonethough, which is cleaner. Edit: Nevermind, actually it can't since it needs to pass throughre.compile.