From d5dd57e41e3cfeddfb7f6e0553d5486cdeb33839 Mon Sep 17 00:00:00 2001 From: Melissa Autumn Date: Tue, 8 Oct 2024 07:52:07 -0700 Subject: [PATCH] wip --- .../controller/apis/google_client.py | 23 ++++++++++++++++--- .../src/appointment/controller/calendar.py | 6 +++++ .../src/appointment/exceptions/calendar.py | 5 ++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/backend/src/appointment/controller/apis/google_client.py b/backend/src/appointment/controller/apis/google_client.py index 1d9762b24..5d4ebf56a 100644 --- a/backend/src/appointment/controller/apis/google_client.py +++ b/backend/src/appointment/controller/apis/google_client.py @@ -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 @@ -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 = [] @@ -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: @@ -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') diff --git a/backend/src/appointment/controller/calendar.py b/backend/src/appointment/controller/calendar.py index b3e13addb..731d281d9 100644 --- a/backend/src/appointment/controller/calendar.py +++ b/backend/src/appointment/controller/calendar.py @@ -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 diff --git a/backend/src/appointment/exceptions/calendar.py b/backend/src/appointment/exceptions/calendar.py index 174009ffd..ee486b49f 100644 --- a/backend/src/appointment/exceptions/calendar.py +++ b/backend/src/appointment/exceptions/calendar.py @@ -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