Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
MelissaAutumn committed Oct 30, 2024
1 parent a4e3a4c commit d5dd57e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
23 changes: 20 additions & 3 deletions backend/src/appointment/controller/apis/google_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
from google_auth_oauthlib.flow import Flow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError

from ... import utils
from ...database import repo
from ...database.models import CalendarProvider
from ...database.schemas import CalendarConnection
from ...defines import DATETIMEFMT
from ...exceptions.calendar import EventNotCreatedException
from ...exceptions.calendar import EventNotCreatedException, FreeBusyTimeException
from ...exceptions.google_api import GoogleScopeChanged, GoogleInvalidCredentials


Expand Down Expand Up @@ -101,7 +103,7 @@ def list_calendars(self, token):
return items

def get_free_busy(self, calendar_ids, time_min, time_max, token):
"""Query the free busy api, we only support 1 calendar id right now
"""Query the free busy api
Ref: https://developers.google.com/calendar/api/v3/reference/freebusy/query"""
response = {}
items = []
Expand All @@ -117,6 +119,20 @@ def get_free_busy(self, calendar_ids, time_min, time_max, token):
while request is not None:
try:
response = request.execute()
errors = [calendar.get('errors') for calendar in response.get('calendars', {}).values()]

# Log errors and throw 'em in sentry
if any(errors):
reasons = [
{
'domain': utils.setup_encryption_engine().encrypt(error.get('domain')),
'reason': error.get('reason')
} for error in errors
]
if os.getenv('SENTRY_DSN'):
ex = FreeBusyTimeException(reasons)
sentry_sdk.capture_exception(ex)
logging.warning(f'[google_client.get_free_time] FreeBusy API Error: {ex}')

calendar_items = [calendar.get('busy', []) for calendar in response.get('calendars', {}).values()]
for busy in calendar_items:
Expand All @@ -128,12 +144,13 @@ def get_free_busy(self, calendar_ids, time_min, time_max, token):
} for entry in busy
]
except HttpError as e:
logging.warning(f'[google_client.list_calendars] Request Error: {e.status_code}/{e.error_details}')
logging.warning(f'[google_client.get_free_time] Request Error: {e.status_code}/{e.error_details}')

request = service.calendarList().list_next(request, response)
perf_end = time.perf_counter_ns()

# Capture the metric if sentry is enabled
print(f"Google FreeBusy response: {(perf_end - perf_start) / 1000000000} seconds")
if os.getenv('SENTRY_DSN'):
sentry_sdk.set_measurement('google_free_busy_time_response', perf_end - perf_start, 'nanosecond')

Expand Down
6 changes: 6 additions & 0 deletions backend/src/appointment/controller/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,15 @@ def get_busy_time(self, calendar_ids: list, start: str, end: str):
time_min = datetime.strptime(start, DATEFMT)
time_max = datetime.strptime(end, DATEFMT)

perf_start = time.perf_counter_ns()

calendar = self.client.calendar(url=calendar_ids[0])
response = calendar.freebusy_request(time_min, time_max)

perf_end = time.perf_counter_ns()
print(f"CALDAV FreeBusy response: {(perf_end - perf_start) / 1000000000} seconds")


items = []

# This is sort of dumb, freebusy object isn't exposed in the icalendar instance except through a list of tuple props
Expand Down
5 changes: 5 additions & 0 deletions backend/src/appointment/exceptions/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ class EventNotCreatedException(Exception):
"""Raise if an event cannot be created on a remote calendar"""

pass


class FreeBusyTimeException(Exception):
"""Generic error with the free busy time api"""
pass

0 comments on commit d5dd57e

Please sign in to comment.