Skip to content

Commit

Permalink
Making changes based on Pat's recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
sylviamclaughlin authored Jan 26, 2024
1 parent 25f54cb commit cead0ae
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 69 deletions.
72 changes: 30 additions & 42 deletions app/commands/incident.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"])
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
24 changes: 12 additions & 12 deletions app/commands/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
]
)
Expand All @@ -187,25 +187,25 @@ 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")

# 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):
Expand Down
1 change: 1 addition & 0 deletions app/integrations/google_drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 15 additions & 15 deletions app/tests/commands/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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():
Expand Down

0 comments on commit cead0ae

Please sign in to comment.