From 3893cf151b36b566498fa8a289c0382e22cba69b Mon Sep 17 00:00:00 2001 From: Joseph H Kennedy Date: Mon, 8 Apr 2024 10:42:47 -0800 Subject: [PATCH 1/7] Specify queue name and add emoji --- status-messages/src/status_messages.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/status-messages/src/status_messages.py b/status-messages/src/status_messages.py index 40aba677..84b34165 100644 --- a/status-messages/src/status_messages.py +++ b/status-messages/src/status_messages.py @@ -2,6 +2,7 @@ import os from datetime import datetime, timezone +from pathlib import Path import boto3 from mattermostdriver import Driver @@ -42,8 +43,9 @@ def lambda_handler(event: dict, context: dict) -> None: channel_info = mattermost.channels.get_channel_by_name_and_team_name('asf', CHANNEL) dead_letter_queue_count = get_queue_count() + alert_emoji = ':alert:' if dead_letter_queue_count != '0' else ':large_green_circle:' mattermost_message = ( - f'Dead Letter Queue Count for ITS_LIVE has ' + f'{alert_emoji} Dead Letter Queue {Path(QUEUE_URL).name} Count for ITS_LIVE has ' f'{dead_letter_queue_count} entries on {datetime.now(tz=timezone.utc).isoformat()}' ) response = mattermost.posts.create_post( From 0fc5a3efd0b5418189e10e6f457bc0354f4f3c48 Mon Sep 17 00:00:00 2001 From: Joseph H Kennedy Date: Mon, 8 Apr 2024 10:50:29 -0800 Subject: [PATCH 2/7] differentiate test messages from prod --- status-messages/src/status_messages.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/status-messages/src/status_messages.py b/status-messages/src/status_messages.py index 84b34165..ff0c28f8 100644 --- a/status-messages/src/status_messages.py +++ b/status-messages/src/status_messages.py @@ -42,10 +42,16 @@ def lambda_handler(event: dict, context: dict) -> None: print(response) channel_info = mattermost.channels.get_channel_by_name_and_team_name('asf', CHANNEL) - dead_letter_queue_count = get_queue_count() - alert_emoji = ':alert:' if dead_letter_queue_count != '0' else ':large_green_circle:' + dead_letter_queue_count = int(get_queue_count()) + + queue_name = Path(QUEUE_URL).name + if 'test' in queue_name: + status_emoji = ':heavy_multiplication_x:' if dead_letter_queue_count != 0 else ':heavy_check_mark:' + else: + status_emoji = ':alert:' if dead_letter_queue_count != 0 else ':large_green_circle:' + mattermost_message = ( - f'{alert_emoji} Dead Letter Queue {Path(QUEUE_URL).name} Count for ITS_LIVE has ' + f'{status_emoji} Dead Letter Queue {queue_name} Count for ITS_LIVE has ' f'{dead_letter_queue_count} entries on {datetime.now(tz=timezone.utc).isoformat()}' ) response = mattermost.posts.create_post( From 9d43dde8f97f4bc428af9afc7e666841090323ce Mon Sep 17 00:00:00 2001 From: Joseph H Kennedy Date: Mon, 8 Apr 2024 10:59:01 -0800 Subject: [PATCH 3/7] Futz with message --- status-messages/src/status_messages.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/status-messages/src/status_messages.py b/status-messages/src/status_messages.py index ff0c28f8..37fe3dbe 100644 --- a/status-messages/src/status_messages.py +++ b/status-messages/src/status_messages.py @@ -51,7 +51,7 @@ def lambda_handler(event: dict, context: dict) -> None: status_emoji = ':alert:' if dead_letter_queue_count != 0 else ':large_green_circle:' mattermost_message = ( - f'{status_emoji} Dead Letter Queue {queue_name} Count for ITS_LIVE has ' + f'{status_emoji} Dead Letter Queue Count for `{queue_name}` has ' f'{dead_letter_queue_count} entries on {datetime.now(tz=timezone.utc).isoformat()}' ) response = mattermost.posts.create_post( From 9c8624c2067e362111dd7914ba8a90f4bbb497ba Mon Sep 17 00:00:00 2001 From: Joseph H Kennedy Date: Mon, 8 Apr 2024 12:31:09 -0800 Subject: [PATCH 4/7] bots can't look up channel ids --- status-messages/src/status_messages.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/status-messages/src/status_messages.py b/status-messages/src/status_messages.py index 37fe3dbe..55c17792 100644 --- a/status-messages/src/status_messages.py +++ b/status-messages/src/status_messages.py @@ -8,10 +8,12 @@ from mattermostdriver import Driver -CHANNEL = 'measures-its_live' QUEUE_URL = os.environ['QUEUE_URL'] MATTERMOST_PAT = os.environ['MATTERMOST_PAT'] +# You can find the ID for a channel by looking in the channel info +MATTERMOST_CHANNEL_ID = 'mmffdcqsafdg8xyr747scyuqnw' # ~measures-its_live + def get_queue_count() -> str: """Retrieve the message count of the Dead Letter Queue. @@ -41,7 +43,6 @@ def lambda_handler(event: dict, context: dict) -> None: response = mattermost.login() print(response) - channel_info = mattermost.channels.get_channel_by_name_and_team_name('asf', CHANNEL) dead_letter_queue_count = int(get_queue_count()) queue_name = Path(QUEUE_URL).name @@ -54,9 +55,10 @@ def lambda_handler(event: dict, context: dict) -> None: f'{status_emoji} Dead Letter Queue Count for `{queue_name}` has ' f'{dead_letter_queue_count} entries on {datetime.now(tz=timezone.utc).isoformat()}' ) + response = mattermost.posts.create_post( options={ - 'channel_id': channel_info['id'], + 'channel_id': MATTERMOST_CHANNEL_ID, 'message': mattermost_message, } ) From 298bc9c1d42ee21a7a537fb6290cc725d3b20c7f Mon Sep 17 00:00:00 2001 From: Joseph H Kennedy Date: Mon, 8 Apr 2024 12:46:10 -0800 Subject: [PATCH 5/7] setup logging --- cloudformation.yml | 1 + status-messages/cloudformation.yml | 8 +++++++ status-messages/src/status_messages.py | 29 ++++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/cloudformation.yml b/cloudformation.yml index 9ad726c7..76189e6d 100644 --- a/cloudformation.yml +++ b/cloudformation.yml @@ -146,4 +146,5 @@ Resources: QueueURL: !Ref DeadLetterQueue QueueName: !GetAtt DeadLetterQueue.QueueName MattermostPAT: !Ref MattermostPAT + LambdaLoggingLevel: !Ref LambdaLoggingLevel TemplateURL: status-messages/cloudformation.yml diff --git a/status-messages/cloudformation.yml b/status-messages/cloudformation.yml index cff3e83b..bcbbe9b9 100644 --- a/status-messages/cloudformation.yml +++ b/status-messages/cloudformation.yml @@ -12,6 +12,13 @@ Parameters: Type: String NoEcho: true + LambdaLoggingLevel: + Type: String + Default: INFO + AllowedValues: + - INFO + - DEBUG + Resources: LogGroup: Type: AWS::Logs::LogGroup @@ -53,6 +60,7 @@ Resources: Variables: MATTERMOST_PAT: !Ref MattermostPAT QUEUE_URL: !Ref QueueURL + LOGGING_LEVEL: !Ref LambdaLoggingLevel Code: src/ Handler: status_messages.lambda_handler Role: !GetAtt Role.Arn diff --git a/status-messages/src/status_messages.py b/status-messages/src/status_messages.py index 55c17792..1f0e62fe 100644 --- a/status-messages/src/status_messages.py +++ b/status-messages/src/status_messages.py @@ -1,6 +1,9 @@ """Lambda function to trigger Mattermost updates for Dead Letter Queue.""" +import argparse +import logging import os +import sys from datetime import datetime, timezone from pathlib import Path @@ -14,6 +17,8 @@ # You can find the ID for a channel by looking in the channel info MATTERMOST_CHANNEL_ID = 'mmffdcqsafdg8xyr747scyuqnw' # ~measures-its_live +log = logging.getLogger(__name__) +log.setLevel(os.environ.get('LOGGING_LEVEL', 'INFO')) def get_queue_count() -> str: """Retrieve the message count of the Dead Letter Queue. @@ -41,7 +46,7 @@ def lambda_handler(event: dict, context: dict) -> None: """ mattermost = Driver({'url': 'chat.asf.alaska.edu', 'token': MATTERMOST_PAT, 'scheme': 'https', 'port': 443}) response = mattermost.login() - print(response) + log.debug(response) dead_letter_queue_count = int(get_queue_count()) @@ -56,10 +61,30 @@ def lambda_handler(event: dict, context: dict) -> None: f'{dead_letter_queue_count} entries on {datetime.now(tz=timezone.utc).isoformat()}' ) + log.info(f'Posting: "{mattermost_message}" to {MATTERMOST_CHANNEL_ID}') response = mattermost.posts.create_post( options={ 'channel_id': MATTERMOST_CHANNEL_ID, 'message': mattermost_message, } ) - print(response) + log.debug(response) + + +def main() -> None: + """Command Line wrapper around `lambda_handler`.""" + parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.ArgumentDefaultsHelpFormatter) + parser.add_argument('-v', '--verbose', action='store_true', help='Turn on verbose logging') + args = parser.parse_args() + + logging.basicConfig(stream=sys.stdout, format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO) + + if args.verbose: + log.setLevel(logging.DEBUG) + + log.debug(' '.join(sys.argv)) + lambda_handler({}, {}) + + +if __name__ == '__main__': + main() From cbb53a7d6773237c1e05be49f57242ea68e971b3 Mon Sep 17 00:00:00 2001 From: Joseph H Kennedy Date: Mon, 8 Apr 2024 12:50:57 -0800 Subject: [PATCH 6/7] linting an logging cleanup --- landsat/src/main.py | 9 +++++---- status-messages/src/status_messages.py | 1 + 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/landsat/src/main.py b/landsat/src/main.py index a774d548..d0b077bd 100644 --- a/landsat/src/main.py +++ b/landsat/src/main.py @@ -31,7 +31,7 @@ password=EARTHDATA_PASSWORD, ) -log = logging.getLogger() +log = logging.getLogger(__name__) log.setLevel(os.environ.get('LOGGING_LEVEL', 'INFO')) @@ -253,10 +253,11 @@ def main() -> None: parser.add_argument('-v', '--verbose', action='store_true', help='Turn on verbose logging') args = parser.parse_args() - level = logging.DEBUG if args.verbose else logging.INFO - logging.basicConfig(stream=sys.stdout, format='%(asctime)s - %(levelname)s - %(message)s', level=level) - log.debug(' '.join(sys.argv)) + logging.basicConfig(stream=sys.stdout, format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO) + if args.verbose: + log.setLevel(logging.DEBUG) + log.debug(' '.join(sys.argv)) _ = process_scene(args.reference, timedelta(days=args.max_pair_separation), args.max_cloud_cover, args.submit) diff --git a/status-messages/src/status_messages.py b/status-messages/src/status_messages.py index 1f0e62fe..72430ede 100644 --- a/status-messages/src/status_messages.py +++ b/status-messages/src/status_messages.py @@ -20,6 +20,7 @@ log = logging.getLogger(__name__) log.setLevel(os.environ.get('LOGGING_LEVEL', 'INFO')) + def get_queue_count() -> str: """Retrieve the message count of the Dead Letter Queue. From f0054ab6081004350be97282e9995a01785e39da Mon Sep 17 00:00:00 2001 From: Joseph H Kennedy Date: Mon, 8 Apr 2024 12:53:39 -0800 Subject: [PATCH 7/7] Update changelog --- CHANGELOG.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 204cf995..aa14f13c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [0.3.0] + +### Added +- A CLI wrapper for `status_messages.py` so that it can more easily be run locally. + +### Fixed +- Status messages can now be posted to mattermost with a bot account. + ## [0.2.0] ### Added