Skip to content

Commit

Permalink
edit: Split Date and time data into two different columns. Fix last_u…
Browse files Browse the repository at this point in the history
…pdate to get only time information.
  • Loading branch information
niccolozanotti committed Sep 9, 2024
1 parent 8945a56 commit cb16186
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,23 @@ def unregister():
def status():
occupants = get_current_occupants()
last_update = get_last_update_time()

if occupants:
status_message = "open"
else:
status_message = "closed"

return json.dumps({
"status": "open" if occupants else "closed",
"status": status_message,
"occupants": list(occupants),
"count": len(occupants),
"last_update": last_update
"last_update": last_update # Only time will be included
}), 200, {'Content-Type': 'application/json'}


def log_action(name, action):
now = datetime.now(timezone.utc)
log_entry = [now.strftime('%H:%M'), name, action]
log_entry = [now.date().isoformat(), now.strftime('%H:%M'), name, action]
append_log_to_s3(log_entry)


Expand All @@ -64,7 +70,14 @@ def append_log_to_s3(log_entry):
s3response = s3.get_object(Bucket=BUCKET_NAME, Key=LOG_FILE_KEY)
existing_content = s3response['Body'].read().decode('utf-8')
except s3.exceptions.NoSuchKey:
existing_content = 'Date,Time,Name,Action\n'
existing_content = 'Date,Time,Name,Action\n' # Log now has separate Date and Time columns

csv_buffer = StringIO()
csv_buffer.write(existing_content)
csv_writer = csv.writer(csv_buffer)
csv_writer.writerow(log_entry)

s3.put_object(Bucket=BUCKET_NAME, Key=LOG_FILE_KEY, Body=csv_buffer.getvalue())

csv_buffer = StringIO()
csv_buffer.write(existing_content)
Expand All @@ -89,8 +102,8 @@ def get_last_update_time():
last_row = row # Keep track of the last row

if last_row:
date_str, time_str = last_row[0], last_row[1]
return f"{date_str} {time_str}"
time_str = last_row[1] # Now we only store the time in the second column
return time_str

except s3.exceptions.NoSuchKey:
return None
Expand Down

0 comments on commit cb16186

Please sign in to comment.