Last active
January 29, 2022 14:40
-
-
Save BharatKalluri/d1b05c89d9e22631454a2a164ca24317 to your computer and use it in GitHub Desktop.
Meeting stats from calendar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
from datetime import timedelta | |
from functools import reduce | |
from typing import TextIO | |
import icalendar | |
ical_contents_raw: TextIO = open('/Users/bharatkalluri/Downloads/[email protected]', 'r') | |
ical_contents: str = ical_contents_raw.read() | |
work_cal = icalendar.Calendar.from_ical(ical_contents) | |
def get_events_btw_dates(from_date: datetime.date, to_date: datetime.date): | |
return [ | |
component for component in work_cal.walk() | |
if component.name == 'VEVENT' and from_date < component.get('dtstart').dt.date() < to_date | |
] | |
def cal_meet_time(cal_events: list): | |
timedelta_lst = [(component.get('dtend').dt - component.get('dtstart').dt) for component in cal_events] | |
return reduce(lambda a, b: a + b, timedelta_lst, timedelta()) | |
def get_working_hrs(from_date: datetime.date, to_date: datetime.date, working_hrs: int): | |
days = [from_date + timedelta(days=i) for i in range((to_date - from_date).days + 1)] | |
return sum([working_hrs for d in days if d.weekday() not in [5, 6]]) | |
if __name__ == '__main__': | |
from_time = datetime.date(2022, 1, 1) | |
to_time = datetime.date(2022, 1, 21) | |
events = get_events_btw_dates(from_time, to_time) | |
total_time_in_meets = cal_meet_time(events) | |
total_working_hrs = get_working_hrs(from_time, to_time, 8) | |
time_in_meet_percentage = (total_time_in_meets.seconds / (total_working_hrs * 60 * 60)) * 100 | |
print(f'meets: {len(events)})') | |
print(f"total working hrs: {total_working_hrs}") | |
print(f'meet hours: {(total_time_in_meets.seconds / 60) / 60}') | |
print(f'meet time in percentage: {round(time_in_meet_percentage, 4)}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment