From cead0aedd4045c0a65a755be9474b65ab390ccf6 Mon Sep 17 00:00:00 2001 From: Sylvia McLaughlin <85905333+sylviamclaughlin@users.noreply.github.com> Date: Fri, 26 Jan 2024 23:26:13 +0000 Subject: [PATCH] Making changes based on Pat's recommendations --- app/commands/incident.py | 72 +++++++++++++------------------- app/commands/utils.py | 24 +++++------ app/integrations/google_drive.py | 1 + app/tests/commands/test_utils.py | 30 ++++++------- 4 files changed, 58 insertions(+), 69 deletions(-) diff --git a/app/commands/incident.py b/app/commands/incident.py index 016ecb32..0babb9d7 100644 --- a/app/commands/incident.py +++ b/app/commands/incident.py @@ -386,27 +386,8 @@ def handle_reaction_added(client, ack, body, logger): if channel_name.startswith("incident-"): # get the message from the conversation try: - result = client.conversations_history( - channel=channel_id, - limit=1, - inclusive=True, - include_all_metadata=True, - oldest=body["event"]["item"]["ts"], - ) - # get the actual message from the result. This is the text of the message - messages = result["messages"] - - # if there are no messages, then the message is part of a thread, so obtain the message from the thread. - if messages.__len__() == 0: - # get the latest message from the thread - result = client.conversations_replies( - channel=channel_id, - ts=body["event"]["item"]["ts"], - inclusive=True, - include_all_metadata=True, - ) - # get the message - messages = result["messages"] + # get the messages from the conversation and incident channel + messages = return_messages(client, body, channel_id) # get the incident report document id from the incident channel # get and update the incident document @@ -422,7 +403,7 @@ def handle_reaction_added(client, ack, body, logger): logger.error("No incident document found for this channel.") for message in messages: - # convert the time which is now in epoch time to standard EST Time + # convert the time which is now in epoch time to standard ET Time message_date_time = convert_epoch_to_datetime_est(message["ts"]) # get the user name from the message user = client.users_profile_get(user=message["user"]) @@ -467,32 +448,15 @@ def handle_reaction_removed(client, ack, body, logger): if channel_name.startswith("incident-"): try: - # Fetch the message that had the reaction removed - result = client.conversations_history( - channel=channel_id, - limit=1, - inclusive=True, - oldest=body["event"]["item"]["ts"], - ) - # get the messages - messages = result["messages"] - # if the lenght is 0, then the message is part of a thread, so get the message from the thread - if messages.__len__() == 0: - # get thread messages - result = client.conversations_replies( - channel=channel_id, - ts=body["event"]["item"]["ts"], - inclusive=True, - include_all_metadata=True, - ) - messages = result["messages"] + messages = return_messages(client, body, channel_id) + if not messages: logger.warning("No messages found") return # get the message we want to delete message = messages[0] - # convert the epoch time to standard EST day/time + # convert the epoch time to standard ET day/time message_date_time = convert_epoch_to_datetime_est(message["ts"]) # get the user of the person that send the message @@ -541,3 +505,27 @@ def handle_reaction_removed(client, ack, body, logger): return except Exception as e: logger.error(e) + +# Function to return the messages from the conversation +def return_messages(client, body, channel_id): + # Fetch the message that had the reaction removed + result = client.conversations_history( + channel=channel_id, + limit=1, + inclusive=True, + oldest=body["event"]["item"]["ts"], + ) + # get the messages + messages = result["messages"] + # if the lenght is 0, then the message is part of a thread, so get the message from the thread + if messages.__len__() == 0: + # get thread messages + result = client.conversations_replies( + channel=channel_id, + ts=body["event"]["item"]["ts"], + inclusive=True, + include_all_metadata=True, + ) + messages = result["messages"] + + return messages \ No newline at end of file diff --git a/app/commands/utils.py b/app/commands/utils.py index bd09d7fb..d8af447d 100644 --- a/app/commands/utils.py +++ b/app/commands/utils.py @@ -138,8 +138,8 @@ def rearrange_by_datetime_ascending(text): # Iterate over each line for line in lines: - # Check if the line starts with a datetime format including 'EST' - if re.match(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} EST", line): + # Check if the line starts with a datetime format including 'ET' + if re.match(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} ET", line): if current_entry: # Combine the lines in current_entry and add to entries entries.append("\n".join(current_entry)) @@ -163,21 +163,21 @@ def rearrange_by_datetime_ascending(text): dated_entries = [] for entry in entries: match = re.match( - r"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} EST):?[\s,]*(.*)", entry, re.DOTALL + r"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} ET):?[\s,]*(.*)", entry, re.DOTALL ) if match: date_str, msg = match.groups() - # Parse the datetime string (ignoring 'EST' for parsing) - dt = datetime.strptime(date_str[:-4].strip(), "%Y-%m-%d %H:%M:%S") + # Parse the datetime string (ignoring 'ET' for parsing) + dt = datetime.strptime(date_str[:-3].strip(), "%Y-%m-%d %H:%M:%S") dated_entries.append((dt, msg)) # Sort the entries by datetime in ascending order sorted_entries = sorted(dated_entries, key=lambda x: x[0], reverse=False) - # Reformat the entries back into strings, including 'EST' + # Reformat the entries back into strings, including 'ET' sorted_text = "\n".join( [ - f"{entry[0].strftime('%Y-%m-%d %H:%M:%S')} EST {entry[1]}" + f"{entry[0].strftime('%Y-%m-%d %H:%M:%S')} ET {entry[1]}" for entry in sorted_entries ] ) @@ -187,13 +187,13 @@ def rearrange_by_datetime_ascending(text): def convert_epoch_to_datetime_est(epoch_time): """ - Convert an epoch time to a standard date/time format in Eastern Standard Time (EST). + Convert an epoch time to a standard date/time format in Eastern Standard Time (ET). Args: epoch_time (float): The epoch time. Returns: - str: The corresponding date and time in the format YYYY-MM-DD HH:MM:SS EST. + str: The corresponding date and time in the format YYYY-MM-DD HH:MM:SS ET. """ # Define the Eastern Standard Timezone est = pytz.timezone("US/Eastern") @@ -201,11 +201,11 @@ def convert_epoch_to_datetime_est(epoch_time): # Convert epoch time to a datetime object in UTC utc_datetime = datetime.utcfromtimestamp(float(epoch_time)) - # Convert UTC datetime object to EST + # Convert UTC datetime object to ET est_datetime = utc_datetime.replace(tzinfo=pytz.utc).astimezone(est) - # Format the datetime object to a string in the desired format with 'EST' at the end - return est_datetime.strftime("%Y-%m-%d %H:%M:%S") + " EST" + # Format the datetime object to a string in the desired format with 'ET' at the end + return est_datetime.strftime("%Y-%m-%d %H:%M:%S") + " ET" def extract_google_doc_id(url): diff --git a/app/integrations/google_drive.py b/app/integrations/google_drive.py index 53eb5089..11982c9e 100644 --- a/app/integrations/google_drive.py +++ b/app/integrations/google_drive.py @@ -298,6 +298,7 @@ def get_timeline_section(document_id): service = get_google_service("docs", "v1") document = service.documents().get(documentId=document_id).execute() content = document.get("body").get("content") + print("In google, content is", content) timeline_content = "" record = False diff --git a/app/tests/commands/test_utils.py b/app/tests/commands/test_utils.py index 68bbef3d..eedbed51 100644 --- a/app/tests/commands/test_utils.py +++ b/app/tests/commands/test_utils.py @@ -185,39 +185,39 @@ def test_get_user_locale_without_locale(): def test_basic_functionality_rearrange_by_datetime_ascending(): input_text = ( - "2024-01-01 10:00:00 EST Message A\n" "2024-01-02 11:00:00 EST Message B" + "2024-01-01 10:00:00 ET Message A\n" "2024-01-02 11:00:00 ET Message B" ) expected_output = ( - "2024-01-01 10:00:00 EST Message A\n" "2024-01-02 11:00:00 EST Message B" + "2024-01-01 10:00:00 ET Message A\n" "2024-01-02 11:00:00 ET Message B" ) assert utils.rearrange_by_datetime_ascending(input_text) == expected_output def test_multiline_entries_rearrange_by_datetime_ascending(): input_text = ( - "2024-01-01 10:00:00 EST Message A\nContinued\n" - "2024-01-02 11:00:00 EST Message B" + "2024-01-01 10:00:00 ET Message A\nContinued\n" + "2024-01-02 11:00:00 ET Message B" ) expected_output = ( - "2024-01-01 10:00:00 EST Message A\nContinued\n" - "2024-01-02 11:00:00 EST Message B" + "2024-01-01 10:00:00 ET Message A\nContinued\n" + "2024-01-02 11:00:00 ET Message B" ) assert utils.rearrange_by_datetime_ascending(input_text) == expected_output def test_entries_out_of_order_rearrange_by_datetime_ascending(): input_text = ( - "2024-01-02 11:00:00 EST Message B\n" "2024-01-01 10:00:00 EST Message A" + "2024-01-02 11:00:00 ET Message B\n" "2024-01-01 10:00:00 ET Message A" ) expected_output = ( - "2024-01-01 10:00:00 EST Message A\n" "2024-01-02 11:00:00 EST Message B" + "2024-01-01 10:00:00 ET Message A\n" "2024-01-02 11:00:00 ET Message B" ) assert utils.rearrange_by_datetime_ascending(input_text) == expected_output def test_invalid_entries_rearrange_by_datetime_ascending(): - input_text = "Invalid Entry\n" "2024-01-01 10:00:00 EST Message A" - expected_output = "2024-01-01 10:00:00 EST Message A" + input_text = "Invalid Entry\n" "2024-01-01 10:00:00 ET Message A" + expected_output = "2024-01-01 10:00:00 ET Message A" assert utils.rearrange_by_datetime_ascending(input_text) == expected_output @@ -232,27 +232,27 @@ def test_no_datetime_entries_rearrange_by_datetime_ascending(): def test_convert_epoch_to_datetime_est_known_epoch_time(): # Example: 0 epoch time corresponds to 1969-12-31 19:00:00 EST - assert utils.convert_epoch_to_datetime_est(0) == "1969-12-31 19:00:00 EST" + assert utils.convert_epoch_to_datetime_est(0) == "1969-12-31 19:00:00 ET" def test_convert_epoch_to_datetime_est_daylight_saving_time_change(): # Test with an epoch time known to fall in DST transition # For example, 1583652000 corresponds to 2020-03-08 03:20:00 EST - assert utils.convert_epoch_to_datetime_est(1583652000) == "2020-03-08 03:20:00 EST" + assert utils.convert_epoch_to_datetime_est(1583652000) == "2020-03-08 03:20:00 ET" def test_convert_epoch_to_datetime_est_current_epoch_time(): time = MagicMock() time.return_value = 1609459200 current_est = utils.convert_epoch_to_datetime_est(time) - assert current_est == "1969-12-31 19:00:01 EST" + assert current_est == "1969-12-31 19:00:01 ET" def test_convert_epoch_to_datetime_est_edge_cases(): # Test with the epoch time at 0 - assert utils.convert_epoch_to_datetime_est(0) == "1969-12-31 19:00:00 EST" + assert utils.convert_epoch_to_datetime_est(0) == "1969-12-31 19:00:00 ET" # Test with a very large epoch time, for example - assert utils.convert_epoch_to_datetime_est(32503680000) == "2999-12-31 19:00:00 EST" + assert utils.convert_epoch_to_datetime_est(32503680000) == "2999-12-31 19:00:00 ET" def test_extract_googe_doc_id_valid_google_docs_url():