Skip to content

Commit

Permalink
feat: post shipping report to slack
Browse files Browse the repository at this point in the history
  • Loading branch information
steveoh authored and stdavis committed Feb 10, 2020
1 parent ddeb3cd commit f947f96
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/forklift/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from .config import get_config_prop
from .messaging import send_email, send_to_slack
from .models import Pallet
from .slack import lift_report_to_blocks
from .slack import lift_report_to_blocks, ship_report_to_blocks

log = logging.getLogger('forklift')
lift_template = join(abspath(dirname(__file__)), 'templates', 'lift.html')
Expand Down Expand Up @@ -366,6 +366,7 @@ def ship_data(pallet_arg=None, by_service=False):
}

_send_report_email(ship_template, status, 'Shipping')
_send_report_to_slack(status, 'Shipping')

report = _generate_ship_console_report(status)

Expand Down Expand Up @@ -671,6 +672,8 @@ def _send_report_to_slack(status, operation):

if operation == 'Lifting':
messages = lift_report_to_blocks(status)
else:
messages = ship_report_to_blocks(status)

send_to_slack(url, messages)

Expand Down
91 changes: 90 additions & 1 deletion src/forklift/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def split(arr, size):


def lift_report_to_blocks(report):
'''turns the forklift lift report object into blocks
'''turns the forklift lift report object into slack blocks
'''
message = Message()

Expand Down Expand Up @@ -110,6 +110,95 @@ def lift_report_to_blocks(report):
return message.get_messages()


def ship_report_to_blocks(report):
'''turns the forklift ship report object into slack blocks
'''
message = Message()

message.add(SectionBlock(f':tractor: :rocket: *Forklift Ship Report* :rocket: :tractor:'))

percent = report['num_success_pallets'] / report['total_pallets'] * 100
if percent == 100:
percent = ':100:'
else:
percent = f'{str(math.floor(percent))}% success'

message.add(
ContextBlock([
f'*{datetime.now().strftime("%B %d, %Y")}*',
report['hostname'],
f'*{report["num_success_pallets"]}* of *{report["total_pallets"]}* pallets ran successfully',
f'{percent}',
f'total time: *{report["total_time"]}*',
])
)

message.add(DividerBlock())

if report['server_reports'] and len(report['server_reports']) > 0:
for server_status in report['server_reports']:

success = ':fire:'
if server_status['success']:
success = ':white_check_mark:'

message.add(SectionBlock(f'{success} *{server_status["name"]}*'))

if server_status['has_service_issues']:
items = split(server_status['problem_services'], MAX_CONTEXT_ELEMENTS)

for item in items:
message.add(ContextBlock(item))
else:
message.add(ContextBlock([':rocket: All services started']))

if len(server_status['message']) > 0:
message.add(ContextBlock([server_status['message']]))

message.add(SectionBlock('Datasets shipped'))

shipped_data = ['No data updated']
if len(server_status['successful_copies']) > 0:
shipped_data = server_status['successful_copies']

items = split(shipped_data, MAX_CONTEXT_ELEMENTS)

for item in items:
message.add(ContextBlock(item))

message.add(DividerBlock())

message.add(SectionBlock('*Pallets Report*'))

for pallet in report['pallets']:
success = ':fire:'

if pallet['success']:
success = ':heavy_check_mark:'

message.add(SectionBlock(f'{success} *{pallet["name"].split(":")[2]}*'))

post_copy_processed = shipped = ':red_circle:'
if pallet['post_copy_processed']:
post_copy_processed = ':white_check_mark:'
if pallet['shipped']:
shipped = ':white_check_mark:'

elements = [pallet['total_processing_time']]

if pallet['message']:
elements.append(pallet['message'])

elements.append(f'Post copy processed: {post_copy_processed}')
elements.append(f'Shipped: {shipped}')

items = split(elements, MAX_CONTEXT_ELEMENTS)
for item in items:
message.add(ContextBlock(item))

return message.get_messages()


class BlockType(Enum):
'''available block type enums
'''
Expand Down

0 comments on commit f947f96

Please sign in to comment.