Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] calculate zoom attendance duration considering overlapping records #95

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions zoom_attendance_to_eventbrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def stringify_dict_ordered_by_name(d):
global_config = get_config(args)

# get the events from the working calendar in the Google spreadsheets
calendar = CQORCcalendar.Calendar(config, args)
calendar = CQORCcalendar.Calendar(global_config, args)
course = None
if args.course_id:
course = calendar[args.course_id]
Expand All @@ -47,7 +47,7 @@ def stringify_dict_ordered_by_name(d):
if args.zoom_id:
webinars = zoom.get_webinars(ids = [int(args.zoom_id)])
elif course:
webinars = zoom.get_webinar(ids = [int(course[0]['zoom_id'])])
webinars = zoom.get_webinars(ids = [int(course['sessions'][0]['zoom_id'])])
elif args.date:
webinars = zoom.get_webinars(date = to_iso8061(args.date).date())

Expand All @@ -65,10 +65,32 @@ def stringify_dict_ordered_by_name(d):
print(f"{v}")
print("===============")

zoom_participants = {p['user_email']: {'user_email': p['user_email'], 'name': p['name'], 'duration': 0} for p in participants_records}
zoom_participants = {p['name']: {'user_email': p['user_email'], 'name': p['name'], 'duration': 0} for p in participants_records}
print(f"{zoom_participants}")
# calculating the total attendance duration for each attendee
for r in participants_records:
zoom_participants[r['user_email']]['duration'] += r['duration']
for name in zoom_participants.keys():
records = [r for r in participants_records if r['name'] == name]
# the participant joined multiple times, we need to combine them
if len(records) >= 1:
# Algorithm from https://medium.com/@saraswatp/solving-overlapping-intervals-with-python-the-merged-interval-problem-dcf16ad09190
# first, sort the records by join_time
records.sort(key=lambda x: to_iso8061(x['join_time']))
merged_records = []
for record in records:
# If the list of merged records is empty or if the current record does not overlap with the previous one,
# simply add it to the merged list
if not merged_records or to_iso8061(record['join_time']) > to_iso8061(merged_records[-1]['leave_time']):
merged_records.append(record)
else:
# If the current interval overlaps with previous one, merge them
merged_records[-1]['leave_time'] = max(to_iso8061(merged_records[-1]['leave_time']), to_iso8061(record['leave_time']))
merged_records[-1]['duration'] = (to_iso8061(merged_records[-1]['leave_time']) - to_iso8061(merged_records[-1]['join_time'])).total_seconds()

records = merged_records

# sum the duration of each records
zoom_participants[name]['duration'] = sum([r['duration'] for r in records])


# retrieve the maximum duration
mean_duration = mean([v['duration'] for k,v in zoom_participants.items()])
Expand Down Expand Up @@ -97,7 +119,7 @@ def stringify_dict_ordered_by_name(d):
if args.eventbrite_id:
eb_event = eb.get_event(args.eventbrite_id)
elif course:
eb_event = eb.get_event(course[0]['eventbrite_id'])
eb_event = eb.get_event(course['sessions'][0]['eventbrite_id'])
else:
eb_events = eb.get_events(global_config['eventbrite']['organization_id'], time_filter="past", flattened=True, order_by="start_desc")
todays_events = []
Expand Down