Skip to content

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 3 commits into from
Nov 16, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 50 additions & 12 deletions sir_complainsalot/delphi_sir_complainsalot/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import sys

from itertools import groupby

from slack import WebClient
from slack.errors import SlackApiError

Expand Down Expand Up @@ -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(
Expand All @@ -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"))
Copy link
Contributor

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

Copy link
Contributor Author

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.


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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line way too long

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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(
Expand Down