-
Notifications
You must be signed in to change notification settings - Fork 16
Aggregate SirCAL messages by source and error #500
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 1 commit
Commits
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 |
---|---|---|
|
@@ -7,6 +7,8 @@ | |
|
||
import sys | ||
|
||
from itertools import groupby | ||
|
||
from slack import WebClient | ||
from slack.errors import SlackApiError | ||
|
||
|
@@ -46,7 +48,7 @@ def report_complaints(all_complaints, params): | |
client = WebClient(token=params["slack_token"]) | ||
|
||
for complaints in split_complaints(all_complaints): | ||
blocks = format_complaints(complaints) | ||
blocks = format_complaints_aggregated_by_source(complaints) | ||
print(f"blocks: {len(blocks)}") | ||
try: | ||
client.chat_postMessage( | ||
|
@@ -57,28 +59,64 @@ def report_complaints(all_complaints, params): | |
# You will get a SlackApiError if "ok" is False | ||
assert False, e.response["error"] | ||
|
||
def format_complaints(complaints): | ||
"""Build a formatted Slack message for posting to the API. | ||
|
||
To find good formatting for blocks, try the block kit builder: | ||
https://api.slack.com/tools/block-kit-builder | ||
|
||
""" | ||
|
||
def get_maintainers_block(complaints): | ||
maintainers = set() | ||
for c in complaints: | ||
maintainers.update(c.maintainers) | ||
|
||
blocks = [ | ||
{ | ||
|
||
maintainers_block = { | ||
"type": "section", | ||
"text": { | ||
"type": "mrkdwn", | ||
"text": "Hi, this is Sir Complains-a-Lot. I need to speak to " + | ||
(", ".join("<@{0}>".format(m) for m in maintainers)) + "." | ||
} | ||
} | ||
] | ||
|
||
return maintainers_block | ||
|
||
|
||
def format_complaints_aggregated_by_source(complaints): | ||
"""Build formatted Slack message for posting to the API, aggregating the | ||
complaints by source to reduce the number of blocks.""" | ||
|
||
blocks = [get_maintainers_block(complaints)] | ||
|
||
def aggregated_message_for_source(x): return "{complaint} - (last update: {last_updated})".format( | ||
complaint=x.message, last_updated=x.last_updated.strftime("%Y-%m-%d")) | ||
|
||
for source, v in groupby(complaints, key=lambda x: x.data_source): | ||
for message, complaint_list in groupby(v, key=aggregated_message_for_source): | ||
signal_and_geo_types = "" | ||
for complaint in complaint_list: | ||
signal_and_geo_types += "`{signal}: [{geo_types}]`\n".format( | ||
signal=complaint.signal, geo_types=", ".join(complaint.geo_types)) | ||
blocks.extend([ | ||
{ | ||
"type": "divider" | ||
}, | ||
{ | ||
"type": "section", | ||
"text": { | ||
"type": "mrkdwn", | ||
"text": "*{source_name}* {message_for_group}:\n{signals}".format(source_name=source.upper(), message_for_group=message, signals=signal_and_geo_types) | ||
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. line way too long 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. Better?? |
||
} | ||
} | ||
]) | ||
|
||
return blocks | ||
|
||
|
||
def format_complaints(complaints): | ||
"""Build a formatted Slack message for posting to the API. | ||
|
||
To find good formatting for blocks, try the block kit builder: | ||
https://api.slack.com/tools/block-kit-builder | ||
|
||
""" | ||
|
||
blocks = [get_maintainers_block(complaints)] | ||
|
||
for complaint in complaints: | ||
blocks.append( | ||
|
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.
While we're not yet including linting of this package in the repo CI, probably best not to introduce new linting errors
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.
Eugh sorry -- I had pylint enabled in vscode but for some reason was not catching line length.