Skip to content

Commit

Permalink
Change approach to event filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
PhongT16 committed Jun 18, 2024
1 parent 38e57f6 commit 9ee5ba1
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 40 deletions.
106 changes: 72 additions & 34 deletions IndividualCalendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,41 +176,77 @@ def get_individual_calendars_using_batch(start_date, end_date, group_members, ac

return list_of_responses

def filter_events(events, events_to_add):
"""
Modifies parameters events and events_to_add if there are overlaps between them.
For example:
OUT AM and OUT PM on same day -> OUT (all day).
OUT AM and OUT (all day) -> OUT (all day)
OUT PM and OUT (all day) -> OUT (all day)
# def filter_events(events, events_to_add):
# """
# Modifies parameters events and events_to_add if there are overlaps between them.
# For example:
# OUT AM and OUT PM on same day -> OUT (all day).
# OUT AM and OUT (all day) -> OUT (all day)
# OUT PM and OUT (all day) -> OUT (all day)

# Args:
# events (SimpleEvent list): Accumulation of events from various calendars
# events_to_add (SimpleEvent list): Represent one single event from the calendar.
# A multiday event is broken down into x single day events. Whereas a one day event will be one event.
# """

# for new_event in events_to_add:
# for event in events:
# if new_event.net_id == event.net_id and new_event.date.date() == event.date.date():
# new_event_subject_id = utils.subject_identifier(new_event.subject)
# event_subject_id = utils.subject_identifier(event.subject)

# if new_event_subject_id > event_subject_id:
# logger.debug(f"Need to remove {event} and add {new_event} since it is ALL DAY")
# events.remove(event)
# elif new_event_subject_id < event_subject_id:
# logger.debug(f"{event} is already added to filtered list (ALL DAY). Remove {new_event} from events list")
# events_to_add.remove(new_event)
# elif new_event_subject_id == 1:
# logger.debug(f"{new_event} is a duplicate event of {event}. Won't be adding {new_event}")
# events_to_add.remove(new_event)
# else:
# logger.debug(f"Need to remove {event} and NOT add {new_event} and create an all day event instead")
# events.remove(event)
# events.append(SimpleEvent.create_all_day_event(event.net_id, event.date))
# events_to_add.remove(new_event)
# return


def test_filter(events):
"""
Removes duplicates in the list of events
Args:
events (SimpleEvent list): Accumulation of events from various calendars
events_to_add (SimpleEvent list): Represent one single event from the calendar.
A multiday event is broken down into x single day events. Whereas a one day event will be one event.
events (SimpleEvent list): contains events extracted from individual calendars
Returns:
SimpleEvent list: a filtered list of events
"""
filtered_events = []
events.sort()
event_to_add = events[0]

for new_event in events_to_add:
for event in events:
if new_event.net_id == event.net_id and new_event.date.date() == event.date.date():
new_event_subject_id = utils.subject_identifier(new_event.subject)
event_subject_id = utils.subject_identifier(event.subject)

if new_event_subject_id > event_subject_id:
logger.debug(f"Need to remove {event} and add {new_event} since it is ALL DAY")
events.remove(event)
elif new_event_subject_id < event_subject_id:
logger.debug(f"{event} is already added to filtered list (ALL DAY). Remove {new_event} from events list")
events_to_add.remove(new_event)
elif new_event_subject_id == 1:
logger.debug(f"{new_event} is a duplicate event of {event}. Won't be adding {new_event}")
events_to_add.remove(new_event)
else:
logger.debug(f"Need to remove {event} and NOT add {new_event} and create an all day event instead")
events.remove(event)
events.append(SimpleEvent.create_all_day_event(event.net_id, event.date))
events_to_add.remove(new_event)
return
# e is the new event
# event is the anchor
for event in events:
if event_to_add.net_id == event.net_id and event_to_add.date.date() == event.date.date():
event_subject_id = utils.subject_identifier(event.subject)
event_to_add_subject_id = utils.subject_identifier(event_to_add.subject)

if event_subject_id > event_to_add_subject_id:
event_to_add = event
elif ("OUT AM" in event.subject and "OUT PM" in event_to_add.subject) or ("OUT PM" in event.subject and "OUT AM" in event_to_add.subject):
event_to_add = SimpleEvent.create_all_day_event(event_to_add.net_id, event_to_add.date)
else:
filtered_events.append(event_to_add)
event_to_add = event

filtered_events.append(event_to_add)

print("filtered_events: ")
print(filtered_events)
return filtered_events

def process_individual_calendars(calendar, start_date, end_date):
"""
Expand All @@ -221,7 +257,7 @@ def process_individual_calendars(calendar, start_date, end_date):
calendar (json): json object of the events within/overlap between
a specified start and end date for indvidual calendars
start_date (datetime): the start date of timeframe being updated
end_date (dateime): the end date of timeframe being updated
end_date (datetime): the end date of timeframe being updated
Returns:
list: A list of SimpleEvent objects
Expand All @@ -237,10 +273,12 @@ def process_individual_calendars(calendar, start_date, end_date):
if event['status'] != EVENT_STATUS: continue

events_to_add = SimpleEvent.create_event_for_individual_calendars(event, start_date, end_date, net_id)
filter_events(events, events_to_add)
#filter_events(events, events_to_add)
events.extend(events_to_add)

except KeyError as e:
logger.warning(f"Unable to find: " + net_id)

return events
ans = test_filter(events)
print(f"TYPE: {type(ans)}")
return ans
13 changes: 7 additions & 6 deletions SimpleEvent.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
end_of_lunch = configs['end_of_lunch']
duration = configs['duration']

@dataclass
@dataclass(order=True)
class SimpleEvent:
net_id : str
subject : str # Our own formatted subject "[netID] [OUT/OUT AM/OUT PM]"
date : datetime
subject : str # Our own formatted subject "[netID] [OUT/OUT AM/OUT PM]"
# date : datetime

# Returns a list of Simple Events
# The list will return 1 item if the event is a one day event
Expand All @@ -47,7 +48,7 @@ def create_event_for_individual_calendars(cls, event, start_date, end_date, net_

if start.date() == end.date():
if SimpleEvent.is_event_valid(start_date, end_date, start, end):
return [cls(net_id, SimpleEvent.get_event_subject(start, end, net_id), start)]
return [cls(net_id, start, SimpleEvent.get_event_subject(start, end, net_id))]
return []

# if an event goes in here, then it's all day because the start date and end date differ by one day so it has to be at least be 1 All Day
Expand Down Expand Up @@ -90,7 +91,7 @@ def create_event_for_individual_calendars(cls, event, start_date, end_date, net_
new_end = new_end.replace(hour=23,minute=59,second=59)

if SimpleEvent.is_event_valid(start_date, end_date, new_start, new_end):
events.append(cls(net_id, SimpleEvent.get_event_subject(new_start, new_end, net_id), new_start))
events.append(cls(net_id, new_start, SimpleEvent.get_event_subject(new_start, new_end, net_id)))

return events

Expand Down Expand Up @@ -120,14 +121,14 @@ def create_event_for_shared_calendar(cls, event, net_ids):
return

if (len(event_identifier) == 2 and (event_identifier[1] == "OUT" or event_identifier[1] == "OUT AM" or event_identifier[1] == "OUT PM")):
simple_event = cls(event_identifier[0], subject, start)
simple_event = cls(event_identifier[0], start, subject)
return simple_event



@classmethod
def create_all_day_event(cls, net_id, date):
return cls(net_id, net_id + " OUT", date)
return cls(net_id, date, net_id + " OUT")


@staticmethod
Expand Down

0 comments on commit 9ee5ba1

Please sign in to comment.